1  import sys 
  2  import random 
  3  import os.path 
  4   
  5  from openid import cryptutil 
  6   
  7   
  8   
  9   
 29   
 31      if hasattr(cryptutil, 'reversed'): 
 32          cases = [ 
 33              ('', ''), 
 34              ('a', 'a'), 
 35              ('ab', 'ba'), 
 36              ('abc', 'cba'), 
 37              ('abcdefg', 'gfedcba'), 
 38              ([], []), 
 39              ([1], [1]), 
 40              ([1,2], [2,1]), 
 41              ([1,2,3], [3,2,1]), 
 42              (range(1000), range(999, -1, -1)), 
 43              ] 
 44   
 45          for case, expected in cases: 
 46              expected = list(expected) 
 47              actual = list(cryptutil.reversed(case)) 
 48              assert actual == expected, (case, expected, actual) 
 49              twice = list(cryptutil.reversed(actual)) 
 50              assert twice == list(case), (actual, case, twice) 
  51   
 53      MAX = sys.maxint 
 54      for iteration in xrange(500): 
 55          n = 0L 
 56          for i in range(10): 
 57              n += long(random.randrange(MAX)) 
 58   
 59          s = cryptutil.longToBinary(n) 
 60          assert type(s) is str 
 61          n_prime = cryptutil.binaryToLong(s) 
 62          assert n == n_prime, (n, n_prime) 
 63   
 64      cases = [ 
 65          ('\x00', 0L), 
 66          ('\x01', 1L), 
 67          ('\x7F', 127L), 
 68          ('\x00\xFF', 255L), 
 69          ('\x00\x80', 128L), 
 70          ('\x00\x81', 129L), 
 71          ('\x00\x80\x00', 32768L), 
 72          ('OpenID is cool', 1611215304203901150134421257416556L) 
 73          ] 
 74   
 75      for s, n in cases: 
 76          n_prime = cryptutil.binaryToLong(s) 
 77          s_prime = cryptutil.longToBinary(n) 
 78          assert n == n_prime, (s, n, n_prime) 
 79          assert s == s_prime, (n, s, s_prime) 
  80   
 82      f = file(os.path.join(os.path.dirname(__file__), 'n2b64')) 
 83      try: 
 84          for line in f: 
 85              parts = line.strip().split(' ') 
 86              assert parts[0] == cryptutil.longToBase64(long(parts[1])) 
 87      finally: 
 88          f.close() 
  89   
 91      f = file(os.path.join(os.path.dirname(__file__), 'n2b64')) 
 92      try: 
 93          for line in f: 
 94              parts = line.strip().split(' ') 
 95              assert long(parts[1]) == cryptutil.base64ToLong(parts[0]) 
 96      finally: 
 97          f.close() 
  98   
 99   
106   
107  if __name__ == '__main__': 
108      test() 
109