blob: 6e4a501a51ac940f51d668854a79dae5a178396e [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
31namespace android {
32namespace idmap2 {
33
34TEST(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
43TEST(ZipFileTests, Crc) {
44 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
45 ASSERT_THAT(zip, NotNull());
46
Mårten Kongstad0f763112018-11-19 14:14:37 +010047 Result<uint32_t> crc = zip->Crc("AndroidManifest.xml");
48 ASSERT_TRUE(crc);
49 ASSERT_EQ(*crc, 0x762f3d24);
Mårten Kongstad02751232018-04-27 13:16:32 +020050
Mårten Kongstad0f763112018-11-19 14:14:37 +010051 Result<uint32_t> crc2 = zip->Crc("does-not-exist");
52 ASSERT_FALSE(crc2);
Mårten Kongstad02751232018-04-27 13:16:32 +020053}
54
55TEST(ZipFileTests, Uncompress) {
56 auto zip = ZipFile::Open(GetTestDataPath() + "/target/target.apk");
57 ASSERT_THAT(zip, NotNull());
58
59 auto data = zip->Uncompress("assets/lorem-ipsum.txt");
60 ASSERT_THAT(data, NotNull());
61 const std::string lorem_ipsum("Lorem ipsum dolor sit amet.\n");
62 ASSERT_THAT(data->size, lorem_ipsum.size());
63 ASSERT_THAT(std::string(reinterpret_cast<const char*>(data->buf), data->size), lorem_ipsum);
64
65 auto fail = zip->Uncompress("does-not-exist");
66 ASSERT_THAT(fail, IsNull());
67}
68
69} // namespace idmap2
70} // namespace android