Debian Codegraph @

testing/fulltests/unit-tests/T024run_exec_command_clib.c

raw
1 /*
2 * HEADER Testing run_exec_command()
3 */
4
5 struct test_data {
6 const char *command;
7 const char *input;
8 const char *expected_output;
9 int res;
10 };
11
12 static struct test_data test_data[] = {
13 { "/bin/sh -c 'echo a'", NULL, "a\n", 0 },
14 { "/bin/sh -c 'echo a b'", NULL, "a b\n", 0 },
15 { "/bin/sh -c 'echo $*' . 0 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",
16 NULL,
17 "0 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\n",
18 0 },
19 { "/bin/cat", "b c\n", "b c\n", 0 },
20 { "/bin/sh -c 'false'", NULL, NULL, 1 },
21 { "/bin/sh -c 'exit 77'", NULL, NULL, 77 },
22 };
23
24 #if 0
25 snmp_set_do_debugging(TRUE);
26 debug_register_tokens("run:exec");
27 #endif
28
29 {
30 char output[512];
31 int output_len;
32 int i;
33
34 for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
35 const struct test_data *p = &test_data[i];
36 int res;
37
38 output_len = sizeof(output);
39 res = run_exec_command(p->command, p->input, output, &output_len);
40 OKF(res == p->res, ("%s: return value %d <> %d", p->command, res,
41 p->res));
42 if (res == p->res && p->expected_output) {
43 OKF(strcmp(output, p->expected_output) == 0,
44 ("%s: output %s <> %s", p->command, output,
45 p->expected_output));
46 }
47 }
48 }