Skip to content

Commit 05f8750

Browse files
committed
Improve Download and Upload module tests
1 parent 0fc1442 commit 05f8750

File tree

5 files changed

+220
-474
lines changed

5 files changed

+220
-474
lines changed
Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "../ChangeDirectory.hpp"
22

3-
#ifdef __linux__
4-
#elif _WIN32
5-
#include <windows.h>
6-
#endif
3+
#include <filesystem>
74

85
bool testChangeDirectory();
96

@@ -17,7 +14,7 @@ int main()
1714
std::cout << "[+] Sucess" << std::endl;
1815
else
1916
std::cout << "[-] Failed" << std::endl;
20-
17+
2118
return !res;
2219
}
2320

@@ -26,81 +23,81 @@ bool testChangeDirectory()
2623
{
2724
std::unique_ptr<ChangeDirectory> changeDirectory = std::make_unique<ChangeDirectory>();
2825

29-
{
30-
std::vector<std::string> splitedCmd;
31-
splitedCmd.push_back("cd");
32-
splitedCmd.push_back("sdghdfhdfgnjdgf");
26+
std::filesystem::path original = std::filesystem::current_path();
27+
std::filesystem::path temp = std::filesystem::temp_directory_path() / "c2core_change_dir_test";
28+
std::filesystem::create_directories(temp);
29+
std::cout << "original=" << original << " temp=" << temp << std::endl;
3330

34-
C2Message c2Message;
35-
C2Message c2RetMessage;
36-
changeDirectory->init(splitedCmd, c2Message);
37-
changeDirectory->process(c2Message, c2RetMessage);
38-
39-
std::string output = "\n\noutput:\n";
40-
output += c2RetMessage.returnvalue();
41-
output += "\n";
42-
std::cout << output << std::endl;
43-
}
31+
// Change to temporary directory
4432
{
4533
std::vector<std::string> splitedCmd;
4634
splitedCmd.push_back("cd");
47-
splitedCmd.push_back("..");
35+
splitedCmd.push_back(temp.string());
4836

4937
C2Message c2Message;
5038
C2Message c2RetMessage;
5139
changeDirectory->init(splitedCmd, c2Message);
40+
c2Message.set_cmd(temp.string());
5241
changeDirectory->process(c2Message, c2RetMessage);
5342

54-
std::string output = "\n\noutput:\n";
55-
output += c2RetMessage.returnvalue();
56-
output += "\n";
57-
std::cout << output << std::endl;
43+
std::cout << "changed to: " << c2RetMessage.returnvalue() << std::endl;
44+
if (c2RetMessage.returnvalue() != temp.string()) {
45+
std::cout << "ret mismatch" << std::endl;
46+
return false;
47+
}
48+
if (std::filesystem::current_path() != temp) {
49+
std::cout << "cwd mismatch" << std::endl;
50+
return false;
51+
}
5852
}
59-
{
60-
std::vector<std::string> splitedCmd;
61-
splitedCmd.push_back("cd");
62-
splitedCmd.push_back("C:\\Temp");
63-
64-
C2Message c2Message;
65-
C2Message c2RetMessage;
66-
changeDirectory->init(splitedCmd, c2Message);
67-
changeDirectory->process(c2Message, c2RetMessage);
6853

69-
std::string output = "\n\noutput:\n";
70-
output += c2RetMessage.returnvalue();
71-
output += "\n";
72-
std::cout << output << std::endl;
73-
}
54+
// Invalid directory should not change current path
7455
{
7556
std::vector<std::string> splitedCmd;
7657
splitedCmd.push_back("cd");
77-
splitedCmd.push_back("...");
58+
splitedCmd.push_back("does_not_exist");
7859

7960
C2Message c2Message;
8061
C2Message c2RetMessage;
8162
changeDirectory->init(splitedCmd, c2Message);
63+
c2Message.set_cmd("does_not_exist");
8264
changeDirectory->process(c2Message, c2RetMessage);
8365

84-
std::string output = "\n\noutput:\n";
85-
output += c2RetMessage.returnvalue();
86-
output += "\n";
87-
std::cout << output << std::endl;
66+
std::cout << "invalid result: " << c2RetMessage.returnvalue() << std::endl;
67+
if (c2RetMessage.returnvalue() != temp.string()) {
68+
std::cout << "invalid ret mismatch" << std::endl;
69+
return false;
70+
}
71+
if (std::filesystem::current_path() != temp) {
72+
std::cout << "invalid cwd mismatch" << std::endl;
73+
return false;
74+
}
8875
}
76+
77+
// Return to original directory
8978
{
9079
std::vector<std::string> splitedCmd;
9180
splitedCmd.push_back("cd");
92-
splitedCmd.push_back("C:\\");
81+
splitedCmd.push_back(original.string());
9382

9483
C2Message c2Message;
9584
C2Message c2RetMessage;
9685
changeDirectory->init(splitedCmd, c2Message);
86+
c2Message.set_cmd(original.string());
9787
changeDirectory->process(c2Message, c2RetMessage);
9888

99-
std::string output = "\n\noutput:\n";
100-
output += c2RetMessage.returnvalue();
101-
output += "\n";
102-
std::cout << output << std::endl;
89+
std::cout << "back result: " << c2RetMessage.returnvalue() << std::endl;
90+
if (c2RetMessage.returnvalue() != original.string()) {
91+
std::cout << "back ret mismatch" << std::endl;
92+
return false;
93+
}
94+
if (std::filesystem::current_path() != original) {
95+
std::cout << "back cwd mismatch" << std::endl;
96+
return false;
97+
}
10398
}
10499

100+
std::filesystem::remove_all(temp);
101+
105102
return true;
106103
}

0 commit comments

Comments
 (0)