Use GetTestDataPath for tests.

Change-Id: I90f121b0138ea64b9695e84f4d2b882eae80cfda
diff --git a/src/base/test/utils.cc b/src/base/test/utils.cc
index 9e2ff4c..188038a 100644
--- a/src/base/test/utils.cc
+++ b/src/base/test/utils.cc
@@ -33,11 +33,6 @@
 namespace base {
 
 std::string GetTestDataPath(const std::string& path) {
-  char const* test_data_root = getenv("TEST_DATA_ROOT");
-  if (test_data_root) {
-    return std::string(test_data_root) + path;
-  }
-
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
     PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
     PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
@@ -49,12 +44,16 @@
   std::string self_path = std::string(buf);
   // Cut binary name.
   self_path = self_path.substr(0, self_path.find_last_of("/"));
-  return self_path + "/../../test/data/" + path;
-#else
+  std::string full_path = self_path + "/../../" + path;
+  if (access(full_path.c_str(), F_OK) == 0)
+    return full_path;
+  full_path = self_path + "/" + path;
+  if (access(full_path.c_str(), F_OK) == 0)
+    return full_path;
+#endif
   // TODO(hjd): Implement on MacOS/Windows
   // Fall back to relative to root dir.
-  return "test/data/" + path;
-#endif
+  return path;
 }
 
 }  // namespace base
diff --git a/src/trace_processor/trace_database_integrationtest.cc b/src/trace_processor/trace_database_integrationtest.cc
index b7061ed..9a1bd3c 100644
--- a/src/trace_processor/trace_database_integrationtest.cc
+++ b/src/trace_processor/trace_database_integrationtest.cc
@@ -36,7 +36,8 @@
 
  protected:
   util::Status LoadTrace(const char* name, int min_chunk_size = 512) {
-    base::ScopedFstream f(fopen(base::GetTestDataPath(name).c_str(), "rb"));
+    base::ScopedFstream f(fopen(
+        base::GetTestDataPath(std::string("test/data/") + name).c_str(), "rb"));
     std::minstd_rand0 rnd_engine(0);
     std::uniform_int_distribution<> dist(min_chunk_size, 1024);
     while (!feof(*f)) {
diff --git a/src/traced/probes/filesystem/file_scanner_unittest.cc b/src/traced/probes/filesystem/file_scanner_unittest.cc
index 59b2d95..5b77009 100644
--- a/src/traced/probes/filesystem/file_scanner_unittest.cc
+++ b/src/traced/probes/filesystem/file_scanner_unittest.cc
@@ -23,6 +23,7 @@
 #include "perfetto/base/logging.h"
 #include "protos/perfetto/trace/filesystem/inode_file_map.pbzero.h"
 #include "src/base/test/test_task_runner.h"
+#include "src/base/test/utils.h"
 #include "test/gtest_and_gmock.h"
 
 namespace perfetto {
@@ -101,7 +102,9 @@
       },
       [&done] { done = true; });
 
-  FileScanner fs({"src/traced/probes/filesystem/testdata"}, &delegate);
+  FileScanner fs(
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata")},
+      &delegate);
   fs.Scan();
 
   EXPECT_EQ(seen, 1u);
@@ -119,7 +122,9 @@
       },
       task_runner.CreateCheckpoint("done"));
 
-  FileScanner fs({"src/traced/probes/filesystem/testdata"}, &delegate, 1, 1);
+  FileScanner fs(
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata")},
+      &delegate, 1, 1);
   fs.Scan(&task_runner);
 
   task_runner.RunUntilCheckpoint("done");
@@ -137,18 +142,24 @@
       },
       [] {});
 
-  FileScanner fs({"src/traced/probes/filesystem/testdata"}, &delegate);
+  FileScanner fs(
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata")},
+      &delegate);
   fs.Scan();
 
   EXPECT_THAT(
       file_entries,
       UnorderedElementsAre(
-          Eq(StatFileEntry("src/traced/probes/filesystem/testdata/dir1/file1",
-                           protos::pbzero::InodeFileMap_Entry_Type_FILE)),
-          Eq(StatFileEntry("src/traced/probes/filesystem/testdata/file2",
+          Eq(StatFileEntry(
+              base::GetTestDataPath(
+                  "src/traced/probes/filesystem/testdata/dir1/file1"),
+              protos::pbzero::InodeFileMap_Entry_Type_FILE)),
+          Eq(StatFileEntry(base::GetTestDataPath(
+                               "src/traced/probes/filesystem/testdata/file2"),
                            protos::pbzero::InodeFileMap_Entry_Type_FILE)),
           Eq(StatFileEntry(
-              "src/traced/probes/filesystem/testdata/dir1",
+              base::GetTestDataPath(
+                  "src/traced/probes/filesystem/testdata/dir1"),
               protos::pbzero::InodeFileMap_Entry_Type_DIRECTORY))));
 }
 
@@ -163,7 +174,9 @@
       },
       task_runner.CreateCheckpoint("done"));
 
-  FileScanner fs({"src/traced/probes/filesystem/testdata"}, &delegate, 1, 1);
+  FileScanner fs(
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata")},
+      &delegate, 1, 1);
   fs.Scan(&task_runner);
 
   task_runner.RunUntilCheckpoint("done");
@@ -171,12 +184,16 @@
   EXPECT_THAT(
       file_entries,
       UnorderedElementsAre(
-          Eq(StatFileEntry("src/traced/probes/filesystem/testdata/dir1/file1",
-                           protos::pbzero::InodeFileMap_Entry_Type_FILE)),
-          Eq(StatFileEntry("src/traced/probes/filesystem/testdata/file2",
+          Eq(StatFileEntry(
+              base::GetTestDataPath(
+                  "src/traced/probes/filesystem/testdata/dir1/file1"),
+              protos::pbzero::InodeFileMap_Entry_Type_FILE)),
+          Eq(StatFileEntry(base::GetTestDataPath(
+                               "src/traced/probes/filesystem/testdata/file2"),
                            protos::pbzero::InodeFileMap_Entry_Type_FILE)),
           Eq(StatFileEntry(
-              "src/traced/probes/filesystem/testdata/dir1",
+              base::GetTestDataPath(
+                  "src/traced/probes/filesystem/testdata/dir1"),
               protos::pbzero::InodeFileMap_Entry_Type_DIRECTORY))));
 }
 
diff --git a/src/traced/probes/filesystem/inode_file_data_source_unittest.cc b/src/traced/probes/filesystem/inode_file_data_source_unittest.cc
index 6631c0c..edd208b 100644
--- a/src/traced/probes/filesystem/inode_file_data_source_unittest.cc
+++ b/src/traced/probes/filesystem/inode_file_data_source_unittest.cc
@@ -18,8 +18,10 @@
 
 #include "perfetto/protozero/scattered_heap_buffer.h"
 #include "src/base/test/test_task_runner.h"
+#include "src/base/test/utils.h"
 #include "src/traced/probes/filesystem/lru_inode_cache.h"
 #include "src/tracing/core/null_trace_writer.h"
+
 #include "test/gtest_and_gmock.h"
 
 #include "protos/perfetto/config/inode_file/inode_file_config.pbzero.h"
@@ -51,8 +53,13 @@
                             cache,
                             std::move(writer)) {
     struct stat buf;
-    PERFETTO_CHECK(lstat("src/traced/probes/filesystem/testdata", &buf) != -1);
-    mount_points_.emplace(buf.st_dev, "src/traced/probes/filesystem/testdata");
+    PERFETTO_CHECK(
+        lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata")
+                  .c_str(),
+              &buf) != -1);
+    mount_points_.emplace(
+        buf.st_dev,
+        base::GetTestDataPath("src/traced/probes/filesystem/testdata"));
   }
 
   MOCK_METHOD3(FillInodeEntry,
@@ -87,12 +94,15 @@
   auto data_source = GetInodeFileDataSource(ds_config);
 
   struct stat buf;
-  PERFETTO_CHECK(lstat("src/traced/probes/filesystem/testdata/file2", &buf) !=
-                 -1);
+  PERFETTO_CHECK(
+      lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
+                .c_str(),
+            &buf) != -1);
 
   auto done = task_runner_.CreateCheckpoint("done");
-  InodeMapValue value(protos::pbzero::InodeFileMap_Entry_Type_FILE,
-                      {"src/traced/probes/filesystem/testdata/file2"});
+  InodeMapValue value(
+      protos::pbzero::InodeFileMap_Entry_Type_FILE,
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)))
       .WillOnce(InvokeWithoutArgs(done));
 
@@ -107,15 +117,19 @@
 TEST_F(InodeFileDataSourceTest, TestStaticMap) {
   DataSourceConfig config;
   auto data_source = GetInodeFileDataSource(config);
-  CreateStaticDeviceToInodeMap("src/traced/probes/filesystem/testdata",
-                               &static_file_map_);
+  CreateStaticDeviceToInodeMap(
+      base::GetTestDataPath("src/traced/probes/filesystem/testdata"),
+      &static_file_map_);
 
   struct stat buf;
-  PERFETTO_CHECK(lstat("src/traced/probes/filesystem/testdata/file2", &buf) !=
-                 -1);
+  PERFETTO_CHECK(
+      lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
+                .c_str(),
+            &buf) != -1);
 
-  InodeMapValue value(protos::pbzero::InodeFileMap_Entry_Type_FILE,
-                      {"src/traced/probes/filesystem/testdata/file2"});
+  InodeMapValue value(
+      protos::pbzero::InodeFileMap_Entry_Type_FILE,
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)));
 
   data_source->OnInodes({{buf.st_ino, buf.st_dev}});
@@ -126,15 +140,19 @@
 TEST_F(InodeFileDataSourceTest, TestCache) {
   DataSourceConfig config;
   auto data_source = GetInodeFileDataSource(config);
-  CreateStaticDeviceToInodeMap("src/traced/probes/filesystem/testdata",
-                               &static_file_map_);
+  CreateStaticDeviceToInodeMap(
+      base::GetTestDataPath("src/traced/probes/filesystem/testdata"),
+      &static_file_map_);
 
   struct stat buf;
-  PERFETTO_CHECK(lstat("src/traced/probes/filesystem/testdata/file2", &buf) !=
-                 -1);
+  PERFETTO_CHECK(
+      lstat(base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")
+                .c_str(),
+            &buf) != -1);
 
-  InodeMapValue value(protos::pbzero::InodeFileMap_Entry_Type_FILE,
-                      {"src/traced/probes/filesystem/testdata/file2"});
+  InodeMapValue value(
+      protos::pbzero::InodeFileMap_Entry_Type_FILE,
+      {base::GetTestDataPath("src/traced/probes/filesystem/testdata/file2")});
   cache_.Insert(std::make_pair(buf.st_dev, buf.st_ino), value);
 
   EXPECT_CALL(*data_source, FillInodeEntry(_, buf.st_ino, Eq(value)));
diff --git a/src/traced/probes/ftrace/BUILD.gn b/src/traced/probes/ftrace/BUILD.gn
index e6440af..719f316 100644
--- a/src/traced/probes/ftrace/BUILD.gn
+++ b/src/traced/probes/ftrace/BUILD.gn
@@ -29,6 +29,7 @@
   deps = [
     ":ftrace",
     "../../../../gn:default_deps",
+    "../../../base:test_support",
     "../../../protozero",
   ]
   public_deps = [
diff --git a/src/traced/probes/ftrace/proto_translation_table_unittest.cc b/src/traced/probes/ftrace/proto_translation_table_unittest.cc
index 947b9fb..5448ba1 100644
--- a/src/traced/probes/ftrace/proto_translation_table_unittest.cc
+++ b/src/traced/probes/ftrace/proto_translation_table_unittest.cc
@@ -19,6 +19,7 @@
 #include "protos/perfetto/trace/ftrace/ftrace_event.pbzero.h"
 #include "protos/perfetto/trace/ftrace/generic.pbzero.h"
 #include "src/base/test/gtest_test_suite.h"
+#include "src/base/test/utils.h"
 #include "src/traced/probes/ftrace/event_info.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 #include "test/gtest_and_gmock.h"
@@ -51,8 +52,8 @@
 class AllTranslationTableTest : public TestWithParam<const char*> {
  public:
   void SetUp() override {
-    std::string path =
-        "src/traced/probes/ftrace/test/data/" + std::string(GetParam()) + "/";
+    std::string path = base::GetTestDataPath(
+        "src/traced/probes/ftrace/test/data/" + std::string(GetParam()) + "/");
     FtraceProcfs ftrace_procfs(path);
     table_ = ProtoTranslationTable::Create(&ftrace_procfs, GetStaticEventInfo(),
                                            GetStaticCommonFieldsInfo());
@@ -105,8 +106,8 @@
 INSTANTIATE_TEST_SUITE_P(ByDevice, AllTranslationTableTest, ValuesIn(kDevices));
 
 TEST(TranslationTableTest, Seed) {
-  std::string path =
-      "src/traced/probes/ftrace/test/data/android_seed_N2F62_3.10.49/";
+  std::string path = base::GetTestDataPath(
+      "src/traced/probes/ftrace/test/data/android_seed_N2F62_3.10.49/");
   FtraceProcfs ftrace_procfs(path);
   auto table = ProtoTranslationTable::Create(
       &ftrace_procfs, GetStaticEventInfo(), GetStaticCommonFieldsInfo());
diff --git a/src/traced/probes/ftrace/test/cpu_reader_support.cc b/src/traced/probes/ftrace/test/cpu_reader_support.cc
index 1481b16..020298b 100644
--- a/src/traced/probes/ftrace/test/cpu_reader_support.cc
+++ b/src/traced/probes/ftrace/test/cpu_reader_support.cc
@@ -17,6 +17,7 @@
 #include "src/traced/probes/ftrace/test/cpu_reader_support.h"
 
 #include "perfetto/ext/base/utils.h"
+#include "src/base/test/utils.h"
 #include "src/traced/probes/ftrace/ftrace_procfs.h"
 
 #include <string.h>
@@ -29,22 +30,6 @@
 
 std::map<std::string, std::unique_ptr<ProtoTranslationTable>>* g_tables;
 
-std::string GetBinaryDirectory() {
-  std::string buf(512, '\0');
-  ssize_t rd = readlink("/proc/self/exe", &buf[0], buf.size());
-  if (rd < 0) {
-    PERFETTO_ELOG("Failed to readlink(\"/proc/self/exe\"");
-    return "";
-  }
-  buf.resize(static_cast<size_t>(rd));
-  size_t end = buf.rfind('/');
-  if (end == std::string::npos) {
-    PERFETTO_ELOG("Failed to find directory.");
-    return "";
-  }
-  return buf.substr(0, end + 1);
-}
-
 }  // namespace
 
 ProtoTranslationTable* GetTable(const std::string& name) {
@@ -56,7 +41,7 @@
     struct stat st;
     if (lstat(path.c_str(), &st) == -1 && errno == ENOENT) {
       // For OSS fuzz, which does not run in the correct cwd.
-      path = GetBinaryDirectory() + path;
+      path = base::GetTestDataPath(path);
     }
     FtraceProcfs ftrace(path);
     auto table = ProtoTranslationTable::Create(&ftrace, GetStaticEventInfo(),