/openid/test/oidutil.py

https://bitbucket.org/jonasteuwen/python-openid
Python | 188 lines | 141 code | 39 blank | 8 comment | 7 complexity | 35e7db1271bcc000b963f81acec10f1a MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. import codecs
  4. import string
  5. import random
  6. from openid import oidutil
  7. def test_base64():
  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. def checkEncoded(s):
  14. for c in s:
  15. assert isAllowed(c), s
  16. cases = [
  17. '',
  18. 'x',
  19. '\x00',
  20. '\x01',
  21. '\x00' * 100,
  22. ''.join(map(chr, range(256))),
  23. ]
  24. for s in cases:
  25. b64 = oidutil.toBase64(s)
  26. checkEncoded(b64)
  27. s_prime = oidutil.fromBase64(b64)
  28. assert s_prime == s, (s, b64, s_prime)
  29. # Randomized test
  30. for _ in xrange(50):
  31. n = random.randrange(2048)
  32. s = ''.join(map(chr, map(lambda _: random.randrange(256), range(n))))
  33. b64 = oidutil.toBase64(s)
  34. checkEncoded(b64)
  35. s_prime = oidutil.fromBase64(b64)
  36. assert s_prime == s, (s, b64, s_prime)
  37. class AppendArgsTest(unittest.TestCase):
  38. def __init__(self, desc, args, expected):
  39. unittest.TestCase.__init__(self)
  40. self.desc = desc
  41. self.args = args
  42. self.expected = expected
  43. def runTest(self):
  44. result = oidutil.appendArgs(*self.args)
  45. self.assertEqual(self.expected, result, self.args)
  46. def shortDescription(self):
  47. return self.desc
  48. class TestUnicodeConversion(unittest.TestCase):
  49. def test_toUnicode(self):
  50. # Unicode objects pass through
  51. self.failUnless(isinstance(oidutil.toUnicode(u'fööbär'), unicode))
  52. self.assertEquals(oidutil.toUnicode(u'fööbär'), u'fööbär')
  53. # UTF-8 encoded string are decoded
  54. self.failUnless(isinstance(oidutil.toUnicode('fööbär'), unicode))
  55. self.assertEquals(oidutil.toUnicode('fööbär'), u'fööbär')
  56. # Other encodings raise exceptions
  57. self.assertRaises(UnicodeDecodeError, lambda: oidutil.toUnicode(u'fööbär'.encode('latin-1')))
  58. class TestSymbol(unittest.TestCase):
  59. def testCopyHash(self):
  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. t = oidutil.Symbol("Bar")
  66. self.failIfEqual(hash(s), hash(t))
  67. def buildAppendTests():
  68. simple = 'http://www.example.com/'
  69. cases = [
  70. ('empty list',
  71. (simple, []),
  72. simple),
  73. ('empty dict',
  74. (simple, {}),
  75. simple),
  76. ('one list',
  77. (simple, [('a', 'b')]),
  78. simple + '?a=b'),
  79. ('one dict',
  80. (simple, {'a':'b'}),
  81. simple + '?a=b'),
  82. ('two list (same)',
  83. (simple, [('a', 'b'), ('a', 'c')]),
  84. simple + '?a=b&a=c'),
  85. ('two list',
  86. (simple, [('a', 'b'), ('b', 'c')]),
  87. simple + '?a=b&b=c'),
  88. ('two list (order)',
  89. (simple, [('b', 'c'), ('a', 'b')]),
  90. simple + '?b=c&a=b'),
  91. ('two dict (order)',
  92. (simple, {'b':'c', 'a':'b'}),
  93. simple + '?a=b&b=c'),
  94. ('escape',
  95. (simple, [('=', '=')]),
  96. simple + '?%3D=%3D'),
  97. ('escape (URL)',
  98. (simple, [('this_url', simple)]),
  99. simple + '?this_url=http%3A%2F%2Fwww.example.com%2F'),
  100. ('use dots',
  101. (simple, [('openid.stuff', 'bother')]),
  102. simple + '?openid.stuff=bother'),
  103. ('args exist (empty)',
  104. (simple + '?stuff=bother', []),
  105. simple + '?stuff=bother'),
  106. ('args exist',
  107. (simple + '?stuff=bother', [('ack', 'ack')]),
  108. simple + '?stuff=bother&ack=ack'),
  109. ('args exist',
  110. (simple + '?stuff=bother', [('ack', 'ack')]),
  111. simple + '?stuff=bother&ack=ack'),
  112. ('args exist (dict)',
  113. (simple + '?stuff=bother', {'ack': 'ack'}),
  114. simple + '?stuff=bother&ack=ack'),
  115. ('args exist (dict 2)',
  116. (simple + '?stuff=bother', {'ack': 'ack', 'zebra':'lion'}),
  117. simple + '?stuff=bother&ack=ack&zebra=lion'),
  118. ('three args (dict)',
  119. (simple, {'stuff': 'bother', 'ack': 'ack', 'zebra':'lion'}),
  120. simple + '?ack=ack&stuff=bother&zebra=lion'),
  121. ('three args (list)',
  122. (simple, [('stuff', 'bother'), ('ack', 'ack'), ('zebra', 'lion')]),
  123. simple + '?stuff=bother&ack=ack&zebra=lion'),
  124. ]
  125. tests = []
  126. for name, args, expected in cases:
  127. test = AppendArgsTest(name, args, expected)
  128. tests.append(test)
  129. return unittest.TestSuite(tests)
  130. def pyUnitTests():
  131. some = buildAppendTests()
  132. some.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
  133. some.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestUnicodeConversion))
  134. return some
  135. def test_appendArgs():
  136. suite = buildAppendTests()
  137. suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(TestSymbol))
  138. runner = unittest.TextTestRunner()
  139. result = runner.run(suite)
  140. assert result.wasSuccessful()
  141. # XXX: there are more functions that could benefit from being better
  142. # specified and tested in oidutil.py These include, but are not
  143. # limited to appendArgs
  144. def test(skipPyUnit=True):
  145. test_base64()
  146. if not skipPyUnit:
  147. test_appendArgs()
  148. if __name__ == '__main__':
  149. test(skipPyUnit=False)