-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtest_tracestate.py
132 lines (108 loc) · 4.08 KB
/
test_tracestate.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import unittest
from tracecontext import Tracestate
class TestTracestate(unittest.TestCase):
def test_ctor_no_arg(self):
state = Tracestate()
self.assertEqual(state.to_string(), '')
def test_ctor_with_dict(self):
state = Tracestate({'foo': '1'})
self.assertEqual(state.to_string(), 'foo=1')
def test_ctor_kwargs(self):
state = Tracestate(foo = '1', bar = '2', baz = '3')
self.assertEqual(state.to_string(), 'foo=1,bar=2,baz=3')
def test_ctor_with_string(self):
state = Tracestate('foo=1,bar=2,baz=3')
self.assertEqual(state.to_string(), 'foo=1,bar=2,baz=3')
self.assertRaises(ValueError, lambda: Tracestate('foobarbaz'))
def test_cctor(self):
state = Tracestate(Tracestate('foo=1,bar=2,baz=3'))
self.assertEqual(state.to_string(), 'foo=1,bar=2,baz=3')
def test_all_allowed_chars(self):
header = ''.join([
# key
''.join(map(chr, range(0x61, 0x7A + 1))), # lcalpha
'0123456789', # DIGIT
'_',
'-',
'*',
'/',
# "="
'=',
# value
''.join(map(chr, range(0x20, 0x2B + 1))),
''.join(map(chr, range(0x2D, 0x3C + 1))),
''.join(map(chr, range(0x3E, 0x7E + 1))),
])
state = Tracestate(header)
self.assertEqual(state.to_string(), header)
def test_delimiter(self):
state = Tracestate('foo=1, \t bar=2')
self.assertEqual(state.to_string(), 'foo=1,bar=2')
state = Tracestate('foo=1,\t \tbar=2')
self.assertEqual(state.to_string(), 'foo=1,bar=2')
def test_getitem(self):
state = Tracestate({'foo': '1'})
self.assertEqual(state['foo'], '1')
def test_method_from_string(self):
state = Tracestate()
state.from_string('foo=1')
state.from_string('bar=2')
state.from_string('baz=3')
self.assertEqual(state.to_string(), 'foo=1,bar=2,baz=3')
# test load order
state = Tracestate()
state.from_string('baz=3')
state.from_string('bar=2')
state.from_string('foo=1')
self.assertNotEqual(state.to_string(), 'foo=1,bar=2,baz=3')
def test_method_is_valid(self):
state = Tracestate()
# empty state not allowed
self.assertFalse(state.is_valid())
state['foo'] = 'x' * 256
self.assertTrue(state.is_valid())
# exceeds 512 bytes
state['bar'] = 'x' * 256
self.assertFalse(state.is_valid())
self.assertTrue(Tracestate(state.to_string()[:512]).is_valid())
self.assertFalse(Tracestate(state.to_string()[:513]).is_valid())
def test_method_repr(self):
state = Tracestate('foo=1, bar=2, baz=3')
self.assertEqual(repr(state), "Tracestate('foo=1,bar=2,baz=3')")
def test_pop(self):
state = Tracestate('foo=1,bar=2,baz=3')
self.assertEqual(state.pop(), ('baz', '3'))
self.assertEqual(state.to_string(), 'foo=1,bar=2')
self.assertEqual(state.pop(), ('bar', '2'))
self.assertEqual(state.to_string(), 'foo=1')
self.assertEqual(state.pop(), ('foo', '1'))
self.assertEqual(state.to_string(), '')
# raise KeyError exception while trying to pop from nothing
self.assertRaises(KeyError, lambda: state.pop())
def test_setitem(self):
state = Tracestate(bar = '0')
state['foo'] = '1'
state['bar'] = '2'
state['baz'] = '3'
self.assertEqual(state.to_string(), 'baz=3,bar=2,foo=1')
# key SHOULD be string
self.assertRaises(ValueError, lambda: state.__setitem__(123, 'abc'))
# value SHOULD NOT be empty string
self.assertRaises(ValueError, lambda: state.__setitem__('', 'abc'))
# key SHOULD NOT have uppercase
self.assertRaises(ValueError, lambda: state.__setitem__('FOO', 'abc'))
# key with vendor format
state['special@vendor'] = 'abracadabra'
self.assertRaises(ValueError, lambda: state.__setitem__('special@', 'abracadabra'))
self.assertRaises(ValueError, lambda: state.__setitem__('@vendor', 'abracadabra'))
# value SHOULD be string
self.assertRaises(ValueError, lambda: state.__setitem__('FOO', 123))
# value SHOULD NOT be empty string
self.assertRaises(ValueError, lambda: state.__setitem__('foo', ''))
# value SHOULD NOT contain invalid character
self.assertRaises(ValueError, lambda: state.__setitem__('foo', 'bar=baz'))
state['foo'] = 'x' * 256
# throw if value exceeds 256 bytes
self.assertRaises(ValueError, lambda: state.__setitem__('foo', 'x' * 257))
if __name__ == '__main__':
unittest.main()