xref: /openbmc/linux/tools/testing/vsock/util.c (revision df7e0e0d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vsock test utilities
4  *
5  * Copyright (C) 2017 Red Hat, Inc.
6  *
7  * Author: Stefan Hajnoczi <stefanha@redhat.com>
8  */
9 
10 #include <errno.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <signal.h>
14 
15 #include "timeout.h"
16 #include "util.h"
17 
18 /* Install signal handlers */
19 void init_signals(void)
20 {
21 	struct sigaction act = {
22 		.sa_handler = sigalrm,
23 	};
24 
25 	sigaction(SIGALRM, &act, NULL);
26 	signal(SIGPIPE, SIG_IGN);
27 }
28 
29 /* Parse a CID in string representation */
30 unsigned int parse_cid(const char *str)
31 {
32 	char *endptr = NULL;
33 	unsigned long n;
34 
35 	errno = 0;
36 	n = strtoul(str, &endptr, 10);
37 	if (errno || *endptr != '\0') {
38 		fprintf(stderr, "malformed CID \"%s\"\n", str);
39 		exit(EXIT_FAILURE);
40 	}
41 	return n;
42 }
43 
44 /* Run test cases.  The program terminates if a failure occurs. */
45 void run_tests(const struct test_case *test_cases,
46 	       const struct test_opts *opts)
47 {
48 	int i;
49 
50 	for (i = 0; test_cases[i].name; i++) {
51 		void (*run)(const struct test_opts *opts);
52 
53 		printf("%s...", test_cases[i].name);
54 		fflush(stdout);
55 
56 		if (opts->mode == TEST_MODE_CLIENT)
57 			run = test_cases[i].run_client;
58 		else
59 			run = test_cases[i].run_server;
60 
61 		if (run)
62 			run(opts);
63 
64 		printf("ok\n");
65 	}
66 }
67