1 : // Copyright 2012 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/pdb/pdb_byte_stream.h"
16 : #include "gtest/gtest.h"
17 :
18 : namespace pdb {
19 :
20 : namespace {
21 :
22 : class TestPdbByteStream : public PdbByteStream {
23 : public:
24 E : TestPdbByteStream() : PdbByteStream() {
25 E : }
26 :
27 : using PdbByteStream::ReadBytes;
28 : };
29 :
30 : class TestPdbStream : public PdbStream {
31 : public:
32 E : explicit TestPdbStream(size_t length) : PdbStream(length) {
33 E : }
34 :
35 E : virtual ~TestPdbStream() {
36 E : }
37 :
38 E : bool ReadBytes(void* dest, size_t count, size_t* bytes_read) {
39 E : DCHECK(dest != NULL);
40 E : DCHECK(bytes_read != NULL);
41 :
42 E : if (pos() == length()) {
43 i : bytes_read = 0;
44 i : return true;
45 : }
46 :
47 E : count = std::min(count, length() - pos());
48 E : memset(dest, 0xFF, count);
49 E : Seek(pos() + count);
50 E : *bytes_read = count;
51 :
52 E : return true;
53 E : }
54 : };
55 :
56 : } // namespace
57 :
58 E : TEST(PdbByteStreamTest, InitFromByteArray) {
59 E : uint8 data[] = {1, 2, 3, 4, 5, 6, 7, 8};
60 :
61 E : scoped_refptr<PdbByteStream> stream(new PdbByteStream());
62 E : EXPECT_TRUE(stream->Init(data, arraysize(data)));
63 E : EXPECT_EQ(arraysize(data), stream->length());
64 E : EXPECT_TRUE(stream->data() != NULL);
65 :
66 E : for (size_t i = 0; i < stream->length(); ++i) {
67 E : uint8 num = 0;
68 E : EXPECT_TRUE(stream->Read(&num, 1));
69 E : EXPECT_EQ(data[i], num);
70 E : }
71 E : }
72 :
73 E : TEST(PdbByteStreamTest, InitFromPdbStream) {
74 E : scoped_refptr<TestPdbStream> test_stream(new TestPdbStream(64));
75 :
76 E : scoped_refptr<PdbByteStream> stream(new PdbByteStream);
77 E : EXPECT_TRUE(stream->Init(test_stream.get()));
78 E : EXPECT_EQ(test_stream->length(), stream->length());
79 E : EXPECT_TRUE(stream->data() != NULL);
80 :
81 E : for (size_t i = 0; i < stream->length(); ++i) {
82 E : uint8 num = 0;
83 E : EXPECT_TRUE(stream->Read(&num, 1));
84 E : EXPECT_EQ(0xFF, num);
85 E : }
86 E : }
87 :
88 E : TEST(PdbByteStreamTest, ReadBytes) {
89 E : size_t len = 17;
90 E : scoped_refptr<TestPdbStream> test_stream(new TestPdbStream(len));
91 :
92 E : scoped_refptr<TestPdbByteStream> stream(new TestPdbByteStream);
93 E : EXPECT_TRUE(stream->Init(test_stream.get()));
94 :
95 E : int total_bytes = 0;
96 E : while (true) {
97 : uint8 buffer[4];
98 E : size_t bytes_read = 0;
99 E : EXPECT_TRUE(stream->ReadBytes(buffer, sizeof(buffer), &bytes_read));
100 E : if (bytes_read == 0)
101 E : break;
102 E : total_bytes += bytes_read;
103 E : }
104 :
105 E : EXPECT_EQ(len, total_bytes);
106 E : }
107 :
108 E : TEST(PdbByteStreamTest, GetWritablePdbStream) {
109 E : scoped_refptr<PdbStream> stream(new PdbByteStream);
110 E : scoped_refptr<WritablePdbStream> writer1 = stream->GetWritablePdbStream();
111 E : EXPECT_TRUE(writer1.get() != NULL);
112 :
113 : // NOTE: This is a condition that only needs to be true currently because
114 : // of limitations in the WritablePdbByteStream implementation. When we
115 : // move to a proper interface implementation with shared storage state,
116 : // this limitation will be removed.
117 E : scoped_refptr<WritablePdbStream> writer2 = stream->GetWritablePdbStream();
118 E : EXPECT_EQ(writer1.get(), writer2.get());
119 E : }
120 :
121 E : TEST(WritablePdbByteStreamTest, WriterChangesReaderLengthButNotCursor) {
122 E : scoped_refptr<PdbStream> reader(new PdbByteStream);
123 E : scoped_refptr<WritablePdbStream> writer = reader->GetWritablePdbStream();
124 E : ASSERT_TRUE(writer.get() != NULL);
125 :
126 E : EXPECT_EQ(reader->length(), 0u);
127 E : EXPECT_EQ(reader->pos(), 0u);
128 E : EXPECT_EQ(writer->length(), 0u);
129 E : EXPECT_EQ(writer->pos(), 0u);
130 E : writer->Consume(10);
131 E : EXPECT_EQ(reader->length(), 10u);
132 E : EXPECT_EQ(reader->pos(), 0u);
133 E : EXPECT_EQ(writer->length(), 10u);
134 E : EXPECT_EQ(writer->pos(), 10u);
135 E : }
136 :
137 : } // namespace pdb
|