-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathzipmerge_test.cpp
More file actions
166 lines (145 loc) · 5.24 KB
/
zipmerge_test.cpp
File metadata and controls
166 lines (145 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "misc/bazel/internal/zipmerge/zipmerge.h"
#include <array>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring>
#include <filesystem>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "rules_cc/cc/runfiles/runfiles.h"
using rules_cc::cc::runfiles::Runfiles;
using namespace std::string_literals;
namespace fs = std::filesystem;
namespace codeql_testing {
TEST(Zipmerge, ReadAndWrite) {
char buf[7] = {0};
write2(buf + 1, 0xF2F1U);
write4(buf + 3, 0xF6F5F4F3UL);
EXPECT_STREQ(buf, "\x00\xF1\xF2\xF3\xF4\xF5\xF6");
EXPECT_EQ(read2(buf + 1), 0xF2F1U);
EXPECT_EQ(read4(buf + 3), 0xF6F5F4F3UL);
}
TEST(Zipmerge, AppendCd) {
output_cd.length = 0;
append_cd((const uint8_t*)"a", 1);
append_cd((const uint8_t*)"bcd", 3);
append_cd((const uint8_t*)"efghijklmno", 11);
EXPECT_EQ(output_cd.length, 15);
std::string_view bytes{reinterpret_cast<char*>(output_cd.bytes), 15};
EXPECT_EQ(bytes, "abcdefghijklmno");
}
TEST(Zipmerge, ShouldIncludeFilenameNow) {
EXPECT_TRUE(should_include_filename_now((const uint8_t*)"x", 1));
EXPECT_FALSE(should_include_filename_now((const uint8_t*)"x", 1));
EXPECT_TRUE(should_include_filename_now((const uint8_t*)"y", 1));
EXPECT_TRUE(should_include_filename_now((const uint8_t*)"yy", 2));
EXPECT_FALSE(should_include_filename_now((const uint8_t*)"x", 1));
EXPECT_FALSE(should_include_filename_now((const uint8_t*)"yy", 2));
}
TEST(Zipmerge, FindEocd) {
uint8_t buf[500] = {0};
auto i = 0u;
for (auto& b : buf) {
b = i % 256;
}
memcpy(buf + 17, eocd_signature.data(), eocd_signature.size());
memcpy(buf + 101, eocd_signature.data(), eocd_signature.size());
EXPECT_EQ(find_eocd(buf, sizeof(buf)), buf + 101);
}
std::string read_file(const std::string& filename) {
std::ifstream f(filename, std::ios::binary);
EXPECT_TRUE(f) << "Could not open '" << filename << "' (" << std::strerror(errno) << ")";
if (!f) {
return 0;
}
std::stringstream contents;
contents << f.rdbuf();
return contents.str();
}
std::string get_file(const char* name) {
static auto runfiles = [] {
std::string error;
auto ret = Runfiles::CreateForTest(&error);
EXPECT_TRUE(ret) << error;
return ret;
}();
// this works from both `codeql` and the internal repository
for (auto prefix : {"_main", "ql+"}) {
auto ret = runfiles->Rlocation(prefix + "/misc/bazel/internal/zipmerge/test-files/"s + name);
if (fs::exists(ret)) {
return ret;
}
}
EXPECT_TRUE(false) << "test file " << name << " not found";
return "";
}
void expect_same_file(const char* actual, const char* expected) {
auto expected_file = get_file(expected);
auto actual_contents = read_file(actual);
unlink(actual); // If tests start failing, you might want to comment out this unlink in order to
// inspect the output.
ASSERT_EQ(actual_contents, read_file(expected_file))
<< "contents of " << actual << " do not match contents of " << expected_file;
}
template <typename... Args>
const char* zipmerge(Args*... inputs) {
reset();
const char* output = nullptr;
std::vector<std::string> args{"self"};
std::array<const char*, sizeof...(Args)> flags{{inputs...}};
auto i = 0u;
for (; i < flags.size() && std::string_view{flags[i]}.starts_with("-"); ++i) {
args.push_back(flags[i]);
}
output = flags[i];
args.push_back(output);
++i;
for (; i < flags.size(); ++i) {
args.push_back(std::string_view{flags[i]}.starts_with("-") ? flags[i] : get_file(flags[i]));
}
std::vector<const char*> argv;
std::transform(args.begin(), args.end(), std::back_inserter(argv),
[](const std::string& s) { return s.c_str(); });
EXPECT_EQ(zipmerge_main(argv.size(), argv.data()), 0);
return output;
}
TEST(Zipmerge, Identity) {
expect_same_file(zipmerge("out.zip", "directory.zip"), "directory.zip");
}
TEST(Zipmerge, Idempotent) {
expect_same_file(zipmerge("out.zip", "directory.zip", "directory.zip", "directory.zip"),
"directory.zip");
}
TEST(Zipmerge, RemoveEverything) {
expect_same_file(zipmerge("--remove=directory", "out.zip", "directory.zip"), "empty.zip");
}
TEST(Zipmerge, RemoveEverythingWildcard) {
expect_same_file(zipmerge("--remove=*ory", "out.zip", "directory.zip"), "empty.zip");
}
TEST(Zipmerge, RemovePrefixedPaths) {
expect_same_file(zipmerge("--remove=My/directory", "out.zip", "--prefix=My", "directory.zip"),
"empty.zip");
}
TEST(Zipmerge, RemoveSome) {
expect_same_file(
zipmerge("--remove=directory/b.txt", "--remove=directory/c.txt", "out.zip", "directory.zip"),
"directory-partial.zip");
}
TEST(Zipmerge, RemoveSomeWildcard) {
expect_same_file(zipmerge("--remove=directory/b*t", "--remove=directory/c*", "--remove=dir*t",
"out.zip", "directory.zip"),
"directory-partial.zip");
}
TEST(Zipmerge, Prefix) {
expect_same_file(
zipmerge("out.zip", "minimal.zip", "--prefix=a", "minimal.zip", "--prefix=b", "minimal.zip"),
"minimal-x3.zip");
}
TEST(Zipmerge, InputFileOrder) {
expect_same_file(zipmerge("out.zip", "minimal.zip", "almost-minimal.zip"), "almost-minimal.zip");
}
TEST(Zipmerge, LocalFileFooters) {
expect_same_file(zipmerge("out.jar", "footers.jar"), "no-footers.jar");
}
} // namespace codeql_testing