Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/util/__tests__/PhabricatorHashTestCase.php
12242 views
1
<?php
2
3
final class PhabricatorHashTestCase extends PhabricatorTestCase {
4
5
public function testHashForIndex() {
6
$map = array(
7
'dog' => 'Aliif7Qjd5ct',
8
'cat' => 'toudDsue3Uv8',
9
'rat' => 'RswaKgTjqOuj',
10
'bat' => 'rAkJKyX4YdYm',
11
);
12
13
foreach ($map as $input => $expect) {
14
$this->assertEqual(
15
$expect,
16
PhabricatorHash::digestForIndex($input),
17
pht('Input: %s', $input));
18
}
19
20
// Test that the encoding produces 6 bits of entropy per byte.
21
$entropy = array(
22
'dog',
23
'cat',
24
'rat',
25
'bat',
26
'dig',
27
'fig',
28
'cot',
29
'cut',
30
'fog',
31
'rig',
32
'rug',
33
'dug',
34
'mat',
35
'pat',
36
'eat',
37
'tar',
38
'pot',
39
);
40
41
$seen = array();
42
foreach ($entropy as $input) {
43
$chars = preg_split('//', PhabricatorHash::digestForIndex($input));
44
foreach ($chars as $char) {
45
$seen[$char] = true;
46
}
47
}
48
49
$this->assertEqual(
50
(1 << 6),
51
count($seen),
52
pht('Distinct characters in hash of: %s', $input));
53
}
54
55
public function testHashForAnchor() {
56
$map = array(
57
// For inputs with no "." or "_" in the output, digesting for an index
58
// or an anchor should be the same.
59
'dog' => array(
60
'Aliif7Qjd5ct',
61
'Aliif7Qjd5ct',
62
),
63
// When an output would contain "." or "_", it should be replaced with
64
// an alphanumeric character in those positions instead.
65
'fig' => array(
66
'OpB9tY4i.MOX',
67
'OpB9tY4imMOX',
68
),
69
'cot' => array(
70
'_iF26XU_PsVY',
71
'3iF26XUkPsVY',
72
),
73
// The replacement characters are well-distributed and generally keep
74
// the entropy of the output high: here, "_" characters in the initial
75
// positions of the digests of "cot" (above) and "dug" (this test) have
76
// different outputs.
77
'dug' => array(
78
'_XuQnp0LUlUW',
79
'7XuQnp0LUlUW',
80
),
81
);
82
83
foreach ($map as $input => $expect) {
84
list($expect_index, $expect_anchor) = $expect;
85
86
$this->assertEqual(
87
$expect_index,
88
PhabricatorHash::digestForIndex($input),
89
pht('Index digest of "%s".', $input));
90
91
$this->assertEqual(
92
$expect_anchor,
93
PhabricatorHash::digestForAnchor($input),
94
pht('Anchor digest of "%s".', $input));
95
}
96
}
97
98
}
99
100