blob: 370ee06b29d8bba58da2cff6e1ef086c2e37365f [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.
Yves Gerey665174f2018-06-19 15:03:05 +020034#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"
Niels Möllerd7b91312018-05-24 11:21:34 +020046#include "rtc_base/stringutils.h"
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 }
Yves Gerey665174f2018-06-19 15:03:05 +020094// 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.
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000097#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};
Yves Gerey665174f2018-06-19 15:03:05 +0200114 return stat(directory_name.c_str(), &directory_info) == 0 &&
115 S_ISDIR(directory_info.st_mode);
alessiobe49fede2017-03-15 06:04:59 -0700116}
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.
Yves Gerey665174f2018-06-19 15:03:05 +0200192std::string TempFilename(const std::string& dir, const std::string& prefix) {
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000193#ifdef WIN32
194 wchar_t filename[MAX_PATH];
Yves Gerey665174f2018-06-19 15:03:05 +0200195 if (::GetTempFileName(rtc::ToUtf16(dir).c_str(), rtc::ToUtf16(prefix).c_str(),
196 0, filename) != 0)
nisse07b83882017-03-14 01:32:50 -0700197 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
Yves Gerey665174f2018-06-19 15:03:05 +0200204 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(), prefix.c_str());
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000205 int fd = ::mkstemp(tempname.get());
206 if (fd == -1) {
207 assert(false);
208 return "";
209 } else {
210 ::close(fd);
211 }
212 std::string ret(tempname.get());
213 return ret;
214#endif
215}
216
Artem Titove62f6002018-03-19 15:40:00 +0100217std::string GenerateTempFilename(const std::string& dir,
218 const std::string& prefix) {
219 std::string filename = TempFilename(dir, prefix);
220 RemoveFile(filename);
221 return filename;
222}
223
Danil Chapovalov431abd92018-06-18 12:54:17 +0200224absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
alessiob00b16f42017-06-01 03:29:40 -0700225 if (path.length() == 0)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200226 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700227
228#if defined(WEBRTC_WIN)
229 // Append separator character if needed.
230 if (path.back() != '\\')
231 path += '\\';
232
233 // Init.
234 WIN32_FIND_DATA data;
235 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
236 if (handle == INVALID_HANDLE_VALUE)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200237 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700238
239 // Populate output.
240 std::vector<std::string> found_entries;
241 do {
242 const std::string name = rtc::ToUtf8(data.cFileName);
243 if (name != "." && name != "..")
244 found_entries.emplace_back(path + name);
245 } while (::FindNextFile(handle, &data) == TRUE);
246
247 // Release resources.
248 if (handle != INVALID_HANDLE_VALUE)
249 ::FindClose(handle);
250#else
251 // Append separator character if needed.
252 if (path.back() != '/')
253 path += '/';
254
255 // Init.
256 DIR* dir = ::opendir(path.c_str());
257 if (dir == nullptr)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200258 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700259
260 // Populate output.
261 std::vector<std::string> found_entries;
262 while (dirent* dirent = readdir(dir)) {
263 const std::string& name = dirent->d_name;
264 if (name != "." && name != "..")
265 found_entries.emplace_back(path + name);
266 }
267
268 // Release resources.
269 closedir(dir);
270#endif
271
Danil Chapovalov431abd92018-06-18 12:54:17 +0200272 return absl::optional<std::vector<std::string>>(std::move(found_entries));
alessiob00b16f42017-06-01 03:29:40 -0700273}
274
ehmaldonado88df0bc2017-02-10 09:27:14 -0800275bool CreateDir(const std::string& directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000276 struct stat path_info = {0};
277 // Check if the path exists already:
278 if (stat(directory_name.c_str(), &path_info) == 0) {
279 if (!S_ISDIR(path_info.st_mode)) {
Yves Gerey665174f2018-06-19 15:03:05 +0200280 fprintf(stderr,
281 "Path %s exists but is not a directory! Remove this "
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000282 "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
Yves Gerey665174f2018-06-19 15:03:05 +0200290 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000291#endif
292 }
293 return true;
294}
295
nisse57efb032017-05-18 03:55:59 -0700296bool RemoveDir(const std::string& directory_name) {
297#ifdef WIN32
Yves Gerey665174f2018-06-19 15:03:05 +0200298 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
nisse57efb032017-05-18 03:55:59 -0700299#else
Yves Gerey665174f2018-06-19 15:03:05 +0200300 return rmdir(directory_name.c_str()) == 0;
nisse57efb032017-05-18 03:55:59 -0700301#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
Yves Gerey665174f2018-06-19 15:03:05 +0200328 std::string resources_path =
329 ProjectRootPath() + kResourcesDirName + kPathDelimiter;
Niels Möller3d95c312018-07-27 10:02:35 +0200330 std::string resource_file =
331 resources_path + name + "_" + platform + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000332 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000333 return resource_file;
334 }
Niels Möller3d95c312018-07-27 10:02:35 +0200335 // Fall back on name without platform.
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000336 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200337#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000338}
339
Niels Möller392f8d02018-06-01 10:14:44 +0200340std::string JoinFilename(const std::string& dir, const std::string& name) {
341 RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
342 return dir + kPathDelimiter + name;
343}
344
ehmaldonado88df0bc2017-02-10 09:27:14 -0800345size_t GetFileSize(const std::string& filename) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000346 FILE* f = fopen(filename.c_str(), "rb");
347 size_t size = 0;
348 if (f != NULL) {
349 if (fseek(f, 0, SEEK_END) == 0) {
350 size = ftell(f);
351 }
352 fclose(f);
353 }
354 return size;
355}
356
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000357} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000358} // namespace webrtc