# Path: mamba/web/websocket.py
# DATA, CLOSE, PING, PONG = range(4)                    # frame control
# HYBI00, HYBI07, HYBI10, RFC6455 = range(4)            # supported versions
# HANDSHAKE, NEGOTIATION, CHALLENGE, FRAMES = range(4)  # state machine.
# NORMAL_CLOSURE = 0x3e8
# class WebSocketError(Exception):
# class InvalidProtocol(WebSocketError):
# class NoWebSocketCodec(WebSocketError):
# class InvalidProtocolVersion(WebSocketError):
# class InvalidCharacterInHyBi00Frame(WebSocketError):
# class ReservedFlagsInFrame(WebSocketError):
# class UnknownFrameOpcode(WebSocketError):
# class HandshakePreamble(object):
# class HyBi00HandshakePreamble(HandshakePreamble):
# class HyBi07HandshakePreamble(HandshakePreamble):
# class InvalidProtocolVersionPreamble(HandshakePreamble):
# class HyBi00Frame(object):
# class HyBi07Frame(object):
# class WebSocketProtocol(ProtocolWrapper):
# class WebSocketFactory(WrappingFactory):
#     def __init__(self):
#     def write_to_transport(self, transport):
#     def __init__(self, protocol):
#     def write_to_transport(self, transport, response):
#     def __init__(self, protocol):
#     def generate_accept_opening(self, protocol):
#     def __init__(self):
#     def __init__(self, buf):
#     def is_valid(self):
#     def generate(self, opcode=0x00):
#     def parse(self):
#     def __init__(self, buf):
#     def generate(self, opcode=0x1):
#     def parse(self):
#     def mask(self, buf, key):
#     def __init__(self, *args, **kwargs):
#     def secure(self):
#     def codecs(self):
#     def is_hybi00(self):
#     def dataReceived(self, data):
#     def handle_handshake(self):
#     def handle_negotiation(self):
#     def handle_challenge(self):
#     def handle_frames(self):
#     def write(self, data):
#     def writeSequence(self, data):
#     def _send(self, binary=False):
#     def close(self, reason=''):
#     def complete_hybi00(self, challenge):
#     def parse_headers(self, headers):


import hashlib
from twisted.trial import unittest
from twisted.internet import address, task
from twisted.web.http import datetimeToString
from twisted.test import test_policies, proto_helpers
from mamba.web import websocket
        from string import digits
        from struct import pack
        from hashlib import md5
        tmp_data = '\r\n'.join(tmp_data)
        tmp_data += '\r\nSec-WebSocket-Version: 29\r\n\r\n'

        self.port.dataReceived(tmp_data)
        self.assertEqual(
            self.tr.value(),
            'HTTP/1.1 400 Bad RequestServer: MambaWebSocketWrapper/1.0\r\n'
            'Date: {}\r\n'
            'Upgrade: WebSocket\r\n'
            'Connection: Upgrade\r\n'
            'Sec-WebSocket-Version: 13\r\n'
            'Sec-WebSocket-Version: 8, 7\r\n'.format(datetimeToString())
        )

    def test_returned_key_for_hybi00(self):

        key1 = '4 @1  46546xW\%0l 1 5'
        key2 = '12998 5 Y3 1  .P00'
        challenge = '^n:ds[4U'

        first = int(''.join(i for i in key1 if i in digits)) / key1.count(' ')
        second = int(''.join(i for i in key2 if i in digits)) / key2.count(' ')

        nonce = pack('>II8s', first, second, challenge)

        self.port.dataReceived(hybi00_data)
        self.assertTrue(md5(nonce).digest() in self.tr.value())

    def test_generate_hybi00frame(self):


# Path: mamba/web/websocket.py
# DATA, CLOSE, PING, PONG = range(4)                    # frame control
# HYBI00, HYBI07, HYBI10, RFC6455 = range(4)            # supported versions
# HANDSHAKE, NEGOTIATION, CHALLENGE, FRAMES = range(4)  # state machine.
# NORMAL_CLOSURE = 0x3e8
# class WebSocketError(Exception):
# class InvalidProtocol(WebSocketError):
# class NoWebSocketCodec(WebSocketError):
# class InvalidProtocolVersion(WebSocketError):
# class InvalidCharacterInHyBi00Frame(WebSocketError):
# class ReservedFlagsInFrame(WebSocketError):
# class UnknownFrameOpcode(WebSocketError):
# class HandshakePreamble(object):
# class HyBi00HandshakePreamble(HandshakePreamble):
# class HyBi07HandshakePreamble(HandshakePreamble):
# class InvalidProtocolVersionPreamble(HandshakePreamble):
# class HyBi00Frame(object):
# class HyBi07Frame(object):
# class WebSocketProtocol(ProtocolWrapper):
# class WebSocketFactory(WrappingFactory):
#     def __init__(self):
#     def write_to_transport(self, transport):
#     def __init__(self, protocol):
#     def write_to_transport(self, transport, response):
#     def __init__(self, protocol):
#     def generate_accept_opening(self, protocol):
#     def __init__(self):
#     def __init__(self, buf):
#     def is_valid(self):
#     def generate(self, opcode=0x00):
#     def parse(self):
#     def __init__(self, buf):
#     def generate(self, opcode=0x1):
#     def parse(self):
#     def mask(self, buf, key):
#     def __init__(self, *args, **kwargs):
#     def secure(self):
#     def codecs(self):
#     def is_hybi00(self):
#     def dataReceived(self, data):
#     def handle_handshake(self):
#     def handle_negotiation(self):
#     def handle_challenge(self):
#     def handle_frames(self):
#     def write(self, data):
#     def writeSequence(self, data):
#     def _send(self, binary=False):
#     def close(self, reason=''):
#     def complete_hybi00(self, challenge):
#     def parse_headers(self, headers):


# Path: mamba/test/test_websocket.py
import hashlib
from twisted.trial import unittest
from twisted.internet import address, task
from twisted.web.http import datetimeToString
from twisted.test import test_policies, proto_helpers
from mamba.web import websocket
        from string import digits
        from struct import pack
        from hashlib import md5

        tmp_data = '\r\n'.join(tmp_data)
        tmp_data += '\r\nSec-WebSocket-Version: 29\r\n\r\n'

        self.port.dataReceived(tmp_data)
        self.assertEqual(
            self.tr.value(),
            'HTTP/1.1 400 Bad RequestServer: MambaWebSocketWrapper/1.0\r\n'
            'Date: {}\r\n'
            'Upgrade: WebSocket\r\n'
            'Connection: Upgrade\r\n'
            'Sec-WebSocket-Version: 13\r\n'
            'Sec-WebSocket-Version: 8, 7\r\n'.format(datetimeToString())
        )

    def test_returned_key_for_hybi00(self):

        key1 = '4 @1  46546xW\%0l 1 5'
        key2 = '12998 5 Y3 1  .P00'
        challenge = '^n:ds[4U'

        first = int(''.join(i for i in key1 if i in digits)) / key1.count(' ')
        second = int(''.join(i for i in key2 if i in digits)) / key2.count(' ')

        nonce = pack('>II8s', first, second, challenge)

        self.port.dataReceived(hybi00_data)
        self.assertTrue(md5(nonce).digest() in self.tr.value())

    def test_generate_hybi00frame(self):


        frame = websocket.HyBi00Frame('LEMOOOOOOOOOON')