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/msf/msf_stream.h"
16 :
17 : #include "gtest/gtest.h"
18 :
19 : namespace msf {
20 :
21 : namespace {
22 :
23 : class TestMsfStream : public MsfStream {
24 : public:
25 E : explicit TestMsfStream(size_t length) : MsfStream(length) {}
26 :
27 : using MsfStream::pos;
28 :
29 : // A simple implementation of ReadBytes.
30 E : bool ReadBytes(void* dest, size_t count, size_t* bytes_read) {
31 E : DCHECK(dest != NULL);
32 E : DCHECK(bytes_read != NULL);
33 :
34 E : if (pos() == length()) {
35 i : *bytes_read = 0;
36 i : return true;
37 E : } else if (count > length() - pos()) {
38 i : return false;
39 : }
40 :
41 E : Seek(pos() + count);
42 E : *bytes_read = count;
43 E : return true;
44 E : }
45 : };
46 :
47 : struct Foo {
48 : uint32 i;
49 : double d;
50 : };
51 :
52 : struct Bar {
53 : Foo foo1;
54 : Foo foo2;
55 : };
56 :
57 : } // namespace
58 :
59 E : TEST(MsfStreamTest, Constructor) {
60 E : scoped_refptr<TestMsfStream> stream(new TestMsfStream(5));
61 E : EXPECT_EQ(5, stream->length());
62 E : EXPECT_EQ(0, stream->pos());
63 :
64 E : scoped_refptr<TestMsfStream> stream2(new TestMsfStream(SIZE_MAX));
65 E : EXPECT_EQ(0, stream2->length());
66 E : EXPECT_EQ(0, stream2->pos());
67 E : }
68 :
69 E : TEST(MsfStreamTest, Read) {
70 E : scoped_refptr<TestMsfStream> stream(new TestMsfStream(12));
71 : uint8 num8;
72 : uint16 num16;
73 : uint32 num32;
74 :
75 : // 3 valid reads.
76 E : EXPECT_TRUE(stream->Read(&num8, 3)); // 0..2
77 E : EXPECT_TRUE(stream->Read(&num16, 2)); // 3..6
78 E : EXPECT_TRUE(stream->Read(&num32, 1)); // 7..10
79 :
80 : // Try to read over the end of the stream.
81 E : EXPECT_FALSE(stream->Read(&num32, 1));
82 :
83 : // Read to the end of the stream, using the version of read that reports
84 : // the number of items read.
85 E : size_t items_read = 0;
86 E : EXPECT_TRUE(stream->Read(&num8, 1, &items_read)); // 11
87 E : EXPECT_EQ(1u, items_read);
88 : // Read over the end of the stream.
89 E : EXPECT_FALSE(stream->Read(&num8, 4));
90 E : }
91 :
92 E : TEST(MsfStreamTest, ReadVector) {
93 E : scoped_refptr<TestMsfStream> stream(new TestMsfStream(sizeof(Foo) * 10));
94 :
95 E : std::vector<Foo> foos;
96 :
97 : // A couple of valid reads.
98 E : EXPECT_TRUE(stream->Read(&foos, 2)); // 0..1
99 E : EXPECT_EQ(2u, foos.size());
100 E : EXPECT_TRUE(stream->Read(&foos, 3)); // 2..4
101 E : EXPECT_EQ(3u, foos.size());
102 :
103 : // Try to read past the end of the stream->
104 E : EXPECT_FALSE(stream->Read(&foos, 6));
105 :
106 : // There are 5 elements left. If we try to read Bars until the end of the
107 : // stream it should fail as 5 Foos = 2.5 Bars.
108 E : std::vector<Bar> bars;
109 E : EXPECT_FALSE(stream->Read(&bars));
110 :
111 : // However, we should be able to read Foos until the end of the stream.
112 E : EXPECT_TRUE(stream->Read(&foos));
113 E : EXPECT_EQ(5u, foos.size());
114 E : }
115 :
116 E : TEST(MsfStreamTest, Seek) {
117 E : scoped_refptr<TestMsfStream> stream(new TestMsfStream(5));
118 E : EXPECT_EQ(0, stream->pos());
119 :
120 : // Valid seeks.
121 E : EXPECT_TRUE(stream->Seek(0));
122 E : EXPECT_EQ(0, stream->pos());
123 :
124 E : EXPECT_TRUE(stream->Seek(3));
125 E : EXPECT_EQ(3, stream->pos());
126 :
127 E : EXPECT_TRUE(stream->Seek(5));
128 E : EXPECT_EQ(5, stream->pos());
129 :
130 : // Invalid seek.
131 E : EXPECT_FALSE(stream->Seek(6));
132 E : EXPECT_EQ(5, stream->pos());
133 E : }
134 :
135 : } // namespace msf
|