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