1 /*
2 * Copyright (C) 2016 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 "TestHelpers.h"
18
19 #include "ziparchive/zip_archive.h"
20
21 using ::testing::AssertionFailure;
22 using ::testing::AssertionResult;
23 using ::testing::AssertionSuccess;
24
25 namespace android {
26
ReadFileFromZipToString(const std::string & zip_path,const std::string & file,std::string * out_contents)27 AssertionResult ReadFileFromZipToString(const std::string& zip_path, const std::string& file,
28 std::string* out_contents) {
29 out_contents->clear();
30 ::ZipArchiveHandle handle;
31 int32_t result = OpenArchive(zip_path.c_str(), &handle);
32 if (result != 0) {
33 return AssertionFailure() << "Failed to open zip '" << zip_path
34 << "': " << ::ErrorCodeString(result);
35 }
36
37 ::ZipString name(file.c_str());
38 ::ZipEntry entry;
39 result = ::FindEntry(handle, name, &entry);
40 if (result != 0) {
41 ::CloseArchive(handle);
42 return AssertionFailure() << "Could not find file '" << file << "' in zip '" << zip_path
43 << "' : " << ::ErrorCodeString(result);
44 }
45
46 out_contents->resize(entry.uncompressed_length);
47 result = ::ExtractToMemory(
48 handle, &entry, const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(out_contents->data())),
49 out_contents->size());
50 if (result != 0) {
51 ::CloseArchive(handle);
52 return AssertionFailure() << "Failed to extract file '" << file << "' from zip '" << zip_path
53 << "': " << ::ErrorCodeString(result);
54 }
55
56 ::CloseArchive(handle);
57 return AssertionSuccess();
58 }
59
IsStringEqual(const ResTable & table,uint32_t resource_id,const char * expected_str)60 AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id,
61 const char* expected_str) {
62 Res_value val;
63 ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG);
64 if (block < 0) {
65 return AssertionFailure() << "could not find resource";
66 }
67
68 if (val.dataType != Res_value::TYPE_STRING) {
69 return AssertionFailure() << "resource is not a string";
70 }
71
72 const ResStringPool* pool = table.getTableStringBlock(block);
73 if (pool == NULL) {
74 return AssertionFailure() << "table has no string pool for block " << block;
75 }
76
77 const String8 actual_str = pool->string8ObjectAt(val.data);
78 if (String8(expected_str) != actual_str) {
79 return AssertionFailure() << actual_str.string();
80 }
81 return AssertionSuccess() << actual_str.string();
82 }
83
84 } // namespace android
85