blob: 79be43ce0e42d5d8197f3c8e2d878dd62f2996ff [file] [log] [blame]
Mårten Kongstad02751232018-04-27 13:16:32 +02001/*
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 Kongstad02751232018-04-27 13:16:32 +020019
Mårten Kongstad0f763112018-11-19 14:14:37 +010020#include "idmap2/Result.h"
Mårten Kongstad02751232018-04-27 13:16:32 +020021#include "idmap2/ZipFile.h"
22
23#include "gmock/gmock.h"
24#include "gtest/gtest.h"
25
26#include "TestHelpers.h"
27
28using ::testing::IsNull;
29using ::testing::NotNull;
30
Mårten Kongstad0eba72a2018-11-29 08:23:14 +010031namespace android::idmap2 {
Mårten Kongstad02751232018-04-27 13:16:32 +020032
33TEST(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
42TEST(ZipFileTests, Crc) {
43 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
44 ASSERT_THAT(zip, NotNull());
45
Mårten Kongstad0f763112018-11-19 14:14:37 +010046 Result<uint32_t> crc = zip->Crc("AndroidManifest.xml");
47 ASSERT_TRUE(crc);
48 ASSERT_EQ(*crc, 0x762f3d24);
Mårten Kongstad02751232018-04-27 13:16:32 +020049
Mårten Kongstad0f763112018-11-19 14:14:37 +010050 Result<uint32_t> crc2 = zip->Crc("does-not-exist");
51 ASSERT_FALSE(crc2);
Mårten Kongstad02751232018-04-27 13:16:32 +020052}
53
54TEST(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 Kongstad0eba72a2018-11-29 08:23:14 +010068} // namespace android::idmap2