Chromium Code Reviews| 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 | |
| 10 namespace safe_browsing { | |
| 11 | |
| 12 class V4RiceTest : public PlatformTest { | |
| 13 public: | |
| 14 V4RiceTest() {} | |
| 15 }; | |
| 16 | |
| 17 TEST_F(V4RiceTest, TestDecoderGetNextWordWithNoData) { | |
| 18 uint32_t word; | |
| 19 V4RiceDecoder decoder(5, 1, ""); | |
| 20 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextWord(&word)); | |
| 21 } | |
| 22 | |
| 23 TEST_F(V4RiceTest, TestDecoderGetNextBitsWithNoData) { | |
| 24 uint32_t word; | |
| 25 V4RiceDecoder decoder(5, 1, ""); | |
| 26 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextBits(1, &word)); | |
| 27 } | |
| 28 | |
| 29 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoData) { | |
| 30 uint32_t word; | |
| 31 V4RiceDecoder decoder(5, 1, ""); | |
| 32 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextValue(&word)); | |
| 33 } | |
| 34 | |
| 35 TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoEntries) { | |
| 36 uint32_t word; | |
| 37 V4RiceDecoder decoder(28, 0, "\xbf\xa8"); | |
| 38 EXPECT_FALSE(decoder.HasAnotherValue()); | |
| 39 EXPECT_EQ(DECODE_NO_MORE_ENTRIES_FAILURE, decoder.GetNextValue(&word)); | |
| 40 } | |
| 41 | |
| 42 TEST_F(V4RiceTest, TestDecoderGetNextValueWithSmallValues) { | |
| 43 uint32_t word; | |
| 44 V4RiceDecoder decoder(2, 2, "\xf7\x2"); | |
| 45 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | |
| 46 EXPECT_EQ(15u, word); | |
| 47 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | |
| 48 EXPECT_EQ(9u, word); | |
| 49 EXPECT_FALSE(decoder.HasAnotherValue()); | |
| 50 } | |
| 51 | |
| 52 TEST_F(V4RiceTest, TestDecoderGetNextValueWithLargeValues) { | |
| 53 uint32_t word; | |
| 54 V4RiceDecoder decoder(28, 3, | |
| 55 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38"); | |
| 56 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | |
| 57 EXPECT_EQ(1777762129u, word); | |
| 58 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | |
| 59 EXPECT_EQ(2093280223u, word); | |
| 60 EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word)); | |
| 61 EXPECT_EQ(924369848u, word); | |
| 62 EXPECT_FALSE(decoder.HasAnotherValue()); | |
| 63 } | |
| 64 | |
| 65 TEST_F(V4RiceTest, TestDecoderIntegersWithNoData) { | |
| 66 RepeatedField<uint32_t> out; | |
| 67 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, | |
| 68 V4RiceDecoder::DecodeIntegers(3, 5, 1, "", &out)); | |
| 69 } | |
| 70 | |
| 71 TEST_F(V4RiceTest, TestDecoderIntegersWithNegativeNumEntries) { | |
| 72 RepeatedField<uint32_t> out; | |
| 73 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, | |
| 74 V4RiceDecoder::DecodeIntegers(3, 5, -1, "", &out)); | |
| 75 } | |
| 76 | |
| 77 TEST_F(V4RiceTest, TestDecoderIntegersWithOneValue) { | |
| 78 RepeatedField<uint32_t> out; | |
| 79 EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeIntegers(3, 2, 0, "", &out)); | |
| 80 EXPECT_EQ(1, out.size()); | |
| 81 EXPECT_EQ(3u, out.Get(0)); | |
| 82 } | |
| 83 | |
| 84 TEST_F(V4RiceTest, TestDecoderIntegersWithMultipleValues) { | |
| 85 RepeatedField<uint32_t> out; | |
| 86 EXPECT_EQ(DECODE_SUCCESS, | |
| 87 V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out)); | |
| 88 EXPECT_EQ(3, out.size()); | |
| 89 EXPECT_EQ(5u, out.Get(0)); | |
| 90 EXPECT_EQ(20u, out.Get(1)); | |
| 91 EXPECT_EQ(29u, out.Get(2)); | |
| 92 } | |
| 93 | |
| 94 TEST_F(V4RiceTest, TestDecoderBytesWithNoData) { | |
| 95 std::string out; | |
| 96 EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, | |
| 97 V4RiceDecoder::DecodeBytes(3, 5, 1, "", &out)); | |
| 98 } | |
| 99 | |
| 100 TEST_F(V4RiceTest, TestDecoderBytesWithNegativeNumEntries) { | |
| 101 std::string out; | |
| 102 EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE, | |
| 103 V4RiceDecoder::DecodeBytes(3, 5, -1, "", &out)); | |
| 104 } | |
| 105 | |
| 106 TEST_F(V4RiceTest, TestDecoderBytesWithOneValue) { | |
| 107 std::string out; | |
| 108 EXPECT_TRUE(out.empty()); | |
| 109 EXPECT_EQ(DECODE_SUCCESS, | |
| 110 V4RiceDecoder::DecodeBytes(0x69F67F51u, 2, 0, "", &out)); | |
| 111 EXPECT_EQ(4u, out.size()); | |
| 112 EXPECT_EQ("Q\x7F\xF6i", out); | |
| 113 } | |
| 114 | |
| 115 TEST_F(V4RiceTest, TestDecoderBytesWithOneSmallValue) { | |
| 116 std::string out; | |
| 117 EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeBytes(17u, 2, 0, "", &out)); | |
| 118 EXPECT_EQ(4u, out.size()); | |
| 119 EXPECT_EQ(std::string("\x11\0\0\0", 4), out); | |
| 120 } | |
| 121 | |
| 122 TEST_F(V4RiceTest, TestDecoderBytesWithMultipleValues) { | |
| 123 std::string out; | |
| 124 EXPECT_EQ(DECODE_SUCCESS, | |
| 125 V4RiceDecoder::DecodeBytes( | |
| 126 5, 28, 3, | |
| 127 "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out)); | |
| 128 EXPECT_EQ(16u, out.size()); | |
| 129 EXPECT_EQ(std::string("\x5\0\0\0V\x7F\xF6i5k\xBB\xE6\xED.\xD4\x1D", 16), out); | |
| 130 } | |
| 131 | |
|
palmer
2016/07/27 23:39:40
Does it make sense to have tests for DECODE_REQUES
vakh (use Gerrit instead)
2016/07/28 07:26:31
I'd love to. However, those have a NOTREACHED() be
vakh (use Gerrit instead)
2016/07/28 16:20:19
Done! Added release mode only tests.
| |
| 132 } // namespace safe_browsing | |
| OLD | NEW |