Eliminate the remaining need for root access in artd_tests.

Bug: 289037540
Test: atest art_standalone_artd_tests
Change-Id: Ie4399e85bcfb3c5d51388a8591c212d8ae9c037d
Merged-In: Ie4399e85bcfb3c5d51388a8591c212d8ae9c037d
diff --git a/artd/artd_test.cc b/artd/artd_test.cc
index a1f5d64..4af8f17 100644
--- a/artd/artd_test.cc
+++ b/artd/artd_test.cc
@@ -120,8 +120,6 @@
 using PrimaryRefProfilePath = ProfilePath::PrimaryRefProfilePath;
 using TmpProfilePath = ProfilePath::TmpProfilePath;
 
-constexpr uid_t kRootUid = 0;
-
 ScopeGuard<std::function<void()>> ScopedSetLogger(android::base::LogFunction&& logger) {
   android::base::LogFunction old_logger = android::base::SetLogger(std::move(logger));
   return make_scope_guard([old_logger = std::move(old_logger)]() mutable {
@@ -328,6 +326,8 @@
     // Remove the trailing '/';
     scratch_path_.resize(scratch_path_.length() - 1);
 
+    TestOnlySetListRootDir(scratch_path_);
+
     ON_CALL(mock_fstat_, Call).WillByDefault(fstat);
 
     // Use an arbitrary existing directory as ART root.
@@ -1972,11 +1972,6 @@
 }
 
 TEST_F(ArtdTest, cleanup) {
-  // TODO(b/289037540): Fix this.
-  if (getuid() != kRootUid) {
-    GTEST_SKIP() << "This test requires root access";
-  }
-
   std::vector<std::string> gc_removed_files;
   std::vector<std::string> gc_kept_files;
 
@@ -2137,11 +2132,6 @@
 }
 
 TEST_F(ArtdTest, deleteRuntimeArtifacts) {
-  // TODO(b/289037540): Fix this.
-  if (getuid() != kRootUid) {
-    GTEST_SKIP() << "This test requires root access";
-  }
-
   std::vector<std::string> removed_files;
   std::vector<std::string> kept_files;
 
@@ -2188,11 +2178,6 @@
 }
 
 TEST_F(ArtdTest, deleteRuntimeArtifactsSpecialChars) {
-  // TODO(b/289037540): Fix this.
-  if (getuid() != kRootUid) {
-    GTEST_SKIP() << "This test requires root access";
-  }
-
   std::vector<std::string> removed_files;
   std::vector<std::string> kept_files;
 
diff --git a/artd/path_utils.cc b/artd/path_utils.cc
index 0bf50c3..aa5a2b1 100644
--- a/artd/path_utils.cc
+++ b/artd/path_utils.cc
@@ -58,6 +58,9 @@
 using TmpProfilePath = ProfilePath::TmpProfilePath;
 using WritableProfilePath = ProfilePath::WritableProfilePath;
 
+// Only to be changed for testing.
+std::string_view gListRootDir = "/";
+
 Result<void> ValidateAbsoluteNormalPath(const std::string& path_str) {
   if (path_str.empty()) {
     return Errorf("Path is empty");
@@ -156,7 +159,7 @@
     }
   }
 
-  return tools::Glob(patterns);
+  return tools::Glob(patterns, gListRootDir);
 }
 
 std::vector<std::string> ListRuntimeArtifactsFiles(
@@ -177,7 +180,7 @@
     }
   }
 
-  return tools::Glob(patterns);
+  return tools::Glob(patterns, gListRootDir);
 }
 
 Result<void> ValidateRuntimeArtifactsPath(const RuntimeArtifactsPath& runtime_artifacts_path) {
@@ -336,5 +339,7 @@
   return entries;
 }
 
+void TestOnlySetListRootDir(std::string_view root_dir) { gListRootDir = root_dir; }
+
 }  // namespace artd
 }  // namespace art
diff --git a/artd/path_utils.h b/artd/path_utils.h
index bb1597d..77aa82a 100644
--- a/artd/path_utils.h
+++ b/artd/path_utils.h
@@ -107,6 +107,11 @@
 android::base::Result<std::vector<android::fs_mgr::FstabEntry>> GetProcMountsEntriesForPath(
     const std::string& path);
 
+// Sets the root dir for `ListManagedFiles` and `ListRuntimeImageFiles`.
+// The passed string must be alive until the test ends.
+// For testing use only.
+void TestOnlySetListRootDir(std::string_view root_dir);
+
 }  // namespace artd
 }  // namespace art