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