| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from rpython.rlib import rsha |
| 8 |
|
| 9 |
class TestSHA: |
| 10 |
def check(self, data, digest): |
| 11 |
computed = rsha.new(data).hexdigest() |
| 12 |
assert computed == digest |
| 13 |
d = rsha.sha() |
| 14 |
d.update(data) |
| 15 |
computed = d.digest() |
| 16 |
assert computed == digest.decode('hex') |
| 17 |
|
| 18 |
def test_case_1(self): |
| 19 |
self.check("abc", |
| 20 |
"a9993e364706816aba3e25717850c26c9cd0d89d") |
| 21 |
|
| 22 |
def test_case_2(self): |
| 23 |
self.check("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", |
| 24 |
"84983e441c3bd26ebaae4aa1f95129e5e54670f1") |
| 25 |
|
| 26 |
def disabled_too_slow_test_case_3(self): |
| 27 |
self.check("a" * 1000000, |
| 28 |
"34aa973cd4c4daa4f61eeb2bdbad27316534016f") |
| 29 |
|
| 30 |
def test_copy(self): |
| 31 |
import sha |
| 32 |
for repeat in [1, 10, 100]: |
| 33 |
d1 = rsha.sha("abc" * repeat) |
| 34 |
d2 = d1.copy() |
| 35 |
d1.update("def" * repeat) |
| 36 |
d2.update("gh" * repeat) |
| 37 |
assert d1.digest() == sha.sha("abc"*repeat+"def"*repeat).digest() |
| 38 |
assert d2.digest() == sha.sha("abc"*repeat+"gh"*repeat).digest() |
| 39 |
|
| 40 |
def test_random(self): |
| 41 |
import random, sha |
| 42 |
for i in range(20): |
| 43 |
input = ''.join([chr(random.randrange(256)) |
| 44 |
for i in range(random.randrange(1000))]) |
| 45 |
m1 = rsha.RSHA() |
| 46 |
m1.update(input) |
| 47 |
m2 = sha.new() |
| 48 |
m2.update(input) |
| 49 |
assert m2.hexdigest() == m1.hexdigest() |