summaryrefslogtreecommitdiff
path: root/libs/androidfw/tests/TestHelpers.cpp
diff options
context:
space:
mode:
authorAdam Lesinski <adamlesinski@google.com>2016-11-10 16:43:59 -0800
committerAdam Lesinski <adamlesinski@google.com>2016-12-05 19:03:47 -0800
commit4c67a475a334e4f65238d439a3339195e03c03be (patch)
treea853680b90b834cb0a81be09f77be8d0ca8a06da /libs/androidfw/tests/TestHelpers.cpp
parent7dbbf956f73012ad0ea50bba37a21555cccfc726 (diff)
Make tests use APKs instead of exploded APKs
Tests would expect parts of the APK to be unzipped and maintained. Instead, we now decompress the required files from the test APKs on test setup. This simplifies test maintenance substantially. Test: make libandroidfw_tests && libandroidfw_tests --testdata=frameworks/base/libs/androidfw/tests/data Change-Id: I3d2100af22df913e02401dedcf9842cdb32b2a3b
Diffstat (limited to 'libs/androidfw/tests/TestHelpers.cpp')
-rw-r--r--libs/androidfw/tests/TestHelpers.cpp41
1 files changed, 37 insertions, 4 deletions
diff --git a/libs/androidfw/tests/TestHelpers.cpp b/libs/androidfw/tests/TestHelpers.cpp
index 702ee5c9d4ad..2c834b14127a 100644
--- a/libs/androidfw/tests/TestHelpers.cpp
+++ b/libs/androidfw/tests/TestHelpers.cpp
@@ -19,6 +19,7 @@
#include <unistd.h>
#include "android-base/logging.h"
+#include "ziparchive/zip_archive.h"
namespace android {
@@ -31,8 +32,41 @@ const std::string& GetTestDataPath() {
return sTestDataPath;
}
-::testing::AssertionResult IsStringEqual(const ResTable& table,
- uint32_t resource_id,
+::testing::AssertionResult ReadFileFromZipToString(const std::string& zip_path,
+ const std::string& file,
+ std::string* out_contents) {
+ out_contents->clear();
+ ::ZipArchiveHandle handle;
+ int32_t result = OpenArchive(zip_path.c_str(), &handle);
+ if (result != 0) {
+ return ::testing::AssertionFailure() << "Failed to open zip '" << zip_path
+ << "': " << ::ErrorCodeString(result);
+ }
+
+ ::ZipString name(file.c_str());
+ ::ZipEntry entry;
+ result = ::FindEntry(handle, name, &entry);
+ if (result != 0) {
+ ::CloseArchive(handle);
+ return ::testing::AssertionFailure() << "Could not find file '" << file << "' in zip '"
+ << zip_path << "' : " << ::ErrorCodeString(result);
+ }
+
+ out_contents->resize(entry.uncompressed_length);
+ result = ::ExtractToMemory(
+ handle, &entry, const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(out_contents->data())),
+ out_contents->size());
+ if (result != 0) {
+ ::CloseArchive(handle);
+ return ::testing::AssertionFailure() << "Failed to extract file '" << file << "' from zip '"
+ << zip_path << "': " << ::ErrorCodeString(result);
+ }
+
+ ::CloseArchive(handle);
+ return ::testing::AssertionSuccess();
+}
+
+::testing::AssertionResult IsStringEqual(const ResTable& table, uint32_t resource_id,
const char* expected_str) {
Res_value val;
ssize_t block = table.getResource(resource_id, &val, MAY_NOT_BE_BAG);
@@ -46,8 +80,7 @@ const std::string& GetTestDataPath() {
const ResStringPool* pool = table.getTableStringBlock(block);
if (pool == NULL) {
- return ::testing::AssertionFailure()
- << "table has no string pool for block " << block;
+ return ::testing::AssertionFailure() << "table has no string pool for block " << block;
}
const String8 actual_str = pool->string8ObjectAt(val.data);