1 /*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 <cstring>
18
19 #include "bcc_zip.h"
20 #include "catch.hpp"
21
22 #define LIB_ENTRY_NAME "libdebuginfo_test_lib.so"
23 #define ENTRY_IN_SUBDIR_NAME "zip_subdir/file.txt"
24 #define NOT_AN_ARCHIVE_PATH CMAKE_CURRENT_BINARY_DIR "/dummy_proc_map.txt"
25 #define TEST_ARCHIVE_PATH CMAKE_CURRENT_BINARY_DIR "/archive.zip"
26
27 namespace {
28
require_entry_name_is(const bcc_zip_entry & entry,const char * name)29 void require_entry_name_is(const bcc_zip_entry& entry, const char* name) {
30 REQUIRE(entry.name_length == strlen(name));
31 REQUIRE(memcmp(entry.name, name, strlen(name)) == 0);
32 }
33
get_required_entry(bcc_zip_archive * archive,const char * asset_name)34 bcc_zip_entry get_required_entry(bcc_zip_archive* archive,
35 const char* asset_name) {
36 bcc_zip_entry out;
37 REQUIRE(bcc_zip_archive_find_entry(archive, asset_name, &out) == 0);
38 require_entry_name_is(out, asset_name);
39 return out;
40 }
41
get_uncompressed_data(const bcc_zip_entry & entry)42 const void* get_uncompressed_data(const bcc_zip_entry& entry) {
43 REQUIRE(entry.compression == 0);
44 REQUIRE(entry.data_offset > 0);
45 REQUIRE(entry.data != nullptr);
46 return entry.data;
47 }
48
49 } // namespace
50
51 TEST_CASE("returns error for non-zip files", "[zip]") {
52 bcc_zip_archive* archive = bcc_zip_archive_open(NOT_AN_ARCHIVE_PATH);
53 REQUIRE(archive == nullptr);
54 }
55
56 TEST_CASE("finds entries in a zip archive by name", "[zip]") {
57 bcc_zip_archive* archive = bcc_zip_archive_open(TEST_ARCHIVE_PATH);
58 REQUIRE(archive != nullptr);
59
60 bcc_zip_entry entry = get_required_entry(archive, LIB_ENTRY_NAME);
61 REQUIRE(memcmp(get_uncompressed_data(entry),
62 "\x7f"
63 "ELF",
64 4) == 0);
65
66 entry = get_required_entry(archive, ENTRY_IN_SUBDIR_NAME);
67 REQUIRE(memcmp(get_uncompressed_data(entry), "This is a text file\n", 20) ==
68 0);
69
70 REQUIRE(bcc_zip_archive_find_entry(archive, "missing", &entry) == -1);
71
72 bcc_zip_archive_close(archive);
73 }
74
75 TEST_CASE("finds entries in a zip archive by offset", "[zip]") {
76 bcc_zip_archive* archive = bcc_zip_archive_open(TEST_ARCHIVE_PATH);
77 REQUIRE(archive != nullptr);
78
79 bcc_zip_entry entry;
80 REQUIRE(bcc_zip_archive_find_entry_at_offset(archive, 100, &entry) == 0);
81 require_entry_name_is(entry, LIB_ENTRY_NAME);
82 REQUIRE(memcmp(get_uncompressed_data(entry),
83 "\x7f"
84 "ELF",
85 4) == 0);
86
87 REQUIRE(bcc_zip_archive_find_entry_at_offset(archive, 100000, &entry) == -1);
88
89 bcc_zip_archive_close(archive);
90 }
91
92 TEST_CASE("open zip archive and finds an entry", "[zip]") {
93 bcc_zip_entry entry;
94 bcc_zip_archive* archive = bcc_zip_archive_open_and_find(
95 TEST_ARCHIVE_PATH "!/" LIB_ENTRY_NAME, &entry);
96 REQUIRE(archive != nullptr);
97 require_entry_name_is(entry, LIB_ENTRY_NAME);
98 REQUIRE(memcmp(get_uncompressed_data(entry),
99 "\x7f"
100 "ELF",
101 4) == 0);
102 bcc_zip_archive_close(archive);
103
104 archive = bcc_zip_archive_open_and_find(
105 TEST_ARCHIVE_PATH "!/" ENTRY_IN_SUBDIR_NAME, &entry);
106 REQUIRE(archive != nullptr);
107 require_entry_name_is(entry, ENTRY_IN_SUBDIR_NAME);
108 REQUIRE(memcmp(get_uncompressed_data(entry), "This is a text file\n", 20) ==
109 0);
110 bcc_zip_archive_close(archive);
111
112 archive =
113 bcc_zip_archive_open_and_find(TEST_ARCHIVE_PATH "!/NOT_FOUND", &entry);
114 REQUIRE(archive == nullptr);
115 }
116