|
1 | 1 | #include "../Download.hpp" |
2 | | - |
3 | | -#ifdef __linux__ |
4 | | -#elif _WIN32 |
5 | | -#include <windows.h> |
6 | | -#endif |
| 2 | +#include <filesystem> |
| 3 | +#include <fstream> |
7 | 4 |
|
8 | 5 | bool testDownload(); |
9 | 6 |
|
10 | | -int main() |
11 | | -{ |
12 | | - bool res; |
13 | | - |
| 7 | +int main() { |
14 | 8 | std::cout << "[+] testDownload" << std::endl; |
15 | | - res = testDownload(); |
16 | | - if (res) |
17 | | - std::cout << "[+] Sucess" << std::endl; |
18 | | - else |
19 | | - std::cout << "[-] Failed" << std::endl; |
20 | | - |
| 9 | + bool res = testDownload(); |
| 10 | + if (res) { |
| 11 | + std::cout << "[+] Success" << std::endl; |
| 12 | + } else { |
| 13 | + std::cout << "[-] Failed" << std::endl; |
| 14 | + } |
21 | 15 | return !res; |
22 | 16 | } |
23 | 17 |
|
24 | | - |
25 | | -bool areFilesIdentical(const std::string& file1, const std::string& file2) { |
26 | | - std::ifstream f1(file1, std::ios::binary | std::ios::ate); |
27 | | - std::ifstream f2(file2, std::ios::binary | std::ios::ate); |
28 | | - |
29 | | - if (!f1 || !f2) { |
30 | | - std::cerr << "Failed to open one or both files." << std::endl; |
31 | | - return false; |
32 | | - } |
33 | | - |
34 | | - if (f1.tellg() != f2.tellg()) { |
35 | | - return false; // Different sizes |
36 | | - } |
37 | | - |
38 | | - f1.seekg(0, std::ios::beg); |
39 | | - f2.seekg(0, std::ios::beg); |
40 | | - |
41 | | - std::vector<char> buffer1((std::istreambuf_iterator<char>(f1)), std::istreambuf_iterator<char>()); |
42 | | - std::vector<char> buffer2((std::istreambuf_iterator<char>(f2)), std::istreambuf_iterator<char>()); |
43 | | - |
44 | | - return buffer1 == buffer2; |
| 18 | +static bool filesEqual(const std::filesystem::path& a, const std::filesystem::path& b) { |
| 19 | + std::ifstream f1(a, std::ios::binary); |
| 20 | + std::ifstream f2(b, std::ios::binary); |
| 21 | + std::string d1((std::istreambuf_iterator<char>(f1)), {}); |
| 22 | + std::string d2((std::istreambuf_iterator<char>(f2)), {}); |
| 23 | + return d1 == d2; |
45 | 24 | } |
46 | 25 |
|
| 26 | +bool testDownload() { |
| 27 | + namespace fs = std::filesystem; |
| 28 | + fs::path temp = fs::temp_directory_path() / "c2core_download_test"; |
| 29 | + fs::create_directories(temp); |
| 30 | + bool ok = true; |
47 | 31 |
|
48 | | -bool testDownload() |
49 | | -{ |
50 | | - std::unique_ptr<Download> download = std::make_unique<Download>(); |
| 32 | + // Small file |
51 | 33 | { |
52 | | - std::string fileName = "test.txt"; |
53 | | - std::string outputFileName = "testDownload.txt"; |
54 | | - std::string fileContent = "tesDownload"; |
55 | | - |
56 | | - std::ofstream outfile(fileName); |
57 | | - outfile << fileContent; |
58 | | - outfile.close(); |
59 | | - |
60 | | - std::vector<std::string> splitedCmd; |
61 | | - splitedCmd.push_back("download"); |
62 | | - splitedCmd.push_back(fileName); |
63 | | - splitedCmd.push_back(outputFileName); |
64 | | - |
65 | | - C2Message c2Message; |
66 | | - C2Message c2RetMessage; |
67 | | - download->init(splitedCmd, c2Message); |
68 | | - download->process(c2Message, c2RetMessage); |
69 | | - download->followUp(c2RetMessage); |
70 | | - |
71 | | - std::string errorMsg; |
72 | | - download->errorCodeToMsg(c2RetMessage, errorMsg); |
73 | | - |
74 | | - std::string error = "errorMsg:\n"; |
75 | | - error += errorMsg; |
76 | | - error += "\n"; |
77 | | - std::cout << error << std::endl; |
78 | | - |
79 | | - std::string output = "output:\n"; |
80 | | - output += c2RetMessage.returnvalue(); |
81 | | - output += "\n"; |
82 | | - std::cout << output << std::endl; |
83 | | - |
84 | | - if (areFilesIdentical(fileName, outputFileName)) |
85 | | - { |
86 | | - } |
87 | | - else |
88 | | - { |
89 | | - std::cout << "The files are different." << std::endl; |
90 | | - return false; |
91 | | - } |
| 34 | + Download dl; |
| 35 | + fs::path src = temp / "src.txt"; |
| 36 | + fs::path dst = temp / "dst.txt"; |
| 37 | + std::string data = "small file"; |
| 38 | + std::ofstream(src) << data; |
| 39 | + |
| 40 | + std::vector<std::string> cmd = {"download", src.string(), dst.string()}; |
| 41 | + C2Message msg, ret; |
| 42 | + dl.init(cmd, msg); |
| 43 | + dl.process(msg, ret); |
| 44 | + dl.followUp(ret); |
| 45 | + std::cout << "small ret: " << ret.returnvalue() << " ec=" << ret.errorCode() << std::endl; |
| 46 | + bool sub = ret.errorCode() == -1 && ret.returnvalue() == "Success" && filesEqual(src, dst); |
| 47 | + std::cout << "small ok=" << sub << std::endl; |
| 48 | + ok &= sub; |
92 | 49 | } |
93 | 50 |
|
| 51 | + // Large file requiring multiple chunks |
94 | 52 | { |
95 | | - std::string fileName = "test with space.txt"; |
96 | | - std::string outputFileName = "testDownload2.txt"; |
97 | | - std::string fileContent = "tesDownload2"; |
98 | | - |
99 | | - std::ofstream outfile(fileName); |
100 | | - outfile << fileContent; |
101 | | - outfile.close(); |
102 | | - |
103 | | - std::vector<std::string> splitedCmd; |
104 | | - splitedCmd.push_back("download"); |
105 | | - splitedCmd.push_back("\"test"); |
106 | | - splitedCmd.push_back("with"); |
107 | | - splitedCmd.push_back("space.txt\""); |
108 | | - splitedCmd.push_back(outputFileName); |
109 | | - |
110 | | - C2Message c2Message; |
111 | | - C2Message c2RetMessage; |
112 | | - download->init(splitedCmd, c2Message); |
113 | | - download->process(c2Message, c2RetMessage); |
114 | | - download->followUp(c2RetMessage); |
115 | | - |
116 | | - std::string errorMsg; |
117 | | - download->errorCodeToMsg(c2RetMessage, errorMsg); |
118 | | - |
119 | | - std::string error = "errorMsg:\n"; |
120 | | - error += errorMsg; |
121 | | - error += "\n"; |
122 | | - std::cout << error << std::endl; |
123 | | - |
124 | | - std::string output = "output:\n"; |
125 | | - output += c2RetMessage.returnvalue(); |
126 | | - output += "\n"; |
127 | | - std::cout << output << std::endl; |
128 | | - |
129 | | - if (areFilesIdentical(fileName, outputFileName)) |
130 | | - { |
131 | | - } |
132 | | - else |
133 | | - { |
134 | | - std::cout << "The files are different." << std::endl; |
135 | | - return false; |
| 53 | + Download dl; |
| 54 | + fs::path src = temp / "large.bin"; |
| 55 | + fs::path dst = temp / "large_copy.bin"; |
| 56 | + const size_t size = 1024 * 1024 + 500; // > CHUNK_SIZE |
| 57 | + std::string data(size, 'A'); |
| 58 | + std::ofstream(src, std::ios::binary).write(data.data(), data.size()); |
| 59 | + |
| 60 | + std::vector<std::string> cmd = {"download", src.string(), dst.string()}; |
| 61 | + C2Message msg, ret; |
| 62 | + dl.init(cmd, msg); |
| 63 | + dl.process(msg, ret); |
| 64 | + dl.followUp(ret); |
| 65 | + std::cout << "large first ret: " << ret.returnvalue() << " ec=" << ret.errorCode() << std::endl; |
| 66 | + while(ret.returnvalue() != "Success") { |
| 67 | + C2Message next; |
| 68 | + dl.recurringExec(next); |
| 69 | + dl.followUp(next); |
| 70 | + ret = next; |
136 | 71 | } |
| 72 | + bool sub = filesEqual(src, dst); |
| 73 | + std::cout << "large ok=" << sub << std::endl; |
| 74 | + ok &= sub; |
137 | 75 | } |
138 | 76 |
|
| 77 | + // Non-existent source file |
139 | 78 | { |
140 | | - std::string fileName = "rockyou.txt.gz"; |
141 | | - std::string outputFileName = "rockyouCopy.txt.gz"; |
142 | | - |
143 | | - std::vector<std::string> splitedCmd; |
144 | | - splitedCmd.push_back("download"); |
145 | | - splitedCmd.push_back(fileName); |
146 | | - splitedCmd.push_back(outputFileName); |
147 | | - |
148 | | - C2Message c2Message; |
149 | | - C2Message c2RetMessage; |
150 | | - download->init(splitedCmd, c2Message); |
151 | | - download->process(c2Message, c2RetMessage); |
152 | | - download->followUp(c2RetMessage); |
153 | | - |
154 | | - std::string errorMsg; |
155 | | - download->errorCodeToMsg(c2RetMessage, errorMsg); |
156 | | - |
157 | | - std::string error = "errorMsg:\n"; |
158 | | - error += errorMsg; |
159 | | - error += "\n"; |
160 | | - std::cout << error << std::endl; |
161 | | - |
162 | | - std::string output = "output:\n"; |
163 | | - output += c2RetMessage.returnvalue(); |
164 | | - output += "\n"; |
165 | | - std::cout << output << std::endl; |
166 | | - |
167 | | - while(true) |
168 | | - { |
169 | | - C2Message c2RetMessageNew; |
170 | | - download->recurringExec(c2RetMessageNew); |
171 | | - download->followUp(c2RetMessageNew); |
172 | | - |
173 | | - std::string errorMsg; |
174 | | - download->errorCodeToMsg(c2RetMessageNew, errorMsg); |
175 | | - |
176 | | - std::string error = "errorMsg:\n"; |
177 | | - error += errorMsg; |
178 | | - error += "\n"; |
179 | | - std::cout << error << std::endl; |
180 | | - |
181 | | - std::string output = "output:\n"; |
182 | | - output += c2RetMessageNew.returnvalue(); |
183 | | - output += "\n"; |
184 | | - std::cout << output << std::endl; |
185 | | - |
186 | | - if("Success" == c2RetMessageNew.returnvalue()) |
187 | | - break; |
188 | | - } |
189 | | - |
190 | | - if (areFilesIdentical(fileName, outputFileName)) |
191 | | - { |
192 | | - } |
193 | | - else |
194 | | - { |
195 | | - std::cout << "The files are different." << std::endl; |
196 | | - return false; |
197 | | - } |
198 | | - } |
199 | | - |
200 | | - { |
201 | | - std::string fileName = "sdgsdfhkjjhgzetreyixwvccn.txt"; |
202 | | - std::string outputFileName = "notHere.txt"; |
203 | | - |
204 | | - std::vector<std::string> splitedCmd; |
205 | | - splitedCmd.push_back("download"); |
206 | | - splitedCmd.push_back(fileName); |
207 | | - splitedCmd.push_back(outputFileName); |
208 | | - |
209 | | - C2Message c2Message; |
210 | | - C2Message c2RetMessage; |
211 | | - download->init(splitedCmd, c2Message); |
212 | | - download->process(c2Message, c2RetMessage); |
213 | | - download->followUp(c2RetMessage); |
214 | | - |
215 | | - std::string errorMsg; |
216 | | - download->errorCodeToMsg(c2RetMessage, errorMsg); |
217 | | - |
218 | | - std::string error = "errorMsg:\n"; |
219 | | - error += errorMsg; |
220 | | - error += "\n"; |
221 | | - std::cout << error << std::endl; |
222 | | - |
223 | | - std::string output = "output:\n"; |
224 | | - output += c2RetMessage.returnvalue(); |
225 | | - output += "\n"; |
226 | | - std::cout << output << std::endl; |
227 | | - |
228 | | - if (c2RetMessage.errorCode()) |
229 | | - { |
230 | | - } |
231 | | - else |
232 | | - { |
233 | | - return false; |
234 | | - } |
235 | | - |
236 | | - std::ifstream myfile; |
237 | | - myfile.open(outputFileName, std::ios::binary); |
238 | | - |
239 | | - if(myfile) |
240 | | - { |
241 | | - return false; |
242 | | - } |
| 79 | + Download dl; |
| 80 | + fs::path src = temp / "missing.txt"; |
| 81 | + fs::path dst = temp / "missing_out.txt"; |
| 82 | + std::vector<std::string> cmd = {"download", src.string(), dst.string()}; |
| 83 | + C2Message msg, ret; |
| 84 | + dl.init(cmd, msg); |
| 85 | + dl.process(msg, ret); |
| 86 | + dl.followUp(ret); |
| 87 | + std::cout << "missing ret: " << ret.returnvalue() << " ec=" << ret.errorCode() << std::endl; |
| 88 | + bool sub = ret.errorCode() != -1 && !fs::exists(dst); |
| 89 | + std::cout << "missing ok=" << sub << std::endl; |
| 90 | + ok &= sub; |
243 | 91 | } |
244 | 92 |
|
245 | | - return true; |
| 93 | + fs::remove_all(temp); |
| 94 | + return ok; |
246 | 95 | } |
0 commit comments