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

Unified Diff: net/tools/transport_security_state_generator/bit_writer_unittest.cc

Issue 2775053002: Add unittests for different transport security state generator components. (Closed)
Patch Set: update tests. Created 3 years, 9 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: net/tools/transport_security_state_generator/bit_writer_unittest.cc
diff --git a/net/tools/transport_security_state_generator/bit_writer_unittest.cc b/net/tools/transport_security_state_generator/bit_writer_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e03e04ff493a690081040aab4af8b8d57c2c5b37
--- /dev/null
+++ b/net/tools/transport_security_state_generator/bit_writer_unittest.cc
@@ -0,0 +1,109 @@
+// Copyright 2017 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 "net/tools/transport_security_state_generator/bit_writer.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace net {
+
+namespace transport_security_state {
+
Ryan Sleevi 2017/03/30 15:03:32 A common idiom is to put these tests in an extra (
martijnc 2017/03/30 19:02:22 I put them in an extra namespace, this still seems
+// Test that single bits are written to the buffer correctly.
+TEST(BitWriterTest, WriteBit) {
+ BitWriter writer;
+
+ EXPECT_EQ(0U, writer.position());
+ EXPECT_EQ(0U, writer.bytes().size());
+
+ writer.WriteBit(0);
+
+ EXPECT_EQ(1U, writer.position());
+
+ writer.WriteBit(1);
+ writer.WriteBit(0);
+ writer.WriteBit(1);
+ writer.WriteBit(0);
+ writer.WriteBit(1);
+ writer.WriteBit(0);
+ writer.WriteBit(1);
+
+ EXPECT_EQ(8U, writer.position());
+
+ writer.WriteBit(0);
+
+ EXPECT_EQ(9U, writer.position());
+
+ writer.WriteBit(1);
+ writer.WriteBit(0);
+
+ EXPECT_EQ(11U, writer.position());
+
+ // Flush should pad the current byte with zero's until it's full.
+ writer.Flush();
+
+ // The writer should have 2 bytes now even though we only wrote 11 bits.
+ EXPECT_EQ(16U, writer.position());
+
+ // 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 00000 (padding) = 0x5540.
+ EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x55, 0x40));
+}
+
+// Test that when multiple bits are written to the buffer, they are appended
+// correctly.
+TEST(BitWriterTest, WriteBits) {
+ BitWriter writer;
+
+ writer.WriteBits(170, 1);
Ryan Sleevi 2017/03/30 15:03:32 It took a quick consult of the table to find out 1
martijnc 2017/03/30 19:02:22 Added a comment and replaced 170 with 0xAA.
+ EXPECT_EQ(1U, writer.position());
+ writer.WriteBits(170, 2);
+ EXPECT_EQ(3U, writer.position());
+ writer.WriteBits(170, 3);
+ EXPECT_EQ(6U, writer.position());
+ writer.WriteBits(170, 2);
+ EXPECT_EQ(8U, writer.position());
+ writer.WriteBits(170, 2);
+ EXPECT_EQ(10U, writer.position());
+
+ // Flush should pad the current byte with zero's until it's full.
+ writer.Flush();
+
+ // The writer should have 2 bytes now even though we only wrote 10 bits.
+ EXPECT_EQ(16U, writer.position());
+
+ // 0 + 10 + 010 + 10 + 10 + 000000 (padding) = 0x4A80
+ EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x4A, 0x80));
+}
+
+// Test that buffering works correct when the methods are mixed.
+TEST(BitWriterTest, WriteBoth) {
+ BitWriter writer;
+
+ writer.WriteBits(170, 1);
+ EXPECT_EQ(1U, writer.position());
+ writer.WriteBit(1);
+ writer.WriteBits(170, 2);
+ EXPECT_EQ(4U, writer.position());
+ writer.WriteBits(170, 3);
+ EXPECT_EQ(7U, writer.position());
+ writer.WriteBit(1);
+ EXPECT_EQ(8U, writer.position());
+
+ writer.WriteBits(170, 2);
+ writer.WriteBit(0);
+ EXPECT_EQ(11U, writer.position());
+
+ // Flush should pad the current byte with zero's until it's full.
+ writer.Flush();
+
+ // The writer should have 2 bytes now even though we only wrote 10 bits.
+ EXPECT_EQ(16U, writer.position());
+
+ // 0 + 1 + 10 + 010 + 1 + 10 + 0 + 00000 (padding) = 0x6580
+ EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x65, 0x80));
+}
+
+} // transport_security_state
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698