blob: 09806913a538c848d30fb244f5a6567c600ad59b [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020020
Artem Titov8f726be2018-10-23 15:50:10 +020021#include <algorithm>
22#include <codecvt>
23#include <locale>
24
25#include "Shlwapi.h"
26#include "WinDef.h"
Artem Titov8f726be2018-10-23 15:50:10 +020027#include "rtc_base/win32.h"
Yves Gerey3e707812018-11-28 16:47:49 +010028
Artem Titov8f726be2018-10-23 15:50:10 +020029#define GET_CURRENT_DIR _getcwd
30#else
Artem Titov8f726be2018-10-23 15:50:10 +020031#include <unistd.h>
Yves Gerey3e707812018-11-28 16:47:49 +010032
Artem Titov8f726be2018-10-23 15:50:10 +020033#define GET_CURRENT_DIR getcwd
34#endif
35
36#if defined(WEBRTC_IOS)
Steve Anton10542f22019-01-11 09:11:00 -080037#include "test/testsupport/ios_file_utils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020038#endif
39
40#if defined(WEBRTC_MAC)
Steve Anton10542f22019-01-11 09:11:00 -080041#include "test/testsupport/mac_file_utils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020042#endif
43
Artem Titov8e6749e2018-10-23 17:02:49 +020044#include "absl/types/optional.h"
Artem Titov8f726be2018-10-23 15:50:10 +020045#include "rtc_base/arraysize.h"
46#include "rtc_base/checks.h"
Steve Anton10542f22019-01-11 09:11:00 -080047#include "rtc_base/string_utils.h"
Artem Titov8f726be2018-10-23 15:50:10 +020048
49namespace webrtc {
50namespace test {
51
52std::string DirName(const std::string& path);
53bool CreateDir(const std::string& directory_name);
54
55namespace internal {
56
57namespace {
58#if defined(WEBRTC_WIN)
59const char* kPathDelimiter = "\\";
60#elif !defined(WEBRTC_IOS)
61const char* kPathDelimiter = "/";
62#endif
63
64#if defined(WEBRTC_ANDROID)
65// This is a special case in Chrome infrastructure. See
66// base/test/test_support_android.cc.
67const char* kAndroidChromiumTestsRoot = "/sdcard/chromium_tests_root/";
68#endif
69
70#if !defined(WEBRTC_IOS)
71const char* kResourcesDirName = "resources";
72#endif
73
74} // namespace
75
Artem Titov8f726be2018-10-23 15:50:10 +020076// Finds the WebRTC src dir.
77// The returned path always ends with a path separator.
Artem Titov8e6749e2018-10-23 17:02:49 +020078absl::optional<std::string> ProjectRootPath() {
Artem Titov8f726be2018-10-23 15:50:10 +020079#if defined(WEBRTC_ANDROID)
80 return kAndroidChromiumTestsRoot;
81#elif defined WEBRTC_IOS
82 return IOSRootPath();
83#elif defined(WEBRTC_MAC)
84 std::string path;
85 GetNSExecutablePath(&path);
86 std::string exe_dir = DirName(path);
87 // On Mac, tests execute in out/Whatever, so src is two levels up except if
88 // the test is bundled (which our tests are not), in which case it's 5 levels.
89 return DirName(DirName(exe_dir)) + kPathDelimiter;
90#elif defined(WEBRTC_POSIX)
91 char buf[PATH_MAX];
92 ssize_t count = ::readlink("/proc/self/exe", buf, arraysize(buf));
93 if (count <= 0) {
94 RTC_NOTREACHED() << "Unable to resolve /proc/self/exe.";
Artem Titov8e6749e2018-10-23 17:02:49 +020095 return absl::nullopt;
Artem Titov8f726be2018-10-23 15:50:10 +020096 }
97 // On POSIX, tests execute in out/Whatever, so src is two levels up.
98 std::string exe_dir = DirName(std::string(buf, count));
99 return DirName(DirName(exe_dir)) + kPathDelimiter;
100#elif defined(WEBRTC_WIN)
101 wchar_t buf[MAX_PATH];
102 buf[0] = 0;
Mirko Bonadei673f7e52019-03-25 09:01:02 +0100103 if (GetModuleFileNameW(NULL, buf, MAX_PATH) == 0)
Artem Titov8e6749e2018-10-23 17:02:49 +0200104 return absl::nullopt;
Artem Titov8f726be2018-10-23 15:50:10 +0200105
106 std::string exe_path = rtc::ToUtf8(std::wstring(buf));
107 std::string exe_dir = DirName(exe_path);
108 return DirName(DirName(exe_dir)) + kPathDelimiter;
109#endif
110}
111
112std::string OutputPath() {
113#if defined(WEBRTC_IOS)
114 return IOSOutputPath();
115#elif defined(WEBRTC_ANDROID)
116 return kAndroidChromiumTestsRoot;
117#else
Artem Titov8e6749e2018-10-23 17:02:49 +0200118 absl::optional<std::string> path_opt = ProjectRootPath();
119 RTC_DCHECK(path_opt);
120 std::string path = *path_opt + "out";
Artem Titov8f726be2018-10-23 15:50:10 +0200121 if (!CreateDir(path)) {
122 return "./";
123 }
124 return path + kPathDelimiter;
125#endif
126}
127
128std::string WorkingDir() {
129#if defined(WEBRTC_ANDROID)
130 return kAndroidChromiumTestsRoot;
131#endif
132 char path_buffer[FILENAME_MAX];
133 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
134 fprintf(stderr, "Cannot get current directory!\n");
135 return "./";
136 } else {
137 return std::string(path_buffer);
138 }
139}
140
141std::string ResourcePath(const std::string& name,
142 const std::string& extension) {
143#if defined(WEBRTC_IOS)
144 return IOSResourcePath(name, extension);
145#else
Artem Titov8e6749e2018-10-23 17:02:49 +0200146 absl::optional<std::string> path_opt = ProjectRootPath();
147 RTC_DCHECK(path_opt);
148 std::string resources_path = *path_opt + kResourcesDirName + kPathDelimiter;
Artem Titov8f726be2018-10-23 15:50:10 +0200149 return resources_path + name + "." + extension;
150#endif
151}
152
Artem Titov8f726be2018-10-23 15:50:10 +0200153} // namespace internal
154} // namespace test
155} // namespace webrtc