File: oidutil.py

package info (click to toggle)
python-openid 2.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,472 kB
  • ctags: 3,328
  • sloc: python: 16,708; xml: 234; sh: 38; makefile: 30
file content (176 lines) | stat: -rw-r--r-- 4,644 bytes parent folder | download | duplicates (5)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import unittest
import codecs
import string
import random
from openid import oidutil

def test_base64():
    allowed_s = string.ascii_letters + string.digits + '+/='
    allowed_d = {}
    for c in allowed_s:
        allowed_d[c] = None
    isAllowed = allowed_d.has_key

    def checkEncoded(s):
        for c in s:
            assert isAllowed(c), s

    cases = [
        '',
        'x',
        '\x00',
        '\x01',
        '\x00' * 100,
        ''.join(map(chr, range(256))),
        ]

    for s in cases:
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert s_prime == s, (s, b64, s_prime)

    # Randomized test
    for _ in xrange(50):
        n = random.randrange(2048)
        s = ''.join(map(chr, map(lambda _: random.randrange(256), range(n))))
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert s_prime == s, (s, b64, s_prime)

class AppendArgsTest(unittest.TestCase):
    def __init__(self, desc, args, expected):
        unittest.TestCase.__init__(self)
        self.desc = desc
        self.args = args
        self.expected = expected

    def runTest(self):
        result = oidutil.appendArgs(*self.args)
        self.assertEqual(self.expected, result, self.args)

    def shortDescription(self):
        return self.desc



class TestSymbol(unittest.TestCase):
    def testCopyHash(self):
        import copy
        s = oidutil.Symbol("Foo")
        d = {s: 1}
        d_prime = copy.deepcopy(d)
        self.failUnless(s in d_prime, "%r isn't in %r" % (s, d_prime))

        t = oidutil.Symbol("Bar")
        self.failIfEqual(hash(s), hash(t))


def buildAppendTests():
    simple = 'http://www.example.com/'
    cases = [
        ('empty list',
         (simple, []),
         simple),

        ('empty dict',
         (simple, {}),
         simple),

        ('one list',
         (simple, [('a', 'b')]),
         simple + '?a=b'),

        ('one dict',
         (simple, {'a':'b'}),
         simple + '?a=b'),

        ('two list (same)',
         (simple, [('a', 'b'), ('a', 'c')]),
         simple + '?a=b&a=c'),

        ('two list',
         (simple, [('a', 'b'), ('b', 'c')]),
         simple + '?a=b&b=c'),

        ('two list (order)',
         (simple, [('b', 'c'), ('a', 'b')]),
         simple + '?b=c&a=b'),

        ('two dict (order)',
         (simple, {'b':'c', 'a':'b'}),
         simple + '?a=b&b=c'),

        ('escape',
         (simple, [('=', '=')]),
         simple + '?%3D=%3D'),

        ('escape (URL)',
         (simple, [('this_url', simple)]),
         simple + '?this_url=http%3A%2F%2Fwww.example.com%2F'),

        ('use dots',
         (simple, [('openid.stuff', 'bother')]),
         simple + '?openid.stuff=bother'),

        ('args exist (empty)',
         (simple + '?stuff=bother', []),
         simple + '?stuff=bother'),

        ('args exist',
         (simple + '?stuff=bother', [('ack', 'ack')]),
         simple + '?stuff=bother&ack=ack'),

        ('args exist',
         (simple + '?stuff=bother', [('ack', 'ack')]),
         simple + '?stuff=bother&ack=ack'),

        ('args exist (dict)',
         (simple + '?stuff=bother', {'ack': 'ack'}),
         simple + '?stuff=bother&ack=ack'),

        ('args exist (dict 2)',
         (simple + '?stuff=bother', {'ack': 'ack', 'zebra':'lion'}),
         simple + '?stuff=bother&ack=ack&zebra=lion'),

        ('three args (dict)',
         (simple, {'stuff': 'bother', 'ack': 'ack', 'zebra':'lion'}),
         simple + '?ack=ack&stuff=bother&zebra=lion'),

        ('three args (list)',
         (simple, [('stuff', 'bother'), ('ack', 'ack'), ('zebra', 'lion')]),
         simple + '?stuff=bother&ack=ack&zebra=lion'),
        ]

    tests = []

    for name, args, expected in cases:
        test = AppendArgsTest(name, args, expected)
        tests.append(test)

    return unittest.TestSuite(tests)

def pyUnitTests():
    some = buildAppendTests()
    some.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
    return some

def test_appendArgs():
    suite = buildAppendTests()
    suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
    runner = unittest.TextTestRunner()
    result = runner.run(suite)
    assert result.wasSuccessful()

# XXX: there are more functions that could benefit from being better
# specified and tested in oidutil.py These include, but are not
# limited to appendArgs

def test(skipPyUnit=True):
    test_base64()
    if not skipPyUnit:
        test_appendArgs()

if __name__ == '__main__':
    test(skipPyUnit=False)