blob: a81bb6ffab06bc86b25a12512193ad1795af742b [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 Lesinski4c67a472016-11-10 16:43:59 -080019#include "ziparchive/zip_archive.h"
Adam Lesinski60293192014-10-21 18:36:42 -070020
Adam Lesinski873ef0e2017-10-11 16:50:37 -070021using ::testing::AssertionFailure;
22using ::testing::AssertionResult;
23using ::testing::AssertionSuccess;
24
Adam Lesinski60293192014-10-21 18:36:42 -070025namespace android {
26
Adam Lesinski873ef0e2017-10-11 16:50:37 -070027AssertionResult ReadFileFromZipToString(const std::string& zip_path, const std::string& file,
28 std::string* out_contents) {
Adam Lesinski4c67a472016-11-10 16:43:59 -080029 out_contents->clear();
30 ::ZipArchiveHandle handle;
31 int32_t result = OpenArchive(zip_path.c_str(), &handle);
32 if (result != 0) {
Adam Lesinski873ef0e2017-10-11 16:50:37 -070033 return AssertionFailure() << "Failed to open zip '" << zip_path
34 << "': " << ::ErrorCodeString(result);
Adam Lesinski4c67a472016-11-10 16:43:59 -080035 }
36
Adam Lesinski4c67a472016-11-10 16:43:59 -080037 ::ZipEntry entry;
Elliott Hughesb97e7372019-05-03 22:42:31 -070038 result = ::FindEntry(handle, file.c_str(), &entry);
Adam Lesinski4c67a472016-11-10 16:43:59 -080039 if (result != 0) {
40 ::CloseArchive(handle);
Adam Lesinski873ef0e2017-10-11 16:50:37 -070041 return AssertionFailure() << "Could not find file '" << file << "' in zip '" << zip_path
42 << "' : " << ::ErrorCodeString(result);
Adam Lesinski4c67a472016-11-10 16:43:59 -080043 }
44
45 out_contents->resize(entry.uncompressed_length);
46 result = ::ExtractToMemory(
47 handle, &entry, const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(out_contents->data())),
48 out_contents->size());
49 if (result != 0) {
50 ::CloseArchive(handle);
Adam Lesinski873ef0e2017-10-11 16:50:37 -070051 return AssertionFailure() << "Failed to extract file '" << file << "' from zip '" << zip_path
52 << "': " << ::ErrorCodeString(result);
Adam Lesinski4c67a472016-11-10 16:43:59 -080053 }
54
55 ::CloseArchive(handle);
Adam Lesinski873ef0e2017-10-11 16:50:37 -070056 return AssertionSuccess();
Adam Lesinski4c67a472016-11-10 16:43:59 -080057}
58
Adam Lesinski873ef0e2017-10-11 16:50:37 -070059AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id,
60 const char* expected_str) {
Adam Lesinski7a37b742016-10-12 14:05:55 -070061 Res_value val;
62 ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG);
63 if (block < 0) {
Adam Lesinski873ef0e2017-10-11 16:50:37 -070064 return AssertionFailure() << "could not find resource";
Adam Lesinski7a37b742016-10-12 14:05:55 -070065 }
Adam Lesinski60293192014-10-21 18:36:42 -070066
Adam Lesinski7a37b742016-10-12 14:05:55 -070067 if (val.dataType != Res_value::TYPE_STRING) {
Adam Lesinski873ef0e2017-10-11 16:50:37 -070068 return AssertionFailure() << "resource is not a string";
Adam Lesinski7a37b742016-10-12 14:05:55 -070069 }
Adam Lesinski60293192014-10-21 18:36:42 -070070
Adam Lesinski7a37b742016-10-12 14:05:55 -070071 const ResStringPool* pool = table.getTableStringBlock(block);
72 if (pool == NULL) {
Adam Lesinski873ef0e2017-10-11 16:50:37 -070073 return AssertionFailure() << "table has no string pool for block " << block;
Adam Lesinski7a37b742016-10-12 14:05:55 -070074 }
Adam Lesinski60293192014-10-21 18:36:42 -070075
Adam Lesinski7a37b742016-10-12 14:05:55 -070076 const String8 actual_str = pool->string8ObjectAt(val.data);
77 if (String8(expected_str) != actual_str) {
Adam Lesinski873ef0e2017-10-11 16:50:37 -070078 return AssertionFailure() << actual_str.string();
Adam Lesinski7a37b742016-10-12 14:05:55 -070079 }
Adam Lesinski873ef0e2017-10-11 16:50:37 -070080 return AssertionSuccess() << actual_str.string();
Adam Lesinski7ad11102016-10-28 16:39:15 -070081}
82
Adam Lesinski7a37b742016-10-12 14:05:55 -070083} // namespace android