Example #1
0
def test_unicode_atom_encode_raises():
    # ATM, we only allow packing latin-1 encoded Atoms because the underlying
    # library sends ATOM_EXT instead of ATOM_UTF8_EXT. Update this test when
    # we are ready to start sending UTF-8 atoms.
    atm = Atom(u'こんにちは世界')  # hello world
    try:
        pack(atm)
        raise Exception('did not raise UnicodeEncodeError')
    except UnicodeEncodeError:
        pass
Example #2
0
    def encode(self, data: Dict[str, Any]) -> str:
        if self.encoding == 'json':
            return json.dumps(data)
        elif self.encoding == 'etf':
            return erlpack.pack(data)

        return ''
def test_erlpack_magic_method():
    u = User('jake', 23)
    packed = pack(u)

    # Could be packed in either order
    assert packed == b'\x83t\x00\x00\x00\x02s\x03agea\x17s\x04namem\x00\x00\x00\x04jake' \
        or packed == b'\x83t\x00\x00\x00\x02s\x04namem\x00\x00\x00\x04jakes\x03agea\x17'
Example #4
0
def test_large_atom():
    atm = Atom('test ' * 100)
    assert pack(atm) == (
        '\x83d\x01\xf4test test test test test test test test test test test test test test test test test test test '
        'test test test test test test test test test test test test test test test test test test test test test '
        'test test test test test test test test test test test test test test test test test test test test test '
        'test test test test test test test test test test test test test test test test test test test test test '
        'test test test test test test test test test test test test test test test test test test '
    )
Example #5
0
def test_large_atom():
    atm = Atom('test ' * 100)
    assert pack(atm) == (
        b'\x83d\x01\xf4test test test test test test test test test test test test test test test test test test test '
        b'test test test test test test test test test test test test test test test test test test test test test '
        b'test test test test test test test test test test test test test test test test test test test test test '
        b'test test test test test test test test test test test test test test test test test test test test test '
        b'test test test test test test test test test test test test test test test test test test '
    )
Example #6
0
def test_userdict():
    items_called = [False]

    class UserDict(dict):
        def items(self):
            items_called[0] = True
            return super(UserDict, self).items()

    assert pack(UserDict({'a': 1, 2: 2, 3: [1, 2, 3]})) == \
           '\x83t\x00\x00\x00\x03m\x00\x00\x00\x01aa\x01a\x02a\x02a\x03l\x00\x00\x00\x03a\x01a\x02a\x03j'

    assert items_called[0]
Example #7
0
def test_userdict():
    items_called = [False]

    class UserDict(dict):
        def items(self):
            items_called[0] = True
            return list(super(UserDict, self).items())

    assert pack(UserDict({'a': 1, 2: 2, 3: [1, 2, 3]})) == \
           b'\x83t\x00\x00\x00\x03m\x00\x00\x00\x01aa\x01a\x02a\x02a\x03l\x00\x00\x00\x03a\x01a\x02a\x03j'

    assert items_called[0]
Example #8
0
def test_large_tuple():
    t = tuple(range(257))
    assert pack(t) == '\x83i\x00\x00\x01\x01a\x00a\x01a\x02a\x03a\x04a\x05a\x06a\x07a\x08a\ta\na\x0ba\x0ca\ra\x0ea' \
                      '\x0fa\x10a\x11a\x12a\x13a\x14a\x15a\x16a\x17a\x18a\x19a\x1aa\x1ba\x1ca\x1da\x1ea\x1fa a!a"a' \
                      '#a$a%a&a\'a(a)a*a+a,a-a.a/a0a1a2a3a4a5a6a7a8a9a:a;a<a=a>a?a@aAaBaCaDaEaFaGaHaIaJaKaLaMaNaOa' \
                      'PaQaRaSaTaUaVaWaXaYaZa[a\\a]a^a_a`aaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayaza{a|a' \
                      '}a~a\x7fa\x80a\x81a\x82a\x83a\x84a\x85a\x86a\x87a\x88a\x89a\x8aa\x8ba\x8ca\x8da\x8ea\x8fa' \
                      '\x90a\x91a\x92a\x93a\x94a\x95a\x96a\x97a\x98a\x99a\x9aa\x9ba\x9ca\x9da\x9ea\x9fa\xa0a\xa1' \
                      'a\xa2a\xa3a\xa4a\xa5a\xa6a\xa7a\xa8a\xa9a\xaaa\xaba\xaca\xada\xaea\xafa\xb0a\xb1a\xb2a\xb3' \
                      'a\xb4a\xb5a\xb6a\xb7a\xb8a\xb9a\xbaa\xbba\xbca\xbda\xbea\xbfa\xc0a\xc1a\xc2a\xc3a\xc4a\xc5' \
                      'a\xc6a\xc7a\xc8a\xc9a\xcaa\xcba\xcca\xcda\xcea\xcfa\xd0a\xd1a\xd2a\xd3a\xd4a\xd5a\xd6a\xd7' \
                      'a\xd8a\xd9a\xdaa\xdba\xdca\xdda\xdea\xdfa\xe0a\xe1a\xe2a\xe3a\xe4a\xe5a\xe6a\xe7a\xe8a\xe9' \
                      'a\xeaa\xeba\xeca\xeda\xeea\xefa\xf0a\xf1a\xf2a\xf3a\xf4a\xf5a\xf6a\xf7a\xf8a\xf9a\xfaa\xfb' \
                      'a\xfca\xfda\xfea\xffb\x00\x00\x01\x00'
Example #9
0
def test_large_tuple():
    t = tuple(range(257))
    assert pack(t) == b'\x83i\x00\x00\x01\x01a\x00a\x01a\x02a\x03a\x04a\x05a\x06a\x07a\x08a\ta\na\x0ba\x0ca\ra\x0ea' \
                      b'\x0fa\x10a\x11a\x12a\x13a\x14a\x15a\x16a\x17a\x18a\x19a\x1aa\x1ba\x1ca\x1da\x1ea\x1fa a!a"a' \
                      b'#a$a%a&a\'a(a)a*a+a,a-a.a/a0a1a2a3a4a5a6a7a8a9a:a;a<a=a>a?a@aAaBaCaDaEaFaGaHaIaJaKaLaMaNaOa' \
                      b'PaQaRaSaTaUaVaWaXaYaZa[a\\a]a^a_a`aaabacadaeafagahaiajakalamanaoapaqarasatauavawaxayaza{a|a' \
                      b'}a~a\x7fa\x80a\x81a\x82a\x83a\x84a\x85a\x86a\x87a\x88a\x89a\x8aa\x8ba\x8ca\x8da\x8ea\x8fa' \
                      b'\x90a\x91a\x92a\x93a\x94a\x95a\x96a\x97a\x98a\x99a\x9aa\x9ba\x9ca\x9da\x9ea\x9fa\xa0a\xa1' \
                      b'a\xa2a\xa3a\xa4a\xa5a\xa6a\xa7a\xa8a\xa9a\xaaa\xaba\xaca\xada\xaea\xafa\xb0a\xb1a\xb2a\xb3' \
                      b'a\xb4a\xb5a\xb6a\xb7a\xb8a\xb9a\xbaa\xbba\xbca\xbda\xbea\xbfa\xc0a\xc1a\xc2a\xc3a\xc4a\xc5' \
                      b'a\xc6a\xc7a\xc8a\xc9a\xcaa\xcba\xcca\xcda\xcea\xcfa\xd0a\xd1a\xd2a\xd3a\xd4a\xd5a\xd6a\xd7' \
                      b'a\xd8a\xd9a\xdaa\xdba\xdca\xdda\xdea\xdfa\xe0a\xe1a\xe2a\xe3a\xe4a\xe5a\xe6a\xe7a\xe8a\xe9' \
                      b'a\xeaa\xeba\xeca\xeda\xeea\xefa\xf0a\xf1a\xf2a\xf3a\xf4a\xf5a\xf6a\xf7a\xf8a\xf9a\xfaa\xfb' \
                      b'a\xfca\xfda\xfea\xffb\x00\x00\x01\x00'
Example #10
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()
Example #11
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()
Example #12
0
def test_pack_empty_dictionary():
    assert pack(()) == '\x83h\x00'
Example #13
0
def test_pack_binary():
    assert pack('alsdjaljf') == '\x83m\x00\x00\x00\talsdjaljf'
Example #14
0
def test_pack_kitchen_sink():
    assert pack([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'}]) == '\x83l\x00\x00\x00\x0cs\x08someatomh\x03s\x04somes\x05otherm\x00\x00\x00\x05tuplel\x00\x00\x00\x03m\x00\x00\x00\x05maybea\x01jjh\x03m\x00\x00\x00\x04withh\x02s\x08embeddedl\x00\x00\x00\x01m\x00\x00\x00\x10tuples and listsjs\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\x03s\x01am\x00\x00\x00\x03maps\x04alsoh\x03m\x00\x00\x00\x06tuplesl\x00\x00\x00\x01m\x00\x00\x00\x03andjl\x00\x00\x00\x01m\x00\x00\x00\x05listsjs\x04withm\x00\x00\x00\x08binariest\x00\x00\x00\x02s\x01am\x00\x00\x00\nanotheronea\x03m\x00\x00\x00\x08int keysjt\x00\x00\x00\x01h\x01s\tsomethingm\x00\x00\x00\x04elsej'
Example #15
0
def test_pack_basic_atom():
    assert pack(Atom('hi')) == '\x83s\x02hi'
Example #16
0
def test_nil():
    assert pack(None) == '\x83s\x03nil'
Example #17
0
def test_smallint():
    for i in xrange(256):
        assert pack(i) == '\x83a%s' % chr(i)
Example #18
0
def test_true():
    assert pack(True) == '\x83s\x04true'
Example #19
0
def test_unicode_with_actual_unicode_chars():
    atm = u"hello world\u202e"

    assert pack(atm) == "\x83m\x00\x00\x00\x0ehello world\xe2\x80\xae"
Example #20
0
def test_unicode():
    atm = u"hello world"

    assert pack(atm) == "\x83m\x00\x00\x00\x0bhello world"
Example #21
0
def test_list():
    assert pack([1, 'two', 3.0, 'four', ['five']]) == (
        b'\x83l\x00\x00\x00\x05a\x01m\x00\x00\x00\x03twoF@\x08\x00\x00\x00\x00\x00\x00m\x00\x00\x00\x04fourl\x00\x00'
        b'\x00\x01m\x00\x00\x00\x04fivejj')
Example #22
0
def test_false():
    assert pack(False) == '\x83s\x05false'
Example #23
0
def test_unicode():
    atm = u'hello world'
    assert pack(atm) == b'\x83m\x00\x00\x00\x0bhello world'
Example #24
0
def test_long_long():
    assert pack(-2147483649) == '\x83n\x04\x01\x01\x00\x00\x80'
    assert pack(
        -123094182304912341) == '\x83n\x08\x01\xd5\x933\xb2\x81Q\xb5\x01'
Example #25
0
def test_string():
    atm = 'hello world'
    assert pack(atm) == '\x83m\x00\x00\x00\x0bhello world'
Example #26
0
def test_string_null_byte():
    null_byte = 'hello\x00 world'
    assert pack(null_byte) == b'\x83m\x00\x00\x00\x0chello\x00 world'
Example #27
0
def test_string_null_byte():
    null_byte = 'hello\x00 world'
    print "null_byte", null_byte
    assert pack(null_byte) == '\x83m\x00\x00\x00\x0chello\x00 world'
Example #28
0
def test_smallint():
    for i in range(256):
        assert pack(i) == b'\x83a' + struct.pack('B', i)
def test_erlpack_magic_method():
    u = User('jake', 23)
    assert pack(u) == '\x83t\x00\x00\x00\x02s\x03agea\x17s\x04namem\x00\x00\x00\x04jake'
Example #30
0
def test_pack_string():
    assert pack('string') == '\x83m\x00\x00\x00\x06string'
Example #31
0
def test_unsigned_long_long():
    assert pack(2147483648) == '\x83n\x04\x00\x00\x00\x00\x80'
    assert pack(1230941823049123411) == '\x83n\x08\x00S\xc6\x03\xf6\x10/\x15\x11'
Example #32
0
def test_pack_float():
    assert pack(123.45) == '\x83F@^\xdc\xcc\xcc\xcc\xcc\xcd'
Example #33
0
def test_long_long():
    assert pack(-2147483649) == '\x83n\x04\x01\x01\x00\x00\x80'
    assert pack(-123094182304912341) == '\x83n\x08\x01\xd5\x933\xb2\x81Q\xb5\x01'
Example #34
0
def test_pack_int():
    assert pack(12345) == '\x83b\x00\x0009'
Example #35
0
def test_really_big_ints():
    with raises(OverflowError):
        pack(123094182304912341123414)
Example #36
0
def test_empty_list():
    assert pack([]) == b'\x83j'
Example #37
0
def test_smallint():
    for i in xrange(256):
        assert pack(i) == '\x83a%s' % chr(i)
Example #38
0
def test_float():
    assert pack(2.5) == '\x83F@\x04\x00\x00\x00\x00\x00\x00'
    assert pack(51512123841234.31423412341435123412341342) == '\x83FB\xc7l\xcc\xeb\xedi('
Example #39
0
def test_int():
    assert pack(1024) == '\x83b\x00\x00\x04\x00'
    assert pack(-2147483648) == '\x83b\x80\x00\x00\x00'
    assert pack(2147483647) == '\x83b\x7f\xff\xff\xff'
Example #40
0
def test_true():
    assert pack(True) == b'\x83s\x04true'
Example #41
0
def test_list():
    assert pack([1, "two", 3.0, "four", ['five']]) == (
        '\x83l\x00\x00\x00\x05a\x01m\x00\x00\x00\x03twoF@\x08\x00\x00\x00\x00\x00\x00m\x00\x00\x00\x04fourl\x00\x00'
        '\x00\x01m\x00\x00\x00\x04fivejj'
    )
Example #42
0
def test_unsigned_long_long():
    assert pack(2147483648) == '\x83n\x04\x00\x00\x00\x00\x80'
    assert pack(
        1230941823049123411) == '\x83n\x08\x00S\xc6\x03\xf6\x10/\x15\x11'
Example #43
0
def test_small_tuple():
    assert pack((1, 2, 3)) == '\x83h\x03a\x01a\x02a\x03'
Example #44
0
def test_really_big_ints():
    with raises(OverflowError):
        pack(123094182304912341123414)
Example #45
0
def test_small_atom():
    atm = Atom('hello world')

    assert pack(atm) == b'\x83s\x0bhello world'
Example #46
0
def test_int():
    assert pack(1024) == '\x83b\x00\x00\x04\x00'
    assert pack(-2147483648) == '\x83b\x80\x00\x00\x00'
    assert pack(2147483647) == '\x83b\x7f\xff\xff\xff'
Example #47
0
def test_pack_empty_list():
    assert pack([]) == '\x83j'
Example #48
0
def test_string():
    atm = 'hello world'
    assert pack(atm) == b'\x83m\x00\x00\x00\x0bhello world'
Example #49
0
def test_pack_large_int():
    assert pack(127552384489488384L) == '\x83n\x08\x00\x00\x00\xc0\xc77(\xc5\x01'
Example #50
0
def test_false():
    assert pack(False) == b'\x83s\x05false'
Example #51
0
def test_small_atom():
    atm = Atom('hello world')

    assert pack(atm) == '\x83s\x0bhello world'