1 // Copyright 2015 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/utils/path_service.h"
6 
7 #ifdef _WIN32
8 #include <Windows.h>
9 #elif defined(__APPLE__)
10 #include <mach-o/dyld.h>
11 #include <sys/stat.h>
12 #else  // Linux
13 #include <linux/limits.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #endif  // _WIN32
17 
18 #include <string>
19 
20 #include "core/fxcrt/fx_system.h"
21 
22 namespace {
23 
24 #if defined(__APPLE__) || (defined(ANDROID) && __ANDROID_API__ < 21)
25 using stat_wrapper_t = struct stat;
26 
CallStat(const char * path,stat_wrapper_t * sb)27 int CallStat(const char* path, stat_wrapper_t* sb) {
28   return stat(path, sb);
29 }
30 #elif !_WIN32
31 using stat_wrapper_t = struct stat64;
32 
33 int CallStat(const char* path, stat_wrapper_t* sb) {
34   return stat64(path, sb);
35 }
36 #endif
37 
38 }  // namespace
39 
40 // static
DirectoryExists(const std::string & path)41 bool PathService::DirectoryExists(const std::string& path) {
42 #ifdef _WIN32
43   DWORD fileattr = GetFileAttributesA(path.c_str());
44   if (fileattr != INVALID_FILE_ATTRIBUTES)
45     return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0;
46   return false;
47 #else
48   stat_wrapper_t file_info;
49   if (CallStat(path.c_str(), &file_info) != 0)
50     return false;
51   return S_ISDIR(file_info.st_mode);
52 #endif
53 }
54 
55 // static
EndsWithSeparator(const std::string & path)56 bool PathService::EndsWithSeparator(const std::string& path) {
57   return path.size() > 1 && path[path.size() - 1] == PATH_SEPARATOR;
58 }
59 
60 // static
GetExecutableDir(std::string * path)61 bool PathService::GetExecutableDir(std::string* path) {
62 // Get the current executable file path.
63 #ifdef _WIN32
64   char path_buffer[MAX_PATH];
65   path_buffer[0] = 0;
66 
67   if (GetModuleFileNameA(NULL, path_buffer, MAX_PATH) == 0)
68     return false;
69   *path = std::string(path_buffer);
70 #elif defined(__APPLE__)
71   ASSERT(path);
72   unsigned int path_length = 0;
73   _NSGetExecutablePath(NULL, &path_length);
74   if (path_length == 0)
75     return false;
76 
77   path->reserve(path_length);
78   path->resize(path_length - 1);
79   if (_NSGetExecutablePath(&((*path)[0]), &path_length))
80     return false;
81 #else   // Linux
82   static const char kProcSelfExe[] = "/proc/self/exe";
83   char buf[PATH_MAX];
84   ssize_t count = ::readlink(kProcSelfExe, buf, PATH_MAX);
85   if (count <= 0)
86     return false;
87 
88   *path = std::string(buf, count);
89 #endif  // _WIN32
90 
91   // Get the directory path.
92   std::size_t pos = path->size() - 1;
93   if (EndsWithSeparator(*path))
94     pos--;
95   std::size_t found = path->find_last_of(PATH_SEPARATOR, pos);
96   if (found == std::string::npos)
97     return false;
98   path->resize(found);
99   return true;
100 }
101 
102 // static
GetSourceDir(std::string * path)103 bool PathService::GetSourceDir(std::string* path) {
104   if (!GetExecutableDir(path))
105     return false;
106 
107   if (!EndsWithSeparator(*path))
108     path->push_back(PATH_SEPARATOR);
109   path->append("..");
110   path->push_back(PATH_SEPARATOR);
111 #if defined(ANDROID)
112   path->append("chromium_tests_root");
113 #else   // Non-Android
114   path->append("..");
115 #endif  // defined(ANDROID)
116   return true;
117 }
118 
119 // static
GetTestDataDir(std::string * path)120 bool PathService::GetTestDataDir(std::string* path) {
121   if (!GetSourceDir(path))
122     return false;
123 
124   if (!EndsWithSeparator(*path))
125     path->push_back(PATH_SEPARATOR);
126 
127   std::string potential_path = *path;
128   potential_path.append("testing");
129   potential_path.push_back(PATH_SEPARATOR);
130   potential_path.append("resources");
131   if (PathService::DirectoryExists(potential_path)) {
132     *path = potential_path;
133     return true;
134   }
135 
136   potential_path = *path;
137   potential_path.append("third_party");
138   potential_path.push_back(PATH_SEPARATOR);
139   potential_path.append("pdfium");
140   potential_path.push_back(PATH_SEPARATOR);
141   potential_path.append("testing");
142   potential_path.push_back(PATH_SEPARATOR);
143   potential_path.append("resources");
144   if (PathService::DirectoryExists(potential_path)) {
145     *path = potential_path;
146     return true;
147   }
148 
149   return false;
150 }
151 
152 // static
GetTestFilePath(const std::string & file_name,std::string * path)153 bool PathService::GetTestFilePath(const std::string& file_name,
154                                   std::string* path) {
155   if (!GetTestDataDir(path))
156     return false;
157 
158   if (!EndsWithSeparator(*path))
159     path->push_back(PATH_SEPARATOR);
160   path->append(file_name);
161   return true;
162 }
163 
164 // static
GetThirdPartyFilePath(const std::string & file_name,std::string * path)165 bool PathService::GetThirdPartyFilePath(const std::string& file_name,
166                                         std::string* path) {
167   if (!GetSourceDir(path))
168     return false;
169 
170   if (!EndsWithSeparator(*path))
171     path->push_back(PATH_SEPARATOR);
172 
173   std::string potential_path = *path;
174   potential_path.append("third_party");
175   if (PathService::DirectoryExists(potential_path)) {
176     *path = potential_path;
177     path->append(PATH_SEPARATOR + file_name);
178     return true;
179   }
180 
181   potential_path = *path;
182   potential_path.append("third_party");
183   potential_path.push_back(PATH_SEPARATOR);
184   potential_path.append("pdfium");
185   potential_path.push_back(PATH_SEPARATOR);
186   potential_path.append("third_party");
187   if (PathService::DirectoryExists(potential_path)) {
188     *path = potential_path;
189     path->append(PATH_SEPARATOR + file_name);
190     return true;
191   }
192 
193   return false;
194 }
195