blob: 309c885f572f95d55e8bf5fb7572f23d481cf222 [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
Artem Titove62f6002018-03-19 15:40:00 +0100218std::string GenerateTempFilename(const std::string& dir,
219 const std::string& prefix) {
220 std::string filename = TempFilename(dir, prefix);
221 RemoveFile(filename);
222 return filename;
223}
224
alessiob00b16f42017-06-01 03:29:40 -0700225rtc::Optional<std::vector<std::string>> ReadDirectory(std::string path) {
226 if (path.length() == 0)
227 return rtc::Optional<std::vector<std::string>>();
228
229#if defined(WEBRTC_WIN)
230 // Append separator character if needed.
231 if (path.back() != '\\')
232 path += '\\';
233
234 // Init.
235 WIN32_FIND_DATA data;
236 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
237 if (handle == INVALID_HANDLE_VALUE)
238 return rtc::Optional<std::vector<std::string>>();
239
240 // Populate output.
241 std::vector<std::string> found_entries;
242 do {
243 const std::string name = rtc::ToUtf8(data.cFileName);
244 if (name != "." && name != "..")
245 found_entries.emplace_back(path + name);
246 } while (::FindNextFile(handle, &data) == TRUE);
247
248 // Release resources.
249 if (handle != INVALID_HANDLE_VALUE)
250 ::FindClose(handle);
251#else
252 // Append separator character if needed.
253 if (path.back() != '/')
254 path += '/';
255
256 // Init.
257 DIR* dir = ::opendir(path.c_str());
258 if (dir == nullptr)
259 return rtc::Optional<std::vector<std::string>>();
260
261 // Populate output.
262 std::vector<std::string> found_entries;
263 while (dirent* dirent = readdir(dir)) {
264 const std::string& name = dirent->d_name;
265 if (name != "." && name != "..")
266 found_entries.emplace_back(path + name);
267 }
268
269 // Release resources.
270 closedir(dir);
271#endif
272
273 return rtc::Optional<std::vector<std::string>>(std::move(found_entries));
274}
275
ehmaldonado88df0bc2017-02-10 09:27:14 -0800276bool CreateDir(const std::string& directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000277 struct stat path_info = {0};
278 // Check if the path exists already:
279 if (stat(directory_name.c_str(), &path_info) == 0) {
280 if (!S_ISDIR(path_info.st_mode)) {
281 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
282 "file and re-run to create the directory.\n",
283 directory_name.c_str());
284 return false;
285 }
286 } else {
287#ifdef WIN32
288 return _mkdir(directory_name.c_str()) == 0;
289#else
290 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
291#endif
292 }
293 return true;
294}
295
nisse57efb032017-05-18 03:55:59 -0700296bool RemoveDir(const std::string& directory_name) {
297#ifdef WIN32
298 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
299#else
300 return rmdir(directory_name.c_str()) == 0;
301#endif
302}
303
304bool RemoveFile(const std::string& file_name) {
305#ifdef WIN32
306 return DeleteFileA(file_name.c_str()) != FALSE;
307#else
308 return unlink(file_name.c_str()) == 0;
309#endif
310}
311
ehmaldonado88df0bc2017-02-10 09:27:14 -0800312std::string ResourcePath(const std::string& name,
313 const std::string& extension) {
henrika1d34fe92015-06-16 10:04:20 +0200314#if defined(WEBRTC_IOS)
315 return IOSResourcePath(name, extension);
316#else
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000317 std::string platform = "win";
318#ifdef WEBRTC_LINUX
319 platform = "linux";
320#endif // WEBRTC_LINUX
321#ifdef WEBRTC_MAC
322 platform = "mac";
323#endif // WEBRTC_MAC
ivoc72c08ed2016-01-20 07:26:24 -0800324#ifdef WEBRTC_ANDROID
325 platform = "android";
326#endif // WEBRTC_ANDROID
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000327
328#ifdef WEBRTC_ARCH_64_BITS
329 std::string architecture = "64";
330#else
331 std::string architecture = "32";
332#endif // WEBRTC_ARCH_64_BITS
333
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000334 std::string resources_path = ProjectRootPath() + kResourcesDirName +
335 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000336 std::string resource_file = resources_path + name + "_" + platform + "_" +
337 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000338 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000339 return resource_file;
340 }
341 // Try without architecture.
342 resource_file = resources_path + name + "_" + platform + "." + extension;
343 if (FileExists(resource_file)) {
344 return resource_file;
345 }
346 // Try without platform.
347 resource_file = resources_path + name + "_" + architecture + "." + extension;
348 if (FileExists(resource_file)) {
349 return resource_file;
350 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000351
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000352 // Fall back on name without architecture or platform.
353 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200354#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000355}
356
ehmaldonado88df0bc2017-02-10 09:27:14 -0800357size_t GetFileSize(const std::string& filename) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000358 FILE* f = fopen(filename.c_str(), "rb");
359 size_t size = 0;
360 if (f != NULL) {
361 if (fseek(f, 0, SEEK_END) == 0) {
362 size = ftell(f);
363 }
364 fclose(f);
365 }
366 return size;
367}
368
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000369} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000370} // namespace webrtc