1 // SPDX-License-Identifier: GPL-2.0 2 3 #define _GNU_SOURCE 4 #include <check.h> 5 #include <signal.h> 6 7 #include "../../src/actions.h" 8 9 static struct actions actions_fixture; 10 11 static void actions_fixture_setup(void) 12 { 13 actions_init(&actions_fixture); 14 } 15 16 static void actions_fixture_teardown(void) 17 { 18 actions_destroy(&actions_fixture); 19 } 20 21 START_TEST(test_actions_init) 22 { 23 struct actions actions; 24 25 actions_init(&actions); 26 27 ck_assert_int_eq(actions.len, 0); 28 ck_assert_int_eq(actions.size, action_default_size); 29 ck_assert(!actions.continue_flag); 30 ck_assert_ptr_eq(actions.trace_output_inst, NULL); 31 } 32 END_TEST 33 34 START_TEST(test_actions_destroy) 35 { 36 struct actions actions; 37 38 actions_init(&actions); 39 actions_destroy(&actions); 40 } 41 END_TEST 42 43 START_TEST(test_actions_reallocate) 44 { 45 struct actions actions; 46 int i; 47 48 actions_init(&actions); 49 50 ck_assert_int_eq(actions.len, 0); 51 ck_assert_int_eq(actions.size, action_default_size); 52 53 /* Fill size of actions array */ 54 for (i = 0; i < action_default_size; i++) 55 actions_add_continue(&actions); 56 57 ck_assert_int_eq(actions.len, action_default_size); 58 ck_assert_int_eq(actions.size, action_default_size); 59 60 /* Add one more action to trigger reallocation */ 61 actions_add_continue(&actions); 62 63 ck_assert_int_eq(actions.len, action_default_size + 1); 64 ck_assert_int_eq(actions.size, action_default_size * 2); 65 66 actions_destroy(&actions); 67 } 68 END_TEST 69 70 START_TEST(test_actions_add_trace_output) 71 { 72 actions_add_trace_output(&actions_fixture, "trace_output.txt"); 73 74 ck_assert_int_eq(actions_fixture.len, 1); 75 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_TRACE_OUTPUT); 76 ck_assert_str_eq(actions_fixture.list[0].trace_output, "trace_output.txt"); 77 ck_assert(actions_fixture.present[ACTION_TRACE_OUTPUT]); 78 } 79 END_TEST 80 81 START_TEST(test_actions_add_signal) 82 { 83 actions_add_signal(&actions_fixture, SIGINT, 1234); 84 85 ck_assert_int_eq(actions_fixture.len, 1); 86 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_SIGNAL); 87 ck_assert_int_eq(actions_fixture.list[0].signal, SIGINT); 88 ck_assert_int_eq(actions_fixture.list[0].pid, 1234); 89 ck_assert(actions_fixture.present[ACTION_SIGNAL]); 90 } 91 END_TEST 92 93 START_TEST(test_actions_add_shell) 94 { 95 actions_add_shell(&actions_fixture, "echo Hello"); 96 97 ck_assert_int_eq(actions_fixture.len, 1); 98 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_SHELL); 99 ck_assert_str_eq(actions_fixture.list[0].command, "echo Hello"); 100 ck_assert(actions_fixture.present[ACTION_SHELL]); 101 } 102 END_TEST 103 104 START_TEST(test_actions_add_continue) 105 { 106 actions_add_continue(&actions_fixture); 107 108 ck_assert_int_eq(actions_fixture.len, 1); 109 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_CONTINUE); 110 ck_assert(actions_fixture.present[ACTION_CONTINUE]); 111 } 112 END_TEST 113 114 START_TEST(test_actions_add_multiple_same_action) 115 { 116 actions_add_trace_output(&actions_fixture, "trace1.txt"); 117 actions_add_trace_output(&actions_fixture, "trace2.txt"); 118 119 ck_assert_int_eq(actions_fixture.len, 2); 120 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_TRACE_OUTPUT); 121 ck_assert_str_eq(actions_fixture.list[0].trace_output, "trace1.txt"); 122 ck_assert_int_eq(actions_fixture.list[1].type, ACTION_TRACE_OUTPUT); 123 ck_assert_str_eq(actions_fixture.list[1].trace_output, "trace2.txt"); 124 ck_assert(actions_fixture.present[ACTION_TRACE_OUTPUT]); 125 } 126 END_TEST 127 128 START_TEST(test_actions_add_multiple_different_action) 129 { 130 actions_add_trace_output(&actions_fixture, "trace_output.txt"); 131 actions_add_signal(&actions_fixture, SIGINT, 1234); 132 133 ck_assert_int_eq(actions_fixture.len, 2); 134 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_TRACE_OUTPUT); 135 ck_assert_str_eq(actions_fixture.list[0].trace_output, "trace_output.txt"); 136 ck_assert(actions_fixture.present[ACTION_TRACE_OUTPUT]); 137 ck_assert_int_eq(actions_fixture.list[1].type, ACTION_SIGNAL); 138 ck_assert_int_eq(actions_fixture.list[1].signal, SIGINT); 139 ck_assert_int_eq(actions_fixture.list[1].pid, 1234); 140 ck_assert(actions_fixture.present[ACTION_SIGNAL]); 141 } 142 END_TEST 143 144 START_TEST(test_actions_parse_trace_output) 145 { 146 ck_assert_int_eq(actions_parse(&actions_fixture, "trace", "trace.txt"), 0); 147 148 ck_assert_int_eq(actions_fixture.len, 1); 149 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_TRACE_OUTPUT); 150 ck_assert_str_eq(actions_fixture.list[0].trace_output, "trace.txt"); 151 ck_assert(actions_fixture.present[ACTION_TRACE_OUTPUT]); 152 } 153 END_TEST 154 155 START_TEST(test_actions_parse_trace_output_arg) 156 { 157 ck_assert_int_eq(actions_parse(&actions_fixture, "trace,file=trace2.txt", "trace1.txt"), 0); 158 159 ck_assert_int_eq(actions_fixture.len, 1); 160 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_TRACE_OUTPUT); 161 ck_assert_str_eq(actions_fixture.list[0].trace_output, "trace2.txt"); 162 ck_assert(actions_fixture.present[ACTION_TRACE_OUTPUT]); 163 } 164 END_TEST 165 166 START_TEST(test_actions_parse_trace_output_arg_bad) 167 { 168 ck_assert_int_eq(actions_parse(&actions_fixture, "trace,foo=bar", "trace_output.txt"), -1); 169 170 ck_assert_int_eq(actions_fixture.len, 0); 171 ck_assert(!actions_fixture.present[ACTION_TRACE_OUTPUT]); 172 } 173 END_TEST 174 175 START_TEST(test_actions_parse_signal) 176 { 177 ck_assert_int_eq(actions_parse(&actions_fixture, "signal,num=1,pid=1234", NULL), 0); 178 179 ck_assert_int_eq(actions_fixture.len, 1); 180 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_SIGNAL); 181 ck_assert_int_eq(actions_fixture.list[0].signal, 1); 182 ck_assert_int_eq(actions_fixture.list[0].pid, 1234); 183 ck_assert(actions_fixture.present[ACTION_SIGNAL]); 184 } 185 END_TEST 186 187 START_TEST(test_actions_parse_signal_swapped) 188 { 189 ck_assert_int_eq(actions_parse(&actions_fixture, "signal,pid=1234,num=1", NULL), 0); 190 191 ck_assert_int_eq(actions_fixture.len, 1); 192 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_SIGNAL); 193 ck_assert_int_eq(actions_fixture.list[0].signal, 1); 194 ck_assert_int_eq(actions_fixture.list[0].pid, 1234); 195 ck_assert(actions_fixture.present[ACTION_SIGNAL]); 196 } 197 END_TEST 198 199 START_TEST(test_actions_parse_signal_parent) 200 { 201 ck_assert_int_eq(actions_parse(&actions_fixture, "signal,pid=parent,num=1", NULL), 0); 202 203 ck_assert_int_eq(actions_fixture.len, 1); 204 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_SIGNAL); 205 ck_assert_int_eq(actions_fixture.list[0].signal, 1); 206 ck_assert_int_eq(actions_fixture.list[0].pid, -1); 207 ck_assert(actions_fixture.present[ACTION_SIGNAL]); 208 } 209 END_TEST 210 211 START_TEST(test_actions_parse_signal_no_arg) 212 { 213 ck_assert_int_eq(actions_parse(&actions_fixture, "signal", NULL), -1); 214 215 ck_assert_int_eq(actions_fixture.len, 0); 216 ck_assert(!actions_fixture.present[ACTION_SIGNAL]); 217 } 218 END_TEST 219 220 START_TEST(test_actions_parse_signal_no_pid) 221 { 222 ck_assert_int_eq(actions_parse(&actions_fixture, "signal,num=1", NULL), -1); 223 224 ck_assert_int_eq(actions_fixture.len, 0); 225 ck_assert(!actions_fixture.present[ACTION_SIGNAL]); 226 } 227 END_TEST 228 229 START_TEST(test_actions_parse_signal_no_num) 230 { 231 ck_assert_int_eq(actions_parse(&actions_fixture, "signal,pid=1234", NULL), -1); 232 233 ck_assert_int_eq(actions_fixture.len, 0); 234 ck_assert(!actions_fixture.present[ACTION_SIGNAL]); 235 } 236 END_TEST 237 238 START_TEST(test_actions_parse_signal_arg_bad) 239 { 240 ck_assert_int_eq(actions_parse(&actions_fixture, "signal,foo=bar", NULL), -1); 241 242 ck_assert_int_eq(actions_fixture.len, 0); 243 ck_assert(!actions_fixture.present[ACTION_SIGNAL]); 244 } 245 END_TEST 246 247 START_TEST(test_actions_parse_shell) 248 { 249 ck_assert_int_eq(actions_parse(&actions_fixture, "shell,command=echo Hello", NULL), 0); 250 251 ck_assert_int_eq(actions_fixture.len, 1); 252 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_SHELL); 253 ck_assert_str_eq(actions_fixture.list[0].command, "echo Hello"); 254 ck_assert(actions_fixture.present[ACTION_SHELL]); 255 } 256 END_TEST 257 258 START_TEST(test_actions_parse_shell_no_arg) 259 { 260 ck_assert_int_eq(actions_parse(&actions_fixture, "shell", NULL), -1); 261 262 ck_assert_int_eq(actions_fixture.len, 0); 263 ck_assert(!actions_fixture.present[ACTION_SHELL]); 264 } 265 END_TEST 266 267 START_TEST(test_actions_parse_shell_arg_bad) 268 { 269 ck_assert_int_eq(actions_parse(&actions_fixture, "shell,foo=bar", NULL), -1); 270 ck_assert_int_eq(actions_fixture.len, 0); 271 ck_assert(!actions_fixture.present[ACTION_SHELL]); 272 } 273 END_TEST 274 275 START_TEST(test_actions_parse_continue) 276 { 277 ck_assert_int_eq(actions_parse(&actions_fixture, "continue", NULL), 0); 278 279 ck_assert_int_eq(actions_fixture.len, 1); 280 ck_assert_int_eq(actions_fixture.list[0].type, ACTION_CONTINUE); 281 ck_assert(actions_fixture.present[ACTION_CONTINUE]); 282 } 283 END_TEST 284 285 START_TEST(test_actions_parse_continue_arg_bad) 286 { 287 ck_assert_int_eq(actions_parse(&actions_fixture, "continue,foo=bar", NULL), -1); 288 289 ck_assert_int_eq(actions_fixture.len, 0); 290 ck_assert(!actions_fixture.present[ACTION_CONTINUE]); 291 } 292 END_TEST 293 294 START_TEST(test_actions_parse_invalid) 295 { 296 ck_assert_int_eq(actions_parse(&actions_fixture, "foobar", NULL), -1); 297 298 ck_assert_int_eq(actions_fixture.len, 0); 299 } 300 END_TEST 301 302 START_TEST(test_actions_perform_continue) 303 { 304 actions_add_continue(&actions_fixture); 305 ck_assert_int_eq(actions_perform(&actions_fixture), 0); 306 307 ck_assert(actions_fixture.continue_flag); 308 } 309 END_TEST 310 311 START_TEST(test_actions_perform_continue_after_successful_shell_command) 312 { 313 actions_add_shell(&actions_fixture, "exit 0"); 314 actions_add_continue(&actions_fixture); 315 ck_assert_int_eq(actions_perform(&actions_fixture), 0 << 8); 316 317 ck_assert(actions_fixture.continue_flag); 318 } 319 END_TEST 320 321 START_TEST(test_actions_perform_continue_after_failed_shell_command) 322 { 323 actions_add_shell(&actions_fixture, "exit 1"); 324 actions_add_continue(&actions_fixture); 325 ck_assert_int_eq(actions_perform(&actions_fixture), 1 << 8); 326 327 ck_assert(!actions_fixture.continue_flag); 328 } 329 END_TEST 330 331 START_TEST(test_actions_perform_continue_unset_flag) 332 { 333 actions_fixture.continue_flag = true; 334 335 actions_add_shell(&actions_fixture, "exit 1"); 336 actions_add_continue(&actions_fixture); 337 ck_assert_int_eq(actions_perform(&actions_fixture), 1 << 8); 338 339 ck_assert(!actions_fixture.continue_flag); 340 } 341 END_TEST 342 343 Suite *actions_suite(void) 344 { 345 Suite *s = suite_create("actions"); 346 TCase *tc; 347 348 tc = tcase_create("alloc"); 349 tcase_add_test(tc, test_actions_init); 350 tcase_add_test(tc, test_actions_destroy); 351 tcase_add_test(tc, test_actions_reallocate); 352 suite_add_tcase(s, tc); 353 354 tc = tcase_create("add"); 355 tcase_add_checked_fixture(tc, actions_fixture_setup, actions_fixture_teardown); 356 tcase_add_test(tc, test_actions_add_trace_output); 357 tcase_add_test(tc, test_actions_add_signal); 358 tcase_add_test(tc, test_actions_add_shell); 359 tcase_add_test(tc, test_actions_add_continue); 360 tcase_add_test(tc, test_actions_add_multiple_same_action); 361 tcase_add_test(tc, test_actions_add_multiple_different_action); 362 suite_add_tcase(s, tc); 363 364 tc = tcase_create("parse"); 365 tcase_add_checked_fixture(tc, actions_fixture_setup, actions_fixture_teardown); 366 tcase_add_test(tc, test_actions_parse_trace_output); 367 tcase_add_test(tc, test_actions_parse_trace_output_arg); 368 tcase_add_test(tc, test_actions_parse_trace_output_arg_bad); 369 tcase_add_test(tc, test_actions_parse_signal); 370 tcase_add_test(tc, test_actions_parse_signal_swapped); 371 tcase_add_test(tc, test_actions_parse_signal_parent); 372 tcase_add_test(tc, test_actions_parse_signal_no_arg); 373 tcase_add_test(tc, test_actions_parse_signal_no_pid); 374 tcase_add_test(tc, test_actions_parse_signal_no_num); 375 tcase_add_test(tc, test_actions_parse_signal_arg_bad); 376 tcase_add_test(tc, test_actions_parse_shell); 377 tcase_add_test(tc, test_actions_parse_shell_no_arg); 378 tcase_add_test(tc, test_actions_parse_shell_arg_bad); 379 tcase_add_test(tc, test_actions_parse_continue); 380 tcase_add_test(tc, test_actions_parse_continue_arg_bad); 381 tcase_add_test(tc, test_actions_parse_invalid); 382 suite_add_tcase(s, tc); 383 384 tc = tcase_create("perform"); 385 tcase_add_checked_fixture(tc, actions_fixture_setup, actions_fixture_teardown); 386 tcase_add_test(tc, test_actions_perform_continue); 387 tcase_add_test(tc, test_actions_perform_continue_after_successful_shell_command); 388 tcase_add_test(tc, test_actions_perform_continue_after_failed_shell_command); 389 tcase_add_test(tc, test_actions_perform_continue_unset_flag); 390 suite_add_tcase(s, tc); 391 392 return s; 393 } 394