File: test_exec.c

package info (click to toggle)
libdebian-installer 0.127
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 680 kB
  • sloc: ansic: 5,007; makefile: 182
file content (77 lines) | stat: -rw-r--r-- 1,890 bytes parent folder | download | duplicates (6)
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
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <check.h>

#include <debian-installer/types.h>
#include <debian-installer/exec.h>

#include "test_exec.h"

START_TEST(test_exec)
{
  const char* argv[] = {"/bin/echo", "test", NULL};
  int retval = di_exec(argv[0], argv);
  ck_assert_int_eq(di_exec_mangle_status(retval), 0);
}
END_TEST

START_TEST(test_exec_fail)
{
  const char* argv[] = {"/bin/false", NULL};
  int retval = di_exec(argv[0], argv);
  ck_assert_int_eq(di_exec_mangle_status(retval), 1);
}
END_TEST

START_TEST(test_exec_shell)
{
  int retval = di_exec_shell("/bin/echo test");
  ck_assert_int_eq(di_exec_mangle_status(retval), 0);
}
END_TEST

START_TEST(test_exec_shell_fail)
{
  int retval = di_exec_shell("/bin/false ignored arguments");
  ck_assert_int_eq(di_exec_mangle_status(retval), 1);
}
END_TEST

static void* outbuf = NULL;
static size_t outlen = 0;

static int stdout_handler(const char* buf, size_t len, __attribute__ ((unused)) void *user_data) {
  outbuf = realloc(outbuf, outlen + len);
  memcpy(outbuf + outlen, buf, len);
  outlen += len;
  return 0;
}

START_TEST(test_exec_stdout_capture)
{
  const char* expected_output = "test";
  int retval;
  retval = di_exec_shell_full("/bin/echo test", stdout_handler, NULL, NULL, NULL, NULL, NULL, NULL);
  ck_assert_int_eq(di_exec_mangle_status(retval), 0);
  ck_assert_int_eq(outlen, strlen(expected_output));
  ck_assert_int_eq(strncmp(expected_output, outbuf, outlen), 0);
}
END_TEST

Suite* make_test_exec_suite() {
  Suite *s;
  TCase *tc_core;

  s = suite_create("test exec");
  tc_core = tcase_create("Core");
  tcase_add_test(tc_core, test_exec);
  tcase_add_test(tc_core, test_exec_fail);
  tcase_add_test(tc_core, test_exec_shell);
  tcase_add_test(tc_core, test_exec_shell_fail);
  tcase_add_test(tc_core, test_exec_stdout_capture);
  suite_add_tcase(s, tc_core);

  return s;
}