2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
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>
26 #include "get_test_data.h"
30 static std::string testdata_dir;
32 #if defined(IN_CTS_TEST)
33 static const std::string testdata_section = ".testzipdata";
35 static bool ExtractTestDataFromElfSection() {
36 if (!MkdirWithParents(testdata_dir)) {
37 PLOG(ERROR) << "failed to create testdata_dir " << testdata_dir;
41 if (!ReadSectionFromElfFile("/proc/self/exe", testdata_section, &content)) {
42 LOG(ERROR) << "failed to read section " << testdata_section;
45 TemporaryFile tmp_file;
46 if (!android::base::WriteStringToFile(content, tmp_file.path)) {
47 PLOG(ERROR) << "failed to write file " << tmp_file.path;
50 ArchiveHelper ahelper(tmp_file.fd, tmp_file.path);
52 LOG(ERROR) << "failed to open archive " << tmp_file.path;
55 ZipArchiveHandle& handle = ahelper.archive_handle();
57 int ret = StartIteration(handle, &cookie, nullptr, nullptr);
59 LOG(ERROR) << "failed to start iterating zip entries";
62 std::unique_ptr<void, decltype(&EndIteration)> guard(cookie, EndIteration);
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;
69 if (path.back() == '/') {
72 if (!MkdirWithParents(path)) {
73 LOG(ERROR) << "failed to create dir for " << path;
76 FileHelper fhelper = FileHelper::OpenWriteOnly(path);
78 PLOG(ERROR) << "failed to create file " << path;
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;
86 if (!android::base::WriteFully(fhelper.fd(), data.data(), data.size())) {
87 LOG(ERROR) << "failed to write file " << path;
93 #endif // defined(IN_CTS_TEST)
95 int 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;
100 for (int i = 1; i < argc; ++i) {
101 if (strcmp(argv[i], "-t") == 0 && i + 1 < argc) {
102 testdata_dir = argv[i + 1];
104 } else if (strcmp(argv[i], "--log") == 0) {
107 if (!GetLogSeverity(argv[i], &log_severity)) {
108 LOG(ERROR) << "Unknown log severity: " << argv[i];
112 LOG(ERROR) << "Missing argument for --log option.\n";
117 android::base::ScopedLogSeverity severity(log_severity);
119 #if defined(IN_CTS_TEST)
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";
130 if (!::testing::GTEST_FLAG(list_tests) && testdata_dir.empty()) {
131 printf("Usage: %s -t <testdata_dir>\n", argv[0]);
134 if (testdata_dir.back() != '/') {
135 testdata_dir.push_back('/');
137 LOG(INFO) << "testdata is in " << testdata_dir;
138 return RUN_ALL_TESTS();
141 std::string GetTestData(const std::string& filename) {
142 return testdata_dir + filename;
145 const std::string& GetTestDataDir() {