1 | /* |
---|
2 | * Test suite for environment variables set by the server. |
---|
3 | * |
---|
4 | * Written by Russ Allbery <rra@stanford.edu> |
---|
5 | * Copyright 2006, 2009 Board of Trustees, Leland Stanford Jr. University |
---|
6 | * |
---|
7 | * See LICENSE for licensing terms. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <config.h> |
---|
11 | #include <portable/system.h> |
---|
12 | |
---|
13 | #include <signal.h> |
---|
14 | #include <sys/wait.h> |
---|
15 | |
---|
16 | #include <client/internal.h> |
---|
17 | #include <client/remctl.h> |
---|
18 | #include <tests/tap/basic.h> |
---|
19 | #include <tests/tap/kerberos.h> |
---|
20 | #include <tests/tap/remctl.h> |
---|
21 | #include <util/util.h> |
---|
22 | |
---|
23 | |
---|
24 | /* |
---|
25 | * Run the remote env command with the given variable and return the value |
---|
26 | * from the server or NULL if there was an error. |
---|
27 | */ |
---|
28 | static char * |
---|
29 | test_env(struct remctl *r, const char *variable) |
---|
30 | { |
---|
31 | struct remctl_output *output; |
---|
32 | char *value = NULL; |
---|
33 | const char *command[] = { "test", "env", NULL, NULL }; |
---|
34 | |
---|
35 | command[2] = variable; |
---|
36 | if (!remctl_command(r, command)) { |
---|
37 | notice("# remctl error %s", remctl_error(r)); |
---|
38 | return NULL; |
---|
39 | } |
---|
40 | do { |
---|
41 | output = remctl_output(r); |
---|
42 | switch (output->type) { |
---|
43 | case REMCTL_OUT_OUTPUT: |
---|
44 | value = xstrndup(output->data, output->length); |
---|
45 | break; |
---|
46 | case REMCTL_OUT_STATUS: |
---|
47 | if (output->status != 0) { |
---|
48 | if (value != NULL) |
---|
49 | free(value); |
---|
50 | notice("# test env returned status %d", output->status); |
---|
51 | return NULL; |
---|
52 | } |
---|
53 | if (value == NULL) |
---|
54 | value = xstrdup(""); |
---|
55 | return value; |
---|
56 | case REMCTL_OUT_ERROR: |
---|
57 | if (value != NULL) |
---|
58 | free(value); |
---|
59 | notice("# test env returned error: %.*s", (int) output->length, |
---|
60 | output->data); |
---|
61 | return NULL; |
---|
62 | case REMCTL_OUT_DONE: |
---|
63 | if (value != NULL) |
---|
64 | free(value); |
---|
65 | notice("# unexpected done token"); |
---|
66 | return NULL; |
---|
67 | } |
---|
68 | } while (output->type == REMCTL_OUT_OUTPUT); |
---|
69 | return value; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | int |
---|
74 | main(void) |
---|
75 | { |
---|
76 | char *principal, *config, *path, *expected, *value; |
---|
77 | struct remctl *r; |
---|
78 | pid_t remctld; |
---|
79 | |
---|
80 | /* Unless we have Kerberos available, we can't really do anything. */ |
---|
81 | if (chdir(getenv("SOURCE")) < 0) |
---|
82 | bail("can't chdir to SOURCE"); |
---|
83 | principal = kerberos_setup(); |
---|
84 | if (principal == NULL) |
---|
85 | skip_all("Kerberos tests not configured"); |
---|
86 | plan(4); |
---|
87 | config = concatpath(getenv("SOURCE"), "data/conf-simple"); |
---|
88 | path = concatpath(getenv("BUILD"), "../server/remctld"); |
---|
89 | remctld = remctld_start(path, principal, config); |
---|
90 | |
---|
91 | /* Run the tests. */ |
---|
92 | r = remctl_new(); |
---|
93 | if (!remctl_open(r, "localhost", 14373, principal)) |
---|
94 | bail("cannot contact remctld"); |
---|
95 | expected = concat(principal, "\n", NULL); |
---|
96 | value = test_env(r, "REMUSER"); |
---|
97 | is_string(expected, value, "value for REMUSER"); |
---|
98 | free(value); |
---|
99 | value = test_env(r, "REMOTE_USER"); |
---|
100 | is_string(expected, value, "value for REMOTE_USER"); |
---|
101 | free(value); |
---|
102 | value = test_env(r, "REMOTE_ADDR"); |
---|
103 | is_string("127.0.0.1\n", value, "value for REMOTE_ADDR"); |
---|
104 | free(value); |
---|
105 | value = test_env(r, "REMOTE_HOST"); |
---|
106 | ok(strcmp(value, "\n") == 0 || strstr(value, "localhost") != NULL, |
---|
107 | "value for REMOTE_HOST"); |
---|
108 | free(value); |
---|
109 | remctl_close(r); |
---|
110 | |
---|
111 | remctld_stop(remctld); |
---|
112 | kerberos_cleanup(); |
---|
113 | return 0; |
---|
114 | } |
---|