Import of code from c-tap-harness
[openafs.git] / tests / volser / vos-t.c
1 #include <afsconfig.h>
2 #include <afs/param.h>
3
4 #include <roken.h>
5
6 #include <sys/wait.h>
7
8 #include <rx/rx.h>
9 #include <ubik.h>
10
11 #include <afs/com_err.h>
12 #include <afs/vldbint.h>
13 #include <afs/cellconfig.h>
14
15 #include <tests/tap/basic.h>
16
17 #include "common.h"
18
19 /* This checks for a bug in vos where it would fail to allocate additional
20  * space for the results of multi homed VL_GetAddrsU, and so would segfault
21  * if a host with a small number of addresses was immediately followed by
22  * a host with a large number of addresses.
23  */
24 void
25 TestListAddrs(struct ubik_client *client, char *dirname)
26 {
27     int code;
28     bulkaddrs addrs;
29     pid_t pid;
30     int outpipe[2];
31     int status;
32     char *buffer;
33     size_t len;
34
35     afs_uint32 addrsA[] = {0x0a000000};
36     afs_uint32 addrsB[] = {0x0a000001, 0x0a000002, 0x0a000003, 0x0a000004,
37                            0x0a000005, 0x0a000006, 0x0a000007, 0x0a000008,
38                            0x0a000009, 0x0a00000a, 0x0a00000b, 0x0a00000c,
39                            0x0a00000d, 0x0a00000e, 0x0a00000f};
40     char uuidA[] = {0x4F, 0x44, 0x94, 0x47, 0x76, 0xBA, 0x47, 0x2C, 0x97, 0x1A,
41                     0x86, 0x6B, 0xC0, 0x10, 0x1A, 0x4B};
42     char uuidB[] = {0x5D, 0x2A, 0x39, 0x36, 0x94, 0xB2, 0x48, 0x90, 0xA8, 0xD2,
43                     0x7F, 0xBC, 0x1B, 0x29, 0xDA, 0x9B};
44     char expecting[] = "10.0.0.0\n10.0.0.1\n10.0.0.2\n10.0.0.3\n10.0.0.4\n"
45                        "10.0.0.5\n10.0.0.6\n10.0.0.7\n10.0.0.8\n10.0.0.9\n"
46                        "10.0.0.10\n10.0.0.11\n10.0.0.12\n10.0.0.13\n"
47                        "10.0.0.14\n10.0.0.15\n";
48
49     addrs.bulkaddrs_len = 1;
50     addrs.bulkaddrs_val = addrsA;
51     code = ubik_VL_RegisterAddrs(client, 0, (afsUUID *)uuidA, 0, &addrs);
52     is_int(0, code, "First address registration succeeds");
53
54     addrs.bulkaddrs_len = 15;
55     addrs.bulkaddrs_val = addrsB;
56     code = ubik_VL_RegisterAddrs(client, 0, (afsUUID *)uuidB, 0, &addrs);
57     is_int(0, code, "Second address registration succeeds");
58
59     /* Now we need to run vos ListAddrs and see what happens ... */
60     if (pipe(outpipe) < 0) {
61         perror("pipe");
62         exit(1);
63     }
64     pid = fork();
65     if (pid == 0) {
66         char *build, *binPath;
67
68         dup2(outpipe[1], STDOUT_FILENO); /* Redirect stdout into pipe */
69         close(outpipe[0]);
70         close(outpipe[1]);
71
72         build = getenv("BUILD");
73         if (build == NULL)
74             build = "..";
75
76         if (asprintf(&binPath, "%s/../src/volser/vos", build) < 0) {
77             fprintf(stderr, "Out of memory building vos arguments\n");
78             exit(1);
79         }
80         execl(binPath, "vos",
81               "listaddrs", "-config", dirname, "-noauth", "-noresolve", NULL);
82         exit(1);
83     }
84     close(outpipe[1]);
85     buffer = malloc(4096);
86     len = read(outpipe[0], buffer, 4096);
87     waitpid(pid, &status, 0);
88     buffer[len]='\0';
89     is_string(expecting, buffer, "vos output matches");
90     free(buffer);
91 }
92
93 int
94 main(int argc, char **argv)
95 {
96     char *dirname;
97     struct afsconf_dir *dir;
98     int code, secIndex;
99     pid_t serverPid = 0;
100     struct rx_securityClass *secClass;
101     struct ubik_client *ubikClient = NULL;
102     int ret = 0;
103     char *argv0 = afstest_GetProgname(argv);
104
105     /* Skip all tests if the current hostname can't be resolved */
106     afstest_SkipTestsIfBadHostname();
107     /* Skip all tests if the current hostname is on the loopback network */
108     afstest_SkipTestsIfLoopbackNetIsDefault();
109     /* Skip all tests if a vlserver is already running on this system. */
110     afstest_SkipTestsIfServerRunning("afs3-vlserver");
111
112     plan(6);
113
114     code = rx_Init(0);
115
116     dirname = afstest_BuildTestConfig();
117
118     dir = afsconf_Open(dirname);
119
120     code = afstest_AddDESKeyFile(dir);
121     if (code) {
122         afs_com_err(argv0, code, "while adding test DES keyfile");
123         ret = 1;
124         goto out;
125     }
126
127     code = afstest_StartVLServer(dirname, &serverPid);
128     if (code) {
129         afs_com_err(argv0, code, "while starting the vlserver");
130         ret = 1;
131         goto out;
132     }
133
134     code = afsconf_ClientAuthSecure(dir, &secClass, &secIndex);
135     is_int(code, 0, "Successfully got security class");
136     if (code) {
137         afs_com_err(argv0, code, "while getting anonymous secClass");
138         ret = 1;
139         goto out;
140     }
141
142     code = afstest_GetUbikClient(dir, AFSCONF_VLDBSERVICE, USER_SERVICE_ID,
143                                  secClass, secIndex, &ubikClient);
144     is_int(code, 0, "Successfully built ubik client structure");
145     if (code) {
146         afs_com_err(argv0, code, "while building ubik client");
147         ret = 1;
148         goto out;
149     }
150
151     TestListAddrs(ubikClient, dirname);
152
153 out:
154     if (serverPid != 0) {
155         code = afstest_StopServer(serverPid);
156         is_int(0, code, "Server exited cleanly");
157     }
158
159     afstest_UnlinkTestConfig(dirname);
160     return ret;
161 }