1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/hash.h" 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <string> 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <vector> 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace base { 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST(HashTest, String) { 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::string str; 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Empty string (should hash to 0). 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = ""; 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(0u, Hash(str)); 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Simple test. 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello world"; 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(2794219650u, Hash(str)); 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Change one bit. 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "helmo world"; 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(1006697176u, Hash(str)); 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Insert a null byte. 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello world"; 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str[5] = '\0'; 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(2319902537u, Hash(str)); 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Test that the bytes after the null contribute to the hash. 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello worle"; 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str[5] = '\0'; 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(553904462u, Hash(str)); 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Extremely long string. 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Also tests strings with high bit set, and null byte. 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) std::vector<char> long_string_buffer; 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (int i = 0; i < 4096; ++i) 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) long_string_buffer.push_back((i % 256) - 128); 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str.assign(&long_string_buffer.front(), long_string_buffer.size()); 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(2797962408u, Hash(str)); 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // All possible lengths (mod 4). Tests separate code paths. Also test with 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // final byte high bit set (regression test for http://crbug.com/90659). 48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Note that the 1 and 3 cases have a weird bug where the final byte is 49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // treated as a signed char. It was decided on the above bug discussion to 50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // enshrine that behaviour as "correct" to avoid invalidating existing hashes. 51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Length mod 4 == 0. 53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello w\xab"; 54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(615571198u, Hash(str)); 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Length mod 4 == 1. 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello wo\xab"; 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(623474296u, Hash(str)); 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Length mod 4 == 2. 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello wor\xab"; 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(4278562408u, Hash(str)); 61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Length mod 4 == 3. 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello worl\xab"; 63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(3224633008u, Hash(str)); 64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TEST(HashTest, CString) { 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const char* str; 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Empty string (should hash to 0). 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = ""; 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(0u, Hash(str, strlen(str))); 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Simple test. 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello world"; 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(2794219650u, Hash(str, strlen(str))); 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Ensure that it stops reading after the given length, and does not expect a 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // null byte. 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) str = "hello world; don't read this part"; 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) EXPECT_EQ(2794219650u, Hash(str, strlen("hello world"))); 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace base 83