blob: 884069c10dd4f610cdbea906acf61aa323b2a0ac [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
11#include "test/testsupport/fileutils_override.h"
12
13#if defined(WEBRTC_WIN)
14#include <direct.h>
15#include <tchar.h>
16#include <windows.h>
17#include <algorithm>
18#include <codecvt>
19#include <locale>
20
21#include "Shlwapi.h"
22#include "WinDef.h"
23
24#include "rtc_base/win32.h"
25#define GET_CURRENT_DIR _getcwd
26#else
27#include <dirent.h>
28#include <unistd.h>
29#define GET_CURRENT_DIR getcwd
30#endif
31
32#if defined(WEBRTC_IOS)
33#include "test/testsupport/iosfileutils.h"
34#endif
35
36#if defined(WEBRTC_MAC)
37#include "test/testsupport/macfileutils.h"
38#endif
39
Artem Titov8e6749e2018-10-23 17:02:49 +020040#include "absl/types/optional.h"
Artem Titov8f726be2018-10-23 15:50:10 +020041#include "rtc_base/arraysize.h"
42#include "rtc_base/checks.h"
43#include "rtc_base/stringutils.h"
44
45namespace webrtc {
46namespace test {
47
48std::string DirName(const std::string& path);
49bool CreateDir(const std::string& directory_name);
50
51namespace internal {
52
53namespace {
54#if defined(WEBRTC_WIN)
55const char* kPathDelimiter = "\\";
56#elif !defined(WEBRTC_IOS)
57const char* kPathDelimiter = "/";
58#endif
59
60#if defined(WEBRTC_ANDROID)
61// This is a special case in Chrome infrastructure. See
62// base/test/test_support_android.cc.
63const char* kAndroidChromiumTestsRoot = "/sdcard/chromium_tests_root/";
64#endif
65
66#if !defined(WEBRTC_IOS)
67const char* kResourcesDirName = "resources";
68#endif
69
70} // namespace
71
Artem Titov8f726be2018-10-23 15:50:10 +020072// Finds the WebRTC src dir.
73// The returned path always ends with a path separator.
Artem Titov8e6749e2018-10-23 17:02:49 +020074absl::optional<std::string> ProjectRootPath() {
Artem Titov8f726be2018-10-23 15:50:10 +020075#if defined(WEBRTC_ANDROID)
76 return kAndroidChromiumTestsRoot;
77#elif defined WEBRTC_IOS
78 return IOSRootPath();
79#elif defined(WEBRTC_MAC)
80 std::string path;
81 GetNSExecutablePath(&path);
82 std::string exe_dir = DirName(path);
83 // On Mac, tests execute in out/Whatever, so src is two levels up except if
84 // the test is bundled (which our tests are not), in which case it's 5 levels.
85 return DirName(DirName(exe_dir)) + kPathDelimiter;
86#elif defined(WEBRTC_POSIX)
87 char buf[PATH_MAX];
88 ssize_t count = ::readlink("/proc/self/exe", buf, arraysize(buf));
89 if (count <= 0) {
90 RTC_NOTREACHED() << "Unable to resolve /proc/self/exe.";
Artem Titov8e6749e2018-10-23 17:02:49 +020091 return absl::nullopt;
Artem Titov8f726be2018-10-23 15:50:10 +020092 }
93 // On POSIX, tests execute in out/Whatever, so src is two levels up.
94 std::string exe_dir = DirName(std::string(buf, count));
95 return DirName(DirName(exe_dir)) + kPathDelimiter;
96#elif defined(WEBRTC_WIN)
97 wchar_t buf[MAX_PATH];
98 buf[0] = 0;
99 if (GetModuleFileName(NULL, buf, MAX_PATH) == 0)
Artem Titov8e6749e2018-10-23 17:02:49 +0200100 return absl::nullopt;
Artem Titov8f726be2018-10-23 15:50:10 +0200101
102 std::string exe_path = rtc::ToUtf8(std::wstring(buf));
103 std::string exe_dir = DirName(exe_path);
104 return DirName(DirName(exe_dir)) + kPathDelimiter;
105#endif
106}
107
108std::string OutputPath() {
109#if defined(WEBRTC_IOS)
110 return IOSOutputPath();
111#elif defined(WEBRTC_ANDROID)
112 return kAndroidChromiumTestsRoot;
113#else
Artem Titov8e6749e2018-10-23 17:02:49 +0200114 absl::optional<std::string> path_opt = ProjectRootPath();
115 RTC_DCHECK(path_opt);
116 std::string path = *path_opt + "out";
Artem Titov8f726be2018-10-23 15:50:10 +0200117 if (!CreateDir(path)) {
118 return "./";
119 }
120 return path + kPathDelimiter;
121#endif
122}
123
124std::string WorkingDir() {
125#if defined(WEBRTC_ANDROID)
126 return kAndroidChromiumTestsRoot;
127#endif
128 char path_buffer[FILENAME_MAX];
129 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
130 fprintf(stderr, "Cannot get current directory!\n");
131 return "./";
132 } else {
133 return std::string(path_buffer);
134 }
135}
136
137std::string ResourcePath(const std::string& name,
138 const std::string& extension) {
139#if defined(WEBRTC_IOS)
140 return IOSResourcePath(name, extension);
141#else
Artem Titov8e6749e2018-10-23 17:02:49 +0200142 absl::optional<std::string> path_opt = ProjectRootPath();
143 RTC_DCHECK(path_opt);
144 std::string resources_path = *path_opt + kResourcesDirName + kPathDelimiter;
Artem Titov8f726be2018-10-23 15:50:10 +0200145 return resources_path + name + "." + extension;
146#endif
147}
148
149} // namespace internal
150} // namespace test
151} // namespace webrtc