16d3238a7ebb65f46e5c793a6a68feb2812533e9
[p2pool.git] / p2pool / test / bitcoin / test_sha256.py
1 from __future__ import division
2
3 import unittest
4 import hashlib
5 import random
6
7 from p2pool.bitcoin import sha256
8
9 class Test(unittest.TestCase):
10     def test_all(self):
11         for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
12             #print test
13             #print sha256.sha256(test).hexdigest()
14             #print hashlib.sha256(test).hexdigest()
15             #print
16             assert sha256.sha256(test).hexdigest() == hashlib.sha256(test).hexdigest()
17         def random_str(l):
18             return ''.join(chr(random.randrange(256)) for i in xrange(l))
19         for length in xrange(150):
20             test = random_str(length)
21             a = sha256.sha256(test).hexdigest()
22             b = hashlib.sha256(test).hexdigest()
23             #print length, a, b
24             if a != b:
25                 print 'ERROR!'
26                 raise ValueError()
27         for i in xrange(100):
28             test = random_str(int(random.expovariate(1/100)))
29             test2 = random_str(int(random.expovariate(1/100)))
30
31             a = sha256.sha256(test)
32             a = a.copy()
33             a.update(test2)
34             a = a.hexdigest()
35
36             b = hashlib.sha256(test)
37             b = b.copy()
38             b.update(test2)
39             b = b.hexdigest()
40             #print a, b
41             if a != b:
42                 print 'ERROR!'
43                 raise ValueError()