1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
"""tests for passlib.util"""
#=========================================================
#imports
#=========================================================
#core
import sys
#site
#pkg
#module
from passlib import util
from passlib.tests.utils import TestCase, Params as ak
#=========================================================
#byte funcs
#=========================================================
class BytesTest(TestCase):
def test_list_to_bytes(self):
self.assertFunctionResults(util.list_to_bytes, [
#standard big endian
ak('\x00', [0], 1),
ak('\x01', [1], 1),
ak('\x00\x01', [1], 2),
ak('\x00\x01', [0, 1], 2),
ak('\x00\x00\x01', [1], 3),
ak('\x00\x00\x00\x00', [0], 4),
ak('\x00\x00\x00\x01', [1], 4),
ak('\x00\x00\x00\xff', [255], 4),
ak('\x00\x00\x01\xff', [1, 255], 4),
ak('\x04\x03\x02\x01', [4, 3, 2, 1], 4),
#standard little endian
ak('\x00', [0], 1, order="little"),
ak('\x01', [1], 1, order="little"),
ak('\x01\x00', [1], 2, order="little"),
ak('\x01\x00', [0, 1], 2, order="little"),
ak('\x01\x00\x00', [1], 3, order="little"),
ak('\x00\x00\x00\x00', [0], 4, order="little"),
ak('\x01\x00\x00\x00', [1], 4, order="little"),
ak('\xff\x00\x00\x00', [255], 4, order="little"),
ak('\xff\x01\x00\x00', [1, 255], 4, order="little"),
ak('\x01\x02\x03\x04', [4, 3, 2, 1], 4, order="little"),
])
#check bytes size check
self.assertRaises(ValueError, util.list_to_bytes, [])
self.assertRaises(ValueError, util.list_to_bytes, [0, 0], bytes=1)
#check bytes bound check
self.assertRaises(ValueError, util.list_to_bytes, [256], bytes=1)
#quick check native mode works right
if sys.byteorder == "little":
self.assertEqual(util.list_to_bytes([1], 3, order="native"), '\x01\x00\x00')
else:
self.assertEqual(util.list_to_bytes([1], 3, order="native"), '\x00\x00\x01')
def test_bytes_to_list(self):
self.assertFunctionResults(util.bytes_to_list, [
#standard big endian
ak([1], '\x01'),
ak([0, 1], '\x00\x01'),
ak([0, 0, 1], '\x00\x00\x01'),
ak([0, 0, 0, 0],'\x00\x00\x00\x00'),
ak([0, 0, 0, 1],'\x00\x00\x00\x01'),
ak([0, 0, 0, 255],'\x00\x00\x00\xff'),
ak([0, 0, 1, 0],'\x00\x00\x01\x00'),
ak([4, 3, 2, 1],'\x04\x03\x02\x01'),
#standard little endian
ak([1], '\x01', order="little"),
ak([0, 1], '\x01\x00', order="little"),
ak([0, 0, 1], '\x01\x00\x00', order="little"),
ak([0, 0, 0, 0], '\x00\x00\x00\x00', order="little"),
ak([0, 0, 0, 1], '\x01\x00\x00\x00', order="little"),
ak([0, 0, 0, 255], '\xff\x00\x00\x00', order="little"),
ak([0, 0, 1, 0], '\x00\x01\x00\x00', order="little"),
ak([4, 3, 2, 1],'\x01\x02\x03\x04', order="little"),
])
#quick check native mode works right
if sys.byteorder == "little":
self.assertEqual(util.bytes_to_list('\x01\x00\x00', order="native"), [0, 0, 1])
else:
self.assertEqual(util.bytes_to_list('\x00\x00\x01', order="native"), [0, 0, 1])
#=========================================================
#EOF
#=========================================================
|