| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" |
| 5 #include "components/safe_browsing_db/v4_rice.h" | 6 #include "components/safe_browsing_db/v4_rice.h" |
| 6 #include "testing/platform_test.h" | 7 #include "testing/platform_test.h" |
| 7 | 8 |
| 8 using ::google::protobuf::RepeatedField; | 9 using ::google::protobuf::RepeatedField; |
| 9 using ::google::protobuf::int32; | 10 using ::google::protobuf::int32; |
| 10 | 11 |
| 11 namespace safe_browsing { | 12 namespace safe_browsing { |
| 12 | 13 |
| 13 class V4RiceTest : public PlatformTest { | 14 class V4RiceTest : public PlatformTest { |
| 14 public: | 15 public: |
| 15 V4RiceTest() {} | 16 V4RiceTest() {} |
| 17 |
| 18 struct RiceDecodingTestInfo { |
| 19 uint32_t rice_parameter; |
| 20 std::vector<uint32_t> expected_values; |
| 21 std::string encoded_string; |
| 22 |
| 23 RiceDecodingTestInfo(const uint32_t in_rice_parameter, |
| 24 const std::vector<uint32_t>& in_expected_values, |
| 25 const std::string& in_encoded_string) { |
| 26 rice_parameter = in_rice_parameter; |
| 27 expected_values = in_expected_values; |
| 28 encoded_string = in_encoded_string; |
| 29 } |
| 30 }; |
| 31 |
| 32 void VerifyRiceDecoding(const RiceDecodingTestInfo& test_info) { |
| 33 const uint32_t num_entries = test_info.expected_values.size(); |
| 34 V4RiceDecoder decoder(test_info.rice_parameter, num_entries, |
| 35 test_info.encoded_string); |
| 36 uint32_t word; |
| 37 for (const auto& expected : test_info.expected_values) { |
| 38 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); |
| 39 EXPECT_EQ(expected, word); |
| 40 } |
| 41 ASSERT_FALSE(decoder.HasAnotherValue()); |
| 42 } |
| 16 }; | 43 }; |
| 17 | 44 |
| 18 TEST_F(V4RiceTest, TestDecoderGetNextWordWithNoData) { | 45 TEST_F(V4RiceTest, TestDecoderGetNextWordWithNoData) { |
| 19 uint32_t word; | 46 uint32_t word; |
| 20 V4RiceDecoder decoder(5, 1, ""); | 47 V4RiceDecoder decoder(5, 1, ""); |
| 21 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextWord(&word)); | 48 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextWord(&word)); |
| 22 } | 49 } |
| 23 | 50 |
| 24 TEST_F(V4RiceTest, TestDecoderGetNextBitsWithNoData) { | 51 TEST_F(V4RiceTest, TestDecoderGetNextBitsWithNoData) { |
| 25 uint32_t word; | 52 uint32_t word; |
| 26 V4RiceDecoder decoder(5, 1, ""); | 53 V4RiceDecoder decoder(5, 1, ""); |
| 27 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextBits(1, &word)); | 54 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextBits(1, &word)); |
| 28 } | 55 } |
| 29 | 56 |
| 30 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoData) { | 57 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoData) { |
| 31 uint32_t word; | 58 uint32_t word; |
| 32 V4RiceDecoder decoder(5, 1, ""); | 59 V4RiceDecoder decoder(5, 1, ""); |
| 33 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextValue(&word)); | 60 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextValue(&word)); |
| 34 } | 61 } |
| 35 | 62 |
| 36 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoEntries) { | 63 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoEntries) { |
| 37 uint32_t word; | 64 uint32_t word; |
| 38 V4RiceDecoder decoder(28, 0, "\xbf\xa8"); | 65 V4RiceDecoder decoder(28, 0, "\xbf\xa8"); |
| 39 EXPECT_FALSE(decoder.HasAnotherValue()); | 66 ASSERT_FALSE(decoder.HasAnotherValue()); |
| 40 EXPECT_EQ(DECODE_NO_MORE_ENTRIES_FAILURE, decoder.GetNextValue(&word)); | 67 EXPECT_EQ(DECODE_NO_MORE_ENTRIES_FAILURE, decoder.GetNextValue(&word)); |
| 41 } | 68 } |
| 42 | 69 |
| 43 TEST_F(V4RiceTest, TestDecoderGetNextValueWithSmallValues) { | 70 TEST_F(V4RiceTest, TestDecoderGetNextValueWithInterestingValues) { |
| 44 uint32_t word; | 71 // These values are interesting because they match the unit test |
| 45 V4RiceDecoder decoder(2, 2, "\xf7\x2"); | 72 // values used within Google to this test this code in other |
| 46 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | 73 // components, such as the SafeBrowsing service itself. |
| 47 EXPECT_EQ(15u, word); | |
| 48 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | |
| 49 EXPECT_EQ(9u, word); | |
| 50 EXPECT_FALSE(decoder.HasAnotherValue()); | |
| 51 } | |
| 52 | 74 |
| 53 TEST_F(V4RiceTest, TestDecoderGetNextValueWithLargeValues) { | 75 std::vector<RiceDecodingTestInfo> test_inputs = { |
| 54 uint32_t word; | 76 RiceDecodingTestInfo(2, {15, 9}, "\xf7\x2"), |
| 55 V4RiceDecoder decoder(28, 3, | 77 RiceDecodingTestInfo( |
| 56 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38"); | 78 28, {1777762129, 2093280223, 924369848}, |
| 57 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | 79 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38"), |
| 58 EXPECT_EQ(1777762129u, word); | 80 RiceDecodingTestInfo( |
| 59 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | 81 28, {62763050, 1046523781, 192522171, 1800511020, 4442775, 582142548}, |
| 60 EXPECT_EQ(2093280223u, word); | 82 "\x54\x60\x7b\xe7\x0a\x5f\xc1\xdc\xee\x69\xde" |
| 61 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | 83 "\xfe\x58\x3c\xa3\xd6\xa5\xf2\x10\x8c\x4a\x59" |
| 62 EXPECT_EQ(924369848u, word); | 84 "\x56\x00"), |
| 63 EXPECT_FALSE(decoder.HasAnotherValue()); | 85 RiceDecodingTestInfo( |
| 86 28, {26067715, 344823336, 8420095, 399843890, 95029378, 731622412, |
| 87 35811335, 1047558127, 1117722715, 78698892}, |
| 88 "\x06\x86\x1b\x23\x14\xcb\x46\xf2\xaf\x07\x08\xc9\x88\x54\x1f\x41\x04" |
| 89 "\xd5\x1a\x03\xeb\xe6\x3a\x80\x13\x91\x7b\xbf\x83\xf3\xb7\x85\xf1\x29" |
| 90 "\x18\xb3\x61\x09"), |
| 91 RiceDecodingTestInfo( |
| 92 27, {225846818, 328287420, 166748623, 29117720, 552397365, 350353215, |
| 93 558267528, 4738273, 567093445, 28563065, 55077698, 73091685, |
| 94 339246010, 98242620, 38060941, 63917830, 206319759, 137700744}, |
| 95 "\x89\x98\xd8\x75\xbc\x44\x91\xeb\x39\x0c\x3e\x30\x9a\x78\xf3\x6a\xd4" |
| 96 "\xd9\xb1\x9f\xfb\x70\x3e\x44\x3e\xa3\x08\x67\x42\xc2\x2b\x46\x69\x8e" |
| 97 "\x3c\xeb\xd9\x10\x5a\x43\x9a\x32\xa5\x2d\x4e\x77\x0f\x87\x78\x20\xb6" |
| 98 "\xab\x71\x98\x48\x0c\x9e\x9e\xd7\x23\x0c\x13\x43\x2c\xa9\x01"), |
| 99 RiceDecodingTestInfo( |
| 100 28, {339784008, 263128563, 63871877, 69723256, 826001074, 797300228, |
| 101 671166008, 207712688}, |
| 102 std::string("\x21\xc5\x02\x91\xf9\x82\xd7\x57\xb8\xe9\x3c\xf0\xc8\x4f" |
| 103 "\xe8\x64\x8d\x77\x62\x04\xd6\x85\x3f\x1c\x97\x00\x04\x1b" |
| 104 "\x17\xc6", |
| 105 30)), |
| 106 RiceDecodingTestInfo( |
| 107 28, {471820069, 196333855, 855579133, 122737976, 203433838, 85354544, |
| 108 1307949392, 165938578, 195134475, 553930435, 49231136}, |
| 109 "\x95\x9c\x7d\xb0\x8f\xe8\xd9\xbd\xfe\x8c\x7f\x81\x53\x0d\x75\xdc\x4e" |
| 110 "\x40\x18\x0c\x9a\x45\x3d\xa8\xdc\xfa\x26\x59\x40\x9e\x16\x08\x43\x77" |
| 111 "\xc3\x4e\x04\x01\xa4\xe6\x5d\x00"), |
| 112 RiceDecodingTestInfo( |
| 113 27, {87336845, 129291033, 30906211, 433549264, 30899891, 53207875, |
| 114 11959529, 354827862, 82919275, 489637251, 53561020, 336722992, |
| 115 408117728, 204506246, 188216092, 9047110, 479817359, 230317256}, |
| 116 "\x1a\x4f\x69\x2a\x63\x9a\xf6\xc6\x2e\xaf\x73\xd0\x6f\xd7\x31\xeb\x77" |
| 117 "\x1d\x43\xe3\x2b\x93\xce\x67\x8b\x59\xf9\x98\xd4\xda\x4f\x3c\x6f\xb0" |
| 118 "\xe8\xa5\x78\x8d\x62\x36\x18\xfe\x08\x1e\x78\xd8\x14\x32\x24\x84\x61" |
| 119 "\x1c\xf3\x37\x63\xc4\xa0\x88\x7b\x74\xcb\x64\xc8\x5c\xba\x05"), |
| 120 RiceDecodingTestInfo( |
| 121 28, {297968956, 19709657, 259702329, 76998112, 1023176123, 29296013, |
| 122 1602741145, 393745181, 177326295, 55225536, 75194472}, |
| 123 "\xf1\x94\x0a\x87\x6c\x5f\x96\x90\xe3\xab\xf7\xc0\xcb\x2d\xe9\x76\xdb" |
| 124 "\xf8\x59\x63\xc1\x6f\x7c\x99\xe3\x87\x5f\xc7\x04\xde\xb9\x46\x8e\x54" |
| 125 "\xc0\xac\x4a\x03\x0d\x6c\x8f\x00"), |
| 126 RiceDecodingTestInfo( |
| 127 28, {532220688, 780594691, 436816483, 163436269, 573044456, 1069604, |
| 128 39629436, 211410997, 227714491, 381562898, 75610008, 196754597, |
| 129 40310339, 15204118, 99010842}, |
| 130 "\x41\x2c\xe4\xfe\x06\xdc\x0d\xbd\x31\xa5\x04\xd5\x6e\xdd\x9b\x43\xb7" |
| 131 "\x3f\x11\x24\x52\x10\x80\x4f\x96\x4b\xd4\x80\x67\xb2\xdd\x52\xc9\x4e" |
| 132 "\x02\xc6\xd7\x60\xde\x06\x92\x52\x1e\xdd\x35\x64\x71\x26\x2c\xfe\xcf" |
| 133 "\x81\x46\xb2\x79\x01"), |
| 134 RiceDecodingTestInfo( |
| 135 28, {219354713, 389598618, 750263679, 554684211, 87381124, 4523497, |
| 136 287633354, 801308671, 424169435, 372520475, 277287849}, |
| 137 "\xb2\x2c\x26\x3a\xcd\x66\x9c\xdb\x5f\x07\x2e\x6f\xe6\xf9\x21\x10\x52" |
| 138 "\xd5\x94\xf4\x82\x22\x48\xf9\x9d\x24\xf6\xff\x2f\xfc\x6d\x3f\x21\x65" |
| 139 "\x1b\x36\x34\x56\xea\xc4\x21\x00"), |
| 140 }; |
| 141 |
| 142 for (size_t i = 0; i < test_inputs.size(); i++) { |
| 143 DVLOG(1) << "Running test case: " << i; |
| 144 VerifyRiceDecoding(test_inputs[i]); |
| 145 } |
| 64 } | 146 } |
| 65 | 147 |
| 66 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 148 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 67 // This test hits a NOTREACHED so it is a release mode only test. | 149 // This test hits a NOTREACHED so it is a release mode only test. |
| 68 TEST_F(V4RiceTest, TestDecoderIntegersWithNoData) { | 150 TEST_F(V4RiceTest, TestDecoderIntegersWithNoData) { |
| 69 RepeatedField<int32> out; | 151 RepeatedField<int32> out; |
| 70 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, | 152 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, |
| 71 V4RiceDecoder::DecodeIntegers(3, 5, 1, "", &out)); | 153 V4RiceDecoder::DecodeIntegers(3, 5, 1, "", &out)); |
| 72 } | 154 } |
| 73 #endif | 155 #endif |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 EXPECT_EQ(DECODE_SUCCESS, | 198 EXPECT_EQ(DECODE_SUCCESS, |
| 117 V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out)); | 199 V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out)); |
| 118 EXPECT_EQ(3, out.size()); | 200 EXPECT_EQ(3, out.size()); |
| 119 EXPECT_EQ(5, out.Get(0)); | 201 EXPECT_EQ(5, out.Get(0)); |
| 120 EXPECT_EQ(20, out.Get(1)); | 202 EXPECT_EQ(20, out.Get(1)); |
| 121 EXPECT_EQ(29, out.Get(2)); | 203 EXPECT_EQ(29, out.Get(2)); |
| 122 } | 204 } |
| 123 | 205 |
| 124 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 206 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 125 // This test hits a NOTREACHED so it is a release mode only test. | 207 // This test hits a NOTREACHED so it is a release mode only test. |
| 126 TEST_F(V4RiceTest, TestDecoderBytesWithNoData) { | 208 TEST_F(V4RiceTest, TestDecoderPrefixesWithNoData) { |
| 127 std::string out; | 209 std::vector<uint32_t> out; |
| 128 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, | 210 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, |
| 129 V4RiceDecoder::DecodeBytes(3, 5, 1, "", &out)); | 211 V4RiceDecoder::DecodePrefixes(3, 5, 1, "", &out)); |
| 130 } | 212 } |
| 131 #endif | 213 #endif |
| 132 | 214 |
| 133 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 215 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 134 // This test hits a NOTREACHED so it is a release mode only test. | 216 // This test hits a NOTREACHED so it is a release mode only test. |
| 135 TEST_F(V4RiceTest, TestDecoderBytesWithNegativeNumEntries) { | 217 TEST_F(V4RiceTest, TestDecoderPrefixesWithNegativeNumEntries) { |
| 136 std::string out; | 218 std::vector<uint32_t> out; |
| 137 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, | 219 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, |
| 138 V4RiceDecoder::DecodeBytes(3, 5, -1, "", &out)); | 220 V4RiceDecoder::DecodePrefixes(3, 5, -1, "", &out)); |
| 139 } | 221 } |
| 140 #endif | 222 #endif |
| 141 | 223 |
| 142 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 224 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 143 // This test hits a NOTREACHED so it is a release mode only test. | 225 // This test hits a NOTREACHED so it is a release mode only test. |
| 144 TEST_F(V4RiceTest, TestDecoderBytesWithNonPositiveRiceParameter) { | 226 TEST_F(V4RiceTest, TestDecoderPrefixesWithNonPositiveRiceParameter) { |
| 145 std::string out; | 227 std::vector<uint32_t> out; |
| 146 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, | 228 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 147 V4RiceDecoder::DecodeBytes(3, 0, 1, "a", &out)); | 229 V4RiceDecoder::DecodePrefixes(3, 0, 1, "a", &out)); |
| 148 | 230 |
| 149 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, | 231 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 150 V4RiceDecoder::DecodeBytes(3, -1, 1, "a", &out)); | 232 V4RiceDecoder::DecodePrefixes(3, -1, 1, "a", &out)); |
| 151 } | 233 } |
| 152 #endif | 234 #endif |
| 153 | 235 |
| 154 TEST_F(V4RiceTest, TestDecoderBytesWithOneValue) { | 236 TEST_F(V4RiceTest, TestDecoderPrefixesWithOneValue) { |
| 155 std::string out; | 237 std::vector<uint32_t> out; |
| 156 EXPECT_TRUE(out.empty()); | 238 EXPECT_TRUE(out.empty()); |
| 157 EXPECT_EQ(DECODE_SUCCESS, | 239 EXPECT_EQ(DECODE_SUCCESS, |
| 158 V4RiceDecoder::DecodeBytes(0x69F67F51u, 2, 0, "", &out)); | 240 V4RiceDecoder::DecodePrefixes(0x69F67F51u, 2, 0, "", &out)); |
| 159 EXPECT_EQ(4u, out.size()); | 241 EXPECT_EQ(1u, out.size()); |
| 160 EXPECT_EQ("Q\x7F\xF6i", out); | 242 EXPECT_EQ(0x69F67F51u, out[0]); |
| 161 } | 243 } |
| 162 | 244 |
| 163 TEST_F(V4RiceTest, TestDecoderBytesWithOneSmallValue) { | 245 TEST_F(V4RiceTest, TestDecoderPrefixesWithMultipleValues) { |
| 164 std::string out; | 246 std::vector<uint32_t> out; |
| 165 EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeBytes(17u, 2, 0, "", &out)); | |
| 166 EXPECT_EQ(4u, out.size()); | |
| 167 EXPECT_EQ(std::string("\x11\0\0\0", 4), out); | |
| 168 } | |
| 169 | |
| 170 TEST_F(V4RiceTest, TestDecoderBytesWithMultipleValues) { | |
| 171 std::string out; | |
| 172 EXPECT_EQ(DECODE_SUCCESS, | 247 EXPECT_EQ(DECODE_SUCCESS, |
| 173 V4RiceDecoder::DecodeBytes( | 248 V4RiceDecoder::DecodePrefixes( |
| 174 5, 28, 3, "\xbf\xa8\x3f\xfb\xf\xf\x5e\x27\xe6\xc3\x1d\xc6\x38", | 249 5, 28, 3, "\xbf\xa8\x3f\xfb\xf\xf\x5e\x27\xe6\xc3\x1d\xc6\x38", |
| 175 &out)); | 250 &out)); |
| 176 EXPECT_EQ(16u, out.size()); | 251 std::vector<uint32_t> expected = {5, 0xad934c0cu, 0x6ff67f56u, 0x81316fceu}; |
| 177 EXPECT_EQ(std::string("\x5\0\0\0V\x7F\xF6o\xCEo1\x81\fL\x93\xAD", 16), out); | 252 EXPECT_EQ(expected.size(), out.size()); |
| 253 for (unsigned i = 0; i < expected.size(); i++) { |
| 254 EXPECT_EQ(expected[i], out[i]); |
| 255 } |
| 178 } | 256 } |
| 179 | 257 |
| 180 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | 258 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 181 // This test hits a NOTREACHED so it is a release mode only test. | 259 // This test hits a NOTREACHED so it is a release mode only test. |
| 182 TEST_F(V4RiceTest, TestDecoderBytesWithOverflowValues) { | 260 TEST_F(V4RiceTest, TestDecoderPrefixesWithOverflowValues) { |
| 183 std::string out; | 261 std::vector<uint32_t> out; |
| 184 EXPECT_EQ(DECODED_INTEGER_OVERFLOW_FAILURE, | 262 EXPECT_EQ(DECODED_INTEGER_OVERFLOW_FAILURE, |
| 185 V4RiceDecoder::DecodeBytes( | 263 V4RiceDecoder::DecodePrefixes( |
| 186 5, 28, 3, | 264 5, 28, 3, |
| 187 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out)); | 265 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out)); |
| 188 } | 266 } |
| 189 #endif | 267 #endif |
| 190 | 268 |
| 191 } // namespace safe_browsing | 269 } // namespace safe_browsing |
| OLD | NEW |