1 : // Copyright 2015 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #include "syzygy/common/binary_stream.h"
16 :
17 : #include "base/strings/string_piece.h"
18 : #include "gtest/gtest.h"
19 :
20 : namespace common {
21 :
22 : namespace {
23 :
24 : const char kTestString[] = "asdf";
25 :
26 : class BinaryBufferStreamReaderTest : public testing::Test {
27 : public:
28 E : void SetUp() override {
29 : // Initialize the read buffer to garbage.
30 E : ::memset(buf_, 0xCC, sizeof(buf_));
31 E : }
32 :
33 E : char* buf() { return buf_; }
34 :
35 : protected:
36 : char buf_[1024];
37 : };
38 :
39 : } // namespace
40 :
41 E : TEST_F(BinaryBufferStreamReaderTest, EmptyConstruction) {
42 E : BinaryBufferStreamReader reader(nullptr, 0);
43 E : EXPECT_FALSE(reader.Read(1, buf()));
44 E : }
45 :
46 E : TEST_F(BinaryBufferStreamReaderTest, BufferAndLenConstruction) {
47 E : BinaryBufferStreamReader reader(kTestString, sizeof(kTestString));
48 E : EXPECT_EQ(0U, reader.Position());
49 E : EXPECT_FALSE(reader.AtEnd());
50 : // Read the string in one slurp.
51 E : EXPECT_TRUE(reader.Read(sizeof(kTestString), buf()));
52 E : EXPECT_EQ(sizeof(kTestString), reader.Position());
53 E : EXPECT_TRUE(reader.AtEnd());
54 :
55 E : EXPECT_EQ(0, ::memcmp(kTestString, buf(), sizeof(kTestString)));
56 : // Should be unable to read more bytes.
57 E : EXPECT_FALSE(reader.Read(1, buf()));
58 E : }
59 :
60 :
61 E : TEST_F(BinaryBufferStreamReaderTest, StringPieceConstruction) {
62 E : BinaryBufferStreamReader reader(
63 : base::StringPiece(kTestString, sizeof(kTestString)));
64 : // Read the string in one slurp.
65 E : EXPECT_TRUE(reader.Read(sizeof(kTestString), buf()));
66 :
67 E : EXPECT_EQ(0, ::memcmp(kTestString, buf(), sizeof(kTestString)));
68 : // Should be unable to read more bytes.
69 E : EXPECT_FALSE(reader.Read(1, buf()));
70 E : }
71 :
72 : using BinaryStreamParserTest = BinaryBufferStreamReaderTest;
73 :
74 E : TEST_F(BinaryStreamParserTest, ReadEmpty) {
75 E : BinaryBufferStreamReader empty(nullptr, 0);
76 E : BinaryStreamParser parser(&empty);
77 :
78 E : EXPECT_EQ(&empty, parser.stream_reader());
79 :
80 E : char chr = 0x1C;
81 E : EXPECT_FALSE(parser.Read(&chr));
82 E : EXPECT_EQ(0x1C, chr);
83 E : }
84 :
85 E : TEST_F(BinaryStreamParserTest, ReadData) {
86 E : const uint32_t kTestData32 = 0xCAFEBABE;
87 :
88 E : BinaryBufferStreamReader reader(&kTestData32, sizeof(kTestData32));
89 E : BinaryStreamParser parser(&reader);
90 :
91 E : uint32_t data = 0;
92 E : EXPECT_TRUE(parser.Read(&data));
93 E : EXPECT_EQ(kTestData32, data);
94 :
95 E : char chr = 0x1C;
96 E : EXPECT_FALSE(parser.Read(&chr));
97 E : EXPECT_EQ(0x1C, chr);
98 E : }
99 :
100 E : TEST_F(BinaryStreamParserTest, ReadBytes) {
101 E : const uint32_t kTestData32 = 0xCAFEBABE;
102 :
103 E : BinaryBufferStreamReader reader(&kTestData32, sizeof(kTestData32));
104 E : BinaryStreamParser parser(&reader);
105 :
106 E : uint32_t data = 0;
107 E : EXPECT_TRUE(parser.ReadBytes(sizeof(data), &data));
108 E : EXPECT_EQ(kTestData32, data);
109 :
110 E : char chr = 0x1C;
111 E : EXPECT_FALSE(parser.Read(&chr));
112 E : EXPECT_EQ(0x1C, chr);
113 E : }
114 :
115 E : TEST_F(BinaryStreamParserTest, ReadMultiple) {
116 : static const uint16_t kTestData[] = {0, 1, 2, 3, 4, 5, 6, 7};
117 :
118 E : BinaryBufferStreamReader reader(kTestData, sizeof(kTestData));
119 E : BinaryStreamParser parser(&reader);
120 :
121 E : std::vector<uint16_t> data;
122 E : const size_t kNumToRead = 3;
123 : static_assert(kNumToRead * 3 > arraysize(kTestData), "Array too small");
124 :
125 : // Read the first third of the data.
126 E : EXPECT_TRUE(parser.ReadMultiple(kNumToRead, &data));
127 E : EXPECT_EQ(kNumToRead, data.size());
128 :
129 : // Read the second third of the data.
130 E : EXPECT_TRUE(parser.ReadMultiple(kNumToRead, &data));
131 E : EXPECT_EQ(kNumToRead * 2, data.size());
132 :
133 : // Read past the end of the data.
134 E : EXPECT_FALSE(parser.ReadMultiple(kNumToRead, &data));
135 E : EXPECT_EQ(arraysize(kTestData), data.size());
136 :
137 E : EXPECT_TRUE(std::equal(data.begin(), data.end(), kTestData));
138 :
139 E : EXPECT_TRUE(reader.AtEnd());
140 E : }
141 :
142 E : TEST_F(BinaryStreamParserTest, ReadString) {
143 : // Two strings back-to-back.
144 : static const char kTestData[] = {
145 : 'h', 'e', 'l', 'l', 'o', '\0', 'w', 'o', 'r', 'l', 'd'};
146 :
147 E : BinaryBufferStreamReader reader(kTestData, sizeof(kTestData));
148 E : BinaryStreamParser parser(&reader);
149 :
150 E : std::string hello;
151 E : EXPECT_TRUE(parser.ReadString(&hello));
152 E : EXPECT_EQ("hello", hello);
153 E : EXPECT_EQ(5U, hello.size());
154 :
155 E : std::string world;
156 E : EXPECT_FALSE(parser.ReadString(&world));
157 E : EXPECT_EQ("world", world);
158 E : EXPECT_EQ(5u, world.size());
159 :
160 E : char chr = 0x1C;
161 E : EXPECT_FALSE(parser.Read(&chr));
162 E : EXPECT_EQ(0x1C, chr);
163 E : }
164 :
165 E : TEST_F(BinaryStreamParserTest, ReadWideString) {
166 : // Two strings back-to-back.
167 : static const wchar_t kTestData[] = {
168 : 'h', 'e', 'l', 'l', 'o', '\0', 'w', 'o', 'r', 'l', 'd'};
169 :
170 E : BinaryBufferStreamReader reader(kTestData, sizeof(kTestData));
171 E : BinaryStreamParser parser(&reader);
172 :
173 E : std::wstring hello;
174 E : EXPECT_TRUE(parser.ReadString(&hello));
175 E : EXPECT_EQ(L"hello", hello);
176 E : EXPECT_EQ(5U, hello.size());
177 :
178 E : std::wstring world;
179 E : EXPECT_FALSE(parser.ReadString(&world));
180 E : EXPECT_EQ(L"world", world);
181 E : EXPECT_EQ(5u, world.size());
182 :
183 E : wchar_t chr = 0x1C;
184 E : EXPECT_FALSE(parser.Read(&chr));
185 E : EXPECT_EQ(0x1C, chr);
186 E : }
187 :
188 E : TEST_F(BinaryStreamParserTest, AlignTo) {
189 E : std::vector<uint8_t> data(1024, 0xCC);
190 E : BinaryBufferStreamReader reader(&data.at(0), data.size());
191 E : BinaryStreamParser parser(&reader);
192 :
193 E : EXPECT_EQ(0U, reader.Position());
194 : // Shouldn't move, zero is aligned to all by definition.
195 E : EXPECT_TRUE(parser.AlignTo(5));
196 E : EXPECT_TRUE(parser.AlignTo(4));
197 E : EXPECT_EQ(0U, reader.Position());
198 :
199 E : EXPECT_TRUE(parser.ReadBytes(5, buf_));
200 E : EXPECT_EQ(5U, reader.Position());
201 :
202 : // Try a couple of alignments.
203 E : EXPECT_TRUE(parser.AlignTo(4));
204 E : EXPECT_EQ(8U, reader.Position());
205 :
206 E : EXPECT_TRUE(parser.AlignTo(5));
207 E : EXPECT_EQ(10U, reader.Position());
208 :
209 E : EXPECT_FALSE(parser.AlignTo(data.size() + 1));
210 E : EXPECT_TRUE(reader.AtEnd());
211 E : }
212 :
213 : } // namespace common
|