|
| 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