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 | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 20 | |
Adam Lesinski | ea78979 | 2016-11-10 14:33:11 -0800 | [diff] [blame] | 21 | #include "android-base/logging.h" |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 22 | #include "ziparchive/zip_archive.h" |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
Adam Lesinski | ea78979 | 2016-11-10 14:33:11 -0800 | [diff] [blame] | 26 | static std::string sTestDataPath; |
| 27 | |
| 28 | void SetTestDataPath(const std::string& path) { sTestDataPath = path; } |
| 29 | |
| 30 | const std::string& GetTestDataPath() { |
| 31 | CHECK(!sTestDataPath.empty()) << "no test data path set."; |
| 32 | return sTestDataPath; |
| 33 | } |
| 34 | |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 35 | ::testing::AssertionResult ReadFileFromZipToString(const std::string& zip_path, |
| 36 | const std::string& file, |
| 37 | std::string* out_contents) { |
| 38 | out_contents->clear(); |
| 39 | ::ZipArchiveHandle handle; |
| 40 | int32_t result = OpenArchive(zip_path.c_str(), &handle); |
| 41 | if (result != 0) { |
| 42 | return ::testing::AssertionFailure() << "Failed to open zip '" << zip_path |
| 43 | << "': " << ::ErrorCodeString(result); |
| 44 | } |
| 45 | |
| 46 | ::ZipString name(file.c_str()); |
| 47 | ::ZipEntry entry; |
| 48 | result = ::FindEntry(handle, name, &entry); |
| 49 | if (result != 0) { |
| 50 | ::CloseArchive(handle); |
| 51 | return ::testing::AssertionFailure() << "Could not find file '" << file << "' in zip '" |
| 52 | << zip_path << "' : " << ::ErrorCodeString(result); |
| 53 | } |
| 54 | |
| 55 | out_contents->resize(entry.uncompressed_length); |
| 56 | result = ::ExtractToMemory( |
| 57 | handle, &entry, const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(out_contents->data())), |
| 58 | out_contents->size()); |
| 59 | if (result != 0) { |
| 60 | ::CloseArchive(handle); |
| 61 | return ::testing::AssertionFailure() << "Failed to extract file '" << file << "' from zip '" |
| 62 | << zip_path << "': " << ::ErrorCodeString(result); |
| 63 | } |
| 64 | |
| 65 | ::CloseArchive(handle); |
| 66 | return ::testing::AssertionSuccess(); |
| 67 | } |
| 68 | |
| 69 | ::testing::AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id, |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 70 | const char* expected_str) { |
| 71 | Res_value val; |
| 72 | ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG); |
| 73 | if (block < 0) { |
| 74 | return ::testing::AssertionFailure() << "could not find resource"; |
| 75 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 77 | if (val.dataType != Res_value::TYPE_STRING) { |
| 78 | return ::testing::AssertionFailure() << "resource is not a string"; |
| 79 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 81 | const ResStringPool* pool = table.getTableStringBlock(block); |
| 82 | if (pool == NULL) { |
Adam Lesinski | 4c67a47 | 2016-11-10 16:43:59 -0800 | [diff] [blame^] | 83 | return ::testing::AssertionFailure() << "table has no string pool for block " << block; |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 84 | } |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 85 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 86 | const String8 actual_str = pool->string8ObjectAt(val.data); |
| 87 | if (String8(expected_str) != actual_str) { |
| 88 | return ::testing::AssertionFailure() << actual_str.string(); |
| 89 | } |
| 90 | return ::testing::AssertionSuccess() << actual_str.string(); |
Adam Lesinski | 6029319 | 2014-10-21 18:36:42 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Adam Lesinski | 7a37b74 | 2016-10-12 14:05:55 -0700 | [diff] [blame] | 93 | } // namespace android |