gtest_main.cpp revision 8f680f60dc800bec880c5c35bfbc1ac36165e1f3
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <memory>
20
21#include <android-base/file.h>
22#include <android-base/logging.h>
23#include <android-base/test_utils.h>
24#include <ziparchive/zip_archive.h>
25
26#include "get_test_data.h"
27#include "read_elf.h"
28#include "utils.h"
29
30static std::string testdata_dir;
31
32#if defined(__ANDROID__)
33static const std::string testdata_section = ".testzipdata";
34
35static bool ExtractTestDataFromElfSection() {
36  if (!MkdirWithParents(testdata_dir)) {
37    PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir;
38    return false;
39  }
40  std::string content;
41  if (!ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content)) {
42    LOG(ERROR) << "failed to read section " << testdata_section;
43    return false;
44  }
45  TemporaryFile tmp_file;
46  if (!android::base::WriteStringToFile(content, tmp_file.path)) {
47    PLOG(ERROR) << "failed to write file " << tmp_file.path;
48    return false;
49  }
50  ArchiveHelper ahelper(tmp_file.fd, tmp_file.path);
51  if (!ahelper) {
52    LOG(ERROR) << "failed to open archive " << tmp_file.path;
53    return false;
54  }
55  ZipArchiveHandle& handle = ahelper.archive_handle();
56  void* cookie;
57  int ret = StartIteration(handle, &cookie, nullptr, nullptr);
58  if (ret != 0) {
59    LOG(ERROR) << "failed to start iterating zip entries";
60    return false;
61  }
62  std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
63  ZipEntry entry;
64  ZipString name;
65  while (Next(cookie, &entry, &name) == 0) {
66    std::string entry_name(name.name, name.name + name.name_length);
67    std::string path = testdata_dir + entry_name;
68    // Skip dir.
69    if (path.back() == '/') {
70      continue;
71    }
72    if (!MkdirWithParents(path)) {
73      LOG(ERROR) << "failed to create dir for " << path;
74      return false;
75    }
76    FileHelper fhelper = FileHelper::OpenWriteOnly(path);
77    if (!fhelper) {
78      PLOG(ERROR) << "failed to create file " << path;
79      return false;
80    }
81    std::vector<uint8_t> data(entry.uncompressed_length);
82    if (ExtractToMemory(handle, &entry, data.data(), data.size()) != 0) {
83      LOG(ERROR) << "failed to extract entry " << entry_name;
84      return false;
85    }
86    if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) {
87      LOG(ERROR) << "failed to write file " << path;
88      return false;
89    }
90  }
91  return true;
92}
93#endif  // defined(__ANDROID__)
94
95int main(int argc, char** argv) {
96  InitLogging(argv, android::base::StderrLogger);
97  testing::InitGoogleTest(&argc, argv);
98  android::base::LogSeverity log_severity = android::base::WARNING;
99
100  for (int i = 1; i < argc; ++i) {
101    if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
102      testdata_dir = argv[i + 1];
103      i++;
104    } else if (strcmp(argv[i], "--log") == 0) {
105      if (i + 1 < argc) {
106        ++i;
107        if (!GetLogSeverity(argv[i], &log_severity)) {
108          LOG(ERROR) << "Unknown log severity: " << argv[i];
109          return 1;
110        }
111      } else {
112        LOG(ERROR) << "Missing argument for --log option.\n";
113        return 1;
114      }
115    }
116  }
117  android::base::ScopedLogSeverity severity(log_severity);
118
119#if defined(__ANDROID__)
120  std::unique_ptr<TemporaryDir> tmp_dir;
121  if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
122    tmp_dir.reset(new TemporaryDir);
123    testdata_dir = std::string(tmp_dir->path) + "/";
124    if (!ExtractTestDataFromElfSection()) {
125      LOG(ERROR) << "failed to extract test data from elf section";
126      return 1;
127    }
128  }
129#endif
130
131  if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
132    printf("Usage: %s -t <testdata_dir>\n", argv[0]);
133    return 1;
134  }
135  if (testdata_dir.back() != '/') {
136    testdata_dir.push_back('/');
137  }
138  LOG(INFO) << "testdata is in " << testdata_dir;
139  return RUN_ALL_TESTS();
140}
141
142std::string GetTestData(const std::string& filename) {
143  return testdata_dir + filename;
144}
145
146const std::string& GetTestDataDir() {
147  return testdata_dir;
148}
149
150bool IsRoot() {
151  static int is_root = -1;
152  if (is_root == -1) {
153#if defined(__linux__)
154    is_root = (getuid() == 0) ? 1 : 0;
155#else
156    is_root = 0;
157#endif
158  }
159  return is_root == 1;
160}
161