1 /*
2 * Copyright (C) 2016 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 <errno.h>
18 #include <unistd.h>
19
20 #include <memory>
21 #include <vector>
22
23 #include <android-base/file.h>
24 #include <android-base/test_utils.h>
25 #include <gtest/gtest.h>
26 #include <otautil/SysUtil.h>
27 #include <ziparchive/zip_archive.h>
28
29 #include "common/test_constants.h"
30
TEST(ZipTest,OpenFromMemory)31 TEST(ZipTest, OpenFromMemory) {
32 std::string zip_path = from_testdata_base("ziptest_dummy-update.zip");
33 MemMapping map;
34 ASSERT_TRUE(map.MapFile(zip_path));
35
36 // Map an update package into memory and open the archive from there.
37 ZipArchiveHandle handle;
38 ASSERT_EQ(0, OpenArchiveFromMemory(map.addr, map.length, zip_path.c_str(), &handle));
39
40 static constexpr const char* BINARY_PATH = "META-INF/com/google/android/update-binary";
41 ZipString binary_path(BINARY_PATH);
42 ZipEntry binary_entry;
43 // Make sure the package opens correctly and its entry can be read.
44 ASSERT_EQ(0, FindEntry(handle, binary_path, &binary_entry));
45
46 TemporaryFile tmp_binary;
47 ASSERT_NE(-1, tmp_binary.fd);
48 ASSERT_EQ(0, ExtractEntryToFile(handle, &binary_entry, tmp_binary.fd));
49
50 CloseArchive(handle);
51 }
52
53