Beispiel #1
0
    def decode(self, data: bytes) -> Dict[str, Any]:
        if self.encoding == 'json':
            return json.loads(data)
        elif self.encoding == 'etf':
            return erlpack.unpack(data)

        return {}
Beispiel #2
0
def start_server():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    s.bind((HOST, PORT))
    s.listen(BACKLOG)
    client, address = s.accept()
    clientfile = client.makefile()
    testcases = ''
    is_name = True
    name = ''
    while 1:
        size_str = clientfile.read(4)
        if not size_str:
            break
        else:
            (size, ) = struct.unpack("!L", size_str)
            data_str = clientfile.read(size)
            term = unpack(data_str)
            encoded_term = pack(term)
            out_size = len(encoded_term)
            clientfile.write(struct.pack("!L", long(out_size)))
            clientfile.write(encoded_term)
            clientfile.flush()
            if is_name:
                name = term
            else:
                testcases += testcase_pack_tmpl.substitute(
                    name=name,
                    python_rep=repr(term),
                    packed_rep=repr(encoded_term))
                testcases += testcase_unpack_tmpl.substitute(
                    name=name,
                    python_rep=repr(term),
                    packed_rep=repr(data_str))

            is_name = not is_name

    with open('tests/test_generated.py', 'w') as f:
        f.write(file_tmpl.substitute(testcases=testcases))

    clientfile.close()
    client.close()
Beispiel #3
0
def start_server():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
    s.bind((HOST, PORT))
    s.listen(BACKLOG)
    client, address = s.accept()
    clientfile = client.makefile()
    testcases = ''
    is_name = True
    name = ''
    while 1:
        size_str = clientfile.read(4)
        if not size_str:
            break
        else:
            (size,) = struct.unpack("!L", size_str)
            data_str = clientfile.read(size)
            term = unpack(data_str)
            encoded_term = pack(term)
            out_size = len(encoded_term)
            clientfile.write(struct.pack("!L", long(out_size)))
            clientfile.write(encoded_term)
            clientfile.flush()
            if is_name:
                name = term
            else:
                testcases += testcase_pack_tmpl.substitute(name=name,
                                                           python_rep=repr(term),
                                                           packed_rep=repr(encoded_term))
                testcases += testcase_unpack_tmpl.substitute(name=name,
                                                             python_rep=repr(term),
                                                             packed_rep=repr(data_str))

            is_name = not is_name

    with open('tests/test_generated.py', 'w') as f:
        f.write(file_tmpl.substitute(testcases=testcases))

    clientfile.close()
    client.close()
Beispiel #4
0
def test_unpack_empty_dictionary():
    assert unpack('\x83h\x00') == ()
Beispiel #5
0
def test_unpack_string():
    assert unpack('\x83m\x00\x00\x00\x06string') == 'string'
Beispiel #6
0
def test_unpack_int():
    assert unpack('\x83b\x00\x0009') == 12345
Beispiel #7
0
def test_unpack_binary():
    assert unpack('\x83m\x00\x00\x00\talsdjaljf') == 'alsdjaljf'
Beispiel #8
0
def test_unpack_float():
    assert unpack('\x83F@^\xdc\xcc\xcc\xcc\xcc\xcd') == 123.45
Beispiel #9
0
def test_unpack_kitchen_sink():
    assert unpack('\x83l\x00\x00\x00\x0cd\x00\x08someatomh\x03d\x00\x04somed\x00\x05otherm\x00\x00\x00\x05tuplel\x00\x00\x00\x03m\x00\x00\x00\x05maybea\x01jjh\x03m\x00\x00\x00\x04withh\x02d\x00\x08embeddedl\x00\x00\x00\x01m\x00\x00\x00\x10tuples and listsjd\x00\x03niln\x08\x00\x90gWs\x1f\x1f\xc5\x01F@\xb4\xd6Q\xeb\x85\x1e\xb8afb\xff\xff\xfa\x8eF\xc0u\xd333333n\x05\x01ch\t\ntl\x00\x00\x00\x02t\x00\x00\x00\x03d\x00\x01am\x00\x00\x00\x03mapd\x00\x04alsoh\x03m\x00\x00\x00\x06tuplesl\x00\x00\x00\x01m\x00\x00\x00\x03andjl\x00\x00\x00\x01m\x00\x00\x00\x05listsjd\x00\x04withm\x00\x00\x00\x08binariest\x00\x00\x00\x02a\x03m\x00\x00\x00\x08int keysd\x00\x01am\x00\x00\x00\nanotheronejt\x00\x00\x00\x01h\x01d\x00\tsomethingm\x00\x00\x00\x04elsej') == [Atom('someatom'), (Atom('some'), Atom('other'), 'tuple'), ['maybe', 1, []], ('with', (Atom('embedded'), ['tuples and lists']), None), 127542384389482384L, 5334.32, 102, -1394, -349.2, -498384595043, [{Atom('a'): 'map', Atom('also'): ('tuples', ['and'], ['lists']), Atom('with'): 'binaries'}, {Atom('a'): 'anotherone', 3: 'int keys'}], {(Atom('something'),): 'else'}]
Beispiel #10
0
def test_unpack_basic_atom():
    assert unpack('\x83d\x00\x02hi') == Atom('hi')
Beispiel #11
0
def test_unpack_large_int():
    assert unpack('\x83n\x08\x00\x00\x00\xc0\xc77(\xc5\x01') == 127552384489488384L
Beispiel #12
0
def test_unpack_empty_list():
    assert unpack('\x83j') == []
Beispiel #13
0
def test_unicode_atom_decodes():
    atm = unpack(
        b'\x83w\x15\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c'
    )
    assert atm == Atom(u'こんにちは世界')
Beispiel #14
0
 def decode(obj):
     return unpack(obj)
Beispiel #15
0
def test_string_ext_unpack():
    assert unpack(b'\x83k\x0b\x00hello world') == \
        [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]