Harald Welte | c6a8697 | 2019-12-16 23:14:45 +0100 | [diff] [blame] | 1 | #include <osmocom/core/utils.h> |
| 2 | #include <osmocom/core/exec.h> |
| 3 | |
| 4 | #include <sys/types.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <sys/stat.h> |
| 7 | #include <sys/wait.h> |
| 8 | #include <unistd.h> |
| 9 | #include <errno.h> |
| 10 | |
| 11 | static void env_dump(char **env) |
| 12 | { |
| 13 | char **ent; |
| 14 | |
| 15 | for (ent = env; *ent; ent++) |
| 16 | printf("\t%s\n", *ent); |
| 17 | } |
| 18 | |
| 19 | static void test_env_filter(void) |
| 20 | { |
| 21 | char *out[256]; |
| 22 | char *env_in[] = { |
| 23 | "FOO=1", |
| 24 | "BAR=2", |
| 25 | "USER=mahlzeit", |
| 26 | "BAZ=3", |
| 27 | "SHELL=/bin/sh", |
| 28 | NULL |
| 29 | }; |
| 30 | const char *filter[] = { |
| 31 | "SHELL", |
| 32 | "USER", |
| 33 | NULL |
| 34 | }; |
| 35 | int rc; |
| 36 | |
| 37 | printf("\n==== osmo_environment_filter ====\n"); |
| 38 | |
| 39 | printf("Input Environment:\n"); |
| 40 | env_dump(env_in); |
| 41 | printf("Input Whitelist:\n"); |
| 42 | env_dump((char **) filter); |
| 43 | rc = osmo_environment_filter(out, ARRAY_SIZE(out), env_in, filter); |
| 44 | printf("Output Environment (%d):\n", rc); |
| 45 | env_dump(out); |
| 46 | OSMO_ASSERT(rc == 3); |
| 47 | |
| 48 | printf("Testing for NULL out\n"); |
| 49 | rc = osmo_environment_filter(NULL, 123, env_in, filter); |
| 50 | OSMO_ASSERT(rc < 0); |
| 51 | |
| 52 | printf("Testing for zero-length out\n"); |
| 53 | rc = osmo_environment_filter(out, 0, env_in, filter); |
| 54 | OSMO_ASSERT(rc < 0); |
| 55 | |
| 56 | printf("Testing for one-length out\n"); |
| 57 | rc = osmo_environment_filter(out, 1, env_in, filter); |
| 58 | OSMO_ASSERT(rc == 1 && out[0] == NULL); |
| 59 | |
| 60 | printf("Testing for no filter\n"); |
| 61 | rc = osmo_environment_filter(out, ARRAY_SIZE(out), env_in, NULL); |
| 62 | OSMO_ASSERT(rc < 0); |
| 63 | |
| 64 | printf("Testing for no input\n"); |
| 65 | rc = osmo_environment_filter(out, ARRAY_SIZE(out), NULL, filter); |
| 66 | OSMO_ASSERT(rc == 1 && out[0] == NULL); |
| 67 | printf("Success!\n"); |
| 68 | } |
| 69 | |
| 70 | static void test_env_append(void) |
| 71 | { |
| 72 | char *out[256] = { |
| 73 | "FOO=a", |
| 74 | "BAR=b", |
| 75 | "BAZ=c", |
| 76 | NULL, |
| 77 | }; |
| 78 | char *add[] = { |
| 79 | "MAHL=zeit", |
| 80 | "GSM=global", |
| 81 | "UMTS=universal", |
| 82 | "LTE=evolved", |
| 83 | NULL, |
| 84 | }; |
| 85 | int rc; |
| 86 | |
| 87 | printf("\n==== osmo_environment_append ====\n"); |
| 88 | |
| 89 | printf("Input Environment:\n"); |
| 90 | env_dump(out); |
| 91 | printf("Input Addition:\n"); |
| 92 | env_dump(add); |
| 93 | rc = osmo_environment_append(out, ARRAY_SIZE(out), add); |
| 94 | printf("Output Environment (%d)\n", rc); |
| 95 | env_dump(out); |
| 96 | OSMO_ASSERT(rc == 8); |
| 97 | printf("Success!\n"); |
| 98 | } |
| 99 | |
| 100 | static void test_close_fd(void) |
| 101 | { |
| 102 | struct stat st; |
| 103 | int fds[2]; |
| 104 | int rc; |
| 105 | |
| 106 | printf("\n==== osmo_close_all_fds_above ====\n"); |
| 107 | |
| 108 | /* create some extra fds */ |
| 109 | rc = socketpair(AF_UNIX, SOCK_STREAM, 0, fds); |
| 110 | OSMO_ASSERT(rc == 0); |
| 111 | |
| 112 | rc = fstat(fds[0], &st); |
| 113 | OSMO_ASSERT(rc == 0); |
| 114 | |
| 115 | osmo_close_all_fds_above(2); |
| 116 | |
| 117 | rc = fstat(fds[0], &st); |
| 118 | OSMO_ASSERT(rc == -1 && errno == EBADF); |
| 119 | rc = fstat(fds[1], &st); |
| 120 | OSMO_ASSERT(rc == -1 && errno == EBADF); |
| 121 | printf("Success!\n"); |
| 122 | } |
| 123 | |
| 124 | static void test_system_nowait(void) |
| 125 | { |
| 126 | char *addl_env[] = { |
| 127 | "MAHLZEIT=spaet", |
| 128 | NULL |
| 129 | }; |
| 130 | int rc, pid, i; |
| 131 | |
| 132 | printf("\n==== osmo_system_nowait ====\n"); |
| 133 | |
| 134 | pid = osmo_system_nowait("env | grep MAHLZEIT 1>&2", osmo_environment_whitelist, addl_env); |
| 135 | OSMO_ASSERT(pid > 0); |
| 136 | for (i = 0; i < 10; i++) { |
| 137 | sleep(1); |
| 138 | rc = waitpid(pid, NULL, WNOHANG); |
| 139 | if (rc == pid) { |
| 140 | printf("Success!\n"); |
| 141 | return; |
| 142 | } |
| 143 | } |
| 144 | printf("ERROR: child didn't terminate within 10s\n"); |
| 145 | } |
| 146 | |
| 147 | int main(int argc, char **argv) |
| 148 | { |
| 149 | test_env_filter(); |
| 150 | test_env_append(); |
| 151 | test_close_fd(); |
| 152 | test_system_nowait(); |
| 153 | |
| 154 | exit(0); |
| 155 | } |