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

Side by Side 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: fix a comment. Created 3 years, 8 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "net/tools/transport_security_state_generator/bit_writer.h"
6 #include "testing/gmock/include/gmock/gmock.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace net {
10
11 namespace transport_security_state {
12
13 namespace {
14
15 // Test that single bits are written to the buffer correctly.
16 TEST(BitWriterTest, WriteBit) {
17 BitWriter writer;
18
19 EXPECT_EQ(0U, writer.position());
20 EXPECT_EQ(0U, writer.bytes().size());
21
22 writer.WriteBit(0);
23
24 EXPECT_EQ(1U, writer.position());
25
26 writer.WriteBit(1);
27 writer.WriteBit(0);
28 writer.WriteBit(1);
29 writer.WriteBit(0);
30 writer.WriteBit(1);
31 writer.WriteBit(0);
32 writer.WriteBit(1);
33
34 EXPECT_EQ(8U, writer.position());
35
36 writer.WriteBit(0);
37
38 EXPECT_EQ(9U, writer.position());
39
40 writer.WriteBit(1);
41 writer.WriteBit(0);
42
43 EXPECT_EQ(11U, writer.position());
44
45 // Flush should pad the current byte with zero's until it's full.
46 writer.Flush();
47
48 // The writer should have 2 bytes now even though we only wrote 11 bits.
49 EXPECT_EQ(16U, writer.position());
50
51 // 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 00000 (padding) = 0x5540.
52 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x55, 0x40));
53 }
54
55 // Test that when multiple bits are written to the buffer, they are appended
56 // correctly.
57 TEST(BitWriterTest, WriteBits) {
58 BitWriter writer;
59
60 // 0xAA is 10101010 in binary. WritBits will write the n least significant
61 // bits where n is given as the second parameter.
62 writer.WriteBits(0xAA, 1);
63 EXPECT_EQ(1U, writer.position());
64 writer.WriteBits(0xAA, 2);
65 EXPECT_EQ(3U, writer.position());
66 writer.WriteBits(0xAA, 3);
67 EXPECT_EQ(6U, writer.position());
68 writer.WriteBits(0xAA, 2);
69 EXPECT_EQ(8U, writer.position());
70 writer.WriteBits(0xAA, 2);
71 EXPECT_EQ(10U, writer.position());
72
73 // Flush should pad the current byte with zero's until it's full.
74 writer.Flush();
75
76 // The writer should have 2 bytes now even though we only wrote 10 bits.
77 EXPECT_EQ(16U, writer.position());
78
79 // 0 + 10 + 010 + 10 + 10 + 000000 (padding) = 0x4A80
80 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x4A, 0x80));
81 }
82
83 // Test that buffering works correct when the methods are mixed.
84 TEST(BitWriterTest, WriteBoth) {
85 BitWriter writer;
86
87 // 0xAA is 10101010 in binary. WritBits will write the n least significant
88 // bits where n is given as the second parameter.
89 writer.WriteBits(0xAA, 1);
90 EXPECT_EQ(1U, writer.position());
91 writer.WriteBit(1);
92 writer.WriteBits(0xAA, 2);
93 EXPECT_EQ(4U, writer.position());
94 writer.WriteBits(0xAA, 3);
95 EXPECT_EQ(7U, writer.position());
96 writer.WriteBit(1);
97 EXPECT_EQ(8U, writer.position());
98
99 writer.WriteBits(0xAA, 2);
100 writer.WriteBit(0);
101 EXPECT_EQ(11U, writer.position());
102
103 // Flush should pad the current byte with zero's until it's full.
104 writer.Flush();
105
106 // The writer should have 2 bytes now even though we only wrote 10 bits.
107 EXPECT_EQ(16U, writer.position());
108
109 // 0 + 1 + 10 + 010 + 1 + 10 + 0 + 00000 (padding) = 0x6580
110 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x65, 0x80));
111 }
112
113 } // namespace
114
115 } // namespace transport_security_state
116
117 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698