blob: 76a635a1a4e4c32ab024aa6b5c7d8174b55cf8df [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"
Niels Möllerd7b91312018-05-24 11:21:34 +020046#include "rtc_base/stringutils.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020047#include "typedefs.h" // NOLINT(build/include) // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000048
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000049namespace webrtc {
50namespace test {
51
henrika1d34fe92015-06-16 10:04:20 +020052#if defined(WEBRTC_IOS)
53// Defined in iosfileutils.mm. No header file to discourage use elsewhere.
kjellander02060002016-02-16 22:06:12 -080054std::string IOSOutputPath();
Kári Tristan Helgason470c0882016-10-03 13:13:29 +020055std::string IOSRootPath();
henrika1d34fe92015-06-16 10:04:20 +020056std::string IOSResourcePath(std::string name, std::string extension);
57#endif
58
henrike@webrtc.org34773d92013-07-08 14:55:23 +000059namespace {
60
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000061#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000062const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000063#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000064const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000065#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000066
67#ifdef WEBRTC_ANDROID
kjellander68208892016-06-16 23:29:30 -070068const char* kRootDirName = "/sdcard/chromium_tests_root/";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000069#else
kjellander02060002016-02-16 22:06:12 -080070#if !defined(WEBRTC_IOS)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000071const char* kOutputDirName = "out";
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000072#endif
kjellander02060002016-02-16 22:06:12 -080073const char* kFallbackPath = "./";
74#endif // !defined(WEBRTC_ANDROID)
75
henrika1d34fe92015-06-16 10:04:20 +020076#if !defined(WEBRTC_IOS)
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000077const char* kResourcesDirName = "resources";
henrika1d34fe92015-06-16 10:04:20 +020078#endif
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000079
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000080char relative_dir_path[FILENAME_MAX];
81bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000082
83} // namespace
84
85const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
86
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000087void SetExecutablePath(const std::string& path) {
88 std::string working_dir = WorkingDir();
89 std::string temp_path = path;
90
91 // Handle absolute paths; convert them to relative paths to the working dir.
92 if (path.find(working_dir) != std::string::npos) {
93 temp_path = path.substr(working_dir.length() + 1);
94 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000095 // On Windows, when tests are run under memory tools like DrMemory and TSan,
96 // slashes occur in the path as directory separators. Make sure we replace
97 // such cases with backslashes in order for the paths to be correct.
98#ifdef WIN32
99 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
100#endif
101
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000102 // Trim away the executable name; only store the relative dir path.
103 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000104 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
105 relative_dir_path_set = true;
106}
107
ehmaldonado88df0bc2017-02-10 09:27:14 -0800108bool FileExists(const std::string& file_name) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000109 struct stat file_info = {0};
110 return stat(file_name.c_str(), &file_info) == 0;
111}
112
alessiobe49fede2017-03-15 06:04:59 -0700113bool DirExists(const std::string& directory_name) {
114 struct stat directory_info = {0};
115 return stat(directory_name.c_str(), &directory_info) == 0 && S_ISDIR(
116 directory_info.st_mode);
117}
118
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000119#ifdef WEBRTC_ANDROID
120
121std::string ProjectRootPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000122 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000123}
124
125std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000126 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000127}
128
129std::string WorkingDir() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000130 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000131}
132
alessiob00b16f42017-06-01 03:29:40 -0700133#else // WEBRTC_ANDROID
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000134
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000135std::string ProjectRootPath() {
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200136#if defined(WEBRTC_IOS)
137 return IOSRootPath();
138#else
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000139 std::string path = WorkingDir();
140 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000141 return kCannotFindProjectRootDir;
142 }
ehmaldonado00f2ee02016-11-18 07:06:41 -0800143 if (relative_dir_path_set) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000144 path = path + kPathDelimiter + relative_dir_path;
145 }
ehmaldonado00f2ee02016-11-18 07:06:41 -0800146 path = path + kPathDelimiter + ".." + kPathDelimiter + "..";
147 char canonical_path[FILENAME_MAX];
148#ifdef WIN32
ehmaldonado37535bf2016-12-05 06:42:45 -0800149 BOOL succeeded = PathCanonicalizeA(canonical_path, path.c_str());
ehmaldonado00f2ee02016-11-18 07:06:41 -0800150#else
151 bool succeeded = realpath(path.c_str(), canonical_path) != NULL;
152#endif
153 if (succeeded) {
154 path = std::string(canonical_path) + kPathDelimiter;
155 return path;
156 } else {
157 fprintf(stderr, "Cannot find project root directory!\n");
158 return kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000159 }
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200160#endif
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000161}
162
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000163std::string OutputPath() {
kjellander02060002016-02-16 22:06:12 -0800164#if defined(WEBRTC_IOS)
165 return IOSOutputPath();
166#else
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000167 std::string path = ProjectRootPath();
168 if (path == kCannotFindProjectRootDir) {
169 return kFallbackPath;
170 }
171 path += kOutputDirName;
172 if (!CreateDir(path)) {
173 return kFallbackPath;
174 }
175 return path + kPathDelimiter;
kjellander02060002016-02-16 22:06:12 -0800176#endif
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000177}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000178
179std::string WorkingDir() {
180 char path_buffer[FILENAME_MAX];
181 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
182 fprintf(stderr, "Cannot get current directory!\n");
183 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000184 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000185 return std::string(path_buffer);
186 }
187}
188
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000189#endif // !WEBRTC_ANDROID
190
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000191// Generate a temporary filename in a safe way.
192// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
193std::string TempFilename(const std::string &dir, const std::string &prefix) {
194#ifdef WIN32
195 wchar_t filename[MAX_PATH];
nisse07b83882017-03-14 01:32:50 -0700196 if (::GetTempFileName(rtc::ToUtf16(dir).c_str(),
197 rtc::ToUtf16(prefix).c_str(), 0, filename) != 0)
198 return rtc::ToUtf8(filename);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000199 assert(false);
200 return "";
201#else
202 int len = dir.size() + prefix.size() + 2 + 6;
kwibergbfefb032016-05-01 14:53:46 -0700203 std::unique_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000204
205 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
206 prefix.c_str());
207 int fd = ::mkstemp(tempname.get());
208 if (fd == -1) {
209 assert(false);
210 return "";
211 } else {
212 ::close(fd);
213 }
214 std::string ret(tempname.get());
215 return ret;
216#endif
217}
218
Artem Titove62f6002018-03-19 15:40:00 +0100219std::string GenerateTempFilename(const std::string& dir,
220 const std::string& prefix) {
221 std::string filename = TempFilename(dir, prefix);
222 RemoveFile(filename);
223 return filename;
224}
225
Danil Chapovalov431abd92018-06-18 12:54:17 +0200226absl::optional<std::vector<std::string>> ReadDirectory(std::string path) {
alessiob00b16f42017-06-01 03:29:40 -0700227 if (path.length() == 0)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200228 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700229
230#if defined(WEBRTC_WIN)
231 // Append separator character if needed.
232 if (path.back() != '\\')
233 path += '\\';
234
235 // Init.
236 WIN32_FIND_DATA data;
237 HANDLE handle = ::FindFirstFile(rtc::ToUtf16(path + '*').c_str(), &data);
238 if (handle == INVALID_HANDLE_VALUE)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200239 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700240
241 // Populate output.
242 std::vector<std::string> found_entries;
243 do {
244 const std::string name = rtc::ToUtf8(data.cFileName);
245 if (name != "." && name != "..")
246 found_entries.emplace_back(path + name);
247 } while (::FindNextFile(handle, &data) == TRUE);
248
249 // Release resources.
250 if (handle != INVALID_HANDLE_VALUE)
251 ::FindClose(handle);
252#else
253 // Append separator character if needed.
254 if (path.back() != '/')
255 path += '/';
256
257 // Init.
258 DIR* dir = ::opendir(path.c_str());
259 if (dir == nullptr)
Danil Chapovalov431abd92018-06-18 12:54:17 +0200260 return absl::optional<std::vector<std::string>>();
alessiob00b16f42017-06-01 03:29:40 -0700261
262 // Populate output.
263 std::vector<std::string> found_entries;
264 while (dirent* dirent = readdir(dir)) {
265 const std::string& name = dirent->d_name;
266 if (name != "." && name != "..")
267 found_entries.emplace_back(path + name);
268 }
269
270 // Release resources.
271 closedir(dir);
272#endif
273
Danil Chapovalov431abd92018-06-18 12:54:17 +0200274 return absl::optional<std::vector<std::string>>(std::move(found_entries));
alessiob00b16f42017-06-01 03:29:40 -0700275}
276
ehmaldonado88df0bc2017-02-10 09:27:14 -0800277bool CreateDir(const std::string& directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000278 struct stat path_info = {0};
279 // Check if the path exists already:
280 if (stat(directory_name.c_str(), &path_info) == 0) {
281 if (!S_ISDIR(path_info.st_mode)) {
282 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
283 "file and re-run to create the directory.\n",
284 directory_name.c_str());
285 return false;
286 }
287 } else {
288#ifdef WIN32
289 return _mkdir(directory_name.c_str()) == 0;
290#else
291 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
292#endif
293 }
294 return true;
295}
296
nisse57efb032017-05-18 03:55:59 -0700297bool RemoveDir(const std::string& directory_name) {
298#ifdef WIN32
299 return RemoveDirectoryA(directory_name.c_str()) != FALSE;
300#else
301 return rmdir(directory_name.c_str()) == 0;
302#endif
303}
304
305bool RemoveFile(const std::string& file_name) {
306#ifdef WIN32
307 return DeleteFileA(file_name.c_str()) != FALSE;
308#else
309 return unlink(file_name.c_str()) == 0;
310#endif
311}
312
ehmaldonado88df0bc2017-02-10 09:27:14 -0800313std::string ResourcePath(const std::string& name,
314 const std::string& extension) {
henrika1d34fe92015-06-16 10:04:20 +0200315#if defined(WEBRTC_IOS)
316 return IOSResourcePath(name, extension);
317#else
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000318 std::string platform = "win";
319#ifdef WEBRTC_LINUX
320 platform = "linux";
321#endif // WEBRTC_LINUX
322#ifdef WEBRTC_MAC
323 platform = "mac";
324#endif // WEBRTC_MAC
ivoc72c08ed2016-01-20 07:26:24 -0800325#ifdef WEBRTC_ANDROID
326 platform = "android";
327#endif // WEBRTC_ANDROID
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000328
329#ifdef WEBRTC_ARCH_64_BITS
330 std::string architecture = "64";
331#else
332 std::string architecture = "32";
333#endif // WEBRTC_ARCH_64_BITS
334
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000335 std::string resources_path = ProjectRootPath() + kResourcesDirName +
336 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000337 std::string resource_file = resources_path + name + "_" + platform + "_" +
338 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000339 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000340 return resource_file;
341 }
342 // Try without architecture.
343 resource_file = resources_path + name + "_" + platform + "." + extension;
344 if (FileExists(resource_file)) {
345 return resource_file;
346 }
347 // Try without platform.
348 resource_file = resources_path + name + "_" + architecture + "." + extension;
349 if (FileExists(resource_file)) {
350 return resource_file;
351 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000352
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000353 // Fall back on name without architecture or platform.
354 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200355#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000356}
357
Niels Möller392f8d02018-06-01 10:14:44 +0200358std::string JoinFilename(const std::string& dir, const std::string& name) {
359 RTC_CHECK(!dir.empty()) << "Special cases not implemented.";
360 return dir + kPathDelimiter + name;
361}
362
ehmaldonado88df0bc2017-02-10 09:27:14 -0800363size_t GetFileSize(const std::string& filename) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000364 FILE* f = fopen(filename.c_str(), "rb");
365 size_t size = 0;
366 if (f != NULL) {
367 if (fseek(f, 0, SEEK_END) == 0) {
368 size = ftell(f);
369 }
370 fclose(f);
371 }
372 return size;
373}
374
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000375} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000376} // namespace webrtc