-
Notifications
You must be signed in to change notification settings - Fork 124
/
memory_zip_test.cpp
217 lines (158 loc) · 8.27 KB
/
memory_zip_test.cpp
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "catch.hpp"
#include <zipper/zipper.h>
#include <zipper/unzipper.h>
#include <zipper/tools.h>
#include <vector>
#include <fstream>
#include <ostream>
#include <string>
using namespace zipper;
SCENARIO("zip vector feed with different inputs", "[zip]")
{
GIVEN("A Zip outputed to a vector")
{
std::vector<unsigned char> zipvec;
zipper::Zipper zipper(zipvec);
WHEN("a file containing 'test file compression' is added and named 'test1'")
{
std::ofstream test1("test1.txt");
test1 << "test file compression";
test1.flush();
test1.close();
std::ifstream test1stream("test1.txt");
zipper.add(test1stream, "test1.txt");
test1stream.close();
zipper.close();
std::remove("test1.txt");
zipper::Unzipper unzipper(zipvec);
THEN("the zip vector has one entry named 'test1.txt'")
{
REQUIRE(unzipper.entries().size() == 1);
REQUIRE(unzipper.entries().front().name == "test1.txt");
AND_THEN("extracting the test1.txt entry creates a file named 'test1.txt' with the text 'test file compression'")
{
unzipper.extractEntry("test1.txt");
// due to sections forking or creating different stacks we need to make sure the local instance is closed to
// prevent mixing the closing when both instances are freed at the end of the scope
unzipper.close();
REQUIRE(checkFileExists("test1.txt"));
std::ifstream testfile("test1.txt");
REQUIRE(testfile.good());
std::string test((std::istreambuf_iterator<char>(testfile)), std::istreambuf_iterator<char>());
testfile.close();
REQUIRE(test == "test file compression");
AND_WHEN("another file containing 'other data to compression test' and named 'test2.dat' is added inside a folder 'TestFolder'")
{
std::ofstream test2("test2.dat");
test2 << "other data to compression test";
test2.flush();
test2.close();
std::ifstream test2stream("test2.dat");
zipper.open();
zipper.add(test2stream, "TestFolder/test2.dat");
zipper.close();
test2stream.close();
std::remove("test2.dat");
zipper::Unzipper unzipper(zipvec);
THEN("the zip vector has two entries named 'test1.txt' and 'TestFolder/test2.dat'")
{
REQUIRE(unzipper.entries().size() == 2);
REQUIRE(unzipper.entries().front().name == "test1.txt");
REQUIRE(unzipper.entries()[1].name == "TestFolder/test2.dat");
AND_THEN("extracting the test2.dat entry creates a folder 'TestFolder' with a file named 'test2.dat' with the text 'other data to compression test'")
{
unzipper.extract();
REQUIRE(checkFileExists("TestFolder/test2.dat"));
std::ifstream testfile("TestFolder/test2.dat");
REQUIRE(testfile.good());
std::string test((std::istreambuf_iterator<char>(testfile)), std::istreambuf_iterator<char>());
testfile.close();
REQUIRE(test == "other data to compression test");
AND_THEN("extracting the test2.dat entry to memory fills a vector with 'other data to compression test'")
{
std::vector<unsigned char> resvec;
unzipper.extractEntryToMemory("TestFolder/test2.dat", resvec);
unzipper.close();
std::string test(resvec.begin(), resvec.end());
REQUIRE(test == "other data to compression test");
}
unzipper.close();
}
}
std::remove("test1.txt");
removeFolder("TestFolder");
}
}
}
zipvec.clear();
}
WHEN("a stringstream containing 'test string data compression' is added and named 'strdata'")
{
std::stringstream strdata;
strdata << "test string data compression";
zipper.add(strdata, "strdata");
zipper.close();
zipper::Unzipper unzipper(zipvec);
THEN("the zip vector has one entry named 'strdata'")
{
REQUIRE(unzipper.entries().size() == 1);
REQUIRE(unzipper.entries().front().name == "strdata");
AND_THEN("extracting the strdata entry creates a file named 'strdata' with the txt 'test string data compression'")
{
unzipper.extract();
REQUIRE(checkFileExists("strdata"));
std::ifstream testfile("strdata");
REQUIRE(testfile.good());
std::string test((std::istreambuf_iterator<char>(testfile)), std::istreambuf_iterator<char>());
testfile.close();
REQUIRE(test == "test string data compression");
AND_THEN("extracting the strdata entry to memory, fills a vector with the txt 'test string data compression'")
{
std::vector<unsigned char> resvec;
unzipper.extractEntryToMemory("strdata", resvec);
unzipper.close();
std::string test(resvec.begin(), resvec.end());
REQUIRE(test == "test string data compression");
}
unzipper.close();
}
}
std::remove("strdata");
zipvec.clear();
}
WHEN("a file containing 'test file compression' is added and named 'test1' in subdirectory 'subdirectory'")
{
bool fileWasCreated = zipper::makedir("subdirectory");
REQUIRE(fileWasCreated);
std::ofstream test1("./subdirectory/test1.txt");
test1 << "test file compression";
test1.flush();
test1.close();
auto flags = Zipper::zipFlags::Better | Zipper::zipFlags::SaveHierarchy;
zipper.add("./subdirectory/test1.txt", static_cast<Zipper::zipFlags>(flags));
zipper.close();
std::remove("./subdirectory/test1.txt");
zipper::Unzipper unzipper(zipvec);
THEN("the zip vector has entry named './subdirectory/test1.txt'")
{
REQUIRE(unzipper.entries().size() == 1);
REQUIRE(unzipper.entries().front().name == "./subdirectory/test1.txt");
AND_THEN("extracting the test1.txt entry creates a file named 'test1.txt' with the text 'test file compression'")
{
unzipper.extractEntry("./subdirectory/test1.txt");
// due to sections forking or creating different stacks we need to make sure the local instance is closed to
// prevent mixing the closing when both instances are freed at the end of the scope
unzipper.close();
REQUIRE(checkFileExists("./subdirectory/test1.txt"));
std::ifstream testfile("./subdirectory/test1.txt");
REQUIRE(testfile.good());
std::string test((std::istreambuf_iterator<char>(testfile)), std::istreambuf_iterator<char>());
testfile.close();
REQUIRE(test == "test file compression");
removeFolder("subdirectory");
}
}
zipvec.clear();
}
}
}