File: oidutil.py

package info (click to toggle)
python3-openid 3.0.2%2Bgit20140828-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,672 kB
  • ctags: 2,679
  • sloc: python: 17,137; xml: 234; sh: 15; makefile: 4
file content (197 lines) | stat: -rw-r--r-- 5,442 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
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[ord(c)] = None
    isAllowed = allowed_d.__contains__

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

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

    for s in cases:
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert str(s_prime, encoding="utf-8") == s, (s, b64, s_prime)

    # Randomized test
    for _ in range(50):
        n = random.randrange(2048)
        s = ''.join(map(chr, [random.randrange(256) for _ in range(n)]))
        b64 = oidutil.toBase64(s)
        checkEncoded(b64)
        s_prime = oidutil.fromBase64(b64)
        assert str(s_prime, encoding="utf-8") == 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 TestUnicodeConversion(unittest.TestCase):

    def test_toUnicode(self):
        # Unicode objects pass through
        self.assertTrue(isinstance(oidutil.toUnicode('fööbär'), str))
        self.assertEqual(oidutil.toUnicode('fööbär'), 'fööbär')
        # UTF-8 encoded string are decoded
        self.assertTrue(isinstance(oidutil.toUnicode('fööbär'), str))
        self.assertEqual(oidutil.toUnicode('fööbär'), 'fööbär')
        # Other encodings raise exceptions
        self.assertRaises(
            UnicodeDecodeError,
            lambda: oidutil.toUnicode('fööbär'.encode('latin-1')))


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

        t = oidutil.Symbol("Bar")
        self.assertNotEqual(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))
    some.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUnicodeConversion))
    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)