Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 1 | /* |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 19 | #include "ziparchive/zip_archive.h" |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 21 | using ::testing::AssertionFailure; |
| 22 | using ::testing::AssertionResult; |
| 23 | using ::testing::AssertionSuccess; |
| 24 | |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 25 | namespace android { |
| 26 | |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 27 | AssertionResult ReadFileFromZipToString(const std::string& zip_path, const std::string& file, |
| 28 | std::string* out_contents) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 29 | out_contents->clear(); |
| 30 | ::ZipArchiveHandle handle; |
| 31 | int32_t result = OpenArchive(zip_path.c_str(), &handle); |
| 32 | if (result != 0) { |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 33 | return AssertionFailure() << "Failed to open zip '" << zip_path |
| 34 | << "': " << ::ErrorCodeString(result); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 37 | ::ZipEntry entry; |
Elliott Hughes | b97e737 | 2019-05-03 22:42:31 -0700 | [diff] [blame] | 38 | result = ::FindEntry(handle, file.c_str(), &entry); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 39 | if (result != 0) { |
| 40 | ::CloseArchive(handle); |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 41 | return AssertionFailure() << "Could not find file '" << file << "' in zip '" << zip_path |
| 42 | << "' : " << ::ErrorCodeString(result); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 43 | } |
| 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 Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 51 | return AssertionFailure() << "Failed to extract file '" << file << "' from zip '" << zip_path |
| 52 | << "': " << ::ErrorCodeString(result); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | ::CloseArchive(handle); |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 56 | return AssertionSuccess(); |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 59 | AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id, |
| 60 | const char* expected_str) { |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 61 | Res_value val; |
| 62 | ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG); |
| 63 | if (block < 0) { |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 64 | return AssertionFailure() << "could not find resource"; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 65 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 67 | if (val.dataType != Res_value::TYPE_STRING) { |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 68 | return AssertionFailure() << "resource is not a string"; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 69 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 71 | const ResStringPool* pool = table.getTableStringBlock(block); |
| 72 | if (pool == NULL) { |
Adam Lesinski | 873ef0e | 2017-10-11 16:50:37 -0700 | [diff] [blame] | 73 | return AssertionFailure() << "table has no string pool for block " << block; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 74 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 75 | |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 76 | auto actual_str = pool->string8ObjectAt(val.data); |
| 77 | if (!actual_str.has_value()) { |
| 78 | return AssertionFailure() << "could not find string entry"; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 79 | } |
Ryan Mitchell | db21f09a | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 80 | |
| 81 | if (String8(expected_str) != *actual_str) { |
| 82 | return AssertionFailure() << actual_str->string(); |
| 83 | } |
| 84 | return AssertionSuccess() << actual_str->string(); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 87 | } // namespace android |