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

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/RICE/Rice and use safe-math to check integer overflow 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
« no previous file with comments | « components/safe_browsing_db/v4_rice.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..0b4c4d214b77a08f1a6e00f022e975896cffcc74
--- /dev/null
+++ b/components/safe_browsing_db/v4_rice_unittest.cc
@@ -0,0 +1,191 @@
+// 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;
+using ::google::protobuf::int32;
+
+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());
+}
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderIntegersWithNoData) {
+ RepeatedField<int32> out;
+ EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE,
+ V4RiceDecoder::DecodeIntegers(3, 5, 1, "", &out));
+}
+#endif
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderIntegersWithNegativeNumEntries) {
+ RepeatedField<int32> out;
+ EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE,
+ V4RiceDecoder::DecodeIntegers(3, 5, -1, "", &out));
+}
+#endif
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderIntegersWithNonPositiveRiceParameter) {
+ RepeatedField<int32> out;
+ EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE,
+ V4RiceDecoder::DecodeIntegers(3, 0, 1, "a", &out));
+
+ EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE,
+ V4RiceDecoder::DecodeIntegers(3, -1, 1, "a", &out));
+}
+#endif
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderIntegersWithOverflowValues) {
+ RepeatedField<int32> out;
+ EXPECT_EQ(DECODED_INTEGER_OVERFLOW_FAILURE,
+ V4RiceDecoder::DecodeIntegers(
+ 5, 28, 3,
+ "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out));
+}
+#endif
+
+TEST_F(V4RiceTest, TestDecoderIntegersWithOneValue) {
+ RepeatedField<int32> out;
+ EXPECT_EQ(DECODE_SUCCESS, V4RiceDecoder::DecodeIntegers(3, 2, 0, "", &out));
+ EXPECT_EQ(1, out.size());
+ EXPECT_EQ(3, out.Get(0));
+}
+
+TEST_F(V4RiceTest, TestDecoderIntegersWithMultipleValues) {
+ RepeatedField<int32> out;
+ EXPECT_EQ(DECODE_SUCCESS,
+ V4RiceDecoder::DecodeIntegers(5, 2, 2, "\xf7\x2", &out));
+ EXPECT_EQ(3, out.size());
+ EXPECT_EQ(5, out.Get(0));
+ EXPECT_EQ(20, out.Get(1));
+ EXPECT_EQ(29, out.Get(2));
+}
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderBytesWithNoData) {
+ std::string out;
+ EXPECT_EQ(ENCODED_DATA_UNEXPECTED_EMPTY_FAILURE,
+ V4RiceDecoder::DecodeBytes(3, 5, 1, "", &out));
+}
+#endif
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderBytesWithNegativeNumEntries) {
+ std::string out;
+ EXPECT_EQ(NUM_ENTRIES_NEGATIVE_FAILURE,
+ V4RiceDecoder::DecodeBytes(3, 5, -1, "", &out));
+}
+#endif
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderBytesWithNonPositiveRiceParameter) {
+ std::string out;
+ EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE,
+ V4RiceDecoder::DecodeBytes(3, 0, 1, "a", &out));
+
+ EXPECT_EQ(RICE_PARAMETER_NON_POSITIVE_FAILURE,
+ V4RiceDecoder::DecodeBytes(3, -1, 1, "a", &out));
+}
+#endif
+
+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\xf\xf\x5e\x27\xe6\xc3\x1d\xc6\x38",
+ &out));
+ EXPECT_EQ(16u, out.size());
+ EXPECT_EQ(std::string("\x5\0\0\0V\x7F\xF6o\xCEo1\x81\fL\x93\xAD", 16), out);
+}
+
+#if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
+// This test hits a NOTREACHED so it is a release mode only test.
+TEST_F(V4RiceTest, TestDecoderBytesWithOverflowValues) {
+ std::string out;
+ EXPECT_EQ(DECODED_INTEGER_OVERFLOW_FAILURE,
+ V4RiceDecoder::DecodeBytes(
+ 5, 28, 3,
+ "\xbf\xa8\x3f\xfb\xfc\xfb\x5e\x27\xe6\xc3\x1d\xc6\x38", &out));
+}
+#endif
+
+} // namespace safe_browsing
« no previous file with comments | « 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