-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
340 lines (316 loc) · 11.3 KB
/
test.cpp
File metadata and controls
340 lines (316 loc) · 11.3 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <iostream>
#include <string>
#include <vector>
#include "misc.h"
#include "io.h"
#include "json.h"
#include "time.h"
using namespace std;
void test_run_command() {
cout << "test_run_command()" << endl;
cout << "Running ls -l..." << endl;
ShellResult result = run_command("ls -l", "", false);
print_shell_result(result);
cout << endl;
cout << "Running ls -l with debug..." << endl;
ShellResult result_with_debug = run_command("ls -l", "", true);
print_shell_result(result_with_debug);
cout << endl;
cout << "Running ls -l from /..." << endl;
ShellResult result_with_cwd = run_command("ls -l", "/", false);
print_shell_result(result_with_cwd);
cout << endl;
cout << "Running ls -l from / with debug..." << endl;
ShellResult result_with_cwd_and_debug = run_command("ls -l", "/", true);
print_shell_result(result_with_cwd_and_debug);
cout << endl << endl << endl;
}
void test_return_output() {
cout << "test_return_output()" << endl;
cout << "Running ls -l..." << endl;
ShellResult result = return_output("ls -l", "", false);
print_shell_result(result);
cout << endl;
cout << "Running ls -l with debug..." << endl;
ShellResult result_with_debug = return_output("ls -l", "", true);
print_shell_result(result_with_debug);
cout << endl;
cout << "Running ls -l from /..." << endl;
ShellResult result_with_cwd = return_output("ls -l", "/", false);
print_shell_result(result_with_cwd);
cout << endl;
cout << "Running ls -l from / with debug..." << endl;
ShellResult result_with_cwd_and_debug = return_output("ls -l", "/", true);
print_shell_result(result_with_cwd_and_debug);
cout << endl << endl;
}
void test_write_file() {
cout << "test_write_file()" << endl;
string str = "Test string\nTest after newline\n";
cout << "Contents of string:" << endl;
cout << str << endl;
write_file("/tmp/test_io.txt", str);
cout << endl << endl;
}
void test_read_file() {
cout << "test_read_file()" << endl;
string contents = read_file("/tmp/test_io.txt");
cout << "Contents of file:" << endl;
cout << contents << endl;
cout << endl << endl;
}
void test_append_file() {
cout << "test_append_file()" << endl;
string str = "Test string\nTest after newline\n";
cout << "Contents of string:" << endl;
cout << str << endl;
append_file("/tmp/test_io.txt", str);
str.append(str);
cout << "Contents of string after doubling:" << endl;
cout << str << endl;
string after_append = read_file("/tmp/test_io.txt");
if (str.compare(after_append) == 0) {
cout << "str is identical to after_append" << endl;
} else {
cout << "str is different from after_append" << endl;
}
cout << endl << endl;
}
void test_split() {
cout << "test_split()" << endl;
string bunch_of_text = "Honey bunches of text of randomness";
cout << "string: \"" << bunch_of_text << "\"" << endl;
cout << "split on whitespace: " << endl;
vector<string> whitespace_split = split(bunch_of_text);
for ( string element: whitespace_split ) {
cout << " element: \"" << element << "\"" << endl;
}
cout << "split on \"o\": " << endl;
vector<string> o_split = split(bunch_of_text, 'o');
for ( string element: o_split ) {
cout << " element: \"" << element << "\"" << endl;
}
cout << endl << endl;
}
void test_get_os_info() {
cout << "test_get_os_info()" << endl;
OsInfo os_info = get_os_info();
cout << " os_type: \"" << os_info.os_type << "\"" << endl;
cout << " architecture: \"" << os_info.architecture << "\"" << endl;
cout << " kernel_version_long: \"" << os_info.kernel_version_long << "\"" << endl;
cout << " kernel_version: \"" << os_info.kernel_version << "\"" << endl;
cout << " hostname: \"" << os_info.hostname << "\"" << endl;
cout << " domain_name: \"" << os_info.domain_name << "\"" << endl;
cout << endl << endl;
}
void test_ascii_to_hex() {
cout << "text_ascii_to_hex()" << endl;
string cooka = "I cooka da pizza";
cout << " ascii: " << cooka << endl;
string cooka_hex = ascii_to_hex(cooka);
cout << " hex: " << cooka_hex << endl;
cout << endl << endl;
}
void test_hex_to_ascii() {
cout << "test_hex_to_ascii()" << endl;
string cooka_hex = "4920636f6f6b612064612070697a7a61";
cout << " hex: " << cooka_hex << endl;
string cooka = hex_to_ascii(cooka_hex);
cout << " ascii: " << cooka << endl;
cout << endl << endl;
}
void test_get_random_int() {
cout << "test_get_random_int()" << endl;
int random_int = get_random_int(1, 50);
cout << "Random int between 1 and 50: " << random_int << endl;
cout << endl << endl;
}
void test_parse_json() {
cout << "test_parse_json()" << endl;
string json_string = R"(
{
"hello": "world",
"other": [
"boring",
"phrases"
]
}
)";
json json_object = parse_json(json_string);
cout << "hello: " << json_object["hello"] << endl;
cout << "other[0]: " << json_object["other"][0] << endl;
cout << "other[1]: " << json_object["other"][1] << endl;
cout << endl << endl;
}
void test_generate_json() {
cout << "test_generate_json()" << endl;
json json_object = {
{"hello", "world"},
{"other", {
"boring",
"phrases"
}}
};
cout << " 4 spaces (default):" << endl;
cout << generate_json(json_object);
cout << " 8 spaces (specified):" << endl;
cout << generate_json(json_object, 8);
cout << endl << endl;
}
void test_get_unix_time() {
cout << "get_unix_time()" << endl;
time_t unix_time = get_unix_time();
cout << "Unix time: " << unix_time << endl;
cout << endl << endl;
}
void test_get_time_string() {
cout << "get_time_string()" << endl;
time_t unix_time = get_unix_time();
string time_string = get_time_string(unix_time);
string time_string_specified = get_time_string(1748154207);
cout << "Time string (now): " << time_string << endl;
cout << "Time string specified: " << time_string_specified << endl;
cout << endl << endl;
}
void test_log() {
cout << "test_log()" << endl;
run_command("rm -f /tmp/test.log", "", false);
LogLevel log_level = LogLevel::INFO;
log("/tmp/test.log", "Test log entry", log_level);
string logfile_text = read_file("/tmp/test.log");
cout << "Logfile text: " << endl;
cout << logfile_text << endl;
cout << endl << endl;
}
void test_file_exists() {
cout << "test_file_exists()" << endl;
cout << "Creating file /tmp/file.ext..." << endl;
write_file("/tmp/file.ext", "file.ext");
if (file_exists("/tmp/file.ext")) {
cout << "/tmp/file.ext exists" << endl;
} else {
cout << "/tmp/file.ext does not exist" << endl;
}
if (file_exists("/tmp/file.extension")) {
cout << "/tmp/file.extension does not exist" << endl;
}
run_command("rm -f /tmp/file.ext", "", false);
cout << "Creating directory /tmp/directory..." << endl;
run_command("mkdir -p /tmp/directory", "", false);
if (file_exists("/tmp/directory")) {
cout << "/tmp/directory exists" << endl;
} else {
cout << "/tmp/directory does not exist" << endl;
}
if (file_exists("/tmp/not_a_directory")) {
cout << "/tmp/not_a_directory exists" << endl;
} else {
cout << "/tmp/not_a_directory does not exist" << endl;
}
run_command("rm -rf /tmp/directory", "", false);
cout << endl << endl;
}
void test_get_symlink_target() {
cout << "test_get_symlink_target()" << endl;
run_command("touch /tmp/symlink_target", "", false);
run_command("ln -sf /tmp/symlink_target /tmp/symlink_source", "", false);
string symlink_source = get_symlink_target("/tmp/symlink_source");
cout << "Symlink target for /tmp/symlink_source: " << symlink_source << endl;
string symlink_target = get_symlink_target("/tmp/symlink_target");
cout << "Symlink target for /tmp/symlink_target: " << symlink_target << endl;
string fake_file = get_symlink_target("/tmp/fake_file");
cout << "Symlink target for /tmp/fake_file: " << fake_file << endl;
cout << endl << endl;
}
void test_create_dir() {
cout << "test_create_dir()" << endl;
cout << "Checking existence before creation..." << endl;
if (file_exists("/tmp/test/dir")) {
cout << "Directory /tmp/test/dir exists" << endl;
} else {
cout << "Directory /tmp/test/dir does not exist" << endl;
}
cout << "Creating directory /tmp/test/dir..." << endl;
create_dir("/tmp/test/dir");
if (file_exists("/tmp/test/dir")) {
cout << "Directory /tmp/test/dir exists" << endl;
} else {
cout << "Directory /tmp/test/dir does not exist" << endl;
}
cout << endl << endl;
}
void test_remove_dir() {
cout << "test_remove_dir()" << endl;
cout << "Checking existence before removal..." << endl;
if (file_exists("/tmp/test/dir")) {
cout << "Directory /tmp/test/dir exists" << endl;
} else {
cout << "Directory /tmp/test/dir does not exist" << endl;
}
cout << "Removing directory /tmp/test/dir..." << endl;
remove_dir("/tmp/test/dir");
if (file_exists("/tmp/test/dir")) {
cout << "Directory /tmp/test/dir exists" << endl;
} else {
cout << "Directory /tmp/test/dir does not exist" << endl;
}
cout << endl << endl;
}
void test_copy_dir() {
cout << "test_copy_dir()" << endl;
cout << "Checking for source existence..." << endl;
if (file_exists("/tmp/test/source/dir") == 0) {
cout << "Populating source dir..." << endl;
create_dir("/tmp/test/source/dir");
write_file("/tmp/test/source/dir/file.txt", "test text");
}
if (file_exists("/tmp/test/target/dir") == 1) {
cout << "Directory /tmp/test/target/dir exists" << endl;
} else {
cout << "Directory /tmp/test/target/dir does not exist" << endl;
}
if (file_exists("/tmp/test/target/dir/file.txt")) {
cout << "File /tmp/test/target/dir/file.txt exists" << endl;
} else {
cout << "File /tmp/test/target/dir/file.txt does not exist" << endl;
}
cout << "Creating /tmp/test/target..." << endl;
create_dir("/tmp/test/target");
cout << "Copying /tmp/test/source/dir/ to /tmp/test/target/dir/..." << endl;
copy_dir("/tmp/test/source/dir", "/tmp/test/target/dir");
if (file_exists("/tmp/test/target/dir")) {
cout << "Directory /tmp/test/target/dir exists" << endl;
} else {
cout << "Directory /tmp/test/target/dir does not exist" << endl;
}
if (file_exists("/tmp/test/target/dir/file.txt")) {
cout << "File /tmp/test/target/dir/file.txt exists" << endl;
} else {
cout << "File /tmp/test/target/dir/file.txt does not exist" << endl;
}
remove_dir("/tmp/test/target/dir");
cout << endl << endl;
}
int main() {
test_run_command();
test_return_output();
test_write_file();
test_read_file();
test_append_file();
test_split();
test_get_os_info();
test_ascii_to_hex();
test_hex_to_ascii();
test_get_random_int();
test_parse_json();
test_generate_json();
test_get_unix_time();
test_get_time_string();
test_log();
test_file_exists();
test_get_symlink_target();
test_create_dir();
test_remove_dir();
test_copy_dir();
return 0;
}