Skip to content

Commit e5eed66

Browse files
committed
[test] Add tests verifying hash randomization. Update tests for new names.
1 parent 3e90284 commit e5eed66

File tree

3 files changed

+51
-32
lines changed

3 files changed

+51
-32
lines changed

validation-test/stdlib/FixedPoint.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ FixedPoint.test("${Self}.hashValue") {
242242
let input = get${Self}(${input})
243243
let output = getInt(input.hashValue)
244244

245-
var hasher = _SipHash13(key: _Hasher._secretKey)
245+
var hasher = _SipHash13(key: _Hasher._seed)
246246
% if prepare_bit_pattern(input, word_bits, self_ty.is_signed) == input:
247247
hasher.append(${input} as ${"" if self_ty.is_signed else "U"}Int)
248248
% else:

validation-test/stdlib/Hashing.swift

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ var HashingTestSuite = TestSuite("Hashing")
1010

1111
func checkHash(
1212
for value: UInt64,
13-
withKey key: (UInt64, UInt64),
13+
withSeed seed: (UInt64, UInt64),
1414
expected: UInt64,
1515
file: String = #file, line: UInt = #line
1616
) {
17-
var hasher = _Hasher(key: key)
17+
var hasher = _Hasher(seed: seed)
1818
hasher.append(bits: value)
1919
let hash = hasher.finalize()
2020
expectEqual(
@@ -24,23 +24,23 @@ func checkHash(
2424

2525
HashingTestSuite.test("_Hasher/CustomKeys") {
2626
// This assumes _Hasher implements SipHash-1-3.
27-
checkHash(for: 0, withKey: (0, 0), expected: 0xbd60acb658c79e45)
28-
checkHash(for: 0, withKey: (0, 1), expected: 0x1ce32b0b44e61175)
29-
checkHash(for: 0, withKey: (1, 0), expected: 0x9c44b7c8df2ca74b)
30-
checkHash(for: 0, withKey: (1, 1), expected: 0x9653ca0a3b455506)
31-
checkHash(for: 0, withKey: (.max, .max), expected: 0x3ab336a4895e4d36)
32-
33-
checkHash(for: 1, withKey: (0, 0), expected: 0x1e9f734161d62dd9)
34-
checkHash(for: 1, withKey: (0, 1), expected: 0xb6fcf32d09f76cba)
35-
checkHash(for: 1, withKey: (1, 0), expected: 0xacb556b13007504a)
36-
checkHash(for: 1, withKey: (1, 1), expected: 0x7defec680db51d24)
37-
checkHash(for: 1, withKey: (.max, .max), expected: 0x212798441870ef6b)
38-
39-
checkHash(for: .max, withKey: (0, 0), expected: 0x2f205be2fec8e38d)
40-
checkHash(for: .max, withKey: (0, 1), expected: 0x3ff7fa33381ecf7b)
41-
checkHash(for: .max, withKey: (1, 0), expected: 0x404afd8eb2c4b22a)
42-
checkHash(for: .max, withKey: (1, 1), expected: 0x855642d657c1bd46)
43-
checkHash(for: .max, withKey: (.max, .max), expected: 0x5b16b7a8181980c2)
27+
checkHash(for: 0, withSeed: (0, 0), expected: 0xbd60acb658c79e45)
28+
checkHash(for: 0, withSeed: (0, 1), expected: 0x1ce32b0b44e61175)
29+
checkHash(for: 0, withSeed: (1, 0), expected: 0x9c44b7c8df2ca74b)
30+
checkHash(for: 0, withSeed: (1, 1), expected: 0x9653ca0a3b455506)
31+
checkHash(for: 0, withSeed: (.max, .max), expected: 0x3ab336a4895e4d36)
32+
33+
checkHash(for: 1, withSeed: (0, 0), expected: 0x1e9f734161d62dd9)
34+
checkHash(for: 1, withSeed: (0, 1), expected: 0xb6fcf32d09f76cba)
35+
checkHash(for: 1, withSeed: (1, 0), expected: 0xacb556b13007504a)
36+
checkHash(for: 1, withSeed: (1, 1), expected: 0x7defec680db51d24)
37+
checkHash(for: 1, withSeed: (.max, .max), expected: 0x212798441870ef6b)
38+
39+
checkHash(for: .max, withSeed: (0, 0), expected: 0x2f205be2fec8e38d)
40+
checkHash(for: .max, withSeed: (0, 1), expected: 0x3ff7fa33381ecf7b)
41+
checkHash(for: .max, withSeed: (1, 0), expected: 0x404afd8eb2c4b22a)
42+
checkHash(for: .max, withSeed: (1, 1), expected: 0x855642d657c1bd46)
43+
checkHash(for: .max, withSeed: (.max, .max), expected: 0x5b16b7a8181980c2)
4444
}
4545

4646
HashingTestSuite.test("_Hasher/DefaultKey") {
@@ -52,22 +52,15 @@ HashingTestSuite.test("_Hasher/DefaultKey") {
5252
defaultHasher.append(bits: value)
5353
expectEqual(defaultHasher.finalize(), defaultHash)
5454

55-
var customHasher = _Hasher(key: _Hasher._secretKey)
55+
var customHasher = _Hasher(seed: _Hasher._seed)
5656
customHasher.append(bits: value)
5757
expectEqual(customHasher.finalize(), defaultHash)
5858
}
5959

60-
HashingTestSuite.test("_Hasher/keyOverride") {
61-
let value: UInt64 = 0x0102030405060708
62-
let expected = Int(truncatingIfNeeded: 0x661dac5d71c78013 as UInt64)
63-
64-
let originalKey = _Hasher._secretKey
65-
_Hasher._secretKey = (1, 2)
66-
let hash = _hashValue(for: value)
67-
_Hasher._secretKey = originalKey
68-
69-
expectEqual(hash, expected)
60+
HashingTestSuite.test("_Hasher/determinism") {
61+
// By defaults, tests are configured to run with deterministic hashing.
62+
expectTrue(_Hasher._isDeterministic)
63+
expectEqual((0, 0), _Hasher._seed)
7064
}
7165

7266
runAllTests()
73-
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -module-name main %s -o %t/hash
3+
// RUN: (export -n %env-SWIFT_DETERMINISTIC_HASHING; %target-run %t/hash && %target-run %t/hash) | %FileCheck %s
4+
// REQUIRES: executable_test
5+
6+
// This check verifies that the hash seed is randomly generated on every
7+
// execution of a Swift program. There is a minuscule chance that the same seed
8+
// is generated on two separate executions; however, a test failure here is more
9+
// likely to indicate an issue with the random number generator or the testing
10+
// environment.
11+
12+
print("Deterministic: \(_Hasher._isDeterministic)")
13+
print("Seed: \(_Hasher._seed)")
14+
print("Hash values: <\(0.hashValue), \(1.hashValue)>")
15+
16+
// On the first run, remember the seed and hash value.
17+
// CHECK: Deterministic: false
18+
// CHECK-NEXT: Seed: [[SEED0:\([0-9]+, [0-9]+\)]]
19+
// CHECK-NEXT: Hash values: [[HASH0:<-?[0-9]+, -?[0-9]+>]]
20+
21+
// Check that the values are different on the second run.
22+
// CHECK-NEXT: Deterministic: false
23+
// CHECK-NEXT: Seed:
24+
// CHECK-NOT: [[SEED0]]
25+
// CHECK-NEXT: Hash values:
26+
// CHECK-NOT: [[HASH0]]

0 commit comments

Comments
 (0)