Skip to content

Commit f65a14f

Browse files
committed
Add test for userproxy mode
This is a very basic test that checks the gssproxy can start, and that socket activation works. Signed-off-by: Simo Sorce <simo@redhat.com>
1 parent d122248 commit f65a14f

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ cli_srv_conn
3939
cli_srv_comm
4040
gssproxy.spec
4141
interposetest
42+
userproxytest
4243
gssproxy.service
4344
gssuserproxy.service
4445
gssuserproxy.socket

Makefile.am

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ CLEANFILES = *.X */*.X */*/*.X \
369369
systemd/gssuserproxy.socket
370370

371371
check: all $(check_PROGRAMS)
372+
if HAVE_SYSTEMD_DAEMON
373+
$(srcdir)/tests/userproxytest
374+
endif
372375
$(srcdir)/tests/runtests.py $(CHECKARGS)
373376

374377
tests: check

external/systemd.m4

+2
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ AC_DEFUN([AM_CHECK_SYSTEMD],
2323
[AC_MSG_NOTICE([Build without $daemon_lib_name support])])],
2424
[AC_MSG_NOTICE([Build without $daemon_lib_name support])])
2525
26+
AM_CONDITIONAL([HAVE_SYSTEMD_DAEMON], [test x"$daemon_lib_name" != x])
27+
AC_MSG_NOTICE([Will enable systemd socket activation])
2628
])

tests/Makefile.am

+7
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ check_PROGRAMS = \
5757
t_setcredopt \
5858
$(NULL)
5959

60+
if HAVE_SYSTEMD_DAEMON
61+
userproxytest_SOURCES = \
62+
userproxytest.c
63+
64+
check_PROGRAMS += userproxytest
65+
endif
66+
6067
noinst_PROGRAMS = $(check_PROGRAMS)
6168

6269
noinst_HEADERS = \

tests/userproxytest.c

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* Copyright (C) 2022 the GSS-PROXY contributors, see COPYING for license */
2+
3+
#define _GNU_SOURCE
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <sys/socket.h>
7+
#include <sys/un.h>
8+
#include <sys/wait.h>
9+
#include <unistd.h>
10+
11+
char *srv_args[] = {
12+
"./gssproxy",
13+
"-u", "-i",
14+
"-s", "./testdir/userproxytest.sock",
15+
"--idle-timeout=3"
16+
};
17+
18+
int mock_activation_sockets(void)
19+
{
20+
struct sockaddr_un addr = {
21+
.sun_family = AF_UNIX,
22+
.sun_path = "./testdir/userproxytest.sock",
23+
};
24+
int fd;
25+
int ret;
26+
27+
unlink(addr.sun_path);
28+
29+
fd = socket(AF_UNIX, SOCK_STREAM, 0);
30+
if (fd == -1) return -1;
31+
32+
ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
33+
if (ret == -1) return -1;
34+
35+
ret = listen(fd, 1);
36+
if (ret == -1) return -1;
37+
38+
return 0;
39+
}
40+
41+
int mock_activation_environment(void)
42+
{
43+
char *onestr = "1";
44+
char *pidstr;
45+
int ret;
46+
47+
ret = asprintf(&pidstr, "%u", (unsigned)getpid());
48+
if (ret == -1) return -1;
49+
50+
setenv("LISTEN_PID", pidstr, 1);
51+
setenv("LISTEN_FDS", onestr, 1);
52+
53+
free(pidstr);
54+
return 0;
55+
}
56+
57+
int main(int argc, const char *main_argv[])
58+
{
59+
pid_t proxy, w;
60+
int ret;
61+
62+
fprintf(stderr, "Test userproxy mode: ");
63+
64+
ret = mock_activation_sockets();
65+
if (ret) return -1;
66+
67+
proxy = fork();
68+
if (proxy == -1) return -1;
69+
70+
if (proxy == 0) {
71+
ret = mock_activation_environment();
72+
if (ret) return -1;
73+
74+
execv("./gssproxy", srv_args);
75+
return -1;
76+
}
77+
78+
sleep(6);
79+
80+
w = waitpid(-1, &ret, WNOHANG);
81+
if (w != proxy || ret != 0) {
82+
fprintf(stderr, "FAIL\n");
83+
fflush(stderr);
84+
return -1;
85+
}
86+
87+
fprintf(stderr, "SUCCESS\n");
88+
fflush(stderr);
89+
return 0;
90+
}

0 commit comments

Comments
 (0)