busybox_info_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/install/busybox_info.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <cstdlib>
  8. #include <filesystem>
  9. #include <fstream>
  10. #include <optional>
  11. #include <system_error>
  12. #include "common/check.h"
  13. #include "common/filesystem.h"
  14. #include "llvm/ADT/ScopeExit.h"
  15. #include "llvm/Support/FileSystem.h"
  16. namespace Carbon {
  17. namespace {
  18. using ::testing::Eq;
  19. class BusyboxInfoTest : public ::testing::Test {
  20. public:
  21. explicit BusyboxInfoTest()
  22. : dir_(std::move(*Filesystem::MakeTmpDir())), path_(dir_.path()) {
  23. // Most tests need the running binary for `MakeBusyboxFile`.
  24. static int static_for_main_addr;
  25. running_binary_ = llvm::sys::fs::getMainExecutable("busybox_info_test",
  26. &static_for_main_addr);
  27. }
  28. // Creates a synthetic install tree to test a batch of interactions.
  29. // Optionally accepts a symlink target for the busybox in the install tree.
  30. // Returns the input prefix for easy use.
  31. auto MakeInstallTree(std::filesystem::path prefix,
  32. std::optional<std::filesystem::path> busybox_target = {})
  33. -> std::filesystem::path {
  34. Filesystem::Dir prefix_dir = *dir_.CreateDirectories(prefix);
  35. Filesystem::Dir lib_carbon = *prefix_dir.CreateDirectories("lib/carbon");
  36. if (busybox_target) {
  37. lib_carbon.Symlink("carbon-busybox", busybox_target->native()).Check();
  38. } else {
  39. lib_carbon.Symlink("carbon-busybox", running_binary_).Check();
  40. }
  41. Filesystem::Dir llvm_bin = *lib_carbon.CreateDirectories("llvm/bin");
  42. llvm_bin.Symlink("clang++", "clang").Check();
  43. llvm_bin.Symlink("clang", "../../carbon-busybox").Check();
  44. Filesystem::Dir bin = *prefix_dir.OpenDir("bin", Filesystem::CreateNew);
  45. bin.Symlink("carbon", "../lib/carbon/carbon-busybox").Check();
  46. return path_ / prefix;
  47. }
  48. // The path to the running binary, `busybox_info_test`. This is provided
  49. // because `GetExecutablePath` can fall back to it.
  50. std::string running_binary_;
  51. Filesystem::RemovingDir dir_;
  52. std::filesystem::path path_;
  53. };
  54. TEST_F(BusyboxInfoTest, Direct) {
  55. dir_.Symlink("carbon-busybox", running_binary_).Check();
  56. auto info = GetBusyboxInfo((path_ / "carbon-busybox").c_str());
  57. ASSERT_TRUE(info.ok()) << info.error();
  58. EXPECT_THAT(info->bin_path, Eq(path_ / "carbon-busybox"));
  59. EXPECT_THAT(info->mode, Eq(std::nullopt));
  60. }
  61. TEST_F(BusyboxInfoTest, SymlinkInCurrentDirectory) {
  62. dir_.Symlink("carbon-busybox", running_binary_).Check();
  63. dir_.Symlink("carbon", "carbon-busybox").Check();
  64. auto info = GetBusyboxInfo((path_ / "carbon").c_str());
  65. ASSERT_TRUE(info.ok()) << info.error();
  66. EXPECT_THAT(info->bin_path, Eq(path_ / "carbon-busybox"));
  67. EXPECT_THAT(info->mode, Eq(std::nullopt));
  68. }
  69. TEST_F(BusyboxInfoTest, SymlinkInCurrentDirectoryWithDot) {
  70. dir_.Symlink("carbon-busybox", running_binary_).Check();
  71. dir_.Symlink("carbon", "./carbon-busybox").Check();
  72. auto info = GetBusyboxInfo((path_ / "carbon").c_str());
  73. ASSERT_TRUE(info.ok()) << info.error();
  74. EXPECT_THAT(info->bin_path, Eq(path_ / "./carbon-busybox"));
  75. EXPECT_THAT(info->mode, Eq(std::nullopt));
  76. }
  77. TEST_F(BusyboxInfoTest, ExtraSymlink) {
  78. dir_.Symlink("carbon-busybox", running_binary_).Check();
  79. dir_.Symlink("c", "carbon-busybox").Check();
  80. dir_.Symlink("carbon", "c").Check();
  81. auto info = GetBusyboxInfo((path_ / "carbon").c_str());
  82. ASSERT_TRUE(info.ok()) << info.error();
  83. EXPECT_THAT(info->bin_path, Eq(path_ / "carbon-busybox"));
  84. EXPECT_THAT(info->mode, Eq(std::nullopt));
  85. }
  86. TEST_F(BusyboxInfoTest, OriginalSymlinkNameFormsMode) {
  87. dir_.Symlink("carbon-busybox", running_binary_).Check();
  88. dir_.Symlink("carbon", "carbon-busybox").Check();
  89. dir_.Symlink("clang", "carbon").Check();
  90. dir_.Symlink("clang++", "clang").Check();
  91. auto info = GetBusyboxInfo((path_ / "clang").c_str());
  92. ASSERT_TRUE(info.ok()) << info.error();
  93. EXPECT_THAT(info->bin_path, Eq(path_ / "carbon-busybox"));
  94. EXPECT_THAT(info->mode, Eq("clang"));
  95. info = GetBusyboxInfo((path_ / "clang++").c_str());
  96. ASSERT_TRUE(info.ok()) << info.error();
  97. EXPECT_THAT(info->bin_path, Eq(path_ / "carbon-busybox"));
  98. EXPECT_THAT(info->mode, Eq("clang++"));
  99. }
  100. TEST_F(BusyboxInfoTest, BusyboxIsSymlinkToNowhere) {
  101. dir_.Symlink("carbon-busybox", "nonexistent").Check();
  102. auto info = GetBusyboxInfo((path_ / "carbon-busybox").c_str());
  103. ASSERT_FALSE(info.ok());
  104. EXPECT_THAT(info.error().message(),
  105. Eq(llvm::formatv("expected carbon-busybox symlink at `{0}`",
  106. running_binary_)
  107. .str()));
  108. }
  109. TEST_F(BusyboxInfoTest, BusyboxIsWrongFile) {
  110. // This has the correct name, but it doesn't map back to the running binary
  111. // and so is ignored.
  112. dir_.WriteFileFromString("carbon-busybox", "stub").Check();
  113. auto info = GetBusyboxInfo((path_ / "carbon-busybox").c_str());
  114. ASSERT_FALSE(info.ok());
  115. EXPECT_THAT(info.error().message(),
  116. Eq(llvm::formatv("expected carbon-busybox symlink at `{0}`",
  117. running_binary_)
  118. .str()));
  119. }
  120. TEST_F(BusyboxInfoTest, RelativeSymlink) {
  121. Filesystem::Dir d1 = *dir_.OpenDir("dir1", Filesystem::CreateNew);
  122. d1.Symlink("carbon-busybox", running_binary_).Check();
  123. Filesystem::Dir d2 = *dir_.OpenDir("dir2", Filesystem::CreateNew);
  124. d2.Symlink("carbon", "../dir1/carbon-busybox").Check();
  125. auto info = GetBusyboxInfo((path_ / "dir2/carbon").c_str());
  126. ASSERT_TRUE(info.ok()) << info.error();
  127. EXPECT_THAT(info->bin_path, Eq(path_ / "dir2/../dir1/carbon-busybox"));
  128. EXPECT_THAT(info->mode, Eq(std::nullopt));
  129. }
  130. TEST_F(BusyboxInfoTest, AbsoluteSymlink) {
  131. Filesystem::Dir d1 = *dir_.OpenDir("dir1", Filesystem::CreateNew);
  132. d1.Symlink("carbon-busybox", running_binary_).Check();
  133. Filesystem::Dir d2 = *dir_.OpenDir("dir2", Filesystem::CreateNew);
  134. ASSERT_TRUE(path_.is_absolute());
  135. d2.Symlink("carbon", (path_ / "dir1/carbon-busybox")).Check();
  136. auto info = GetBusyboxInfo((path_ / "dir2/carbon").c_str());
  137. ASSERT_TRUE(info.ok()) << info.error();
  138. EXPECT_THAT(info->bin_path, Eq(path_ / "dir1/carbon-busybox"));
  139. EXPECT_THAT(info->mode, Eq(std::nullopt));
  140. }
  141. TEST_F(BusyboxInfoTest, NotBusyboxFile) {
  142. dir_.WriteFileFromString("file", "stub").Check();
  143. auto info = GetBusyboxInfo((path_ / "file").c_str());
  144. EXPECT_FALSE(info.ok());
  145. }
  146. TEST_F(BusyboxInfoTest, NotBusyboxSymlink) {
  147. dir_.WriteFileFromString("file", "stub").Check();
  148. dir_.Symlink("carbon", "file").Check();
  149. auto info = GetBusyboxInfo((path_ / "carbon").c_str());
  150. EXPECT_FALSE(info.ok());
  151. }
  152. TEST_F(BusyboxInfoTest, LayerSymlinksInstallTree) {
  153. dir_.Symlink("actual-busybox", running_binary_).Check();
  154. // Create a facsimile of the install prefix with even the busybox as a
  155. // symlink. Also include potential relative sibling symlinks like `clang++` to
  156. // `clang`.
  157. auto prefix = MakeInstallTree("test_prefix", (path_ / "actual-busybox"));
  158. auto info = GetBusyboxInfo((prefix / "bin/carbon").c_str());
  159. ASSERT_TRUE(info.ok()) << info.error();
  160. EXPECT_THAT(info->bin_path, Eq(prefix / "bin/../lib/carbon/carbon-busybox"));
  161. EXPECT_THAT(info->mode, Eq(std::nullopt));
  162. info = GetBusyboxInfo((prefix / "lib/carbon/llvm/bin/clang").c_str());
  163. ASSERT_TRUE(info.ok()) << info.error();
  164. EXPECT_THAT(info->bin_path,
  165. Eq(prefix / "lib/carbon/llvm/bin/../../carbon-busybox"));
  166. EXPECT_THAT(info->mode, Eq("clang"));
  167. info = GetBusyboxInfo((prefix / "lib/carbon/llvm/bin/clang++").c_str());
  168. ASSERT_TRUE(info.ok()) << info.error();
  169. EXPECT_THAT(info->bin_path,
  170. Eq(prefix / "lib/carbon/llvm/bin/../../carbon-busybox"));
  171. EXPECT_THAT(info->mode, Eq("clang++"));
  172. }
  173. TEST_F(BusyboxInfoTest, StopSearchAtFirstSymlinkWithRelativeBusybox) {
  174. // Some install of Carbon under `opt`.
  175. std::filesystem::path opt_prefix = MakeInstallTree("opt");
  176. // A second install, but with its symlinks pointing into the `opt` tree rather
  177. // than at its busybox.
  178. {
  179. Filesystem::Dir lib_carbon = *dir_.CreateDirectories("lib/carbon");
  180. lib_carbon.Symlink("carbon-busybox", running_binary_).Check();
  181. Filesystem::Dir bin = *dir_.OpenDir("bin", Filesystem::CreateNew);
  182. bin.Symlink("carbon", "../opt/bin/carbon").Check();
  183. Filesystem::Dir llvm_bin = *lib_carbon.CreateDirectories("llvm/bin");
  184. llvm_bin.Symlink("clang", (opt_prefix / "lib/carbon/llvm/bin/clang"))
  185. .Check();
  186. }
  187. // Starting from the second install uses the relative busybox rather than
  188. // traversing the symlink further.
  189. auto info = GetBusyboxInfo((path_ / "bin/carbon").c_str());
  190. ASSERT_TRUE(info.ok()) << info.error();
  191. EXPECT_THAT(info->bin_path, Eq(path_ / "bin/../lib/carbon/carbon-busybox"));
  192. info = GetBusyboxInfo((path_ / "lib/carbon/llvm/bin/clang").c_str());
  193. ASSERT_TRUE(info.ok()) << info.error();
  194. EXPECT_THAT(info->bin_path,
  195. Eq(path_ / "lib/carbon/llvm/bin/../../carbon-busybox"));
  196. }
  197. TEST_F(BusyboxInfoTest, RejectSymlinkInUnrelatedInstall) {
  198. // Add two installs of Carbon nested inside each other in a realistic
  199. // scenario: `/usr` and `/usr/local`.
  200. MakeInstallTree("usr");
  201. std::filesystem::path usr_local = MakeInstallTree("usr/local");
  202. // Now add a stray symlink directly in `.../usr/local` to the local install.
  203. //
  204. // This has the interesting property that both of these "work" and find the
  205. // same busybox but probably wanted to find different ones:
  206. // - `.../usr/local/../lib/carbon/carbon-busybox`
  207. // - `.../usr/bin/../lib/carbon/carbon-busybox`
  208. Filesystem::Dir usr_local_dir = *dir_.OpenDir("usr/local");
  209. usr_local_dir.Symlink("carbon", "bin/carbon").Check();
  210. // Check that the busybox doesn't use the relative busybox in this case, and
  211. // walks the symlink to find the correct installation.
  212. auto info = GetBusyboxInfo((usr_local / "carbon").c_str());
  213. ASSERT_TRUE(info.ok()) << info.error();
  214. EXPECT_THAT(info->bin_path,
  215. Eq(usr_local / "bin/../lib/carbon/carbon-busybox"));
  216. // Ensure this works even with intervening `.` directory components.
  217. usr_local_dir.Symlink("carbon2", "bin/././carbon").Check();
  218. // Check that the busybox doesn't use the relative busybox in this case, and
  219. // walks the symlink to find the correct installation.
  220. info = GetBusyboxInfo((usr_local / "carbon2").c_str());
  221. ASSERT_TRUE(info.ok()) << info.error();
  222. EXPECT_THAT(info->bin_path,
  223. Eq(usr_local / "bin/../lib/carbon/carbon-busybox"));
  224. }
  225. TEST_F(BusyboxInfoTest, EnvBinaryPathOverride) {
  226. // The test should not have this environment variable set.
  227. ASSERT_THAT(getenv(Argv0OverrideEnv), Eq(nullptr));
  228. // Set the environment to our actual busybox.
  229. dir_.Symlink("carbon-busybox", running_binary_).Check();
  230. setenv(Argv0OverrideEnv, (path_ / "carbon-busybox").c_str(), /*overwrite=*/1);
  231. auto info = GetBusyboxInfo("/some/nonexistent/path");
  232. if (getenv(Argv0OverrideEnv)) {
  233. unsetenv(Argv0OverrideEnv);
  234. ADD_FAILURE() << "GetBusyboxInfo should unset Argv0OverrideEnv";
  235. }
  236. ASSERT_TRUE(info.ok()) << info.error();
  237. EXPECT_THAT(info->bin_path, Eq(path_ / "carbon-busybox"));
  238. EXPECT_THAT(info->mode, Eq(std::nullopt));
  239. // Make sure that we cleaned up the environment afterward.
  240. EXPECT_THAT(getenv(Argv0OverrideEnv), Eq(nullptr));
  241. }
  242. } // namespace
  243. } // namespace Carbon