/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/base/test/utils.h"

#include <stdlib.h>

#include "perfetto/base/build_config.h"
#include "perfetto/base/logging.h"

#if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) ||   \
    PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
    PERFETTO_BUILDFLAG(PERFETTO_OS_MACOSX) ||  \
    PERFETTO_BUILDFLAG(PERFETTO_OS_FUCHSIA)
#include <limits.h>
#include <unistd.h>
#endif

namespace perfetto {
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)
  char buf[PATH_MAX];
  ssize_t bytes = readlink("/proc/self/exe", buf, sizeof(buf));
  PERFETTO_CHECK(bytes != -1);
  // readlink does not null terminate.
  buf[bytes] = 0;
  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
  // TODO(hjd): Implement on MacOS/Windows
  // Fall back to relative to root dir.
  return "test/data/" + path;
#endif
}

}  // namespace base
}  // namespace perfetto