| 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 TEST_F(V4RiceTest, TestDecoderIntegersWithOneValue) { |
| 97 RepeatedField<int32> out; |
| 98 EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeIntegers(3, 2, 0, "", &out)); |
| 99 EXPECT_EQ(1, out.size()); |
| 100 EXPECT_EQ(3, out.Get(0)); |
| 101 } |
| 102 |
| 103 TEST_F(V4RiceTest, TestDecoderIntegersWithMultipleValues) { |
| 104 RepeatedField<int32> out; |
| 105 EXPECT_EQ(DECODE_SUCCESS, |
| 106 V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out)); |
| 107 EXPECT_EQ(3, out.size()); |
| 108 EXPECT_EQ(5, out.Get(0)); |
| 109 EXPECT_EQ(20, out.Get(1)); |
| 110 EXPECT_EQ(29, out.Get(2)); |
| 111 } |
| 112 |
| 113 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 114 // This test hits a NOTREACHED so it is a release mode only test. |
| 115 TEST_F(V4RiceTest, TestDecoderBytesWithNoData) { |
| 116 std::string out; |
| 117 EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE, |
| 118 V4RiceDecoder::DecodeBytes(3, 5, 1, "", &out)); |
| 119 } |
| 120 #endif |
| 121 |
| 122 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 123 // This test hits a NOTREACHED so it is a release mode only test. |
| 124 TEST_F(V4RiceTest, TestDecoderBytesWithNegativeNumEntries) { |
| 125 std::string out; |
| 126 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, |
| 127 V4RiceDecoder::DecodeBytes(3, 5, -1, "", &out)); |
| 128 } |
| 129 #endif |
| 130 |
| 131 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) |
| 132 // This test hits a NOTREACHED so it is a release mode only test. |
| 133 TEST_F(V4RiceTest, TestDecoderBytesWithNonPositiveRiceParameter) { |
| 134 std::string out; |
| 135 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 136 V4RiceDecoder::DecodeBytes(3, 0, 1, "a", &out)); |
| 137 |
| 138 EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE, |
| 139 V4RiceDecoder::DecodeBytes(3, -1, 1, "a", &out)); |
| 140 } |
| 141 #endif |
| 142 |
| 143 TEST_F(V4RiceTest, TestDecoderBytesWithOneValue) { |
| 144 std::string out; |
| 145 EXPECT_TRUE(out.empty()); |
| 146 EXPECT_EQ(DECODE_SUCCESS, |
| 147 V4RiceDecoder::DecodeBytes(0x69F67F51u, 2, 0, "", &out)); |
| 148 EXPECT_EQ(4u, out.size()); |
| 149 EXPECT_EQ("Q\x7F\xF6i", out); |
| 150 } |
| 151 |
| 152 TEST_F(V4RiceTest, TestDecoderBytesWithOneSmallValue) { |
| 153 std::string out; |
| 154 EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeBytes(17u, 2, 0, "", &out)); |
| 155 EXPECT_EQ(4u, out.size()); |
| 156 EXPECT_EQ(std::string("\x11\0\0\0", 4), out); |
| 157 } |
| 158 |
| 159 TEST_F(V4RiceTest, TestDecoderBytesWithMultipleValues) { |
| 160 std::string out; |
| 161 EXPECT_EQ(DECODE_SUCCESS, |
| 162 V4RiceDecoder::DecodeBytes( |
| 163 5, 28, 3, "\xbf\xa8\x3f\xfb\xf\xf\x5e\x27\xe6\xc3\x1d\xc6\x38", |
| 164 &out)); |
| 165 EXPECT_EQ(16u, out.size()); |
| 166 EXPECT_EQ(std::string("\x5\0\0\0V\x7F\xF6o\xCEo1\x81\fL\x93\xAD", 16), out); |
| 167 } |
| 168 |
| 169 } // namespace safe_browsing |
| OLD | NEW |