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/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 | |
|
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
| |
| 13 // Test that single bits are written to the buffer correctly. | |
| 14 TEST(BitWriterTest, WriteBit) { | |
| 15 BitWriter writer; | |
| 16 | |
| 17 EXPECT_EQ(0U, writer.position()); | |
| 18 EXPECT_EQ(0U, writer.bytes().size()); | |
| 19 | |
| 20 writer.WriteBit(0); | |
| 21 | |
| 22 EXPECT_EQ(1U, writer.position()); | |
| 23 | |
| 24 writer.WriteBit(1); | |
| 25 writer.WriteBit(0); | |
| 26 writer.WriteBit(1); | |
| 27 writer.WriteBit(0); | |
| 28 writer.WriteBit(1); | |
| 29 writer.WriteBit(0); | |
| 30 writer.WriteBit(1); | |
| 31 | |
| 32 EXPECT_EQ(8U, writer.position()); | |
| 33 | |
| 34 writer.WriteBit(0); | |
| 35 | |
| 36 EXPECT_EQ(9U, writer.position()); | |
| 37 | |
| 38 writer.WriteBit(1); | |
| 39 writer.WriteBit(0); | |
| 40 | |
| 41 EXPECT_EQ(11U, writer.position()); | |
| 42 | |
| 43 // Flush should pad the current byte with zero's until it's full. | |
| 44 writer.Flush(); | |
| 45 | |
| 46 // The writer should have 2 bytes now even though we only wrote 11 bits. | |
| 47 EXPECT_EQ(16U, writer.position()); | |
| 48 | |
| 49 // 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 00000 (padding) = 0x5540. | |
| 50 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x55, 0x40)); | |
| 51 } | |
| 52 | |
| 53 // Test that when multiple bits are written to the buffer, they are appended | |
| 54 // correctly. | |
| 55 TEST(BitWriterTest, WriteBits) { | |
| 56 BitWriter writer; | |
| 57 | |
| 58 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.
| |
| 59 EXPECT_EQ(1U, writer.position()); | |
| 60 writer.WriteBits(170, 2); | |
| 61 EXPECT_EQ(3U, writer.position()); | |
| 62 writer.WriteBits(170, 3); | |
| 63 EXPECT_EQ(6U, writer.position()); | |
| 64 writer.WriteBits(170, 2); | |
| 65 EXPECT_EQ(8U, writer.position()); | |
| 66 writer.WriteBits(170, 2); | |
| 67 EXPECT_EQ(10U, writer.position()); | |
| 68 | |
| 69 // Flush should pad the current byte with zero's until it's full. | |
| 70 writer.Flush(); | |
| 71 | |
| 72 // The writer should have 2 bytes now even though we only wrote 10 bits. | |
| 73 EXPECT_EQ(16U, writer.position()); | |
| 74 | |
| 75 // 0 + 10 + 010 + 10 + 10 + 000000 (padding) = 0x4A80 | |
| 76 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x4A, 0x80)); | |
| 77 } | |
| 78 | |
| 79 // Test that buffering works correct when the methods are mixed. | |
| 80 TEST(BitWriterTest, WriteBoth) { | |
| 81 BitWriter writer; | |
| 82 | |
| 83 writer.WriteBits(170, 1); | |
| 84 EXPECT_EQ(1U, writer.position()); | |
| 85 writer.WriteBit(1); | |
| 86 writer.WriteBits(170, 2); | |
| 87 EXPECT_EQ(4U, writer.position()); | |
| 88 writer.WriteBits(170, 3); | |
| 89 EXPECT_EQ(7U, writer.position()); | |
| 90 writer.WriteBit(1); | |
| 91 EXPECT_EQ(8U, writer.position()); | |
| 92 | |
| 93 writer.WriteBits(170, 2); | |
| 94 writer.WriteBit(0); | |
| 95 EXPECT_EQ(11U, writer.position()); | |
| 96 | |
| 97 // Flush should pad the current byte with zero's until it's full. | |
| 98 writer.Flush(); | |
| 99 | |
| 100 // The writer should have 2 bytes now even though we only wrote 10 bits. | |
| 101 EXPECT_EQ(16U, writer.position()); | |
| 102 | |
| 103 // 0 + 1 + 10 + 010 + 1 + 10 + 0 + 00000 (padding) = 0x6580 | |
| 104 EXPECT_THAT(writer.bytes(), testing::ElementsAre(0x65, 0x80)); | |
| 105 } | |
| 106 | |
| 107 } // transport_security_state | |
| 108 | |
| 109 } // namespace net | |
| OLD | NEW |