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_file_stream.h"
16 :
17 : #include "base/path_service.h"
18 : #include "base/files/file_util.h"
19 : #include "gtest/gtest.h"
20 : #include "syzygy/core/unittest_util.h"
21 : #include "syzygy/msf/msf_constants.h"
22 : #include "syzygy/msf/msf_data.h"
23 : #include "syzygy/msf/unittest_util.h"
24 :
25 : namespace msf {
26 :
27 : namespace {
28 :
29 : class TestMsfFileStream : public MsfFileStream {
30 : public:
31 E : TestMsfFileStream(RefCountedFILE* file,
32 : size_t length,
33 : const uint32_t* pages,
34 : size_t page_size)
35 E : : MsfFileStream(file, length, pages, page_size) {}
36 :
37 E : virtual ~TestMsfFileStream() {}
38 :
39 : using MsfFileStream::ReadFromPage;
40 : };
41 :
42 : class MsfFileStreamTest : public testing::Test {
43 : public:
44 E : virtual void SetUp() {
45 E : file_ = new RefCountedFILE(base::OpenFile(
46 : testing::GetSrcRelativePath(testing::kTestPdbFilePath), "rb"));
47 E : ASSERT_TRUE(file_.get() != NULL);
48 E : }
49 :
50 : protected:
51 : scoped_refptr<RefCountedFILE> file_;
52 : };
53 :
54 : } // namespace
55 :
56 E : TEST_F(MsfFileStreamTest, Constructor) {
57 E : size_t pages[] = {1, 2, 3};
58 E : scoped_refptr<MsfFileStream> stream(
59 : new MsfFileStream(file_.get(), 10, pages, 8));
60 E : EXPECT_EQ(10, stream->length());
61 E : }
62 :
63 E : TEST_F(MsfFileStreamTest, ReadFromPage) {
64 : struct TestCase {
65 : uint32_t page_num;
66 : size_t offset;
67 : size_t count;
68 : char* expected;
69 : };
70 :
71 : // Test calling ReadFromPage with different combinations of page number,
72 : // offset and count.
73 E : TestCase test_cases[] = {{0, 0, 3, "Mic"},
74 E : {0, 0, 4, "Micr"},
75 E : {0, 1, 2, "ic"},
76 E : {0, 2, 2, "cr"},
77 E : {1, 0, 2, "os"},
78 E : {1, 1, 3, "sof"},
79 E : {2, 0, 4, "t C/"},
80 E : {2, 2, 2, "C/"}};
81 :
82 E : size_t pages[] = {0, 1, 2};
83 E : size_t page_size = 4;
84 E : scoped_refptr<TestMsfFileStream> stream(
85 : new TestMsfFileStream(file_.get(), sizeof(MsfHeader), pages, page_size));
86 :
87 E : char buffer[4] = {0};
88 E : for (uint32_t i = 0; i < arraysize(test_cases); ++i) {
89 E : TestCase test_case = test_cases[i];
90 E : EXPECT_TRUE(stream->ReadFromPage(&buffer, test_case.page_num,
91 E : test_case.offset, test_case.count));
92 E : EXPECT_EQ(0,
93 E : ::memcmp(buffer, test_case.expected, strlen(test_case.expected)));
94 E : }
95 E : }
96 :
97 E : TEST_F(MsfFileStreamTest, ReadBytesAt) {
98 : // Different sections of the MSF header magic string.
99 E : char* test_cases[] = {"Mic", "roso", "ft", " C/C+", "+ MS", "F 7.00"};
100 :
101 : // Test that we can read varying sizes of bytes from the header of the
102 : // file with varying page sizes.
103 E : char buffer[8] = {0};
104 E : for (size_t page_size = 4; page_size <= 32; page_size *= 2) {
105 E : size_t pages[] = {0, 1, 2, 3, 4, 5, 6, 7};
106 E : scoped_refptr<TestMsfFileStream> stream(new TestMsfFileStream(
107 : file_.get(), sizeof(MsfHeader), pages, page_size));
108 :
109 E : size_t pos = 0;
110 E : for (uint32_t j = 0; j < arraysize(test_cases); ++j) {
111 E : char* test_case = test_cases[j];
112 E : size_t len = strlen(test_case);
113 E : EXPECT_TRUE(stream->ReadBytesAt(pos, len, &buffer));
114 E : EXPECT_EQ(0U, ::memcmp(buffer, test_case, len));
115 E : pos += len;
116 E : }
117 :
118 : // Try a read past the end of the file.
119 E : EXPECT_FALSE(stream->ReadBytesAt(sizeof(MsfHeader) - 1, 2, buffer));
120 E : }
121 E : }
122 :
123 : } // namespace msf
|