1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/*
* Copyright (C) 2013 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/platform/wtf/text/string_hasher.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/platform/wtf/text/case_folding_hash.h"
#include "third_party/blink/renderer/platform/wtf/text/convert_to_8bit_hash_reader.h"
namespace WTF {
namespace {
const char kNullLChars[1] = {0};
const UChar kNullUChars[1] = {0};
const uint64_t kEmptyStringHash = 0x5A6EF77074EBC84B;
const uint64_t kSingleNullCharacterHash = 0x48DFCE108249B3F8;
const LChar kTestALChars[5] = {0x41, 0x95, 0xFF, 0x50, 0x01};
const UChar kTestAUChars[5] = {0x41, 0x95, 0xFF, 0x50, 0x01};
const UChar kTestBUChars[5] = {0x41, 0x95, 0xFFFF, 0x1080, 0x01};
const uint64_t kTestAHash = 0xE9422771E0A5DDE6;
const uint64_t kTestBHash = 0x4A2DA770EEA75C1E;
} // anonymous namespace
TEST(StringHasherTest, StringHasher_ComputeHashAndMaskTop8Bits) {
EXPECT_EQ(kEmptyStringHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits(nullptr, 0));
EXPECT_EQ(kEmptyStringHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits(kNullLChars, 0));
EXPECT_EQ(kEmptyStringHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits<ConvertTo8BitHashReader>(
nullptr, 0));
EXPECT_EQ(kEmptyStringHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits<ConvertTo8BitHashReader>(
(const char*)kNullUChars, 0));
EXPECT_EQ(kSingleNullCharacterHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits(kNullLChars, 1));
EXPECT_EQ(kSingleNullCharacterHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits<ConvertTo8BitHashReader>(
(const char*)kNullUChars, 1));
EXPECT_EQ(kTestAHash & 0xFFFFFF, StringHasher::ComputeHashAndMaskTop8Bits(
(const char*)kTestALChars, 5));
EXPECT_EQ(kTestAHash & 0xFFFFFF,
StringHasher::ComputeHashAndMaskTop8Bits<ConvertTo8BitHashReader>(
(const char*)kTestAUChars, 5));
EXPECT_EQ(kTestBHash & 0xFFFFFF, StringHasher::ComputeHashAndMaskTop8Bits(
(const char*)kTestBUChars, 10));
// Test a slightly longer case (including characters that fit in Latin1
// but not in ASCII).
const char kStr[] = "A quick browñ föx jumps over thé lazy dog";
UChar kWideStr[sizeof(kStr)];
for (unsigned i = 0; i < sizeof(kStr); ++i) {
kWideStr[i] = static_cast<uint8_t>(kStr[i]);
}
EXPECT_EQ(StringHasher::ComputeHashAndMaskTop8Bits(kStr, strlen(kStr)),
StringHasher::ComputeHashAndMaskTop8Bits<ConvertTo8BitHashReader>(
(const char*)kWideStr, strlen(kStr)));
EXPECT_NE(StringHasher::ComputeHashAndMaskTop8Bits(kStr, strlen(kStr)),
StringHasher::ComputeHashAndMaskTop8Bits((const char*)kWideStr,
strlen(kStr)));
EXPECT_NE(StringHasher::ComputeHashAndMaskTop8Bits(kStr, strlen(kStr)),
StringHasher::ComputeHashAndMaskTop8Bits((const char*)kWideStr,
strlen(kStr) * 2));
}
TEST(StringHasherTest, StringHasher_HashMemory) {
EXPECT_EQ(kEmptyStringHash, StringHasher::HashMemory({}));
EXPECT_EQ(kEmptyStringHash, StringHasher::HashMemory(
base::as_byte_span(kNullUChars).first(0u)));
EXPECT_EQ(
kSingleNullCharacterHash,
StringHasher::HashMemory(base::as_byte_span(kNullUChars).first(1u)));
EXPECT_EQ(kTestAHash, StringHasher::HashMemory(kTestALChars));
EXPECT_EQ(kTestBHash,
StringHasher::HashMemory(base::as_byte_span(kTestBUChars)));
}
TEST(StringHasherTest, CaseFoldingHash) {
EXPECT_NE(CaseFoldingHash::GetHash("foo"), CaseFoldingHash::GetHash("bar"));
EXPECT_EQ(CaseFoldingHash::GetHash("foo"), CaseFoldingHash::GetHash("FOO"));
EXPECT_EQ(CaseFoldingHash::GetHash("foo"), CaseFoldingHash::GetHash("Foo"));
EXPECT_EQ(CaseFoldingHash::GetHash("Longer string 123"),
CaseFoldingHash::GetHash("longEr String 123"));
EXPECT_EQ(CaseFoldingHash::GetHash(String::FromUTF8("Ünicode")),
CaseFoldingHash::GetHash(String::FromUTF8("ünicode")));
}
TEST(StringHasherTest, ContractionAndExpansion) {
// CaseFoldingHash is the only current reader using the expansion logic,
// so we use it to test that the expansion logic is correct for various sizes;
// we don't really use the case folding itself here. We make a string that's
// long enough that we will hit most of the paths.
String str =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_!?'$";
for (unsigned i = 0; i < str.length(); ++i) {
String s8 = str.Substring(0, i);
String s16 = s8;
s16.Ensure16Bit();
EXPECT_EQ(CaseFoldingHash::GetHash(s8), CaseFoldingHash::GetHash(s16));
EXPECT_EQ(WTF::GetHash(s8), WTF::GetHash(s16));
}
}
} // namespace WTF
|