blob: 97d58eba60f2835e348a0a9f8413ddb248a6ec69 [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
pbos@webrtc.org34741c82013-05-27 08:02:22 +000011#include "webrtc/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"
22
Henrik Kjellander98f53512015-10-28 18:17:40 +010023#include "webrtc/system_wrappers/include/utf_util_win.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000024#define GET_CURRENT_DIR _getcwd
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000025#else
26#include <unistd.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000027
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000028#define GET_CURRENT_DIR getcwd
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000029#endif
30
31#include <sys/stat.h> // To check for directory existence.
32#ifndef S_ISDIR // Not defined in stat.h on Windows.
33#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000034#endif
35
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000036#include <stdio.h>
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000037#include <stdlib.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000038#include <string.h>
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000039
kwibergbfefb032016-05-01 14:53:46 -070040#include <memory>
41
pbos@webrtc.org34741c82013-05-27 08:02:22 +000042#include "webrtc/typedefs.h" // For architecture defines
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000043
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000044namespace webrtc {
45namespace test {
46
henrika1d34fe92015-06-16 10:04:20 +020047#if defined(WEBRTC_IOS)
48// Defined in iosfileutils.mm. No header file to discourage use elsewhere.
kjellander02060002016-02-16 22:06:12 -080049std::string IOSOutputPath();
Kári Tristan Helgason470c0882016-10-03 13:13:29 +020050std::string IOSRootPath();
henrika1d34fe92015-06-16 10:04:20 +020051std::string IOSResourcePath(std::string name, std::string extension);
52#endif
53
henrike@webrtc.org34773d92013-07-08 14:55:23 +000054namespace {
55
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000056#ifdef WIN32
henrike@webrtc.org34773d92013-07-08 14:55:23 +000057const char* kPathDelimiter = "\\";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000058#else
henrike@webrtc.org34773d92013-07-08 14:55:23 +000059const char* kPathDelimiter = "/";
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000060#endif
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000061
62#ifdef WEBRTC_ANDROID
kjellander68208892016-06-16 23:29:30 -070063const char* kRootDirName = "/sdcard/chromium_tests_root/";
leozwang@webrtc.org363efef2012-10-16 04:31:20 +000064#else
kjellander02060002016-02-16 22:06:12 -080065#if !defined(WEBRTC_IOS)
henrike@webrtc.org34773d92013-07-08 14:55:23 +000066const char* kOutputDirName = "out";
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000067#endif
kjellander02060002016-02-16 22:06:12 -080068const char* kFallbackPath = "./";
69#endif // !defined(WEBRTC_ANDROID)
70
henrika1d34fe92015-06-16 10:04:20 +020071#if !defined(WEBRTC_IOS)
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000072const char* kResourcesDirName = "resources";
henrika1d34fe92015-06-16 10:04:20 +020073#endif
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +000074
pbos@webrtc.orgdb7d82f2013-07-05 08:49:09 +000075char relative_dir_path[FILENAME_MAX];
76bool relative_dir_path_set = false;
henrike@webrtc.org34773d92013-07-08 14:55:23 +000077
78} // namespace
79
80const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
81
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000082void SetExecutablePath(const std::string& path) {
83 std::string working_dir = WorkingDir();
84 std::string temp_path = path;
85
86 // Handle absolute paths; convert them to relative paths to the working dir.
87 if (path.find(working_dir) != std::string::npos) {
88 temp_path = path.substr(working_dir.length() + 1);
89 }
kjellander@webrtc.orgde499662013-08-29 11:26:41 +000090 // On Windows, when tests are run under memory tools like DrMemory and TSan,
91 // slashes occur in the path as directory separators. Make sure we replace
92 // such cases with backslashes in order for the paths to be correct.
93#ifdef WIN32
94 std::replace(temp_path.begin(), temp_path.end(), '/', '\\');
95#endif
96
kjellander@webrtc.org193600b2012-10-17 04:39:44 +000097 // Trim away the executable name; only store the relative dir path.
98 temp_path = temp_path.substr(0, temp_path.find_last_of(kPathDelimiter));
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +000099 strncpy(relative_dir_path, temp_path.c_str(), FILENAME_MAX);
100 relative_dir_path_set = true;
101}
102
103bool FileExists(std::string& file_name) {
104 struct stat file_info = {0};
105 return stat(file_name.c_str(), &file_info) == 0;
106}
107
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000108#ifdef WEBRTC_ANDROID
109
110std::string ProjectRootPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000111 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000112}
113
114std::string OutputPath() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000115 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000116}
117
118std::string WorkingDir() {
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000119 return kRootDirName;
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000120}
121
122#else // WEBRTC_ANDROID
123
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000124std::string ProjectRootPath() {
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200125#if defined(WEBRTC_IOS)
126 return IOSRootPath();
127#else
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000128 std::string path = WorkingDir();
129 if (path == kFallbackPath) {
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000130 return kCannotFindProjectRootDir;
131 }
ehmaldonado00f2ee02016-11-18 07:06:41 -0800132 if (relative_dir_path_set) {
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000133 path = path + kPathDelimiter + relative_dir_path;
134 }
ehmaldonado00f2ee02016-11-18 07:06:41 -0800135 path = path + kPathDelimiter + ".." + kPathDelimiter + "..";
136 char canonical_path[FILENAME_MAX];
137#ifdef WIN32
138 bool succeeded = PathCanonicalizeA(canonical_path, path.c_str());
139#else
140 bool succeeded = realpath(path.c_str(), canonical_path) != NULL;
141#endif
142 if (succeeded) {
143 path = std::string(canonical_path) + kPathDelimiter;
144 return path;
145 } else {
146 fprintf(stderr, "Cannot find project root directory!\n");
147 return kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000148 }
Kári Tristan Helgason470c0882016-10-03 13:13:29 +0200149#endif
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000150}
151
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +0000152std::string OutputPath() {
kjellander02060002016-02-16 22:06:12 -0800153#if defined(WEBRTC_IOS)
154 return IOSOutputPath();
155#else
kjellander@webrtc.org72fd3392014-11-05 06:28:50 +0000156 std::string path = ProjectRootPath();
157 if (path == kCannotFindProjectRootDir) {
158 return kFallbackPath;
159 }
160 path += kOutputDirName;
161 if (!CreateDir(path)) {
162 return kFallbackPath;
163 }
164 return path + kPathDelimiter;
kjellander02060002016-02-16 22:06:12 -0800165#endif
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000166}
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000167
168std::string WorkingDir() {
169 char path_buffer[FILENAME_MAX];
170 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
171 fprintf(stderr, "Cannot get current directory!\n");
172 return kFallbackPath;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000173 } else {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000174 return std::string(path_buffer);
175 }
176}
177
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000178#endif // !WEBRTC_ANDROID
179
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000180// Generate a temporary filename in a safe way.
181// Largely copied from talk/base/{unixfilesystem,win32filesystem}.cc.
182std::string TempFilename(const std::string &dir, const std::string &prefix) {
183#ifdef WIN32
184 wchar_t filename[MAX_PATH];
185 if (::GetTempFileName(ToUtf16(dir).c_str(),
186 ToUtf16(prefix).c_str(), 0, filename) != 0)
187 return ToUtf8(filename);
188 assert(false);
189 return "";
190#else
191 int len = dir.size() + prefix.size() + 2 + 6;
kwibergbfefb032016-05-01 14:53:46 -0700192 std::unique_ptr<char[]> tempname(new char[len]);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +0000193
194 snprintf(tempname.get(), len, "%s/%sXXXXXX", dir.c_str(),
195 prefix.c_str());
196 int fd = ::mkstemp(tempname.get());
197 if (fd == -1) {
198 assert(false);
199 return "";
200 } else {
201 ::close(fd);
202 }
203 std::string ret(tempname.get());
204 return ret;
205#endif
206}
207
208bool CreateDir(std::string directory_name) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000209 struct stat path_info = {0};
210 // Check if the path exists already:
211 if (stat(directory_name.c_str(), &path_info) == 0) {
212 if (!S_ISDIR(path_info.st_mode)) {
213 fprintf(stderr, "Path %s exists but is not a directory! Remove this "
214 "file and re-run to create the directory.\n",
215 directory_name.c_str());
216 return false;
217 }
218 } else {
219#ifdef WIN32
220 return _mkdir(directory_name.c_str()) == 0;
221#else
222 return mkdir(directory_name.c_str(), S_IRWXU | S_IRWXG | S_IRWXO) == 0;
223#endif
224 }
225 return true;
226}
227
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000228std::string ResourcePath(std::string name, std::string extension) {
henrika1d34fe92015-06-16 10:04:20 +0200229#if defined(WEBRTC_IOS)
230 return IOSResourcePath(name, extension);
231#else
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000232 std::string platform = "win";
233#ifdef WEBRTC_LINUX
234 platform = "linux";
235#endif // WEBRTC_LINUX
236#ifdef WEBRTC_MAC
237 platform = "mac";
238#endif // WEBRTC_MAC
ivoc72c08ed2016-01-20 07:26:24 -0800239#ifdef WEBRTC_ANDROID
240 platform = "android";
241#endif // WEBRTC_ANDROID
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000242
243#ifdef WEBRTC_ARCH_64_BITS
244 std::string architecture = "64";
245#else
246 std::string architecture = "32";
247#endif // WEBRTC_ARCH_64_BITS
248
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000249 std::string resources_path = ProjectRootPath() + kResourcesDirName +
250 kPathDelimiter;
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000251 std::string resource_file = resources_path + name + "_" + platform + "_" +
252 architecture + "." + extension;
kjellander@webrtc.org80b26612011-12-07 18:50:17 +0000253 if (FileExists(resource_file)) {
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000254 return resource_file;
255 }
256 // Try without architecture.
257 resource_file = resources_path + name + "_" + platform + "." + extension;
258 if (FileExists(resource_file)) {
259 return resource_file;
260 }
261 // Try without platform.
262 resource_file = resources_path + name + "_" + architecture + "." + extension;
263 if (FileExists(resource_file)) {
264 return resource_file;
265 }
leozwang@webrtc.org363efef2012-10-16 04:31:20 +0000266
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000267 // Fall back on name without architecture or platform.
268 return resources_path + name + "." + extension;
henrika1d34fe92015-06-16 10:04:20 +0200269#endif // defined (WEBRTC_IOS)
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +0000270}
271
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000272size_t GetFileSize(std::string filename) {
273 FILE* f = fopen(filename.c_str(), "rb");
274 size_t size = 0;
275 if (f != NULL) {
276 if (fseek(f, 0, SEEK_END) == 0) {
277 size = ftell(f);
278 }
279 fclose(f);
280 }
281 return size;
282}
283
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000284} // namespace test
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +0000285} // namespace webrtc