1 : // Copyright 2011 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 : // Unittests for core::file_util.h.
16 :
17 : #include "syzygy/core/file_util.h"
18 :
19 : #include "base/command_line.h"
20 : #include "base/files/file_util.h"
21 : #include "base/files/scoped_temp_dir.h"
22 : #include "gtest/gtest.h"
23 : #include "syzygy/core/unittest_util.h"
24 :
25 : namespace core {
26 :
27 : namespace {
28 :
29 : // For FilePath pretty-printing.
30 : std::ostream& operator<<(std::ostream& ostream, const base::FilePath& path) {
31 : ostream << "base::FilePath(" << path.value().c_str() << ")";
32 : return ostream;
33 : }
34 :
35 : class CompareFilePathsTest : public testing::Test {
36 : public:
37 E : virtual void SetUp() {
38 : // Initialize the temp directory for the first test.
39 E : if (temp_dir_.get() == NULL) {
40 E : temp_dir_.reset(new base::ScopedTempDir());
41 E : ASSERT_TRUE(temp_dir_->CreateUniqueTempDir());
42 : }
43 :
44 E : existing_path_ = testing::GetSrcRelativePath(L"syzygy\\core\\file_util.h");
45 : alternate_existing_path_ = testing::GetSrcRelativePath(
46 E : L"syzygy\\core\\..\\..\\syzygy\\core\\file_util.h");
47 : another_existing_path_ =
48 E : testing::GetSrcRelativePath(L"syzygy\\core\\file_util.cc");
49 :
50 E : ASSERT_TRUE(base::PathExists(existing_path_));
51 E : ASSERT_TRUE(base::PathExists(alternate_existing_path_));
52 E : ASSERT_NE(existing_path_, alternate_existing_path_);
53 E : ASSERT_TRUE(base::PathExists(another_existing_path_));
54 :
55 E : nonexisting_path_ = temp_dir_->path().Append(L"does\\not\\exist.txt");
56 : alternate_nonexisting_path_ = temp_dir_->path().Append(
57 E : L"does\\not\\..\\not\\exist.txt");
58 : another_nonexisting_path_ = temp_dir_->path().Append(
59 E : L"nonexisting.txt");
60 :
61 E : ASSERT_FALSE(base::PathExists(nonexisting_path_));
62 E : ASSERT_FALSE(base::PathExists(alternate_nonexisting_path_));
63 E : ASSERT_NE(nonexisting_path_, alternate_nonexisting_path_);
64 E : ASSERT_FALSE(base::PathExists(another_nonexisting_path_));
65 E : }
66 :
67 : base::FilePath existing_path_;
68 : base::FilePath alternate_existing_path_;
69 : base::FilePath another_existing_path_;
70 :
71 : base::FilePath nonexisting_path_;
72 : base::FilePath alternate_nonexisting_path_;
73 : base::FilePath another_nonexisting_path_;
74 :
75 : // This is static so that it is only initialized once for this whole group
76 : // of tests.
77 : static scoped_ptr<base::ScopedTempDir> temp_dir_;
78 : };
79 :
80 E : scoped_ptr<base::ScopedTempDir> CompareFilePathsTest::temp_dir_;
81 :
82 : } // namespace
83 :
84 E : TEST_F(CompareFilePathsTest, NeitherExistsDistinctPaths) {
85 : EXPECT_EQ(kUnableToCompareFilePaths,
86 : CompareFilePaths(nonexisting_path_,
87 E : another_nonexisting_path_));
88 E : }
89 :
90 E : TEST_F(CompareFilePathsTest, NeitherExistsIdenticalPaths) {
91 : EXPECT_EQ(kEquivalentFilePaths,
92 : CompareFilePaths(nonexisting_path_,
93 E : nonexisting_path_));
94 E : }
95 :
96 E : TEST_F(CompareFilePathsTest, NeitherExistsEquivalentPaths) {
97 : EXPECT_EQ(kEquivalentFilePaths,
98 : CompareFilePaths(nonexisting_path_,
99 E : alternate_nonexisting_path_));
100 E : }
101 :
102 E : TEST_F(CompareFilePathsTest, OnlyPath1Exists) {
103 : EXPECT_EQ(kDistinctFilePaths,
104 : CompareFilePaths(existing_path_,
105 E : nonexisting_path_));
106 E : }
107 :
108 E : TEST_F(CompareFilePathsTest, OnlyPath2Exists) {
109 : EXPECT_EQ(kDistinctFilePaths,
110 : CompareFilePaths(nonexisting_path_,
111 E : existing_path_));
112 E : }
113 :
114 E : TEST_F(CompareFilePathsTest, BothExistDistinctPaths) {
115 : EXPECT_EQ(kDistinctFilePaths,
116 : CompareFilePaths(existing_path_,
117 E : another_existing_path_));
118 E : }
119 :
120 E : TEST_F(CompareFilePathsTest, BothExistSamePath) {
121 : EXPECT_EQ(kEquivalentFilePaths,
122 : CompareFilePaths(existing_path_,
123 E : existing_path_));
124 E : }
125 :
126 E : TEST_F(CompareFilePathsTest, BothExistEquivalentPath) {
127 : EXPECT_EQ(kEquivalentFilePaths,
128 : CompareFilePaths(existing_path_,
129 E : alternate_existing_path_));
130 E : }
131 :
132 E : TEST(GuessFileTypeTest, GuessFromInMemoryBuffer) {
133 : // Read a file into memory.
134 : base::FilePath path = testing::GetSrcRelativePath(
135 E : testing::kExampleCoffImportDefinition);
136 E : int64 file_size = 0;
137 E : ASSERT_TRUE(base::GetFileSize(path, &file_size));
138 E : size_t length = static_cast<size_t>(file_size);
139 E : std::vector<uint8> buffer(length);
140 : ASSERT_TRUE(base::ReadFile(
141 E : path, reinterpret_cast<char*>(buffer.data()), buffer.size()));
142 :
143 E : FileType file_type = kUnknownFileType;
144 E : EXPECT_TRUE(GuessFileType(buffer.data(), buffer.size(), &file_type));
145 E : EXPECT_EQ(kImportDefinitionFileType, file_type);
146 E : }
147 :
148 E : TEST(GuessFileTypeTest, IdentifiesAllTypes) {
149 E : base::FilePath fake(L"C:\\this\\path\\should\\not\\exist-at.all");
150 E : base::FilePath dir = testing::GetSrcRelativePath(L"syzygy\\core\\test_data");
151 E : base::FilePath pe_dll = testing::GetSrcRelativePath(testing::kExamplePeDll);
152 E : base::FilePath coff_obj = testing::GetSrcRelativePath(testing::kExampleCoff);
153 : base::FilePath ltcg_obj = testing::GetSrcRelativePath(
154 E : testing::kExampleCoffLtcgName);
155 E : base::FilePath pe_exe = testing::GetSrcRelativePath(testing::kExamplePeExe);
156 E : base::FilePath pdb = testing::GetSrcRelativePath(testing::kExamplePdbName);
157 : base::FilePath null_machine_coff = testing::GetSrcRelativePath(
158 E : testing::kExampleCoffMachineTypeNullName);
159 : base::FilePath resources32 = testing::GetSrcRelativePath(
160 E : testing::kExampleResources32Name);
161 : base::FilePath archive = testing::GetSrcRelativePath(
162 E : testing::kExampleArchiveName);
163 : base::FilePath import_def = testing::GetSrcRelativePath(
164 E : testing::kExampleCoffImportDefinition);
165 :
166 : // Doesn't exist.
167 E : FileType file_type = kUnknownFileType;
168 E : EXPECT_FALSE(GuessFileType(fake, &file_type));
169 E : EXPECT_EQ(kUnknownFileType, file_type);
170 :
171 : // Can't be opened for reading.
172 E : file_type = kUnknownFileType;
173 E : EXPECT_FALSE(GuessFileType(dir, &file_type));
174 E : EXPECT_EQ(kUnknownFileType, file_type);
175 :
176 E : file_type = kUnknownFileType;
177 E : EXPECT_TRUE(GuessFileType(pe_dll, &file_type));
178 E : EXPECT_EQ(kPeFileType, file_type);
179 :
180 E : file_type = kUnknownFileType;
181 E : EXPECT_TRUE(GuessFileType(coff_obj, &file_type));
182 E : EXPECT_EQ(kCoffFileType, file_type);
183 :
184 E : file_type = kUnknownFileType;
185 E : EXPECT_TRUE(GuessFileType(ltcg_obj, &file_type));
186 E : EXPECT_EQ(kAnonymousCoffFileType, file_type);
187 :
188 E : file_type = kUnknownFileType;
189 E : EXPECT_TRUE(GuessFileType(pe_exe, &file_type));
190 E : EXPECT_EQ(kPeFileType, file_type);
191 :
192 E : file_type = kUnknownFileType;
193 E : EXPECT_TRUE(GuessFileType(pdb, &file_type));
194 E : EXPECT_EQ(kPdbFileType, file_type);
195 :
196 E : file_type = kUnknownFileType;
197 E : EXPECT_TRUE(GuessFileType(null_machine_coff, &file_type));
198 E : EXPECT_EQ(kCoffFileType, file_type);
199 :
200 E : file_type = kUnknownFileType;
201 E : EXPECT_TRUE(GuessFileType(resources32, &file_type));
202 E : EXPECT_EQ(kResourceFileType, file_type);
203 :
204 E : file_type = kUnknownFileType;
205 E : EXPECT_TRUE(GuessFileType(archive, &file_type));
206 E : EXPECT_EQ(kArchiveFileType, file_type);
207 :
208 E : file_type = kUnknownFileType;
209 E : EXPECT_TRUE(GuessFileType(import_def, &file_type));
210 E : EXPECT_EQ(kImportDefinitionFileType, file_type);
211 E : }
212 :
213 : } // namespace core
|