Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(455)

Unified Diff: components/safe_browsing_db/v4_rice_unittest.cc

Issue 2183433002: PVer4: RICE decode bytes to list of uint32 or 4-byte hash prefixes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: s/size_t/unsigned int: to fix some compile errors on Windows Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/safe_browsing_db/v4_rice_unittest.cc
diff --git a/components/safe_browsing_db/v4_rice_unittest.cc b/components/safe_browsing_db/v4_rice_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5bd28c21d4c9c394be7b6875a67c41b7b8b8bc82
--- /dev/null
+++ b/components/safe_browsing_db/v4_rice_unittest.cc
@@ -0,0 +1,132 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/safe_browsing_db/v4_rice.h"
+#include "testing/platform_test.h"
+
+using google::protobuf::RepeatedField;
+
+namespace safe_browsing {
+
+class V4RiceTest : public PlatformTest {
+ public:
+ V4RiceTest() {}
+};
+
+TEST_F(V4RiceTest, TestDecoderGetNextWordWithNoData) {
+ uint32_t word;
+ V4RiceDecoder decoder(5, 1, "");
+ EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextWord(&word));
+}
+
+TEST_F(V4RiceTest, TestDecoderGetNextBitsWithNoData) {
+ uint32_t word;
+ V4RiceDecoder decoder(5, 1, "");
+ EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextBits(1, &word));
+}
+
+TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoData) {
+ uint32_t word;
+ V4RiceDecoder decoder(5, 1, "");
+ EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE, decoder.GetNextValue(&word));
+}
+
+TEST_F(V4RiceTest, TestDecoderGetNextValueWithNoEntries) {
+ uint32_t word;
+ V4RiceDecoder decoder(28, 0, "\xbf\xa8");
+ EXPECT_FALSE(decoder.HasAnotherValue());
+ EXPECT_EQ(DECODE_NO_MORE_ENTRIES_FAILURE, decoder.GetNextValue(&word));
+}
+
+TEST_F(V4RiceTest, TestDecoderGetNextValueWithSmallValues) {
+ uint32_t word;
+ V4RiceDecoder decoder(2, 2, "\xf7\x2");
+ EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word));
+ EXPECT_EQ(15u, word);
+ EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word));
+ EXPECT_EQ(9u, word);
+ EXPECT_FALSE(decoder.HasAnotherValue());
+}
+
+TEST_F(V4RiceTest, TestDecoderGetNextValueWithLargeValues) {
+ uint32_t word;
+ V4RiceDecoder decoder(28, 3,
+ "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38");
+ EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word));
+ EXPECT_EQ(1777762129u, word);
+ EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word));
+ EXPECT_EQ(2093280223u, word);
+ EXPECT_EQ(DECODE_SUCCESS, decoder.GetNextValue(&word));
+ EXPECT_EQ(924369848u, word);
+ EXPECT_FALSE(decoder.HasAnotherValue());
+}
+
+TEST_F(V4RiceTest, TestDecoderIntegersWithNoData) {
+ RepeatedField<uint32_t> out;
+ EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE,
+ V4RiceDecoder::DecodeIntegers(3, 5, 1, "", &out));
+}
+
+TEST_F(V4RiceTest, TestDecoderIntegersWithNegativeNumEntries) {
+ RepeatedField<uint32_t> out;
+ EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE,
+ V4RiceDecoder::DecodeIntegers(3, 5, -1, "", &out));
+}
+
+TEST_F(V4RiceTest, TestDecoderIntegersWithOneValue) {
+ RepeatedField<uint32_t> out;
+ EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeIntegers(3, 2, 0, "", &out));
+ EXPECT_EQ(1, out.size());
+ EXPECT_EQ(3u, out.Get(0));
+}
+
+TEST_F(V4RiceTest, TestDecoderIntegersWithMultipleValues) {
+ RepeatedField<uint32_t> out;
+ EXPECT_EQ(DECODE_SUCCESS,
+ V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out));
+ EXPECT_EQ(3, out.size());
+ EXPECT_EQ(5u, out.Get(0));
+ EXPECT_EQ(20u, out.Get(1));
+ EXPECT_EQ(29u, out.Get(2));
+}
+
+TEST_F(V4RiceTest, TestDecoderBytesWithNoData) {
+ std::string out;
+ EXPECT_EQ(DECODE_RAN_OUT_OF_BITS_FAILURE,
+ V4RiceDecoder::DecodeBytes(3, 5, 1, "", &out));
+}
+
+TEST_F(V4RiceTest, TestDecoderBytesWithNegativeNumEntries) {
+ std::string out;
+ EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE,
+ V4RiceDecoder::DecodeBytes(3, 5, -1, "", &out));
+}
+
+TEST_F(V4RiceTest, TestDecoderBytesWithOneValue) {
+ std::string out;
+ EXPECT_TRUE(out.empty());
+ EXPECT_EQ(DECODE_SUCCESS,
+ V4RiceDecoder::DecodeBytes(0x69F67F51u, 2, 0, "", &out));
+ EXPECT_EQ(4u, out.size());
+ EXPECT_EQ("Q\x7F\xF6i", out);
+}
+
+TEST_F(V4RiceTest, TestDecoderBytesWithOneSmallValue) {
+ std::string out;
+ EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeBytes(17u, 2, 0, "", &out));
+ EXPECT_EQ(4u, out.size());
+ EXPECT_EQ(std::string("\x11\0\0\0", 4), out);
+}
+
+TEST_F(V4RiceTest, TestDecoderBytesWithMultipleValues) {
+ std::string out;
+ EXPECT_EQ(DECODE_SUCCESS,
+ V4RiceDecoder::DecodeBytes(
+ 5, 28, 3,
+ "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out));
+ EXPECT_EQ(16u, out.size());
+ EXPECT_EQ(std::string("\x5\0\0\0V\x7F\xF6i5k\xBB\xE6\xED.\xD4\x1D", 16), out);
+}
+
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.
+} // namespace safe_browsing
« components/safe_browsing_db/v4_rice.cc ('K') | « components/safe_browsing_db/v4_rice.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698