Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <stdio.h> |
| 15 | |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 16 | #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 Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 26 | #include "rtc_base/win32.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 27 | |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 28 | #define GET_CURRENT_DIR _getcwd |
| 29 | #else |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 30 | #include <unistd.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 31 | |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 32 | #define GET_CURRENT_DIR getcwd |
| 33 | #endif |
| 34 | |
| 35 | #if defined(WEBRTC_IOS) |
| 36 | #include "test/testsupport/iosfileutils.h" |
| 37 | #endif |
| 38 | |
| 39 | #if defined(WEBRTC_MAC) |
| 40 | #include "test/testsupport/macfileutils.h" |
| 41 | #endif |
| 42 | |
Artem Titov | 8e6749e | 2018-10-23 17:02:49 +0200 | [diff] [blame] | 43 | #include "absl/types/optional.h" |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 44 | #include "rtc_base/arraysize.h" |
| 45 | #include "rtc_base/checks.h" |
| 46 | #include "rtc_base/stringutils.h" |
| 47 | |
| 48 | namespace webrtc { |
| 49 | namespace test { |
| 50 | |
| 51 | std::string DirName(const std::string& path); |
| 52 | bool CreateDir(const std::string& directory_name); |
| 53 | |
| 54 | namespace internal { |
| 55 | |
| 56 | namespace { |
| 57 | #if defined(WEBRTC_WIN) |
| 58 | const char* kPathDelimiter = "\\"; |
| 59 | #elif !defined(WEBRTC_IOS) |
| 60 | const 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. |
| 66 | const char* kAndroidChromiumTestsRoot = "/sdcard/chromium_tests_root/"; |
| 67 | #endif |
| 68 | |
| 69 | #if !defined(WEBRTC_IOS) |
| 70 | const char* kResourcesDirName = "resources"; |
| 71 | #endif |
| 72 | |
| 73 | } // namespace |
| 74 | |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 75 | // Finds the WebRTC src dir. |
| 76 | // The returned path always ends with a path separator. |
Artem Titov | 8e6749e | 2018-10-23 17:02:49 +0200 | [diff] [blame] | 77 | absl::optional<std::string> ProjectRootPath() { |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 78 | #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 Titov | 8e6749e | 2018-10-23 17:02:49 +0200 | [diff] [blame] | 94 | return absl::nullopt; |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 95 | } |
| 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; |
| 102 | if (GetModuleFileName(NULL, buf, MAX_PATH) == 0) |
Artem Titov | 8e6749e | 2018-10-23 17:02:49 +0200 | [diff] [blame] | 103 | return absl::nullopt; |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 104 | |
| 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 | |
| 111 | std::string OutputPath() { |
| 112 | #if defined(WEBRTC_IOS) |
| 113 | return IOSOutputPath(); |
| 114 | #elif defined(WEBRTC_ANDROID) |
| 115 | return kAndroidChromiumTestsRoot; |
| 116 | #else |
Artem Titov | 8e6749e | 2018-10-23 17:02:49 +0200 | [diff] [blame] | 117 | absl::optional<std::string> path_opt = ProjectRootPath(); |
| 118 | RTC_DCHECK(path_opt); |
| 119 | std::string path = *path_opt + "out"; |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 120 | if (!CreateDir(path)) { |
| 121 | return "./"; |
| 122 | } |
| 123 | return path + kPathDelimiter; |
| 124 | #endif |
| 125 | } |
| 126 | |
| 127 | std::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 | |
| 140 | std::string ResourcePath(const std::string& name, |
| 141 | const std::string& extension) { |
| 142 | #if defined(WEBRTC_IOS) |
| 143 | return IOSResourcePath(name, extension); |
| 144 | #else |
Artem Titov | 8e6749e | 2018-10-23 17:02:49 +0200 | [diff] [blame] | 145 | absl::optional<std::string> path_opt = ProjectRootPath(); |
| 146 | RTC_DCHECK(path_opt); |
| 147 | std::string resources_path = *path_opt + kResourcesDirName + kPathDelimiter; |
Artem Titov | 8f726be | 2018-10-23 15:50:10 +0200 | [diff] [blame] | 148 | return resources_path + name + "." + extension; |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | } // namespace internal |
| 153 | } // namespace test |
| 154 | } // namespace webrtc |