blob: b50c4373258b307f3efb2326684e9c039fad5b9e [file] [log] [blame]
Artem Titov8f726be2018-10-23 15:50:10 +02001/*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "test/testsupport/file_utils_override.h"
Artem Titov8f726be2018-10-23 15:50:10 +020012
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <limits.h>
14#include <stdio.h>
15
Artem Titov8f726be2018-10-23 15:50:10 +020016#if defined(WEBRTC_WIN)
17#include <direct.h>
18#include <tchar.h>
19#include <windows.h>
20#include <algorithm>
21#include <codecvt>
22#include <locale>
23
24#include "Shlwapi.h"
25#include "WinDef.h"
Artem Titov8f726be2018-10-23 15:50:10 +020026#include "rtc_base/win32.h"
Yves Gerey3e707812018-11-28 16:47:49 +010027
Artem Titov8f726be2018-10-23 15:50:10 +020028#define GET_CURRENT_DIR _getcwd
29#else
Artem Titov8f726be2018-10-23 15:50:10 +020030#include <unistd.h>
Yves Gerey3e707812018-11-28 16:47:49 +010031
Artem Titov8f726be2018-10-23 15:50:10 +020032#define GET_CURRENT_DIR getcwd
33#endif
34
35#if defined(WEBRTC_IOS)
Steve Anton10542f22019-01-11 09:11:00 -080036#include "test/testsupport/ios_file_utils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020037#endif
38
39#if defined(WEBRTC_MAC)
Steve Anton10542f22019-01-11 09:11:00 -080040#include "test/testsupport/mac_file_utils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020041#endif
42
Artem Titov8e6749e2018-10-23 17:02:49 +020043#include "absl/types/optional.h"
Artem Titov8f726be2018-10-23 15:50:10 +020044#include "rtc_base/arraysize.h"
45#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080046#include "rtc_base/string_utils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020047
48namespace webrtc {
49namespace test {
50
51std::string DirName(const std::string& path);
52bool CreateDir(const std::string& directory_name);
53
54namespace internal {
55
56namespace {
57#if defined(WEBRTC_WIN)
58const char* kPathDelimiter = "\\";
59#elif !defined(WEBRTC_IOS)
60const char* kPathDelimiter = "/";
61#endif
62
63#if defined(WEBRTC_ANDROID)
64// This is a special case in Chrome infrastructure. See
65// base/test/test_support_android.cc.
66const char* kAndroidChromiumTestsRoot = "/sdcard/chromium_tests_root/";
67#endif
68
69#if !defined(WEBRTC_IOS)
70const char* kResourcesDirName = "resources";
71#endif
72
73} // namespace
74
Artem Titov8f726be2018-10-23 15:50:10 +020075// Finds the WebRTC src dir.
76// The returned path always ends with a path separator.
Artem Titov8e6749e2018-10-23 17:02:49 +020077absl::optional<std::string> ProjectRootPath() {
Artem Titov8f726be2018-10-23 15:50:10 +020078#if defined(WEBRTC_ANDROID)
79 return kAndroidChromiumTestsRoot;
80#elif defined WEBRTC_IOS
81 return IOSRootPath();
82#elif defined(WEBRTC_MAC)
83 std::string path;
84 GetNSExecutablePath(&path);
85 std::string exe_dir = DirName(path);
86 // On Mac, tests execute in out/Whatever, so src is two levels up except if
87 // the test is bundled (which our tests are not), in which case it's 5 levels.
88 return DirName(DirName(exe_dir)) + kPathDelimiter;
89#elif defined(WEBRTC_POSIX)
90 char buf[PATH_MAX];
91 ssize_t count = ::readlink("/proc/self/exe", buf, arraysize(buf));
92 if (count <= 0) {
93 RTC_NOTREACHED() << "Unable to resolve /proc/self/exe.";
Artem Titov8e6749e2018-10-23 17:02:49 +020094 return absl::nullopt;
Artem Titov8f726be2018-10-23 15:50:10 +020095 }
96 // On POSIX, tests execute in out/Whatever, so src is two levels up.
97 std::string exe_dir = DirName(std::string(buf, count));
98 return DirName(DirName(exe_dir)) + kPathDelimiter;
99#elif defined(WEBRTC_WIN)
100 wchar_t buf[MAX_PATH];
101 buf[0] = 0;
Mirko Bonadei673f7e52019-03-25 09:01:02 +0100102 if (GetModuleFileNameW(NULL, buf, MAX_PATH) == 0)
Artem Titov8e6749e2018-10-23 17:02:49 +0200103 return absl::nullopt;
Artem Titov8f726be2018-10-23 15:50:10 +0200104
105 std::string exe_path = rtc::ToUtf8(std::wstring(buf));
106 std::string exe_dir = DirName(exe_path);
107 return DirName(DirName(exe_dir)) + kPathDelimiter;
108#endif
109}
110
111std::string OutputPath() {
112#if defined(WEBRTC_IOS)
113 return IOSOutputPath();
114#elif defined(WEBRTC_ANDROID)
115 return kAndroidChromiumTestsRoot;
116#else
Artem Titov8e6749e2018-10-23 17:02:49 +0200117 absl::optional<std::string> path_opt = ProjectRootPath();
118 RTC_DCHECK(path_opt);
119 std::string path = *path_opt + "out";
Artem Titov8f726be2018-10-23 15:50:10 +0200120 if (!CreateDir(path)) {
121 return "./";
122 }
123 return path + kPathDelimiter;
124#endif
125}
126
127std::string WorkingDir() {
128#if defined(WEBRTC_ANDROID)
129 return kAndroidChromiumTestsRoot;
130#endif
131 char path_buffer[FILENAME_MAX];
132 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
133 fprintf(stderr, "Cannot get current directory!\n");
134 return "./";
135 } else {
136 return std::string(path_buffer);
137 }
138}
139
140std::string ResourcePath(const std::string& name,
141 const std::string& extension) {
142#if defined(WEBRTC_IOS)
143 return IOSResourcePath(name, extension);
144#else
Artem Titov8e6749e2018-10-23 17:02:49 +0200145 absl::optional<std::string> path_opt = ProjectRootPath();
146 RTC_DCHECK(path_opt);
147 std::string resources_path = *path_opt + kResourcesDirName + kPathDelimiter;
Artem Titov8f726be2018-10-23 15:50:10 +0200148 return resources_path + name + "." + extension;
149#endif
150}
151
152} // namespace internal
153} // namespace test
154} // namespace webrtc