blob: f29ddb8804436f298634ec4faabd8d3cb129d6c3 [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
40#include "rtc_base/arraysize.h"
41#include "rtc_base/checks.h"
42#include "rtc_base/stringutils.h"
43
44namespace webrtc {
45namespace test {
46
47std::string DirName(const std::string& path);
48bool CreateDir(const std::string& directory_name);
49
50namespace internal {
51
52namespace {
53#if defined(WEBRTC_WIN)
54const char* kPathDelimiter = "\\";
55#elif !defined(WEBRTC_IOS)
56const char* kPathDelimiter = "/";
57#endif
58
59#if defined(WEBRTC_ANDROID)
60// This is a special case in Chrome infrastructure. See
61// base/test/test_support_android.cc.
62const char* kAndroidChromiumTestsRoot = "/sdcard/chromium_tests_root/";
63#endif
64
65#if !defined(WEBRTC_IOS)
66const char* kResourcesDirName = "resources";
67#endif
68
69} // namespace
70
71const char* kCannotFindProjectRootDir = "ERROR_CANNOT_FIND_PROJECT_ROOT_DIR";
72
73// Finds the WebRTC src dir.
74// The returned path always ends with a path separator.
75std::string ProjectRootPath() {
76#if defined(WEBRTC_ANDROID)
77 return kAndroidChromiumTestsRoot;
78#elif defined WEBRTC_IOS
79 return IOSRootPath();
80#elif defined(WEBRTC_MAC)
81 std::string path;
82 GetNSExecutablePath(&path);
83 std::string exe_dir = DirName(path);
84 // On Mac, tests execute in out/Whatever, so src is two levels up except if
85 // the test is bundled (which our tests are not), in which case it's 5 levels.
86 return DirName(DirName(exe_dir)) + kPathDelimiter;
87#elif defined(WEBRTC_POSIX)
88 char buf[PATH_MAX];
89 ssize_t count = ::readlink("/proc/self/exe", buf, arraysize(buf));
90 if (count <= 0) {
91 RTC_NOTREACHED() << "Unable to resolve /proc/self/exe.";
92 return kCannotFindProjectRootDir;
93 }
94 // On POSIX, tests execute in out/Whatever, so src is two levels up.
95 std::string exe_dir = DirName(std::string(buf, count));
96 return DirName(DirName(exe_dir)) + kPathDelimiter;
97#elif defined(WEBRTC_WIN)
98 wchar_t buf[MAX_PATH];
99 buf[0] = 0;
100 if (GetModuleFileName(NULL, buf, MAX_PATH) == 0)
101 return kCannotFindProjectRootDir;
102
103 std::string exe_path = rtc::ToUtf8(std::wstring(buf));
104 std::string exe_dir = DirName(exe_path);
105 return DirName(DirName(exe_dir)) + kPathDelimiter;
106#endif
107}
108
109std::string OutputPath() {
110#if defined(WEBRTC_IOS)
111 return IOSOutputPath();
112#elif defined(WEBRTC_ANDROID)
113 return kAndroidChromiumTestsRoot;
114#else
115 std::string path = ProjectRootPath();
116 RTC_DCHECK_NE(path, kCannotFindProjectRootDir);
117 path += "out";
118 if (!CreateDir(path)) {
119 return "./";
120 }
121 return path + kPathDelimiter;
122#endif
123}
124
125std::string WorkingDir() {
126#if defined(WEBRTC_ANDROID)
127 return kAndroidChromiumTestsRoot;
128#endif
129 char path_buffer[FILENAME_MAX];
130 if (!GET_CURRENT_DIR(path_buffer, sizeof(path_buffer))) {
131 fprintf(stderr, "Cannot get current directory!\n");
132 return "./";
133 } else {
134 return std::string(path_buffer);
135 }
136}
137
138std::string ResourcePath(const std::string& name,
139 const std::string& extension) {
140#if defined(WEBRTC_IOS)
141 return IOSResourcePath(name, extension);
142#else
143 std::string resources_path =
144 ProjectRootPath() + kResourcesDirName + kPathDelimiter;
145 return resources_path + name + "." + extension;
146#endif
147}
148
149} // namespace internal
150} // namespace test
151} // namespace webrtc