Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 <cstdio> // fclose |
| 18 | #include <string> |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 19 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 20 | #include "idmap2/Result.h" |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 21 | #include "idmap2/ZipFile.h" |
| 22 | |
| 23 | #include "gmock/gmock.h" |
| 24 | #include "gtest/gtest.h" |
| 25 | |
| 26 | #include "TestHelpers.h" |
| 27 | |
| 28 | using ::testing::IsNull; |
| 29 | using ::testing::NotNull; |
| 30 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 31 | namespace android::idmap2 { |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 32 | |
| 33 | TEST(ZipFileTests, BasicOpen) { |
| 34 | auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk"); |
| 35 | ASSERT_THAT(zip, NotNull()); |
| 36 | |
| 37 | fclose(stderr); // silence expected warnings from libziparchive |
| 38 | auto fail = ZipFile::Open(GetTestDataPath() + "/does-not-exist"); |
| 39 | ASSERT_THAT(fail, IsNull()); |
| 40 | } |
| 41 | |
| 42 | TEST(ZipFileTests, Crc) { |
| 43 | auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk"); |
| 44 | ASSERT_THAT(zip, NotNull()); |
| 45 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 46 | Result<uint32_t> crc = zip->Crc("AndroidManifest.xml"); |
| 47 | ASSERT_TRUE(crc); |
| 48 | ASSERT_EQ(*crc, 0x762f3d24); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 49 | |
Mårten Kongstad | 0f76311 | 2018-11-19 14:14:37 +0100 | [diff] [blame] | 50 | Result<uint32_t> crc2 = zip->Crc("does-not-exist"); |
| 51 | ASSERT_FALSE(crc2); |
Mårten Kongstad | 0275123 | 2018-04-27 13:16:32 +0200 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(ZipFileTests, Uncompress) { |
| 55 | auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk"); |
| 56 | ASSERT_THAT(zip, NotNull()); |
| 57 | |
| 58 | auto data = zip->Uncompress("assets/lorem-ipsum.txt"); |
| 59 | ASSERT_THAT(data, NotNull()); |
| 60 | const std::string lorem_ipsum("Lorem ipsum dolor sit amet.\n"); |
| 61 | ASSERT_THAT(data->size, lorem_ipsum.size()); |
| 62 | ASSERT_THAT(std::string(reinterpret_cast<const char*>(data->buf), data->size), lorem_ipsum); |
| 63 | |
| 64 | auto fail = zip->Uncompress("does-not-exist"); |
| 65 | ASSERT_THAT(fail, IsNull()); |
| 66 | } |
| 67 | |
Mårten Kongstad | 0eba72a | 2018-11-29 08:23:14 +0100 | [diff] [blame] | 68 | } // namespace android::idmap2 |