Path: blob/master/src/infrastructure/util/__tests__/PhabricatorHashTestCase.php
12242 views
<?php12final class PhabricatorHashTestCase extends PhabricatorTestCase {34public function testHashForIndex() {5$map = array(6'dog' => 'Aliif7Qjd5ct',7'cat' => 'toudDsue3Uv8',8'rat' => 'RswaKgTjqOuj',9'bat' => 'rAkJKyX4YdYm',10);1112foreach ($map as $input => $expect) {13$this->assertEqual(14$expect,15PhabricatorHash::digestForIndex($input),16pht('Input: %s', $input));17}1819// Test that the encoding produces 6 bits of entropy per byte.20$entropy = array(21'dog',22'cat',23'rat',24'bat',25'dig',26'fig',27'cot',28'cut',29'fog',30'rig',31'rug',32'dug',33'mat',34'pat',35'eat',36'tar',37'pot',38);3940$seen = array();41foreach ($entropy as $input) {42$chars = preg_split('//', PhabricatorHash::digestForIndex($input));43foreach ($chars as $char) {44$seen[$char] = true;45}46}4748$this->assertEqual(49(1 << 6),50count($seen),51pht('Distinct characters in hash of: %s', $input));52}5354public function testHashForAnchor() {55$map = array(56// For inputs with no "." or "_" in the output, digesting for an index57// or an anchor should be the same.58'dog' => array(59'Aliif7Qjd5ct',60'Aliif7Qjd5ct',61),62// When an output would contain "." or "_", it should be replaced with63// an alphanumeric character in those positions instead.64'fig' => array(65'OpB9tY4i.MOX',66'OpB9tY4imMOX',67),68'cot' => array(69'_iF26XU_PsVY',70'3iF26XUkPsVY',71),72// The replacement characters are well-distributed and generally keep73// the entropy of the output high: here, "_" characters in the initial74// positions of the digests of "cot" (above) and "dug" (this test) have75// different outputs.76'dug' => array(77'_XuQnp0LUlUW',78'7XuQnp0LUlUW',79),80);8182foreach ($map as $input => $expect) {83list($expect_index, $expect_anchor) = $expect;8485$this->assertEqual(86$expect_index,87PhabricatorHash::digestForIndex($input),88pht('Index digest of "%s".', $input));8990$this->assertEqual(91$expect_anchor,92PhabricatorHash::digestForAnchor($input),93pht('Anchor digest of "%s".', $input));94}95}9697}9899100