blob: 2aff75a4d91d61d91cf97e78bd3ba79bd4f89322 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
andrew@webrtc.org7a281a52012-06-27 03:22:37 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "test/testsupport/fileutils.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000012
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000013#include <assert.h>
henrike@webrtc.orgf2aafe42014-04-29 17:54:17 +000014
15#ifdef WIN32
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000016#include <direct.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000017#include <tchar.h>
18#include <windows.h>
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000019#include <algorithm>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000020
ehmaldonado00f2ee02016-11-18 07:06:41 -080021#include "Shlwapi.h"
ehmaldonado37535bf2016-12-05 06:42:45 -080022#include "WinDef.h"
ehmaldonado00f2ee02016-11-18 07:06:41 -080023
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/win32.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000025#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000026#else
alessiob00b16f42017-06-01 03:29:40 -070027#include <dirent.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000028#include <unistd.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000029
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000030#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000031#endif
32
33#include <sys/stat.h> // To check for directory existence.
34#ifndef S_ISDIR // Not defined in stat.h on Windows.
35#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000036#endif
37
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000038#include <stdio.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000039#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000040#include <string.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000041
kwibergbfefb032016-05-01 14:53:46 -070042#include <memory>
alessiob00b16f42017-06-01 03:29:40 -070043#include <utility>
kwibergbfefb032016-05-01 14:53:46 -070044
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020045#include "rtc_base/checks.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020046#include "typedefs.h" // NOLINT(build/include) // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000047
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000048namespace webrtc {
49namespace test {
50
henrika1d34fe92015-06-16 10:04:20 +020051#if defined(WEBRTC_IOS)
52// Defined in iosfileutils.mm. No header file to discourage use elsewhere.
kjellander02060002016-02-16 22:06:12 -080053std::string IOSOutputPath();
Kári Tristan Helgason470c0882016-10-03 13:13:29 +020054std::string IOSRootPath();
henrika1d34fe92015-06-16 10:04:20 +020055std::string IOSResourcePath(std::string name, std::string extension);
56#endif
57
henrike@webrtc.org34773d92013-07-08 14:55:23 +000058namespace {
59
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000060#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000061const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000062#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000063const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000064#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000065
66#ifdef WEBRTC_ANDROID
kjellander68208892016-06-16 23:29:30 -070067const char* kRootDirName = "/sdcard/chromium_tests_root/";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000068#else
kjellander02060002016-02-16 22:06:12 -080069#if !defined(WEBRTC_IOS)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000070const char* kOutputDirName = "out";
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000071#endif
kjellander02060002016-02-16 22:06:12 -080072const char* kFallbackPath = "./";
73#endif // !defined(WEBRTC_ANDROID)
74
henrika1d34fe92015-06-16 10:04:20 +020075#if !defined(WEBRTC_IOS)
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000076const char* kResourcesDirName = "resources";
henrika1d34fe92015-06-16 10:04:20 +020077#endif
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000078
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000079char relative_dir_path[FILENAME_MAX];
80bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000081
82} // namespace
83
84const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
85
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000086void SetExecutablePath(const std::string& path) {
87 std::string working_dir = WorkingDir();
88 std::string temp_path = path;
89
90 // Handle absolute paths; convert them to relative paths to the working dir.
91 if (path.find(working_dir) != std::string::npos) {
92 temp_path = path.substr(working_dir.length() + 1);
93 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000094 // On Windows, when tests are run under memory tools like DrMemory and TSan,
95 // slashes occur in the path as directory separators. Make sure we replace
96 // such cases with backslashes in order for the paths to be correct.
97#ifdef WIN32
98 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
99#endif
100
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000101 // Trim away the executable name; only store the relative dir path.
102 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000103 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
104 relative_dir_path_set = true;
105}
106
ehmaldonado88df0bc2017-02-10 09:27:14 -0800107bool FileExists(const std::string& file_name) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000108 struct stat file_info = {0};
109 return stat(file_name.c_str(), &file_info) == 0;
110}
111
alessiobe49fede2017-03-15 06:04:59 -0700112bool DirExists(const std::string& directory_name) {
113 struct stat directory_info = {0};
114 return stat(directory_name.c_str(), &directory_info) == 0 && S_ISDIR(
115 directory_info.st_mode);
116}
117
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000118#ifdef WEBRTC_ANDROID
119
120std::string ProjectRootPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000121 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000122}
123
124std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000125 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000126}
127
128std::string WorkingDir() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000129 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000130}
131
alessiob00b16f42017-06-01 03:29:40 -0700132#else // WEBRTC_ANDROID
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000133
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000134std::string ProjectRootPath() {
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200135#if defined(WEBRTC_IOS)
136 return IOSRootPath();
137#else
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000138 std::string path = WorkingDir();
139 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000140 return kCannotFindProjectRootDir;
141 }
ehmaldonado00f2ee02016-11-18 07:06:41 -0800142 if (relative_dir_path_set) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000143 path = path + kPathDelimiter + relative_dir_path;
144 }
ehmaldonado00f2ee02016-11-18 07:06:41 -0800145 path = path + kPathDelimiter + ".." + kPathDelimiter + "..";
146 char canonical_path[FILENAME_MAX];
147#ifdef WIN32
ehmaldonado37535bf2016-12-05 06:42:45 -0800148 BOOL succeeded = PathCanonicalizeA(canonical_path, path.c_str());
ehmaldonado00f2ee02016-11-18 07:06:41 -0800149#else
150 bool succeeded = realpath(path.c_str(), canonical_path) != NULL;
151#endif
152 if (succeeded) {
153 path = std::string(canonical_path) + kPathDelimiter;
154 return path;
155 } else {
156 fprintf(stderr, "Cannot find project root directory!\n");
157 return kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000158 }
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200159#endif
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000160}
161
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000162std::string OutputPath() {
kjellander02060002016-02-16 22:06:12 -0800163#if defined(WEBRTC_IOS)
164 return IOSOutputPath();
165#else
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000166 std::string path = ProjectRootPath();
167 if (path == kCannotFindProjectRootDir) {
168 return kFallbackPath;
169 }
170 path += kOutputDirName;
171 if (!CreateDir(path)) {
172 return kFallbackPath;
173 }
174 return path + kPathDelimiter;
kjellander02060002016-02-16 22:06:12 -0800175#endif
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000176}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000177
178std::string WorkingDir() {
179 char path_buffer[FILENAME_MAX];
180 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
181 fprintf(stderr, "Cannot get current directory!\n");
182 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000183 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000184 return std::string(path_buffer);
185 }
186}
187
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000188#endif // !WEBRTC_ANDROID
189
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000190// Generate a temporary filename in a safe way.
191// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
192std::string TempFilename(const std::string &dir, const std::string &prefix) {
193#ifdef WIN32
194 wchar_t filename[MAX_PATH];
nisse07b83882017-03-14 01:32:50 -0700195 if (::GetTempFileName(rtc::ToUtf16(dir).c_str(),
196 rtc::ToUtf16(prefix).c_str(), 0, filename) != 0)
197 return rtc::ToUtf8(filename);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000198 assert(false);
199 return "";
200#else
201 int len = dir.size() + prefix.size() + 2 + 6;
kwibergbfefb032016-05-01 14:53:46 -0700202 std::unique_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000203
204 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
205 prefix.c_str());
206 int fd = ::mkstemp(tempname.get());
207 if (fd == -1) {
208 assert(false);
209 return "";
210 } else {
211 ::close(fd);
212 }
213 std::string ret(tempname.get());
214 return ret;
215#endif
216}
217
alessiob00b16f42017-06-01 03:29:40 -0700218rtc::Optional<std::vector<std::string>> ReadDirectory(std::string path) {
219 if (path.length() == 0)
220 return rtc::Optional<std::vector<std::string>>();
221
222#if defined(WEBRTC_WIN)
223 // Append separator character if needed.
224 if (path.back() != '\\')
225 path += '\\';
226
227 // Init.
228 WIN32_FIND_DATA data;
229 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
230 if (handle == INVALID_HANDLE_VALUE)
231 return rtc::Optional<std::vector<std::string>>();
232
233 // Populate output.
234 std::vector<std::string> found_entries;
235 do {
236 const std::string name = rtc::ToUtf8(data.cFileName);
237 if (name != "." && name != "..")
238 found_entries.emplace_back(path + name);
239 } while (::FindNextFile(handle, &data) == TRUE);
240
241 // Release resources.
242 if (handle != INVALID_HANDLE_VALUE)
243 ::FindClose(handle);
244#else
245 // Append separator character if needed.
246 if (path.back() != '/')
247 path += '/';
248
249 // Init.
250 DIR* dir = ::opendir(path.c_str());
251 if (dir == nullptr)
252 return rtc::Optional<std::vector<std::string>>();
253
254 // Populate output.
255 std::vector<std::string> found_entries;
256 while (dirent* dirent = readdir(dir)) {
257 const std::string& name = dirent->d_name;
258 if (name != "." && name != "..")
259 found_entries.emplace_back(path + name);
260 }
261
262 // Release resources.
263 closedir(dir);
264#endif
265
266 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
267}
268
ehmaldonado88df0bc2017-02-10 09:27:14 -0800269bool CreateDir(const std::string& directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000270 struct stat path_info = {0};
271 // Check if the path exists already:
272 if (stat(directory_name.c_str(), &path_info) == 0) {
273 if (!S_ISDIR(path_info.st_mode)) {
274 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
275 "file and re-run to create the directory.\n",
276 directory_name.c_str());
277 return false;
278 }
279 } else {
280#ifdef WIN32
281 return _mkdir(directory_name.c_str()) == 0;
282#else
283 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
284#endif
285 }
286 return true;
287}
288
nisse57efb032017-05-18 03:55:59 -0700289bool RemoveDir(const std::string& directory_name) {
290#ifdef WIN32
291 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
292#else
293 return rmdir(directory_name.c_str()) == 0;
294#endif
295}
296
297bool RemoveFile(const std::string& file_name) {
298#ifdef WIN32
299 return DeleteFileA(file_name.c_str()) != FALSE;
300#else
301 return unlink(file_name.c_str()) == 0;
302#endif
303}
304
ehmaldonado88df0bc2017-02-10 09:27:14 -0800305std::string ResourcePath(const std::string& name,
306 const std::string& extension) {
henrika1d34fe92015-06-16 10:04:20 +0200307#if defined(WEBRTC_IOS)
308 return IOSResourcePath(name, extension);
309#else
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000310 std::string platform = "win";
311#ifdef WEBRTC_LINUX
312 platform = "linux";
313#endif // WEBRTC_LINUX
314#ifdef WEBRTC_MAC
315 platform = "mac";
316#endif // WEBRTC_MAC
ivoc72c08ed2016-01-20 07:26:24 -0800317#ifdef WEBRTC_ANDROID
318 platform = "android";
319#endif // WEBRTC_ANDROID
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000320
321#ifdef WEBRTC_ARCH_64_BITS
322 std::string architecture = "64";
323#else
324 std::string architecture = "32";
325#endif // WEBRTC_ARCH_64_BITS
326
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000327 std::string resources_path = ProjectRootPath() + kResourcesDirName +
328 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000329 std::string resource_file = resources_path + name + "_" + platform + "_" +
330 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000331 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000332 return resource_file;
333 }
334 // Try without architecture.
335 resource_file = resources_path + name + "_" + platform + "." + extension;
336 if (FileExists(resource_file)) {
337 return resource_file;
338 }
339 // Try without platform.
340 resource_file = resources_path + name + "_" + architecture + "." + extension;
341 if (FileExists(resource_file)) {
342 return resource_file;
343 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000344
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000345 // Fall back on name without architecture or platform.
346 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200347#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000348}
349
ehmaldonado88df0bc2017-02-10 09:27:14 -0800350size_t GetFileSize(const std::string& filename) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000351 FILE* f = fopen(filename.c_str(), "rb");
352 size_t size = 0;
353 if (f != NULL) {
354 if (fseek(f, 0, SEEK_END) == 0) {
355 size = ftell(f);
356 }
357 fclose(f);
358 }
359 return size;
360}
361
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000362} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000363} // namespace webrtc