~ubuntu-branches/debian/sid/pyrlp/sid

« back to all changes in this revision

Viewing changes to tests/test_binary_sedes.py

  • Committer: Package Import Robot
  • Author(s): Ben Finney
  • Date: 2017-07-15 05:25:42 UTC
  • Revision ID: package-import@ubuntu.com-20170715052542-a20zzsypt1qfecq1
Tags: upstream-0.5.1
Import upstream version 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: UTF-8 -*-
 
2
from __future__ import unicode_literals
 
3
 
 
4
import pytest
 
5
from rlp import SerializationError, utils
 
6
from rlp.sedes import Binary
 
7
 
 
8
 
 
9
def test_binary():
 
10
    b1 = Binary()
 
11
    f = {
 
12
        '': b'',
 
13
        'asdf': b'asdf',
 
14
        ('\x00' * 20): (b'\x00' * 20),
 
15
        'fdsa': b'fdsa'
 
16
    }
 
17
    for k in f:
 
18
        assert b1.serialize(k) == f[k]
 
19
    for d in ([], 5, str):
 
20
        with pytest.raises(SerializationError):
 
21
            b1.serialize(d)
 
22
 
 
23
    b2 = Binary.fixed_length(5)
 
24
    f = {
 
25
        'asdfg': b'asdfg',
 
26
        b'\x00\x01\x02\x03\x04': b'\x00\x01\x02\x03\x04',
 
27
        utils.str_to_bytes('ababa'): b'ababa'
 
28
    }
 
29
    for k in f:
 
30
        assert b2.serialize(k) == f[k]
 
31
 
 
32
    for d in ('asdf', 'asdfgh', '', 'bababa'):
 
33
        with pytest.raises(SerializationError):
 
34
            b2.serialize(d)
 
35
 
 
36
    b3 = Binary(2, 4)
 
37
    f = {
 
38
        'as': b'as',
 
39
        'dfg': b'dfg',
 
40
        'hjkl': b'hjkl',
 
41
        b'\x00\x01\x02': b'\x00\x01\x02'
 
42
    }
 
43
    for k in f:
 
44
        assert b3.serialize(k) == f[k]
 
45
    for d in ('', 'a', 'abcde', 'äää'):
 
46
        with pytest.raises(SerializationError):
 
47
            b3.serialize(d)
 
48
 
 
49
    b4 = Binary(min_length=3)
 
50
    f = {'abc': b'abc', 'abcd': b'abcd', ('x' * 132): (b'x' * 132)}
 
51
    for k in f:
 
52
        assert b4.serialize(k) == f[k]
 
53
    for d in ('ab', '', 'a', 'xy'):
 
54
        with pytest.raises(SerializationError):
 
55
            b4.serialize(d)
 
56
 
 
57
    b5 = Binary(max_length=3)
 
58
    f = {'': b'', 'ab': b'ab', 'abc': b'abc'}
 
59
    for k in f:
 
60
        assert b5.serialize(k) == f[k]
 
61
    for d in ('abcd', 'vwxyz', 'a' * 32):
 
62
        with pytest.raises(SerializationError):
 
63
            b5.serialize(d)
 
64
 
 
65
    b6 = Binary(min_length=3, max_length=5, allow_empty=True)
 
66
    f = {'': b'', 'abc': b'abc', 'abcd': b'abcd', 'abcde': b'abcde'}
 
67
    for k in f:
 
68
        assert b6.serialize(k) == f[k]
 
69
    for d in ('a', 'ab', 'abcdef', 'abcdefgh' * 10):
 
70
        with pytest.raises(SerializationError):
 
71
            b6.serialize(d)