blob: 1e763a5e53a88aaf86da1dcbc505ee05e8c69933 [file] [log] [blame]
Adam Lesinski60293192014-10-21 18:36:42 -07001/*
Adam Lesinski7a37b742016-10-12 14:05:55 -07002 * Copyright (C) 2016 The Android Open Source Project
Adam Lesinski60293192014-10-21 18:36:42 -07003 *
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 "TestHelpers.h"
18
Adam Lesinski7ad11102016-10-28 16:39:15 -070019#include <libgen.h>
Adam Lesinski7a37b742016-10-12 14:05:55 -070020#include <unistd.h>
Adam Lesinski7a37b742016-10-12 14:05:55 -070021
Adam Lesinski7ad11102016-10-28 16:39:15 -070022#include <memory>
23#include <string>
24
25#include "android-base/file.h"
Adam Lesinskiea789792016-11-10 14:33:11 -080026#include "android-base/logging.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070027#include "android-base/strings.h"
Adam Lesinski4c67a472016-11-10 16:43:59 -080028#include "ziparchive/zip_archive.h"
Adam Lesinski60293192014-10-21 18:36:42 -070029
30namespace android {
31
Adam Lesinskiea789792016-11-10 14:33:11 -080032static std::string sTestDataPath;
33
Adam Lesinski7ad11102016-10-28 16:39:15 -070034// Extract the directory of the current executable path.
35static std::string GetExecutableDir() {
36 const std::string path = base::GetExecutablePath();
37 std::unique_ptr<char, decltype(&std::free)> mutable_path = {strdup(path.c_str()), std::free};
38 std::string executable_dir = dirname(mutable_path.get());
39 return executable_dir;
40}
41
42void InitializeTest(int* argc, char** argv) {
43 // Set the default test data path to be the executable path directory.
44 SetTestDataPath(GetExecutableDir());
45
46 for (int i = 1; i < *argc; i++) {
47 const std::string arg = argv[i];
48 if (base::StartsWith(arg, "--testdata=")) {
49 SetTestDataPath(arg.substr(strlen("--testdata=")));
50 for (int j = i; j != *argc; j++) {
51 argv[j] = argv[j + 1];
52 }
53 --(*argc);
54 --i;
55 } else if (arg == "-h" || arg == "--help") {
56 std::cerr << "\nAdditional options specific to this test:\n"
57 " --testdata=[PATH]\n"
58 " Specify the location of test data used within the tests.\n";
59 exit(1);
60 }
61 }
62}
63
Adam Lesinskiea789792016-11-10 14:33:11 -080064void SetTestDataPath(const std::string& path) { sTestDataPath = path; }
65
66const std::string& GetTestDataPath() {
67 CHECK(!sTestDataPath.empty()) << "no test data path set.";
68 return sTestDataPath;
69}
70
Adam Lesinski4c67a472016-11-10 16:43:59 -080071::testing::AssertionResult ReadFileFromZipToString(const std::string& zip_path,
72 const std::string& file,
73 std::string* out_contents) {
74 out_contents->clear();
75 ::ZipArchiveHandle handle;
76 int32_t result = OpenArchive(zip_path.c_str(), &handle);
77 if (result != 0) {
78 return ::testing::AssertionFailure() << "Failed to open zip '" << zip_path
79 << "': " << ::ErrorCodeString(result);
80 }
81
82 ::ZipString name(file.c_str());
83 ::ZipEntry entry;
84 result = ::FindEntry(handle, name, &entry);
85 if (result != 0) {
86 ::CloseArchive(handle);
87 return ::testing::AssertionFailure() << "Could not find file '" << file << "' in zip '"
88 << zip_path << "' : " << ::ErrorCodeString(result);
89 }
90
91 out_contents->resize(entry.uncompressed_length);
92 result = ::ExtractToMemory(
93 handle, &entry, const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(out_contents->data())),
94 out_contents->size());
95 if (result != 0) {
96 ::CloseArchive(handle);
97 return ::testing::AssertionFailure() << "Failed to extract file '" << file << "' from zip '"
98 << zip_path << "': " << ::ErrorCodeString(result);
99 }
100
101 ::CloseArchive(handle);
102 return ::testing::AssertionSuccess();
103}
104
105::testing::AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id,
Adam Lesinski7a37b742016-10-12 14:05:55 -0700106 const char* expected_str) {
107 Res_value val;
108 ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG);
109 if (block < 0) {
110 return ::testing::AssertionFailure() << "could not find resource";
111 }
Adam Lesinski60293192014-10-21 18:36:42 -0700112
Adam Lesinski7a37b742016-10-12 14:05:55 -0700113 if (val.dataType != Res_value::TYPE_STRING) {
114 return ::testing::AssertionFailure() << "resource is not a string";
115 }
Adam Lesinski60293192014-10-21 18:36:42 -0700116
Adam Lesinski7a37b742016-10-12 14:05:55 -0700117 const ResStringPool* pool = table.getTableStringBlock(block);
118 if (pool == NULL) {
Adam Lesinski4c67a472016-11-10 16:43:59 -0800119 return ::testing::AssertionFailure() << "table has no string pool for block " << block;
Adam Lesinski7a37b742016-10-12 14:05:55 -0700120 }
Adam Lesinski60293192014-10-21 18:36:42 -0700121
Adam Lesinski7a37b742016-10-12 14:05:55 -0700122 const String8 actual_str = pool->string8ObjectAt(val.data);
123 if (String8(expected_str) != actual_str) {
124 return ::testing::AssertionFailure() << actual_str.string();
125 }
126 return ::testing::AssertionSuccess() << actual_str.string();
Adam Lesinski60293192014-10-21 18:36:42 -0700127}
128
Adam Lesinski7ad11102016-10-28 16:39:15 -0700129std::string GetStringFromPool(const ResStringPool* pool, uint32_t idx) {
130 String8 str = pool->string8ObjectAt(idx);
131 return std::string(str.string(), str.length());
132}
133
Adam Lesinski7a37b742016-10-12 14:05:55 -0700134} // namespace android