aboutsummaryrefslogtreecommitdiffstats
path: root/tests/exec/exec_test.c
blob: 5f4b460c117fd88e142f1b1d1f387ed5f9082ea4 (plain)
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
#include <osmocom/core/utils.h>
#include <osmocom/core/exec.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

static void env_dump(char **env)
{
	char **ent;

	for (ent = env; *ent; ent++)
		printf("\t%s\n", *ent);
}

static void test_env_filter(void)
{
	char *out[256];
	char *env_in[] = {
		"FOO=1",
		"BAR=2",
		"USER=mahlzeit",
		"BAZ=3",
		"SHELL=/bin/sh",
		NULL
	};
	const char *filter[] = {
		"SHELL",
		"USER",
		NULL
	};
	int rc;

	printf("\n==== osmo_environment_filter ====\n");

	printf("Input Environment:\n");
	env_dump(env_in);
	printf("Input Whitelist:\n");
	env_dump((char **) filter);
	rc = osmo_environment_filter(out, ARRAY_SIZE(out), env_in, filter);
	printf("Output Environment (%d):\n", rc);
	env_dump(out);
	OSMO_ASSERT(rc == 3);

	printf("Testing for NULL out\n");
	rc = osmo_environment_filter(NULL, 123, env_in, filter);
	OSMO_ASSERT(rc < 0);

	printf("Testing for zero-length out\n");
	rc = osmo_environment_filter(out, 0, env_in, filter);
	OSMO_ASSERT(rc < 0);

	printf("Testing for one-length out\n");
	rc = osmo_environment_filter(out, 1, env_in, filter);
	OSMO_ASSERT(rc == 1 && out[0] == NULL);

	printf("Testing for no filter\n");
	rc = osmo_environment_filter(out, ARRAY_SIZE(out), env_in, NULL);
	OSMO_ASSERT(rc < 0);

	printf("Testing for no input\n");
	rc = osmo_environment_filter(out, ARRAY_SIZE(out), NULL, filter);
	OSMO_ASSERT(rc == 1 && out[0] == NULL);
	printf("Success!\n");
}

static void test_env_append(void)
{
	char *out[256] = {
		"FOO=a",
		"BAR=b",
		"BAZ=c",
		NULL,
	};
	char *add[] = {
		"MAHL=zeit",
		"GSM=global",
		"UMTS=universal",
		"LTE=evolved",
		NULL,
	};
	int rc;

	printf("\n==== osmo_environment_append ====\n");

	printf("Input Environment:\n");
	env_dump(out);
	printf("Input Addition:\n");
	env_dump(add);
	rc = osmo_environment_append(out, ARRAY_SIZE(out), add);
	printf("Output Environment (%d)\n", rc);
	env_dump(out);
	OSMO_ASSERT(rc == 8);
	printf("Success!\n");
}

static void test_close_fd(void)
{
	struct stat st;
	int fds[2];
	int rc;

	printf("\n==== osmo_close_all_fds_above ====\n");

	/* create some extra fds */
	rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds);
	OSMO_ASSERT(rc == 0);

	rc = fstat(fds[0], &st);
	OSMO_ASSERT(rc == 0);

	osmo_close_all_fds_above(2);

	rc = fstat(fds[0], &st);
	OSMO_ASSERT(rc == -1 && errno == EBADF);
	rc = fstat(fds[1], &st);
	OSMO_ASSERT(rc == -1 && errno == EBADF);
	printf("Success!\n");
}

static void test_system_nowait(void)
{
	char *addl_env[] = {
		"MAHLZEIT=spaet",
		NULL
	};
	int rc, pid, i;

	printf("\n==== osmo_system_nowait ====\n");

	pid = osmo_system_nowait("env | grep MAHLZEIT 1>&2", osmo_environment_whitelist, addl_env);
	OSMO_ASSERT(pid > 0);
	for (i = 0; i < 10; i++) {
		sleep(1);
		rc = waitpid(pid, NULL, WNOHANG);
		if (rc == pid) {
			printf("Success!\n");
			return;
		}
	}
	printf("ERROR: child didn't terminate within 10s\n");
}

int main(int argc, char **argv)
{
	test_env_filter();
	test_env_append();
	test_close_fd();
	test_system_nowait();

	exit(0);
}