Skip to content

Commit 2eeb53d

Browse files
authored
Merge pull request #6 from maxDcb/codex/improve-test-coverage-for-modules
Improve module tests
2 parents 02a7db2 + 63b37f0 commit 2eeb53d

File tree

4 files changed

+164
-227
lines changed

4 files changed

+164
-227
lines changed

modules/Cat/tests/testsCat.cpp

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "../Cat.hpp"
22

3+
#include <filesystem>
4+
35
#ifdef __linux__
46
#elif _WIN32
57
#include <windows.h>
@@ -23,64 +25,60 @@ int main()
2325

2426
bool testCat()
2527
{
26-
std::string testFile = "test1.txt";
27-
std::string fileContent = "testCat";
28+
namespace fs = std::filesystem;
29+
fs::path temp = fs::temp_directory_path() / "c2core_cat_test";
30+
fs::create_directories(temp);
2831

29-
std::ofstream outfile("test1.txt");
30-
outfile << "testCat" << std::endl;
31-
outfile.close();
32+
bool ok = true;
3233

3334
std::unique_ptr<Cat> cat = std::make_unique<Cat>();
35+
36+
// ----- valid file -----
37+
fs::path file = temp / "file.txt";
3438
{
35-
std::vector<std::string> splitedCmd;
36-
splitedCmd.push_back("cat");
37-
splitedCmd.push_back(testFile);
38-
39-
C2Message c2Message;
40-
C2Message c2RetMessage;
41-
cat->init(splitedCmd, c2Message);
42-
cat->process(c2Message, c2RetMessage);
43-
44-
std::string output = "\n\noutput:\n";
45-
output += c2RetMessage.returnvalue();
46-
output += "\n";
47-
std::cout << output << std::endl;
48-
49-
if (c2RetMessage.returnvalue().compare(0, fileContent.length(), fileContent) == 0)
50-
{
51-
}
52-
else
53-
{
54-
std::cout << "[-] fileContent " << fileContent << std::endl;
55-
std::cout << "[-] c2RetMessage.returnvalue() " << c2RetMessage.returnvalue() << std::endl;
56-
return false;
57-
}
39+
std::ofstream(file) << "hello";
40+
std::vector<std::string> cmd = {"cat", file.string()};
41+
C2Message msg, ret;
42+
cat->init(cmd, msg);
43+
msg.set_inputfile(file.string());
44+
cat->process(msg, ret);
45+
46+
ok &= ret.returnvalue().find("hello") == 0;
5847
}
59-
48+
49+
// ----- path containing spaces and tokens splitted -----
50+
fs::path spaced = temp / "space file.txt";
6051
{
61-
std::vector<std::string> splitedCmd;
62-
splitedCmd.push_back("cat");
63-
splitedCmd.push_back(".\\test space folder\\test space.txt");
64-
65-
C2Message c2Message;
66-
C2Message c2RetMessage;
67-
cat->init(splitedCmd, c2Message);
68-
cat->process(c2Message, c2RetMessage);
69-
70-
std::string output = "\n\noutput:\n";
71-
output += c2RetMessage.returnvalue();
72-
output += "\n";
73-
std::cout << output << std::endl;
74-
75-
if (c2RetMessage.errorCode())
76-
{
77-
std::cout << "[+] c2RetMessage.errorCode() " << c2RetMessage.errorCode() << std::endl;
78-
}
79-
else
80-
{
81-
return false;
82-
}
52+
std::ofstream(spaced) << "space";
53+
std::vector<std::string> cmd = {"cat", (temp.string()+"/space").c_str(), "file.txt"};
54+
C2Message msg, ret;
55+
cat->init(cmd, msg);
56+
msg.set_inputfile(spaced.string());
57+
cat->process(msg, ret);
58+
59+
ok &= ret.returnvalue().find("space") == 0;
8360
}
8461

85-
return true;
62+
// ----- invalid file -----
63+
{
64+
fs::path invalid = temp / "does_not_exist.txt";
65+
std::vector<std::string> cmd = {"cat", invalid.string()};
66+
C2Message msg, ret;
67+
cat->init(cmd, msg);
68+
msg.set_inputfile(invalid.string());
69+
cat->process(msg, ret);
70+
std::string err;
71+
cat->errorCodeToMsg(ret, err);
72+
#ifdef BUILD_TEAMSERVER
73+
ok &= ret.errorCode() == 1 && !err.empty();
74+
#else
75+
76+
ok &= ret.errorCode() == 1;
77+
#endif
78+
}
79+
80+
81+
82+
fs::remove_all(temp);
83+
return ok;
8684
}

modules/ModuleTemplate/tests/testsModuleTemplate.cpp

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,47 @@ bool testModuleTemplate()
2929
{
3030

3131
std::unique_ptr<ModuleTemplate> moduleTemplate = std::make_unique<ModuleTemplate>();
32-
33-
{
34-
std::vector<std::string> splitedCmd;
35-
splitedCmd.push_back("moduleTemplate");
36-
splitedCmd.push_back("arg1");
37-
38-
C2Message c2Message;
39-
C2Message c2RetMessage;
40-
moduleTemplate->init(splitedCmd, c2Message);
41-
moduleTemplate->process(c2Message, c2RetMessage);
32+
bool ok = true;
4233

43-
std::string output = "output:\n";
44-
output += c2RetMessage.returnvalue();
45-
output += "\n";
46-
std::cout << output << std::endl;
34+
// ----- correct argument -----
35+
{
36+
std::vector<std::string> cmd = {"moduleTemplate", "arg1"};
37+
C2Message msg, ret;
38+
moduleTemplate->init(cmd, msg);
39+
moduleTemplate->process(msg, ret);
40+
ok &= ret.returnvalue().find("return value") != std::string::npos;
4741
}
4842

43+
// ----- wrong argument triggers error -----
4944
{
50-
std::vector<std::string> splitedCmd;
51-
splitedCmd.push_back("moduleTemplate");
52-
splitedCmd.push_back("notarg1");
53-
54-
C2Message c2Message;
55-
C2Message c2RetMessage;
56-
moduleTemplate->init(splitedCmd, c2Message);
57-
moduleTemplate->process(c2Message, c2RetMessage);
45+
std::vector<std::string> cmd = {"moduleTemplate", "notarg1"};
46+
C2Message msg, ret;
47+
moduleTemplate->init(cmd, msg);
48+
moduleTemplate->process(msg, ret);
49+
std::string err;
50+
moduleTemplate->errorCodeToMsg(ret, err);
51+
#ifdef BUILD_TEAMSERVER
52+
ok &= ret.errorCode() == 1 && !err.empty();
53+
#else
54+
ok &= ret.errorCode() == 1;
55+
#endif
56+
}
5857

59-
std::string output = "output:\n";
60-
output += c2RetMessage.returnvalue();
61-
output += "\n";
62-
std::cout << output << std::endl;
58+
// ----- missing argument -----
59+
{
60+
std::vector<std::string> cmd = {"moduleTemplate"};
61+
C2Message msg, ret;
62+
ok &= moduleTemplate->init(cmd, msg) == -1;
63+
ok &= !msg.returnvalue().empty();
64+
}
6365

64-
if (c2RetMessage.errorCode())
65-
{
66-
std::string errorMsg;
67-
moduleTemplate->errorCodeToMsg(c2RetMessage, errorMsg);
68-
std::cout << "[+] error: \n" << errorMsg << std::endl;
69-
}
70-
else
71-
{
72-
return false;
73-
}
66+
// ----- followUp path -----
67+
{
68+
C2Message ret;
69+
ret.set_errorCode(-1);
70+
ret.set_args("dummy");
71+
ok &= moduleTemplate->followUp(ret) == 0;
7472
}
7573

76-
return true;
74+
return ok;
7775
}

modules/Run/tests/testsRun.cpp

Lines changed: 30 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "../Run.hpp"
22

3+
#include <filesystem>
4+
35
#ifdef __linux__
46
#elif _WIN32
57
#include <windows.h>
@@ -24,101 +26,44 @@ int main()
2426
bool testRun()
2527
{
2628
std::unique_ptr<Run> run = std::make_unique<Run>();
29+
bool ok = true;
2730

31+
// ----- simple echo -----
2832
{
29-
std::vector<std::string> splitedCmd;
30-
splitedCmd.push_back("run");
31-
splitedCmd.push_back("whoami");
32-
33-
C2Message c2Message;
34-
C2Message c2RetMessage;
35-
run->init(splitedCmd, c2Message);
36-
run->process(c2Message, c2RetMessage);
37-
38-
std::string output = "\n\noutput:\n";
39-
output += c2RetMessage.returnvalue();
40-
output += "\n";
41-
std::cout << output << std::endl;
33+
std::vector<std::string> cmd = {"run", "echo", "hello"};
34+
C2Message msg, ret;
35+
run->init(cmd, msg);
36+
msg.set_cmd("echo hello");
37+
run->process(msg, ret);
38+
ok &= ret.returnvalue().find("hello") != std::string::npos;
4239
}
43-
{
44-
std::vector<std::string> splitedCmd;
45-
splitedCmd.push_back("run");
46-
splitedCmd.push_back("id");
47-
48-
C2Message c2Message;
49-
C2Message c2RetMessage;
50-
run->init(splitedCmd, c2Message);
51-
run->process(c2Message, c2RetMessage);
5240

53-
std::string output = "\n\noutput:\n";
54-
output += c2RetMessage.returnvalue();
55-
output += "\n";
56-
std::cout << output << std::endl;
57-
}
41+
// ----- command with spaces (split tokens) -----
5842
{
59-
std::vector<std::string> splitedCmd;
60-
splitedCmd.push_back("run");
61-
splitedCmd.push_back("dir");
62-
63-
C2Message c2Message;
64-
C2Message c2RetMessage;
65-
run->init(splitedCmd, c2Message);
66-
run->process(c2Message, c2RetMessage);
67-
68-
std::string output = "\n\noutput:\n";
69-
output += c2RetMessage.returnvalue();
70-
output += "\n";
71-
std::cout << output << std::endl;
43+
std::vector<std::string> cmd = {"run", "echo", "hello", "world"};
44+
C2Message msg, ret;
45+
run->init(cmd, msg);
46+
msg.set_cmd("echo hello world");
47+
run->process(msg, ret);
48+
ok &= ret.returnvalue().find("hello world") != std::string::npos;
7249
}
73-
{
74-
std::vector<std::string> splitedCmd;
75-
splitedCmd.push_back("run");
76-
#ifdef __linux__
77-
splitedCmd.push_back("/c ping 127.0.0.1 -c 1");
78-
#elif _WIN32
79-
splitedCmd.push_back("/c ping 127.0.0.1 /n 1");
80-
#endif
81-
82-
C2Message c2Message;
83-
C2Message c2RetMessage;
84-
run->init(splitedCmd, c2Message);
85-
run->process(c2Message, c2RetMessage);
8650

87-
std::string output = "\n\noutput:\n";
88-
output += c2RetMessage.returnvalue();
89-
output += "\n";
90-
std::cout << output << std::endl;
91-
}
51+
// ----- invalid command should return error text -----
9252
{
93-
std::vector<std::string> splitedCmd;
94-
splitedCmd.push_back("run");
95-
splitedCmd.push_back("calc.exe");
96-
97-
C2Message c2Message;
98-
C2Message c2RetMessage;
99-
run->init(splitedCmd, c2Message);
100-
run->process(c2Message, c2RetMessage);
101-
102-
std::string output = "\n\noutput:\n";
103-
output += c2RetMessage.returnvalue();
104-
output += "\n";
105-
std::cout << output << std::endl;
53+
std::vector<std::string> cmd = {"run", "nonexistent_command_foo"};
54+
C2Message msg, ret;
55+
run->init(cmd, msg);
56+
msg.set_cmd("nonexistent_command_foo");
57+
run->process(msg, ret);
58+
ok &= ret.returnvalue().empty();
10659
}
107-
{
108-
std::vector<std::string> splitedCmd;
109-
splitedCmd.push_back("run");
110-
splitedCmd.push_back(".\\test.bat");
11160

112-
C2Message c2Message;
113-
C2Message c2RetMessage;
114-
run->init(splitedCmd, c2Message);
115-
run->process(c2Message, c2RetMessage);
116-
117-
std::string output = "\n\noutput:\n";
118-
output += c2RetMessage.returnvalue();
119-
output += "\n";
120-
std::cout << output << std::endl;
61+
// ----- missing argument -----
62+
{
63+
std::vector<std::string> cmd = {"run"};
64+
C2Message msg, ret;
65+
ok &= run->init(cmd, msg) == -1;
12166
}
12267

123-
return true;
68+
return ok;
12469
}

0 commit comments

Comments
 (0)