1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/hash/hash.h"
6
7 #include <cstdint>
8 #include <string>
9 #include <string_view>
10 #include <vector>
11
12 #include "base/containers/span.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16
TEST(HashTest,DeprecatedHashFromString)17 TEST(HashTest, DeprecatedHashFromString) {
18 std::string str;
19 // Empty string (should hash to 0).
20 str = "";
21 EXPECT_EQ(0u, Hash(str));
22
23 // Simple test.
24 str = "hello world";
25 EXPECT_EQ(2794219650u, Hash(str));
26
27 // Change one bit.
28 str = "helmo world";
29 EXPECT_EQ(1006697176u, Hash(str));
30
31 // Insert a null byte.
32 str = "hello world";
33 str[5] = '\0';
34 EXPECT_EQ(2319902537u, Hash(str));
35
36 // Test that the bytes after the null contribute to the hash.
37 str = "hello worle";
38 str[5] = '\0';
39 EXPECT_EQ(553904462u, Hash(str));
40
41 // Extremely long string.
42 // Also tests strings with high bit set, and null byte.
43 std::vector<char> long_string_buffer;
44 for (int i = 0; i < 4096; ++i)
45 long_string_buffer.push_back((i % 256) - 128);
46 str.assign(&long_string_buffer.front(), long_string_buffer.size());
47 EXPECT_EQ(2797962408u, Hash(str));
48
49 // All possible lengths (mod 4). Tests separate code paths. Also test with
50 // final byte high bit set (regression test for http://crbug.com/90659).
51 // Note that the 1 and 3 cases have a weird bug where the final byte is
52 // treated as a signed char. It was decided on the above bug discussion to
53 // enshrine that behaviour as "correct" to avoid invalidating existing hashes.
54
55 // Length mod 4 == 0.
56 str = "hello w\xab";
57 EXPECT_EQ(615571198u, Hash(str));
58 // Length mod 4 == 1.
59 str = "hello wo\xab";
60 EXPECT_EQ(623474296u, Hash(str));
61 // Length mod 4 == 2.
62 str = "hello wor\xab";
63 EXPECT_EQ(4278562408u, Hash(str));
64 // Length mod 4 == 3.
65 str = "hello worl\xab";
66 EXPECT_EQ(3224633008u, Hash(str));
67 }
68
TEST(HashTest,DeprecatedHashFromCString)69 TEST(HashTest, DeprecatedHashFromCString) {
70 const char* str;
71 // Empty string (should hash to 0).
72 str = "";
73 EXPECT_EQ(0u, Hash(str));
74
75 // Simple test.
76 str = "hello world";
77 EXPECT_EQ(2794219650u, Hash(str));
78 }
79
TEST(HashTest,PersistentHashFromSpan)80 TEST(HashTest, PersistentHashFromSpan) {
81 // Empty span (should hash to 0).
82 EXPECT_EQ(0u, PersistentHash(base::span<const uint8_t>()));
83
84 // The hash loads four bytes at a time, and is sensitive to the high bit of
85 // the last few bytes. Test hashes of various lengths, and with and without
86 // the high bit set, to confirm the persistent hash remains persistent.
87 const std::string_view str1 = "hello world";
88 const std::array<uint32_t, 12> kHashesByLength1 = {
89 0u, 1213478405u, 2371107848u, 2412215855u,
90 2296013106u, 2963130491u, 342812795u, 1345887711u,
91 2394271580u, 2806845956u, 2484860346u, 2794219650u};
92 for (size_t i = 0; i <= str1.size(); i++) {
93 SCOPED_TRACE(i);
94 EXPECT_EQ(kHashesByLength1[i],
95 PersistentHash(as_byte_span(str1.substr(0, i))));
96 }
97
98 const std::string_view str2 = "\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa";
99 const std::array<uint32_t, 12> kHashesByLength2 = {
100 0u, 2524495555u, 901867827u, 52332316u,
101 1053305007u, 4170027104u, 1891345481u, 2246421829u,
102 1241531838u, 4191939542u, 4100345281u, 896950651u};
103 for (size_t i = 0; i <= str2.size(); i++) {
104 SCOPED_TRACE(i);
105 EXPECT_EQ(kHashesByLength2[i],
106 PersistentHash(as_byte_span(str2.substr(0, i))));
107 }
108 }
109
TEST(HashTest,FastHash)110 TEST(HashTest, FastHash) {
111 std::string s;
112 constexpr char kEmptyString[] = "";
113 EXPECT_EQ(FastHash(s), FastHash(kEmptyString));
114 }
115
116 } // namespace base
117