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