1 import unittest
2 import codecs
3 import string
4 import random
5 from openid import oidutil
6
8 allowed_s = string.ascii_letters + string.digits + '+/='
9 allowed_d = {}
10 for c in allowed_s:
11 allowed_d[c] = None
12 isAllowed = allowed_d.has_key
13
14 def checkEncoded(s):
15 for c in s:
16 assert isAllowed(c), s
17
18 cases = [
19 '',
20 'x',
21 '\x00',
22 '\x01',
23 '\x00' * 100,
24 ''.join(map(chr, range(256))),
25 ]
26
27 for s in cases:
28 b64 = oidutil.toBase64(s)
29 checkEncoded(b64)
30 s_prime = oidutil.fromBase64(b64)
31 assert s_prime == s, (s, b64, s_prime)
32
33
34 for _ in xrange(50):
35 n = random.randrange(2048)
36 s = ''.join(map(chr, map(lambda _: random.randrange(256), range(n))))
37 b64 = oidutil.toBase64(s)
38 checkEncoded(b64)
39 s_prime = oidutil.fromBase64(b64)
40 assert s_prime == s, (s, b64, s_prime)
41
43 - def __init__(self, desc, args, expected):
44 unittest.TestCase.__init__(self)
45 self.desc = desc
46 self.args = args
47 self.expected = expected
48
50 result = oidutil.appendArgs(*self.args)
51 self.assertEqual(self.expected, result, self.args)
52
55
56
57
60 import copy
61 s = oidutil.Symbol("Foo")
62 d = {s: 1}
63 d_prime = copy.deepcopy(d)
64 self.failUnless(s in d_prime, "%r isn't in %r" % (s, d_prime))
65
66 t = oidutil.Symbol("Bar")
67 self.failIfEqual(hash(s), hash(t))
68
69
71 simple = 'http://www.example.com/'
72 cases = [
73 ('empty list',
74 (simple, []),
75 simple),
76
77 ('empty dict',
78 (simple, {}),
79 simple),
80
81 ('one list',
82 (simple, [('a', 'b')]),
83 simple + '?a=b'),
84
85 ('one dict',
86 (simple, {'a':'b'}),
87 simple + '?a=b'),
88
89 ('two list (same)',
90 (simple, [('a', 'b'), ('a', 'c')]),
91 simple + '?a=b&a=c'),
92
93 ('two list',
94 (simple, [('a', 'b'), ('b', 'c')]),
95 simple + '?a=b&b=c'),
96
97 ('two list (order)',
98 (simple, [('b', 'c'), ('a', 'b')]),
99 simple + '?b=c&a=b'),
100
101 ('two dict (order)',
102 (simple, {'b':'c', 'a':'b'}),
103 simple + '?a=b&b=c'),
104
105 ('escape',
106 (simple, [('=', '=')]),
107 simple + '?%3D=%3D'),
108
109 ('escape (URL)',
110 (simple, [('this_url', simple)]),
111 simple + '?this_url=http%3A%2F%2Fwww.example.com%2F'),
112
113 ('use dots',
114 (simple, [('openid.stuff', 'bother')]),
115 simple + '?openid.stuff=bother'),
116
117 ('args exist (empty)',
118 (simple + '?stuff=bother', []),
119 simple + '?stuff=bother'),
120
121 ('args exist',
122 (simple + '?stuff=bother', [('ack', 'ack')]),
123 simple + '?stuff=bother&ack=ack'),
124
125 ('args exist',
126 (simple + '?stuff=bother', [('ack', 'ack')]),
127 simple + '?stuff=bother&ack=ack'),
128
129 ('args exist (dict)',
130 (simple + '?stuff=bother', {'ack': 'ack'}),
131 simple + '?stuff=bother&ack=ack'),
132
133 ('args exist (dict 2)',
134 (simple + '?stuff=bother', {'ack': 'ack', 'zebra':'lion'}),
135 simple + '?stuff=bother&ack=ack&zebra=lion'),
136
137 ('three args (dict)',
138 (simple, {'stuff': 'bother', 'ack': 'ack', 'zebra':'lion'}),
139 simple + '?ack=ack&stuff=bother&zebra=lion'),
140
141 ('three args (list)',
142 (simple, [('stuff', 'bother'), ('ack', 'ack'), ('zebra', 'lion')]),
143 simple + '?stuff=bother&ack=ack&zebra=lion'),
144 ]
145
146 tests = []
147
148 for name, args, expected in cases:
149 test = AppendArgsTest(name, args, expected)
150 tests.append(test)
151
152 return unittest.TestSuite(tests)
153
158
160 suite = buildAppendTests()
161 suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
162 runner = unittest.TextTestRunner()
163 result = runner.run(suite)
164 assert result.wasSuccessful()
165
166
167
168
169
170 -def test(skipPyUnit=True):
174
175 if __name__ == '__main__':
176 test(skipPyUnit=False)
177