| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/safe_browsing_db/v4_rice.h" |
| 6 #include "testing/platform_test.h" |
| 7 |
| 8 using ::google::protobuf::RepeatedField; |
| 9 using ::google::protobuf::int32; |
| 10 |
| 11 namespace safe_browsing { |
| 12 |
| 13 class V4RiceTest : public PlatformTest { |
| 14 public: |
| 15 V4RiceTest() {} |
| 16 }; |
| 17 |
| 18 TEST_F(V4RiceTest, TestDecoderGetNextWordWithNoData) { |
| 19 uint32_t word; |
| 20 V4RiceDecoder decoder(5, 1, ""); |
| 21 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextWord(&word)); |
| 22 } |
| 23 |
| 24 TEST_F(V4RiceTest, TestDecoderGetNextBitsWithNoData) { |
| 25 uint32_t word; |
| 26 V4RiceDecoder decoder(5, 1, ""); |
| 27 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextBits(1, &word)); |
| 28 } |
| 29 |
| 30 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoData) { |
| 31 uint32_t word; |
| 32 V4RiceDecoder decoder(5, 1, ""); |
| 33 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextValue(&word)); |
| 34 } |
| 35 |
| 36 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoEntries) { |
| 37 uint32_t word; |
| 38 V4RiceDecoder decoder(28, 0, "\xbf\xa8"); |
| 39 EXPECT_FALSE(decoder.HasAnotherValue()); |
| 40 EXPECT_EQ(DECODE_NO_MORE_ENTRIES_FAILURE, decoder.GetNextValue(&word)); |
| 41 } |
| 42 |
| 43 TEST_F(V4RiceTest, TestDecoderGetNextValueWithSmallValues) { |
| 44 uint32_t word; |
| 45 V4RiceDecoder decoder(2, 2, "\xf7\x2"); |
| 46 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); |
| 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 |
| 53 TEST_F(V4RiceTest, TestDecoderGetNextValueWithLargeValues) { |
| 54 uint32_t word; |
| 55 V4RiceDecoder decoder(28, 3, |
| 56 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38"); |
| 57 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); |
| 58 EXPECT_EQ(1777762129u, word); |
| 59 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); |
| 60 EXPECT_EQ(2093280223u, word); |
| 61 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); |
| 62 EXPECT_EQ(924369848u, word); |
| 63 EXPECT_FALSE(decoder.HasAnotherValue()); |
| 64 } |
| 65 |
| 66 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 67 // This test hits a NOTREACHED so it is a release mode only test. |
| 68 TEST_F(V4RiceTest, TestDecoderIntegersWithNoData) { |
| 69 RepeatedField<int32> out; |
| 70 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, |
| 71 V4RiceDecoder::DecodeIntegers(3, 5, 1, "", &out)); |
| 72 } |
| 73 #endif |
| 74 |
| 75 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 76 // This test hits a NOTREACHED so it is a release mode only test. |
| 77 TEST_F(V4RiceTest, TestDecoderIntegersWithNegativeNumEntries) { |
| 78 RepeatedField<int32> out; |
| 79 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, |
| 80 V4RiceDecoder::DecodeIntegers(3, 5, -1, "", &out)); |
| 81 } |
| 82 #endif |
| 83 |
| 84 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 85 // This test hits a NOTREACHED so it is a release mode only test. |
| 86 TEST_F(V4RiceTest, TestDecoderIntegersWithNonPositiveRiceParameter) { |
| 87 RepeatedField<int32> out; |
| 88 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 89 V4RiceDecoder::DecodeIntegers(3, 0, 1, "a", &out)); |
| 90 |
| 91 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 92 V4RiceDecoder::DecodeIntegers(3, -1, 1, "a", &out)); |
| 93 } |
| 94 #endif |
| 95 |
| 96 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 97 // This test hits a NOTREACHED so it is a release mode only test. |
| 98 TEST_F(V4RiceTest, TestDecoderIntegersWithOverflowValues) { |
| 99 RepeatedField<int32> out; |
| 100 EXPECT_EQ(DECODED_INTEGER_OVERFLOW_FAILURE, |
| 101 V4RiceDecoder::DecodeIntegers( |
| 102 5, 28, 3, |
| 103 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out)); |
| 104 } |
| 105 #endif |
| 106 |
| 107 TEST_F(V4RiceTest, TestDecoderIntegersWithOneValue) { |
| 108 RepeatedField<int32> out; |
| 109 EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeIntegers(3, 2, 0, "", &out)); |
| 110 EXPECT_EQ(1, out.size()); |
| 111 EXPECT_EQ(3, out.Get(0)); |
| 112 } |
| 113 |
| 114 TEST_F(V4RiceTest, TestDecoderIntegersWithMultipleValues) { |
| 115 RepeatedField<int32> out; |
| 116 EXPECT_EQ(DECODE_SUCCESS, |
| 117 V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out)); |
| 118 EXPECT_EQ(3, out.size()); |
| 119 EXPECT_EQ(5, out.Get(0)); |
| 120 EXPECT_EQ(20, out.Get(1)); |
| 121 EXPECT_EQ(29, out.Get(2)); |
| 122 } |
| 123 |
| 124 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 125 // This test hits a NOTREACHED so it is a release mode only test. |
| 126 TEST_F(V4RiceTest, TestDecoderBytesWithNoData) { |
| 127 std::string out; |
| 128 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, |
| 129 V4RiceDecoder::DecodeBytes(3, 5, 1, "", &out)); |
| 130 } |
| 131 #endif |
| 132 |
| 133 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 134 // This test hits a NOTREACHED so it is a release mode only test. |
| 135 TEST_F(V4RiceTest, TestDecoderBytesWithNegativeNumEntries) { |
| 136 std::string out; |
| 137 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, |
| 138 V4RiceDecoder::DecodeBytes(3, 5, -1, "", &out)); |
| 139 } |
| 140 #endif |
| 141 |
| 142 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 143 // This test hits a NOTREACHED so it is a release mode only test. |
| 144 TEST_F(V4RiceTest, TestDecoderBytesWithNonPositiveRiceParameter) { |
| 145 std::string out; |
| 146 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 147 V4RiceDecoder::DecodeBytes(3, 0, 1, "a", &out)); |
| 148 |
| 149 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 150 V4RiceDecoder::DecodeBytes(3, -1, 1, "a", &out)); |
| 151 } |
| 152 #endif |
| 153 |
| 154 TEST_F(V4RiceTest, TestDecoderBytesWithOneValue) { |
| 155 std::string out; |
| 156 EXPECT_TRUE(out.empty()); |
| 157 EXPECT_EQ(DECODE_SUCCESS, |
| 158 V4RiceDecoder::DecodeBytes(0x69F67F51u, 2, 0, "", &out)); |
| 159 EXPECT_EQ(4u, out.size()); |
| 160 EXPECT_EQ("Q\x7F\xF6i", out); |
| 161 } |
| 162 |
| 163 TEST_F(V4RiceTest, TestDecoderBytesWithOneSmallValue) { |
| 164 std::string 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, |
| 173 V4RiceDecoder::DecodeBytes( |
| 174 5, 28, 3, "\xbf\xa8\x3f\xfb\xf\xf\x5e\x27\xe6\xc3\x1d\xc6\x38", |
| 175 &out)); |
| 176 EXPECT_EQ(16u, out.size()); |
| 177 EXPECT_EQ(std::string("\x5\0\0\0V\x7F\xF6o\xCEo1\x81\fL\x93\xAD", 16), out); |
| 178 } |
| 179 |
| 180 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 181 // This test hits a NOTREACHED so it is a release mode only test. |
| 182 TEST_F(V4RiceTest, TestDecoderBytesWithOverflowValues) { |
| 183 std::string out; |
| 184 EXPECT_EQ(DECODED_INTEGER_OVERFLOW_FAILURE, |
| 185 V4RiceDecoder::DecodeBytes( |
| 186 5, 28, 3, |
| 187 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out)); |
| 188 } |
| 189 #endif |
| 190 |
| 191 } // namespace safe_browsing |
| OLD | NEW |