1 /*
2  * xs-test.c
3  *
4  * Do Xenstore tests.
5  *
6  * Copyright (C) 2016  Juergen Gross <jgross@suse.com>,
7  *                     SUSE Linux GmbH
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms and conditions of the GNU General Public
11  * License, version 2, as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program; If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #define _GNU_SOURCE
23 #include <getopt.h>
24 #include <inttypes.h>
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <time.h>
31 #include <xenstore.h>
32 
33 #include <xen-tools/libs.h>
34 
35 #define TEST_PATH "xenstore-test"
36 #define WRITE_BUFFERS_N    10
37 #define WRITE_BUFFERS_SIZE 4000
38 #define MAX_TA_LOOPS       100
39 
40 struct test {
41     char *name;
42     int (*func_init)(uintptr_t par);
43     int (*func)(uintptr_t par);
44     int (*func_deinit)(uintptr_t par);
45     uintptr_t par;
46     char *descr;
47 };
48 
49 static struct xs_handle *xsh;
50 static char *path;
51 static char *paths[WRITE_BUFFERS_N];
52 static char write_buffers[WRITE_BUFFERS_N][WRITE_BUFFERS_SIZE];
53 static int ta_loops;
54 
55 static struct option options[] = {
56     { "list-tests", 0, NULL, 'l' },
57     { "test", 1, NULL, 't' },
58     { "random", 1, NULL, 'r' },
59     { "help", 0, NULL, 'h' },
60     { "iterations", 1, NULL, 'i' },
61     { NULL, 0, NULL, 0 }
62 };
63 
call_test(struct test * tst,int iters,bool no_clock)64 static int call_test(struct test *tst, int iters, bool no_clock)
65 {
66     char *stage = "?";
67     struct timespec tp1, tp2;
68     uint64_t nsec, nsec_min, nsec_max, nsec_sum;
69     int i, ret = 0;
70 
71     nsec_min = -1;
72     nsec_max = 0;
73     nsec_sum = 0;
74 
75     for ( i = 0; i < iters; i++ )
76     {
77         stage = "pre-init";
78         xs_rm(xsh, XBT_NULL, path);
79         if ( !xs_write(xsh, XBT_NULL, path, "", 0) )
80         {
81             ret = errno;
82             break;
83         }
84         stage = "init";
85         ret = tst->func_init(tst->par);
86         if ( ret )
87             break;
88         if ( clock_gettime(CLOCK_REALTIME, &tp1) )
89             no_clock = true;
90         stage = "run";
91         ret = tst->func(tst->par);
92         if ( ret )
93             break;
94         if ( clock_gettime(CLOCK_REALTIME, &tp2) )
95             no_clock = true;
96         if ( !no_clock )
97         {
98             nsec = tp2.tv_sec * 1000000000 + tp2.tv_nsec -
99                    tp1.tv_sec * 1000000000 - tp1.tv_nsec;
100             if ( nsec < nsec_min )
101                 nsec_min = nsec;
102             if ( nsec > nsec_max )
103                 nsec_max = nsec;
104             nsec_sum += nsec;
105         }
106         stage = "deinit";
107         ret = tst->func_deinit(tst->par);
108         if ( ret )
109             break;
110     }
111 
112     if ( ret )
113         printf("%-10s: failed (ret = %d, stage %s)\n", tst->name, ret, stage);
114     else if ( !no_clock )
115     {
116         printf("%-10s:", tst->name);
117         if ( iters > 1 )
118             printf(" avg: %"PRIu64" ns (%"PRIu64" ns .. %"PRIu64" ns)",
119                    nsec_sum / iters, nsec_min, nsec_max);
120         else
121             printf(" %"PRIu64" ns", nsec_sum);
122         printf("\n");
123     }
124 
125     return ret;
126 }
127 
usage(int ret)128 static void usage(int ret)
129 {
130     FILE *out;
131 
132     out = ret ? stderr : stdout;
133 
134     fprintf(out, "usage: xs-test [<options>]\n");
135     fprintf(out, "  <options> are:\n");
136     fprintf(out, "  -i|--iterations <i>  perform each test <i> times (default 1)\n");
137     fprintf(out, "  -l|--list-tests      list available tests\n");
138     fprintf(out, "  -r|--random <time>   perform random tests for <time> seconds\n");
139     fprintf(out, "  -t|--test <test>     run <test> (default is all tests)\n");
140     fprintf(out, "  -h|--help            print this usage information\n");
141     exit(ret);
142 }
143 
ret0(uintptr_t par)144 static int ret0(uintptr_t par)
145 {
146     return 0;
147 }
148 
verify_node(char * node,char * data,unsigned int size)149 static int verify_node(char *node, char *data, unsigned int size)
150 {
151     char *buf;
152     unsigned int len;
153     int ret;
154 
155     buf = xs_read(xsh, XBT_NULL, node, &len);
156     if ( !buf )
157         return errno;
158 
159     ret = (len == size && !memcmp(buf, data, len)) ? 0 : ENOENT;
160     free(buf);
161 
162     return ret;
163 }
164 
test_read_init(uintptr_t par)165 static int test_read_init(uintptr_t par)
166 {
167     if ( par > WRITE_BUFFERS_SIZE )
168         return EFBIG;
169     return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], par) ? 0 : errno;
170 }
171 
test_read(uintptr_t par)172 static int test_read(uintptr_t par)
173 {
174     char *buf;
175     unsigned int len;
176 
177     buf = xs_read(xsh, XBT_NULL, paths[0], &len);
178     if ( !buf )
179         return errno;
180     free(buf);
181     return 0;
182 }
183 
184 #define test_read_deinit ret0
185 
test_write_init(uintptr_t par)186 static int test_write_init(uintptr_t par)
187 {
188     return (par > WRITE_BUFFERS_SIZE) ? EFBIG : 0;
189 }
190 
test_write(uintptr_t par)191 static int test_write(uintptr_t par)
192 {
193     return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], par) ? 0 : errno;
194 }
195 
test_write_deinit(uintptr_t par)196 static int test_write_deinit(uintptr_t par)
197 {
198     return verify_node(paths[0], write_buffers[0], par);
199 }
200 
test_dir_init(uintptr_t par)201 static int test_dir_init(uintptr_t par)
202 {
203     unsigned int i;
204 
205     for ( i = 0; i < WRITE_BUFFERS_N; i++ )
206         if ( !xs_write(xsh, XBT_NULL, paths[i], write_buffers[i], 1) )
207             return errno;
208 
209     return 0;
210 }
211 
test_dir(uintptr_t par)212 static int test_dir(uintptr_t par)
213 {
214     char **dir;
215     unsigned int num;
216 
217     dir = xs_directory(xsh, XBT_NULL, path, &num);
218     if ( !dir )
219         return errno;
220 
221     free(dir);
222     return 0;
223 }
224 
test_dir_deinit(uintptr_t par)225 static int test_dir_deinit(uintptr_t par)
226 {
227     char **dir;
228     unsigned int i, j, num;
229     int rc = 0;
230 
231     dir = xs_directory(xsh, XBT_NULL, path, &num);
232     if ( !dir )
233         return errno;
234 
235     for ( j = 0; j < WRITE_BUFFERS_N; j++ )
236     {
237         for ( i = 0; i < num; i++ )
238             if ( dir[i][0] == 'a' + j && dir[i][1] == 0 )
239                 break;
240         if ( i == num )
241             rc = ENOENT;
242     }
243     if ( num != WRITE_BUFFERS_N )
244             rc = ENOENT;
245     free(dir);
246     return rc;
247 }
248 
test_rm_init(uintptr_t par)249 static int test_rm_init(uintptr_t par)
250 {
251     unsigned int i;
252 
253     if ( par > WRITE_BUFFERS_N )
254         return EFBIG;
255 
256     for ( i = 0; i < par; i++ )
257         if ( xs_write(xsh, XBT_NULL, paths[i], write_buffers[i], 1) )
258             return errno;
259 
260     return 0;
261 }
262 
test_rm(uintptr_t par)263 static int test_rm(uintptr_t par)
264 {
265     if ( !xs_rm(xsh, XBT_NULL, path) )
266         return errno;
267 
268     return 0;
269 }
270 
271 #define test_rm_deinit ret0
272 
273 #define test_ta1_init ret0
274 
test_ta1(uintptr_t par)275 static int test_ta1(uintptr_t par)
276 {
277     xs_transaction_t t;
278     int l;
279 
280     for ( l = 0; l < MAX_TA_LOOPS; l++ )
281     {
282         t = xs_transaction_start(xsh);
283         if ( t == XBT_NULL )
284             return errno;
285         if ( xs_transaction_end(xsh, t, par ? true : false) )
286             return 0;
287         if ( errno != EAGAIN )
288             return errno;
289     }
290 
291     ta_loops++;
292     return 0;
293 }
294 
295 #define test_ta1_deinit ret0
296 
test_ta2_init(uintptr_t par)297 static int test_ta2_init(uintptr_t par)
298 {
299     return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], 1) ? 0 : errno;
300 }
301 
test_ta2(uintptr_t par)302 static int test_ta2(uintptr_t par)
303 {
304     xs_transaction_t t;
305     char *buf;
306     unsigned int len;
307     int ret;
308     int l;
309 
310     for ( l = 0; l < MAX_TA_LOOPS; l++ )
311     {
312         t = xs_transaction_start(xsh);
313         if ( t == XBT_NULL )
314             return errno;
315         buf = xs_read(xsh, t, paths[0], &len);
316         if ( !buf )
317             goto out;
318         free(buf);
319         if ( !xs_write(xsh, t, paths[0], "b", 1) )
320             goto out;
321         buf = xs_read(xsh, t, paths[0], &len);
322         if ( !buf )
323             goto out;
324         errno = (len == 1 && buf[0] == 'b') ? 0 : ENOENT;
325         free(buf);
326         if ( errno )
327             goto out;
328         buf = xs_read(xsh, XBT_NULL, paths[0], &len);
329         if ( !buf )
330             goto out;
331         errno = (len == 1 && buf[0] == 'a') ? 0 : ENOENT;
332         free(buf);
333         if ( errno )
334             goto out;
335         if ( xs_transaction_end(xsh, t, par ? true : false) )
336             return 0;
337         if ( errno != EAGAIN )
338             return errno;
339     }
340 
341     ta_loops++;
342     return 0;
343 
344  out:
345     ret = errno;
346     xs_transaction_end(xsh, t, true);
347     return ret;
348 }
349 
test_ta2_deinit(uintptr_t par)350 static int test_ta2_deinit(uintptr_t par)
351 {
352     return verify_node(paths[0], par ? "a" : "b", 1);
353 }
354 
test_ta3_init(uintptr_t par)355 static int test_ta3_init(uintptr_t par)
356 {
357     return xs_write(xsh, XBT_NULL, paths[0], write_buffers[0], 1) ? 0 : errno;
358 }
359 
test_ta3(uintptr_t par)360 static int test_ta3(uintptr_t par)
361 {
362     xs_transaction_t t;
363     char *buf;
364     unsigned int len;
365     int ret;
366 
367     t = xs_transaction_start(xsh);
368     if ( t == XBT_NULL )
369         return errno;
370     buf = xs_read(xsh, t, paths[0], &len);
371     if ( !buf )
372         goto out;
373     free(buf);
374     if ( !xs_write(xsh, XBT_NULL, paths[0], "b", 1) )
375         goto out;
376     buf = xs_read(xsh, t, paths[0], &len);
377     if ( !buf )
378         goto out;
379     errno = (len == 1 && buf[0] == 'a') ? 0 : ENOENT;
380     free(buf);
381     if ( errno )
382         goto out;
383     if ( !xs_write(xsh, t, paths[0], "c", 1) )
384         goto out;
385     buf = xs_read(xsh, t, paths[0], &len);
386     if ( !buf )
387         goto out;
388     errno = (len == 1 && buf[0] == 'c') ? 0 : ENOENT;
389     free(buf);
390     if ( errno )
391         goto out;
392     if ( xs_transaction_end(xsh, t, false) || errno != EAGAIN )
393         return ENOENT;
394     return 0;
395 
396  out:
397     ret = errno;
398     xs_transaction_end(xsh, t, true);
399     return ret;
400 }
401 
test_ta3_deinit(uintptr_t par)402 static int test_ta3_deinit(uintptr_t par)
403 {
404     return verify_node(paths[0], "b", 1);
405 }
406 
407 #define TEST(s, f, p, l) { s, f ## _init, f, f ## _deinit, (uintptr_t)(p), l }
408 struct test tests[] = {
409 TEST("read 1", test_read, 1, "Read node with 1 byte data"),
410 TEST("read 3000", test_read, 3000, "Read node with 3000 bytes data"),
411 TEST("write 1", test_write, 1, "Write node with 1 byte data"),
412 TEST("write 3000", test_write, 3000, "Write node with 3000 bytes data"),
413 TEST("dir", test_dir, 0, "List directory"),
414 TEST("rm node", test_rm, 0, "Remove single node"),
415 TEST("rm dir", test_rm, WRITE_BUFFERS_N, "Remove node with sub-nodes"),
416 TEST("ta empty", test_ta1, 0, "Empty transaction"),
417 TEST("ta empty x", test_ta1, 1, "Empty transaction abort"),
418 TEST("ta rmw", test_ta2, 0, "Read-modify-write transaction"),
419 TEST("ta rmw x", test_ta2, 1, "Read-modify-write transaction abort"),
420 TEST("ta err", test_ta3, 0, "Transaction with conflict"),
421 };
422 
cleanup(void)423 static void cleanup(void)
424 {
425     xs_transaction_t t;
426     char **dir;
427     unsigned int num;
428 
429     xs_rm(xsh, XBT_NULL, path);
430 
431     while ( true )
432     {
433         t = xs_transaction_start(xsh);
434         if ( t == XBT_NULL )
435             return;
436 
437         dir = xs_directory(xsh, t, TEST_PATH, &num);
438         if ( dir && !num )
439             xs_rm(xsh, t, TEST_PATH);
440         free(dir);
441 
442         if ( xs_transaction_end(xsh, t, false) || errno != EAGAIN )
443             return;
444     }
445 }
446 
main(int argc,char * argv[])447 int main(int argc, char *argv[])
448 {
449     int opt, t, iters = 1, ret = 0, randtime = 0;
450     char *test = NULL;
451     bool list = false;
452     time_t stop;
453 
454     while ( (opt = getopt_long(argc, argv, "lr:t:hi:", options,
455                                NULL)) != -1 )
456     {
457         switch ( opt )
458         {
459         case 'i':
460             iters = atoi(optarg);
461             break;
462         case 'l':
463             list = true;
464             break;
465         case 'r':
466             randtime = atoi(optarg);
467             break;
468         case 't':
469             test = optarg;
470             break;
471         case 'h':
472             usage(0);
473             break;
474         }
475     }
476     if ( optind != argc )
477         usage(1);
478 
479     if ( list )
480     {
481         for ( t = 0; t < ARRAY_SIZE(tests); t++ )
482             printf("%-10s: %s\n", tests[t].name, tests[t].descr);
483         return 0;
484     }
485 
486     asprintf(&path, "%s/%u", TEST_PATH, getpid());
487     for ( t = 0; t < WRITE_BUFFERS_N; t++ )
488     {
489         memset(write_buffers[t], 'a' + t, WRITE_BUFFERS_SIZE);
490         asprintf(&paths[t], "%s/%c", path, 'a' + t);
491     }
492 
493     xsh = xs_open(0);
494     if ( !xsh )
495     {
496         fprintf(stderr, "could not connect to xenstore\n");
497         exit(2);
498     }
499 
500     if ( randtime )
501     {
502         stop = time(NULL) + randtime;
503         srandom((unsigned int)stop);
504 
505         while ( time(NULL) < stop )
506         {
507             t = random() % ARRAY_SIZE(tests);
508             ret = call_test(tests + t, iters, true);
509         }
510     }
511     else
512         for ( t = 0; t < ARRAY_SIZE(tests); t++ )
513         {
514             if ( !test || !strcmp(test, tests[t].name) )
515                 ret = call_test(tests + t, iters, false);
516         }
517 
518     if ( !ret )
519         cleanup();
520 
521     xs_close(xsh);
522 
523     if ( ta_loops )
524         printf("Exhaustive transaction retries (%d) occurrred %d times.\n",
525                MAX_TA_LOOPS, ta_loops);
526 
527     return 0;
528 }
529 
530 /*
531  * Local variables:
532  * mode: C
533  * c-file-style: "BSD"
534  * c-basic-offset: 4
535  * indent-tabs-mode: nil
536  * End:
537  */
538