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> |
| 19 | #include <utility> |
| 20 | |
| 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 | |
| 31 | namespace android { |
| 32 | namespace idmap2 { |
| 33 | |
| 34 | TEST(ZipFileTests, BasicOpen) { |
| 35 | auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk"); |
| 36 | ASSERT_THAT(zip, NotNull()); |
| 37 | |
| 38 | fclose(stderr); // silence expected warnings from libziparchive |
| 39 | auto fail = ZipFile::Open(GetTestDataPath() + "/does-not-exist"); |
| 40 | ASSERT_THAT(fail, IsNull()); |
| 41 | } |
| 42 | |
| 43 | TEST(ZipFileTests, Crc) { |
| 44 | auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk"); |
| 45 | ASSERT_THAT(zip, NotNull()); |
| 46 | |
| 47 | bool status; |
| 48 | uint32_t crc; |
| 49 | std::tie(status, crc) = zip->Crc("AndroidManifest.xml"); |
| 50 | ASSERT_TRUE(status); |
| 51 | ASSERT_EQ(crc, 0x762f3d24); |
| 52 | |
| 53 | std::tie(status, std::ignore) = zip->Crc("does-not-exist"); |
| 54 | ASSERT_FALSE(status); |
| 55 | } |
| 56 | |
| 57 | TEST(ZipFileTests, Uncompress) { |
| 58 | auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk"); |
| 59 | ASSERT_THAT(zip, NotNull()); |
| 60 | |
| 61 | auto data = zip->Uncompress("assets/lorem-ipsum.txt"); |
| 62 | ASSERT_THAT(data, NotNull()); |
| 63 | const std::string lorem_ipsum("Lorem ipsum dolor sit amet.\n"); |
| 64 | ASSERT_THAT(data->size, lorem_ipsum.size()); |
| 65 | ASSERT_THAT(std::string(reinterpret_cast<const char*>(data->buf), data->size), lorem_ipsum); |
| 66 | |
| 67 | auto fail = zip->Uncompress("does-not-exist"); |
| 68 | ASSERT_THAT(fail, IsNull()); |
| 69 | } |
| 70 | |
| 71 | } // namespace idmap2 |
| 72 | } // namespace android |