| 1 |
import os |
| 2 |
from rpython.rlib.rsiphash import siphash24, _siphash24, choosen_seed |
| 3 |
from rpython.rlib.rsiphash import initialize_from_env, enable_siphash24 |
| 4 |
from rpython.rlib.rsiphash import ll_hash_string_siphash24 |
| 5 |
from rpython.rlib.rsiphash import siphash24_with_key |
| 6 |
from rpython.rlib.objectmodel import compute_hash |
| 7 |
from rpython.rlib.rarithmetic import intmask |
| 8 |
from rpython.rtyper.annlowlevel import llstr, llunicode |
| 9 |
from rpython.rtyper.lltypesystem import llmemory, rffi |
| 10 |
from rpython.translator.c.test.test_genc import compile |
| 11 |
|
| 12 |
|
| 13 |
CASES = [ |
| 14 |
(2323638336262702335 , ""), |
| 15 |
(5150479602681463644 , "h"), |
| 16 |
(1013213613370725794 , "he"), |
| 17 |
(7028032310911240238 , "hel"), |
| 18 |
(9535960132410784494 , "hell"), |
| 19 |
(3256502711089771242 , "hello"), |
| 20 |
(2389188832234450176 , "hello "), |
| 21 |
(13253855839845990393, "hello w"), |
| 22 |
(7850036019043917323 , "hello wo"), |
| 23 |
(14283308628425005953, "hello wor"), |
| 24 |
(9605549962279590084 , "hello worl"), |
| 25 |
(16371281469632894235, "hello world"), |
| 26 |
(7298637955795769949 , "hello world\x9a"), |
| 27 |
(13530878135053370821, "hello world\xf3\x80"), |
| 28 |
(1643533543579802994 , "\xffhel\x82lo world\xbc"), |
| 29 |
(14632093238728197380, "hexlylxox rewqw"), |
| 30 |
(3434253029196696424 , "hexlylxox rewqws"), |
| 31 |
(9855754545877066788 , "hexlylxox rewqwsv"), |
| 32 |
(5233065012564472454 , "hexlylxox rewqwkashdw89"), |
| 33 |
(16768585622569081808, "hexlylxox rewqwkeashdw89"), |
| 34 |
(17430482483431293463, "HEEExlylxox rewqwkashdw89"), |
| 35 |
(695783005783737705 , "hello woadwealidewd 3829ez 32ig dxwaebderld"), |
| 36 |
] |
| 37 |
|
| 38 |
def check(s): |
| 39 |
q = rffi.str2charp('?' + s) |
| 40 |
with choosen_seed(0x8a9f065a358479f4, 0x11cb1e9ee7f40e1f, |
| 41 |
test_misaligned_path=True): |
| 42 |
x = siphash24(s) |
| 43 |
y = _siphash24(llmemory.cast_ptr_to_adr(rffi.ptradd(q, 1)), len(s)) |
| 44 |
z = ll_hash_string_siphash24(llstr(s)) |
| 45 |
rffi.free_charp(q) |
| 46 |
assert x == y |
| 47 |
assert z == intmask(x) |
| 48 |
return x |
| 49 |
|
| 50 |
def test_siphash24(): |
| 51 |
for expected, string in CASES: |
| 52 |
assert check(string) == expected |
| 53 |
|
| 54 |
def test_siphash24_with_key(): |
| 55 |
from rpython.rlib.rarithmetic import r_uint64 |
| 56 |
assert siphash24_with_key(b"abcdef", r_uint64(1)) == r_uint64(7956077396882317016L) |
| 57 |
|
| 58 |
def check_latin1(s, expected, test_prebuilt=False): |
| 59 |
with choosen_seed(0x8a9f065a358479f4, 0x11cb1e9ee7f40e1f, |
| 60 |
test_misaligned_path=True, test_prebuilt=test_prebuilt): |
| 61 |
z = ll_hash_string_siphash24(llunicode(s)) |
| 62 |
assert z == intmask(expected) |
| 63 |
|
| 64 |
def test_siphash24_latin1_unicode(): |
| 65 |
for expected, string in CASES: |
| 66 |
check_latin1(string.decode('latin1'), expected) |
| 67 |
|
| 68 |
def test_siphash24_latin1_unicode_prebuilt(): |
| 69 |
for expected, string in CASES: |
| 70 |
check_latin1(string.decode('latin1'), expected, test_prebuilt=True) |
| 71 |
|
| 72 |
def test_fix_seed(): |
| 73 |
old_val = os.environ.get('PYTHONHASHSEED', None) |
| 74 |
try: |
| 75 |
os.environ['PYTHONHASHSEED'] = '0' |
| 76 |
initialize_from_env() |
| 77 |
assert siphash24("foo") == 15988776847138518036 |
| 78 |
|
| 79 |
|
| 80 |
os.environ['PYTHONHASHSEED'] = '4000000000' |
| 81 |
initialize_from_env() |
| 82 |
assert siphash24("foo") == 13829150778707464258 |
| 83 |
|
| 84 |
|
| 85 |
for env in ['', 'random']: |
| 86 |
os.environ['PYTHONHASHSEED'] = env |
| 87 |
initialize_from_env() |
| 88 |
hash1 = siphash24("foo") |
| 89 |
initialize_from_env() |
| 90 |
hash2 = siphash24("foo") |
| 91 |
assert hash1 != hash2 |
| 92 |
finally: |
| 93 |
if old_val is None: |
| 94 |
del os.environ['PYTHONHASHSEED'] |
| 95 |
else: |
| 96 |
os.environ['PYTHONHASHSEED'] = old_val |
| 97 |
|
| 98 |
def test_translated(): |
| 99 |
d1 = {"foo": 123} |
| 100 |
d2 = {u"foo": 456, u"\u1234\u5678": 789} |
| 101 |
class G: |
| 102 |
pass |
| 103 |
g = G() |
| 104 |
g.v1 = d1.copy() |
| 105 |
g.v2 = d2.copy() |
| 106 |
|
| 107 |
def fetch(n): |
| 108 |
if n == 0: return d1.get("foo", -1) |
| 109 |
if n == 1: return g.v1.get("foo", -1) |
| 110 |
if n == 2: return compute_hash("foo") |
| 111 |
if n == 3: return d2.get(u"foo", -1) |
| 112 |
if n == 4: return g.v2.get(u"foo", -1) |
| 113 |
if n == 5: return compute_hash(u"foo") |
| 114 |
if n == 6: return d2.get(u"\u1234\u5678", -1) |
| 115 |
if n == 7: return g.v2.get(u"\u1234\u5678", -1) |
| 116 |
if n == 8: return compute_hash(u"\u1234\u5678") |
| 117 |
assert 0 |
| 118 |
|
| 119 |
def entrypoint(n): |
| 120 |
enable_siphash24() |
| 121 |
g.v1["bar"] = -2 |
| 122 |
g.v2[u"bar"] = -2 |
| 123 |
if n >= 0: |
| 124 |
|
| 125 |
return str(fetch(n)) |
| 126 |
else: |
| 127 |
|
| 128 |
|
| 129 |
return ' '.join([str(fetch(n)) for n in range(9)]) |
| 130 |
|
| 131 |
fn = compile(entrypoint, [int]) |
| 132 |
|
| 133 |
def getall(): |
| 134 |
return [int(fn(i)) for i in range(9)] |
| 135 |
|
| 136 |
old_val = os.environ.get('PYTHONHASHSEED', None) |
| 137 |
try: |
| 138 |
os.environ['PYTHONHASHSEED'] = '0' |
| 139 |
s1 = getall() |
| 140 |
assert s1[:8] == [ |
| 141 |
123, 123, intmask(15988776847138518036), |
| 142 |
456, 456, intmask(15988776847138518036), |
| 143 |
789, 789] |
| 144 |
assert s1[8] in [intmask(17593683438421985039), |
| 145 |
intmask(94801584261658677), |
| 146 |
intmask(3849431280840015342),] |
| 147 |
|
| 148 |
os.environ['PYTHONHASHSEED'] = '3987654321' |
| 149 |
s1 = getall() |
| 150 |
assert s1[:8] == [ |
| 151 |
123, 123, intmask(5890804383681474441), |
| 152 |
456, 456, intmask(5890804383681474441), |
| 153 |
789, 789] |
| 154 |
assert s1[8] in [intmask(4192582507672183374), |
| 155 |
intmask(7179255293164649778), |
| 156 |
intmask(-3945781295304514711),] |
| 157 |
|
| 158 |
for env in ['', 'random']: |
| 159 |
os.environ['PYTHONHASHSEED'] = env |
| 160 |
s1 = map(int, fn(-1).split()) |
| 161 |
s2 = map(int, fn(-1).split()) |
| 162 |
assert s1[0:2]+s1[3:5]+s1[6:8] == [123, 123, 456, 456, 789, 789] |
| 163 |
assert s1[2] == s1[5] |
| 164 |
assert s2[0:2]+s2[3:5]+s2[6:8] == [123, 123, 456, 456, 789, 789] |
| 165 |
assert s2[2] == s2[5] |
| 166 |
|
| 167 |
assert len(set([s1[2], s2[2], s1[8], s2[8]])) == 4 |
| 168 |
|
| 169 |
finally: |
| 170 |
if old_val is None: |
| 171 |
del os.environ['PYTHONHASHSEED'] |
| 172 |
else: |
| 173 |
os.environ['PYTHONHASHSEED'] = old_val |