busybox_info_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <fstream>
  9. #include "common/check.h"
  10. #include "llvm/ADT/ScopeExit.h"
  11. namespace Carbon {
  12. namespace {
  13. using ::testing::Eq;
  14. class BusyboxInfoTest : public ::testing::Test {
  15. public:
  16. // Set up a temp directory for the test case.
  17. explicit BusyboxInfoTest() {
  18. const char* tmpdir = std::getenv("TEST_TMPDIR");
  19. CARBON_CHECK(tmpdir);
  20. dir_ = MakeDir(std::filesystem::absolute(
  21. std::filesystem::path(tmpdir) /
  22. ::testing::UnitTest::GetInstance()->current_test_info()->name()));
  23. }
  24. // Delete the test case's temp directory.
  25. ~BusyboxInfoTest() override {
  26. std::error_code ec;
  27. std::filesystem::remove_all(dir_, ec);
  28. CARBON_CHECK(!ec, "error removing {0}: {1}", dir_, ec.message());
  29. }
  30. // Creates a stub file. Returns the input file for easier use.
  31. auto MakeFile(std::filesystem::path file) -> std::filesystem::path {
  32. std::ofstream out(file.c_str());
  33. out << "stub";
  34. CARBON_CHECK(out, "error creating {0}", file);
  35. return file;
  36. }
  37. // Creates a symlink to the target. Returns the input file for easier use.
  38. auto MakeSymlink(std::filesystem::path file, auto target)
  39. -> std::filesystem::path {
  40. std::error_code ec;
  41. std::filesystem::create_symlink(target, file, ec);
  42. CARBON_CHECK(!ec, "error creating {0}: {1}", file, ec.message());
  43. return file;
  44. }
  45. // Creates a directory, recursively if needed. Returns the input file for
  46. // easier use.
  47. auto MakeDir(std::filesystem::path dir) -> std::filesystem::path {
  48. std::error_code ec;
  49. std::filesystem::create_directories(dir, ec);
  50. CARBON_CHECK(!ec, "error creating {0}: {1}", dir, ec.message());
  51. return dir;
  52. }
  53. // Creates a synthetic install tree to test a batch of interactions.
  54. // Optionally accepts a symlink target for the busybox in the install tree.
  55. // Returns the input prefix for easy use.
  56. auto MakeInstallTree(std::filesystem::path prefix,
  57. std::optional<std::filesystem::path> busybox_target = {})
  58. -> std::filesystem::path {
  59. MakeDir(prefix / "lib/carbon");
  60. if (busybox_target) {
  61. MakeSymlink(prefix / "lib/carbon/carbon-busybox", *busybox_target);
  62. } else {
  63. MakeFile(prefix / "lib/carbon/carbon-busybox");
  64. }
  65. MakeDir(prefix / "lib/carbon/llvm/bin");
  66. MakeSymlink(prefix / "lib/carbon/llvm/bin/clang++", "clang");
  67. MakeSymlink(prefix / "lib/carbon/llvm/bin/clang", "../../carbon-busybox");
  68. MakeDir(prefix / "bin");
  69. MakeSymlink(prefix / "bin/carbon", "../lib/carbon/carbon-busybox");
  70. return prefix;
  71. }
  72. // The test's temp directory, deleted on destruction.
  73. std::filesystem::path dir_;
  74. };
  75. TEST_F(BusyboxInfoTest, Direct) {
  76. auto busybox = MakeFile(dir_ / "carbon-busybox");
  77. auto info = GetBusyboxInfo(busybox.string());
  78. ASSERT_TRUE(info.ok()) << info.error();
  79. EXPECT_THAT(info->bin_path, Eq(busybox));
  80. EXPECT_THAT(info->mode, Eq(std::nullopt));
  81. }
  82. TEST_F(BusyboxInfoTest, SymlinkInCurrentDirectory) {
  83. MakeFile(dir_ / "carbon-busybox");
  84. auto target = MakeSymlink(dir_ / "carbon", "carbon-busybox");
  85. auto info = GetBusyboxInfo(target.string());
  86. ASSERT_TRUE(info.ok()) << info.error();
  87. EXPECT_THAT(info->bin_path, Eq(dir_ / "carbon-busybox"));
  88. EXPECT_THAT(info->mode, Eq(std::nullopt));
  89. }
  90. TEST_F(BusyboxInfoTest, SymlinkInCurrentDirectoryWithDot) {
  91. MakeFile(dir_ / "carbon-busybox");
  92. auto target = MakeSymlink(dir_ / "carbon", "./carbon-busybox");
  93. auto info = GetBusyboxInfo(target.string());
  94. ASSERT_TRUE(info.ok()) << info.error();
  95. EXPECT_THAT(info->bin_path, Eq(dir_ / "./carbon-busybox"));
  96. EXPECT_THAT(info->mode, Eq(std::nullopt));
  97. }
  98. TEST_F(BusyboxInfoTest, ExtraSymlink) {
  99. MakeFile(dir_ / "carbon-busybox");
  100. MakeSymlink(dir_ / "c", "carbon-busybox");
  101. auto target = MakeSymlink(dir_ / "carbon", "c");
  102. auto info = GetBusyboxInfo(target.string());
  103. ASSERT_TRUE(info.ok()) << info.error();
  104. EXPECT_THAT(info->bin_path, Eq(dir_ / "carbon-busybox"));
  105. EXPECT_THAT(info->mode, Eq(std::nullopt));
  106. }
  107. TEST_F(BusyboxInfoTest, OriginalSymlinkNameFormsMode) {
  108. MakeFile(dir_ / "carbon-busybox");
  109. MakeSymlink(dir_ / "carbon", "carbon-busybox");
  110. auto clang_target = MakeSymlink(dir_ / "clang", "carbon");
  111. auto clang_plusplus_target = MakeSymlink(dir_ / "clang++", "clang");
  112. auto info = GetBusyboxInfo(clang_target.string());
  113. ASSERT_TRUE(info.ok()) << info.error();
  114. EXPECT_THAT(info->bin_path, Eq(dir_ / "carbon-busybox"));
  115. EXPECT_THAT(info->mode, Eq("clang"));
  116. info = GetBusyboxInfo(clang_plusplus_target.string());
  117. ASSERT_TRUE(info.ok()) << info.error();
  118. EXPECT_THAT(info->bin_path, Eq(dir_ / "carbon-busybox"));
  119. EXPECT_THAT(info->mode, Eq("clang++"));
  120. }
  121. TEST_F(BusyboxInfoTest, BusyboxIsSymlink) {
  122. MakeFile(dir_ / "actual-busybox");
  123. auto target = MakeSymlink(dir_ / "carbon-busybox", "actual-busybox");
  124. auto info = GetBusyboxInfo(target.string());
  125. ASSERT_TRUE(info.ok()) << info.error();
  126. EXPECT_THAT(info->bin_path, Eq(target));
  127. EXPECT_THAT(info->mode, Eq(std::nullopt));
  128. }
  129. TEST_F(BusyboxInfoTest, BusyboxIsSymlinkToNowhere) {
  130. auto target = MakeSymlink(dir_ / "carbon-busybox", "nonexistent");
  131. auto info = GetBusyboxInfo(target.string());
  132. ASSERT_TRUE(info.ok()) << info.error();
  133. EXPECT_THAT(info->bin_path, Eq(dir_ / "carbon-busybox"));
  134. EXPECT_THAT(info->mode, Eq(std::nullopt));
  135. }
  136. TEST_F(BusyboxInfoTest, RelativeSymlink) {
  137. MakeDir(dir_ / "dir1");
  138. MakeFile(dir_ / "dir1/carbon-busybox");
  139. MakeDir(dir_ / "dir2");
  140. auto target = MakeSymlink(dir_ / "dir2/carbon", "../dir1/carbon-busybox");
  141. auto info = GetBusyboxInfo(target.string());
  142. ASSERT_TRUE(info.ok()) << info.error();
  143. EXPECT_THAT(info->bin_path, Eq(dir_ / "dir2/../dir1/carbon-busybox"));
  144. EXPECT_THAT(info->mode, Eq(std::nullopt));
  145. }
  146. TEST_F(BusyboxInfoTest, AbsoluteSymlink) {
  147. MakeDir(dir_ / "dir1");
  148. auto busybox = MakeFile(dir_ / "dir1/carbon-busybox");
  149. ASSERT_TRUE(busybox.is_absolute());
  150. MakeDir(dir_ / "dir2");
  151. auto target = MakeSymlink(dir_ / "dir2/carbon", busybox);
  152. auto info = GetBusyboxInfo(target.string());
  153. ASSERT_TRUE(info.ok()) << info.error();
  154. EXPECT_THAT(info->bin_path, Eq(busybox));
  155. EXPECT_THAT(info->mode, Eq(std::nullopt));
  156. }
  157. TEST_F(BusyboxInfoTest, NotBusyboxFile) {
  158. auto target = MakeFile(dir_ / "file");
  159. auto info = GetBusyboxInfo(target.string());
  160. EXPECT_FALSE(info.ok());
  161. }
  162. TEST_F(BusyboxInfoTest, NotBusyboxSymlink) {
  163. MakeFile(dir_ / "file");
  164. auto target = MakeSymlink(dir_ / "carbon", "file");
  165. auto info = GetBusyboxInfo(target.string());
  166. EXPECT_FALSE(info.ok());
  167. }
  168. TEST_F(BusyboxInfoTest, LayerSymlinksInstallTree) {
  169. auto actual_busybox = MakeFile(dir_ / "actual-busybox");
  170. // Create a facsimile of the install prefix with even the busybox as a
  171. // symlink. Also include potential relative sibling symlinks like `clang++` to
  172. // `clang`.
  173. auto prefix = MakeInstallTree(dir_ / "test_prefix", actual_busybox);
  174. auto info = GetBusyboxInfo((prefix / "bin/carbon").string());
  175. ASSERT_TRUE(info.ok()) << info.error();
  176. EXPECT_THAT(info->bin_path, Eq(prefix / "bin/../lib/carbon/carbon-busybox"));
  177. EXPECT_THAT(info->mode, Eq(std::nullopt));
  178. info = GetBusyboxInfo((prefix / "lib/carbon/llvm/bin/clang").string());
  179. ASSERT_TRUE(info.ok()) << info.error();
  180. EXPECT_THAT(info->bin_path,
  181. Eq(prefix / "lib/carbon/llvm/bin/../../carbon-busybox"));
  182. EXPECT_THAT(info->mode, Eq("clang"));
  183. info = GetBusyboxInfo((prefix / "lib/carbon/llvm/bin/clang++").string());
  184. ASSERT_TRUE(info.ok()) << info.error();
  185. EXPECT_THAT(info->bin_path,
  186. Eq(prefix / "lib/carbon/llvm/bin/../../carbon-busybox"));
  187. EXPECT_THAT(info->mode, Eq("clang++"));
  188. }
  189. TEST_F(BusyboxInfoTest, StopSearchAtFirstSymlinkWithRelativeBusybox) {
  190. // Some install of Carbon under `opt`.
  191. auto opt_prefix = MakeInstallTree(dir_ / "opt");
  192. // A second install, but with its symlinks pointing into the `opt` tree rather
  193. // than at its busybox.
  194. MakeDir(dir_ / "lib/carbon");
  195. MakeFile(dir_ / "lib/carbon/carbon-busybox");
  196. MakeDir(dir_ / "bin");
  197. auto target = MakeSymlink(dir_ / "bin/carbon", "../opt/bin/carbon");
  198. MakeDir(dir_ / "lib/carbon/llvm/bin");
  199. auto clang_target = MakeSymlink(dir_ / "lib/carbon/llvm/bin/clang",
  200. opt_prefix / "lib/carbon/llvm/bin/clang");
  201. // Starting from the second install uses the relative busybox rather than
  202. // traversing the symlink further.
  203. auto info = GetBusyboxInfo(target.string());
  204. ASSERT_TRUE(info.ok()) << info.error();
  205. EXPECT_THAT(info->bin_path, Eq(dir_ / "bin/../lib/carbon/carbon-busybox"));
  206. info = GetBusyboxInfo(clang_target.string());
  207. ASSERT_TRUE(info.ok()) << info.error();
  208. EXPECT_THAT(info->bin_path,
  209. Eq(dir_ / "lib/carbon/llvm/bin/../../carbon-busybox"));
  210. }
  211. TEST_F(BusyboxInfoTest, RejectSymlinkInUnrelatedInstall) {
  212. // Add two installs of Carbon nested inside each other in a realistic
  213. // scenario: `/usr` and `/usr/local`.
  214. MakeInstallTree(dir_ / "usr");
  215. MakeInstallTree(dir_ / "usr/local");
  216. // Now add a stray symlink directly in `.../usr/local` to the local install.
  217. //
  218. // This has the interesting property that both of these "work" and find the
  219. // same busybox but probably wanted to find different ones:
  220. // - `.../usr/local/../lib/carbon/carbon-busybox`
  221. // - `.../usr/bin/../lib/carbon/carbon-busybox`
  222. auto stray_target = MakeSymlink(dir_ / "usr/local/carbon", "bin/carbon");
  223. // Check that the busybox doesn't use the relative busybox in this case, and
  224. // walks the symlink to find the correct installation.
  225. auto info = GetBusyboxInfo(stray_target.string());
  226. ASSERT_TRUE(info.ok()) << info.error();
  227. EXPECT_THAT(info->bin_path,
  228. Eq(dir_ / "usr/local/bin/../lib/carbon/carbon-busybox"));
  229. // Ensure this works even with intervening `.` directory components.
  230. stray_target = MakeSymlink(dir_ / "usr/local/carbon2", "bin/././carbon");
  231. // Check that the busybox doesn't use the relative busybox in this case, and
  232. // walks the symlink to find the correct installation.
  233. info = GetBusyboxInfo(stray_target.string());
  234. ASSERT_TRUE(info.ok()) << info.error();
  235. EXPECT_THAT(info->bin_path,
  236. Eq(dir_ / "usr/local/bin/../lib/carbon/carbon-busybox"));
  237. }
  238. TEST_F(BusyboxInfoTest, EnvBinaryPathOverride) {
  239. // The test should not have this environment variable set.
  240. ASSERT_THAT(getenv(Argv0OverrideEnv), Eq(nullptr));
  241. // Clean up this environment variable when this test finishes.
  242. auto _ = llvm::make_scope_exit([] { unsetenv(Argv0OverrideEnv); });
  243. // Set the environment to our actual busybox.
  244. auto busybox = MakeFile(dir_ / "carbon-busybox");
  245. setenv(Argv0OverrideEnv, busybox.c_str(), /*overwrite=*/1);
  246. auto info = GetBusyboxInfo("/some/nonexistent/path");
  247. ASSERT_TRUE(info.ok()) << info.error();
  248. EXPECT_THAT(info->bin_path, Eq(busybox));
  249. EXPECT_THAT(info->mode, Eq(std::nullopt));
  250. }
  251. } // namespace
  252. } // namespace Carbon