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/core/unittest_util.h"
16 :
17 : #include "base/path_service.h"
18 : #include "syzygy/core/file_util.h"
19 :
20 : namespace testing {
21 :
22 : const wchar_t kExampleArchiveName[] =
23 : L"syzygy\\core\\test_data\\archive.lib";
24 : const wchar_t kExampleCoff[] =
25 : L"syzygy\\core\\test_data\\coff.obj";
26 : const wchar_t kExampleCoffImportDefinition[] =
27 : L"syzygy\\core\\test_data\\import_definition.obj";
28 : const wchar_t kExampleCoffLtcgName[] =
29 : L"syzygy\\core\\test_data\\coff_ltcg.obj";
30 : const wchar_t kExampleCoffMachineTypeNullName[] =
31 : L"syzygy\\core\\test_data\\machine_type_null.obj";
32 : const wchar_t kExamplePdbName[] =
33 : L"syzygy\\core\\test_data\\foo.pdb";
34 : const wchar_t kExamplePeDll[] =
35 : L"syzygy\\core\\test_data\\foo.dll";
36 : const wchar_t kExamplePeExe[] =
37 : L"syzygy\\core\\test_data\\foo.exe";
38 : const wchar_t kExampleResources32Name[] =
39 : L"syzygy\\core\\test_data\\resources32.obj";
40 :
41 E : base::FilePath GetSrcRelativePath(const wchar_t* rel_path) {
42 E : base::FilePath src_dir;
43 E : PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
44 E : return src_dir.Append(rel_path);
45 E : }
46 :
47 E : base::FilePath GetExeRelativePath(const wchar_t* rel_path) {
48 E : base::FilePath exe_dir;
49 E : PathService::Get(base::DIR_EXE, &exe_dir);
50 E : return exe_dir.Append(rel_path);
51 E : }
52 :
53 E : base::FilePath GetOutputRelativePath(const wchar_t* rel_path) {
54 E : base::FilePath src_dir;
55 E : PathService::Get(base::DIR_SOURCE_ROOT, &src_dir);
56 : // Append the output path configured by the build system.
57 E : src_dir = src_dir.AppendASCII(BUILD_OUTPUT_DIR);
58 E : return src_dir.Append(rel_path);
59 E : }
60 :
61 E : base::FilePath GetExeTestDataRelativePath(const wchar_t* rel_path) {
62 E : base::FilePath exe_dir;
63 E : PathService::Get(base::DIR_EXE, &exe_dir);
64 E : base::FilePath test_data = exe_dir.Append(L"test_data");
65 E : return test_data.Append(rel_path);
66 E : }
67 :
68 : base::FilePath GetRelativePath(const base::FilePath& abs_path,
69 E : const base::FilePath& root_path) {
70 E : DCHECK(abs_path.IsAbsolute());
71 E : DCHECK(!abs_path.empty());
72 E : DCHECK(root_path.IsAbsolute());
73 E : DCHECK(!root_path.empty());
74 :
75 : typedef std::vector<base::FilePath::StringType> PathComponents;
76 :
77 : // Get the components of the target path.
78 E : PathComponents abs_parts;
79 E : abs_path.GetComponents(&abs_parts);
80 :
81 : // Get the components of the current working directory.
82 E : PathComponents root_parts;
83 E : root_path.GetComponents(&root_parts);
84 :
85 : // Make sure they have a common root.
86 E : if (!base::FilePath::CompareEqualIgnoreCase(root_parts[0], abs_parts[0]))
87 E : return base::FilePath();
88 :
89 : // Figure out how much is shared.
90 E : size_t i = 1;
91 : while (i < std::min(root_parts.size(), abs_parts.size()) &&
92 E : base::FilePath::CompareEqualIgnoreCase(root_parts[i], abs_parts[i])) {
93 E : ++i;
94 E : }
95 :
96 E : base::FilePath rel_path;
97 :
98 : // Add parent directory traversal.
99 E : for (size_t j = i; j < root_parts.size(); ++j)
100 E : rel_path = rel_path.Append(base::FilePath::kParentDirectory);
101 :
102 : // Append the rest of the path.
103 E : for (size_t k = i; k < abs_parts.size(); ++k)
104 E : rel_path = rel_path.Append(abs_parts[k]);
105 :
106 E : if (rel_path.empty())
107 E : rel_path = base::FilePath(base::FilePath::kCurrentDirectory);
108 :
109 E : return rel_path;
110 E : }
111 :
112 E : base::FilePath GetRelativePath(const base::FilePath& abs_path) {
113 E : base::FilePath cur_dir;
114 E : base::GetCurrentDirectory(&cur_dir);
115 E : return GetRelativePath(abs_path, cur_dir);
116 E : }
117 :
118 : AssertionResult AssertAreSameFile(const char* path1_expr,
119 : const char* path2_expr,
120 : const base::FilePath& path1,
121 E : const base::FilePath& path2) {
122 E : core::FilePathCompareResult result = core::CompareFilePaths(path1, path2);
123 E : if (result == core::kEquivalentFilePaths)
124 E : return ::testing::AssertionSuccess();
125 :
126 : return ::testing::AssertionFailure() << "FilePathsReferToSameFile("
127 : << path1_expr << ", " << path2_expr << ") returned " << result
128 : << ", expected " << core::kEquivalentFilePaths << " (" << path1_expr
129 : << " = \"" << path1.value() << "\", " << path2_expr << " = \""
130 i : << path2.value() << "\").";
131 E : }
132 :
133 : } // namespace testing
|