kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | 7a281a5 | 2012-06-27 03:22:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
pbos@webrtc.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 11 | #include "webrtc/test/testsupport/fileutils.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 12 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 13 | #include <assert.h> |
henrike@webrtc.org | f2aafe4 | 2014-04-29 17:54:17 +0000 | [diff] [blame] | 14 | |
| 15 | #ifdef WIN32 |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 16 | #include <direct.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 17 | #include <tchar.h> |
| 18 | #include <windows.h> |
kjellander@webrtc.org | de49966 | 2013-08-29 11:26:41 +0000 | [diff] [blame] | 19 | #include <algorithm> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 20 | |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 21 | #include "Shlwapi.h" |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame^] | 22 | #include "WinDef.h" |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 23 | |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/include/utf_util_win.h" |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 25 | #define GET_CURRENT_DIR _getcwd |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 26 | #else |
| 27 | #include <unistd.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 28 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 29 | #define GET_CURRENT_DIR getcwd |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 30 | #endif |
| 31 | |
| 32 | #include <sys/stat.h> // To check for directory existence. |
| 33 | #ifndef S_ISDIR // Not defined in stat.h on Windows. |
| 34 | #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 37 | #include <stdio.h> |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 38 | #include <stdlib.h> |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 39 | #include <string.h> |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 40 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 41 | #include <memory> |
| 42 | |
pbos@webrtc.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 43 | #include "webrtc/typedefs.h" // For architecture defines |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 44 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 45 | namespace webrtc { |
| 46 | namespace test { |
| 47 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 48 | #if defined(WEBRTC_IOS) |
| 49 | // Defined in iosfileutils.mm. No header file to discourage use elsewhere. |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 50 | std::string IOSOutputPath(); |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 51 | std::string IOSRootPath(); |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 52 | std::string IOSResourcePath(std::string name, std::string extension); |
| 53 | #endif |
| 54 | |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 57 | #ifdef WIN32 |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 58 | const char* kPathDelimiter = "\\"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 59 | #else |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 60 | const char* kPathDelimiter = "/"; |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 61 | #endif |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 62 | |
| 63 | #ifdef WEBRTC_ANDROID |
kjellander | 6820889 | 2016-06-16 23:29:30 -0700 | [diff] [blame] | 64 | const char* kRootDirName = "/sdcard/chromium_tests_root/"; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 65 | #else |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 66 | #if !defined(WEBRTC_IOS) |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 67 | const char* kOutputDirName = "out"; |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 68 | #endif |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 69 | const char* kFallbackPath = "./"; |
| 70 | #endif // !defined(WEBRTC_ANDROID) |
| 71 | |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 72 | #if !defined(WEBRTC_IOS) |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 73 | const char* kResourcesDirName = "resources"; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 74 | #endif |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 75 | |
pbos@webrtc.org | db7d82f | 2013-07-05 08:49:09 +0000 | [diff] [blame] | 76 | char relative_dir_path[FILENAME_MAX]; |
| 77 | bool relative_dir_path_set = false; |
henrike@webrtc.org | 34773d9 | 2013-07-08 14:55:23 +0000 | [diff] [blame] | 78 | |
| 79 | } // namespace |
| 80 | |
| 81 | const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR"; |
| 82 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 83 | void SetExecutablePath(const std::string& path) { |
| 84 | std::string working_dir = WorkingDir(); |
| 85 | std::string temp_path = path; |
| 86 | |
| 87 | // Handle absolute paths; convert them to relative paths to the working dir. |
| 88 | if (path.find(working_dir) != std::string::npos) { |
| 89 | temp_path = path.substr(working_dir.length() + 1); |
| 90 | } |
kjellander@webrtc.org | de49966 | 2013-08-29 11:26:41 +0000 | [diff] [blame] | 91 | // On Windows, when tests are run under memory tools like DrMemory and TSan, |
| 92 | // slashes occur in the path as directory separators. Make sure we replace |
| 93 | // such cases with backslashes in order for the paths to be correct. |
| 94 | #ifdef WIN32 |
| 95 | std::replace(temp_path.begin(), temp_path.end(), '/', '\\'); |
| 96 | #endif |
| 97 | |
kjellander@webrtc.org | 193600b | 2012-10-17 04:39:44 +0000 | [diff] [blame] | 98 | // Trim away the executable name; only store the relative dir path. |
| 99 | temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter)); |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 100 | strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX); |
| 101 | relative_dir_path_set = true; |
| 102 | } |
| 103 | |
| 104 | bool FileExists(std::string& file_name) { |
| 105 | struct stat file_info = {0}; |
| 106 | return stat(file_name.c_str(), &file_info) == 0; |
| 107 | } |
| 108 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 109 | #ifdef WEBRTC_ANDROID |
| 110 | |
| 111 | std::string ProjectRootPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 112 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | std::string OutputPath() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 116 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | std::string WorkingDir() { |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 120 | return kRootDirName; |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #else // WEBRTC_ANDROID |
| 124 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 125 | std::string ProjectRootPath() { |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 126 | #if defined(WEBRTC_IOS) |
| 127 | return IOSRootPath(); |
| 128 | #else |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 129 | std::string path = WorkingDir(); |
| 130 | if (path == kFallbackPath) { |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 131 | return kCannotFindProjectRootDir; |
| 132 | } |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 133 | if (relative_dir_path_set) { |
kjellander@webrtc.org | 83b767b | 2012-10-15 18:14:12 +0000 | [diff] [blame] | 134 | path = path + kPathDelimiter + relative_dir_path; |
| 135 | } |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 136 | path = path + kPathDelimiter + ".." + kPathDelimiter + ".."; |
| 137 | char canonical_path[FILENAME_MAX]; |
| 138 | #ifdef WIN32 |
ehmaldonado | 37535bf | 2016-12-05 06:42:45 -0800 | [diff] [blame^] | 139 | BOOL succeeded = PathCanonicalizeA(canonical_path, path.c_str()); |
ehmaldonado | 00f2ee0 | 2016-11-18 07:06:41 -0800 | [diff] [blame] | 140 | #else |
| 141 | bool succeeded = realpath(path.c_str(), canonical_path) != NULL; |
| 142 | #endif |
| 143 | if (succeeded) { |
| 144 | path = std::string(canonical_path) + kPathDelimiter; |
| 145 | return path; |
| 146 | } else { |
| 147 | fprintf(stderr, "Cannot find project root directory!\n"); |
| 148 | return kCannotFindProjectRootDir; |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 149 | } |
Kári Tristan Helgason | 470c088 | 2016-10-03 13:13:29 +0200 | [diff] [blame] | 150 | #endif |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 151 | } |
| 152 | |
andrew@webrtc.org | 0db7dc6 | 2011-11-13 01:34:05 +0000 | [diff] [blame] | 153 | std::string OutputPath() { |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 154 | #if defined(WEBRTC_IOS) |
| 155 | return IOSOutputPath(); |
| 156 | #else |
kjellander@webrtc.org | 72fd339 | 2014-11-05 06:28:50 +0000 | [diff] [blame] | 157 | std::string path = ProjectRootPath(); |
| 158 | if (path == kCannotFindProjectRootDir) { |
| 159 | return kFallbackPath; |
| 160 | } |
| 161 | path += kOutputDirName; |
| 162 | if (!CreateDir(path)) { |
| 163 | return kFallbackPath; |
| 164 | } |
| 165 | return path + kPathDelimiter; |
kjellander | 0206000 | 2016-02-16 22:06:12 -0800 | [diff] [blame] | 166 | #endif |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 167 | } |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 168 | |
| 169 | std::string WorkingDir() { |
| 170 | char path_buffer[FILENAME_MAX]; |
| 171 | if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) { |
| 172 | fprintf(stderr, "Cannot get current directory!\n"); |
| 173 | return kFallbackPath; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 174 | } else { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 175 | return std::string(path_buffer); |
| 176 | } |
| 177 | } |
| 178 | |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 179 | #endif // !WEBRTC_ANDROID |
| 180 | |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 181 | // Generate a temporary filename in a safe way. |
| 182 | // Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc. |
| 183 | std::string TempFilename(const std::string &dir, const std::string &prefix) { |
| 184 | #ifdef WIN32 |
| 185 | wchar_t filename[MAX_PATH]; |
| 186 | if (::GetTempFileName(ToUtf16(dir).c_str(), |
| 187 | ToUtf16(prefix).c_str(), 0, filename) != 0) |
| 188 | return ToUtf8(filename); |
| 189 | assert(false); |
| 190 | return ""; |
| 191 | #else |
| 192 | int len = dir.size() + prefix.size() + 2 + 6; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 193 | std::unique_ptr<char[]> tempname(new char[len]); |
kjellander@webrtc.org | 7de47bc | 2014-04-16 08:04:26 +0000 | [diff] [blame] | 194 | |
| 195 | snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), |
| 196 | prefix.c_str()); |
| 197 | int fd = ::mkstemp(tempname.get()); |
| 198 | if (fd == -1) { |
| 199 | assert(false); |
| 200 | return ""; |
| 201 | } else { |
| 202 | ::close(fd); |
| 203 | } |
| 204 | std::string ret(tempname.get()); |
| 205 | return ret; |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | bool CreateDir(std::string directory_name) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 210 | struct stat path_info = {0}; |
| 211 | // Check if the path exists already: |
| 212 | if (stat(directory_name.c_str(), &path_info) == 0) { |
| 213 | if (!S_ISDIR(path_info.st_mode)) { |
| 214 | fprintf(stderr, "Path %s exists but is not a directory! Remove this " |
| 215 | "file and re-run to create the directory.\n", |
| 216 | directory_name.c_str()); |
| 217 | return false; |
| 218 | } |
| 219 | } else { |
| 220 | #ifdef WIN32 |
| 221 | return _mkdir(directory_name.c_str()) == 0; |
| 222 | #else |
| 223 | return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0; |
| 224 | #endif |
| 225 | } |
| 226 | return true; |
| 227 | } |
| 228 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 229 | std::string ResourcePath(std::string name, std::string extension) { |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 230 | #if defined(WEBRTC_IOS) |
| 231 | return IOSResourcePath(name, extension); |
| 232 | #else |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 233 | std::string platform = "win"; |
| 234 | #ifdef WEBRTC_LINUX |
| 235 | platform = "linux"; |
| 236 | #endif // WEBRTC_LINUX |
| 237 | #ifdef WEBRTC_MAC |
| 238 | platform = "mac"; |
| 239 | #endif // WEBRTC_MAC |
ivoc | 72c08ed | 2016-01-20 07:26:24 -0800 | [diff] [blame] | 240 | #ifdef WEBRTC_ANDROID |
| 241 | platform = "android"; |
| 242 | #endif // WEBRTC_ANDROID |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 243 | |
| 244 | #ifdef WEBRTC_ARCH_64_BITS |
| 245 | std::string architecture = "64"; |
| 246 | #else |
| 247 | std::string architecture = "32"; |
| 248 | #endif // WEBRTC_ARCH_64_BITS |
| 249 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 250 | std::string resources_path = ProjectRootPath() + kResourcesDirName + |
| 251 | kPathDelimiter; |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 252 | std::string resource_file = resources_path + name + "_" + platform + "_" + |
| 253 | architecture + "." + extension; |
kjellander@webrtc.org | 80b2661 | 2011-12-07 18:50:17 +0000 | [diff] [blame] | 254 | if (FileExists(resource_file)) { |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 255 | return resource_file; |
| 256 | } |
| 257 | // Try without architecture. |
| 258 | resource_file = resources_path + name + "_" + platform + "." + extension; |
| 259 | if (FileExists(resource_file)) { |
| 260 | return resource_file; |
| 261 | } |
| 262 | // Try without platform. |
| 263 | resource_file = resources_path + name + "_" + architecture + "." + extension; |
| 264 | if (FileExists(resource_file)) { |
| 265 | return resource_file; |
| 266 | } |
leozwang@webrtc.org | 363efef | 2012-10-16 04:31:20 +0000 | [diff] [blame] | 267 | |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 268 | // Fall back on name without architecture or platform. |
| 269 | return resources_path + name + "." + extension; |
henrika | 1d34fe9 | 2015-06-16 10:04:20 +0200 | [diff] [blame] | 270 | #endif // defined (WEBRTC_IOS) |
kjellander@webrtc.org | 4ed4f24 | 2011-12-05 16:31:12 +0000 | [diff] [blame] | 271 | } |
| 272 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 273 | size_t GetFileSize(std::string filename) { |
| 274 | FILE* f = fopen(filename.c_str(), "rb"); |
| 275 | size_t size = 0; |
| 276 | if (f != NULL) { |
| 277 | if (fseek(f, 0, SEEK_END) == 0) { |
| 278 | size = ftell(f); |
| 279 | } |
| 280 | fclose(f); |
| 281 | } |
| 282 | return size; |
| 283 | } |
| 284 | |
kjellander@webrtc.org | 7951e81 | 2011-10-13 12:24:41 +0000 | [diff] [blame] | 285 | } // namespace test |
kjellander@webrtc.org | 4d8cd9d | 2011-11-09 11:24:14 +0000 | [diff] [blame] | 286 | } // namespace webrtc |