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

Side by Side Diff: net/tools/transport_security_state_generator/trie/trie_bit_buffer_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
« no previous file with comments | « net/tools/transport_security_state_generator/spki_hash_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/trie/trie_bit_buffer.h"
6 #include "net/tools/transport_security_state_generator/bit_writer.h"
7 #include "net/tools/transport_security_state_generator/huffman/huffman_builder.h "
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12
13 namespace transport_security_state {
14
15 namespace {
16
17 // Test writing single bits to the buffer.
18 TEST(TrieBitBufferTest, WriteBit) {
19 TrieBitBuffer buffer;
20
21 buffer.WriteBit(0);
22 buffer.WriteBit(1);
23 buffer.WriteBit(0);
24 buffer.WriteBit(1);
25 buffer.WriteBit(0);
26 buffer.WriteBit(1);
27 buffer.WriteBit(0);
28 buffer.WriteBit(1);
29
30 BitWriter writer;
31 buffer.WriteToBitWriter(&writer);
32
33 writer.Flush();
34
35 // 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 = 0x55
36 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x55, 0x0));
37 EXPECT_EQ(16U, writer.position());
38
39 buffer.WriteBit(0);
40 buffer.WriteBit(1);
41 buffer.WriteBit(0);
42
43 BitWriter writer2;
44 buffer.WriteToBitWriter(&writer2);
45 EXPECT_EQ(11U, writer2.position());
46
47 writer2.Flush();
48
49 // 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 00000 (padding) = 0x5540.
50 EXPECT_THAT(writer2.bytes(), testing::ElementsAre(0x55, 0x40));
51 }
52
53 // Test writing multiple bits at once. Specifically, that the correct bits are
54 // written and byte boundaries are respected.
55 TEST(TrieBitBufferTest, WriteBits) {
56 TrieBitBuffer buffer;
57
58 // 0xAA is 10101010 in binary. WritBits will write the n least significant
59 // bits where n is given as the second parameter.
60 buffer.WriteBits(0xAA, 1);
61 buffer.WriteBits(0xAA, 2);
62 buffer.WriteBits(0xAA, 3);
63
64 BitWriter writer;
65 buffer.WriteToBitWriter(&writer);
66 EXPECT_EQ(6U, writer.position());
67
68 writer.Flush();
69
70 // 0 + 10 + 010 + 00 (padding) = 0x48
71 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x48));
72
73 buffer.WriteBits(0xAA, 2);
74 buffer.WriteBits(0xAA, 2);
75
76 BitWriter writer2;
77 buffer.WriteToBitWriter(&writer2);
78 EXPECT_EQ(10U, writer2.position());
79
80 writer2.Flush();
81
82 // 0 + 10 + 010 + 10 + 10 + 000000 (padding) = 0x4A80.
83 EXPECT_THAT(writer2.bytes(), testing::ElementsAre(0x4A, 0x80));
84
85 buffer.WriteBits(0xAA, 2);
86
87 BitWriter writer3;
88 buffer.WriteToBitWriter(&writer3);
89 EXPECT_EQ(12U, writer3.position());
90
91 writer3.Flush();
92
93 // 0 + 10 + 010 + 10 + 10 + 10 + 0000 (padding) = 0x4AA0.
94 EXPECT_THAT(writer3.bytes(), testing::ElementsAre(0x4A, 0xA0));
95 }
96
97 // Test writing position (delta's).
98 TEST(TrieBitBufferTest, WritePosition) {
99 TrieBitBuffer buffer;
100 BitWriter writer;
101
102 buffer.WriteBit(1);
103 // 0xAA is 10101010 in binary. WritBits will write the n least significant
104 // bits where n is given as the second parameter.
105 buffer.WriteBits(0xAA, 6);
106
107 buffer.WriteToBitWriter(&writer);
108
109 TrieBitBuffer buffer2;
110 int32_t last_position = -1;
111 buffer2.WritePosition(4, &last_position);
112 EXPECT_EQ(4, last_position);
113
114 buffer2.WriteBits(0xAA, 8);
115 buffer2.WritePosition(8, &last_position);
116 EXPECT_EQ(8, last_position);
117
118 buffer2.WriteToBitWriter(&writer);
119 writer.Flush();
120
121 EXPECT_EQ(4U, writer.bytes().size());
122
123 // The buffer should contain, in order:
124 // - the bit 1
125 // - the last 6 bits of '0xAA'
126 // - five bits representing '2'; the bit length of the following field
127 // - 2 bits representing '3' (the delta 7 - 4)
128 // - 8 bits representing 0xAA
129 // - A zero indicating the following 7 bits represent a delta
130 // - 7 bits representing 4 (the delta 8 - 4)
131 // - padding
132 //
133 // 1 + 101010 + 00010 + 11 + 10101010 + 0 + 0000100 + 00 (padding)
134 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0xD4, 0x2E, 0xA8, 0x10));
135 }
136
137 // Test writing characters to the buffer using Huffman.
138 TEST(TrieBitBufferTest, WriteChar) {
139 TrieBitBuffer buffer;
140 HuffmanBuilder huffman_builder;
141 HuffmanRepresentationTable table;
142
143 table['a'] = HuffmanRepresentation();
144 table['a'].bits = 0x0A;
145 table['a'].number_of_bits = 4;
146
147 table['b'] = HuffmanRepresentation();
148 table['b'].bits = 0x0F;
149 table['b'].number_of_bits = 4;
150
151 buffer.WriteChar('a', table, &huffman_builder);
152
153 HuffmanRepresentationTable encoding = huffman_builder.ToTable();
154
155 // 'a' should have a Huffman encoding.
156 EXPECT_NE(encoding.cend(), encoding.find('a'));
157
158 buffer.WriteChar('a', table, &huffman_builder);
159 buffer.WriteChar('b', table, &huffman_builder);
160
161 encoding = huffman_builder.ToTable();
162
163 // Both 'a' and 'b' should have a Huffman encoding.
164 EXPECT_NE(encoding.cend(), encoding.find('a'));
165 EXPECT_NE(encoding.cend(), encoding.find('b'));
166
167 BitWriter writer;
168 buffer.WriteToBitWriter(&writer);
169 writer.Flush();
170
171 // There should be 3 characters in the writer. 'a' twice followed by 'b' once.
172 // The characters are written as the representation in |table|.
173 EXPECT_EQ(2U, writer.bytes().size());
174
175 // Twice 'a', once 'b' and padding
176 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0xAA, 0xF0));
177 }
178
179 // Test writing a mix of items. Specifically, that the correct values are
180 // written in the correct order and byte boundaries are respected.
181 TEST(TrieBitBufferTest, WriteMix) {
182 TrieBitBuffer buffer;
183
184 HuffmanRepresentationTable table;
185 table['a'] = HuffmanRepresentation();
186 table['a'].bits = 0x0A;
187 table['a'].number_of_bits = 4;
188
189 // 0xAA is 10101010 in binary. WritBits will write the n least significant
190 // bits where n is given as the second parameter.
191 buffer.WriteBits(0xAA, 1);
192 buffer.WriteBit(1);
193
194 buffer.WriteChar('a', table, nullptr);
195
196 buffer.WriteBits(0xAA, 2);
197 buffer.WriteBits(0xAA, 3);
198
199 BitWriter writer;
200 buffer.WriteToBitWriter(&writer);
201
202 // 1 + 1 + 4 + 2 + 3 = 11.
203 EXPECT_EQ(writer.position(), 11U);
204
205 TrieBitBuffer buffer2;
206 buffer2.WriteBit(1);
207 buffer2.WriteBits(0xAA, 2);
208 buffer2.WriteBit(0);
209
210 buffer2.WriteToBitWriter(&writer);
211 EXPECT_EQ(writer.position(), 15U);
212 EXPECT_EQ(writer.bytes().size(), 1U);
213
214 writer.Flush();
215
216 EXPECT_EQ(writer.bytes().size(), 2U);
217
218 // 0 + 1 + 1010 + 10 + 010 + 1 + 10 + 0 + 0 (padding) = 0x6A58.
219 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x6A, 0x58));
220 }
221
222 } // namespace
223
224 } // namespace transport_security_state
225
226 } // namespace net
OLDNEW
« no previous file with comments | « net/tools/transport_security_state_generator/spki_hash_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698