aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/rvtest.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/rvtest.c')
-rw-r--r--src/tools/rvtest.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/tools/rvtest.c b/src/tools/rvtest.c
new file mode 100644
index 000000000..95050e671
--- /dev/null
+++ b/src/tools/rvtest.c
@@ -0,0 +1,144 @@
1/*
2 * Copyright (C) 2014, 2015 netblue30 (netblue30@yahoo.com)
3 *
4 * This file is part of firejail project
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19*/
20
21// run it as "rvtest 2>/dev/null | grep TESTING"
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <string.h>
27#include <sys/types.h>
28#include <signal.h>
29
30#define MAXBUF 1024 // line buffer
31#define TIMEOUT 30 // timeout time in seconds
32
33static pid_t pid;
34static void catch_alarm(int sig) {
35 kill(pid, SIGTERM);
36 sleep(1);
37 kill(pid, SIGKILL);
38 printf("TESTING ERROR: SIGALARM triggered\n");
39 exit(1);
40}
41
42static void usage(void) {
43 printf("Usage: rvtest testfile\n");
44 printf("\n");
45 printf("Testfile format:\n");
46 printf("\tretval command\n");
47 printf("\n");
48 printf("Testfile example:\n");
49 printf("\n");
50 printf("0 firejail --net=none exit\n");
51 printf("1 firejail --private=/etc sleep 1\n");
52 printf("1 firejail --blablabla\n");
53}
54
55int main(int argc, char **argv) {
56 if (argc != 2) {
57 fprintf(stderr, "Error: test file missing\n");
58 usage();
59 return 1;
60 }
61
62 signal (SIGALRM, catch_alarm);
63
64 // open test file
65 char *fname = argv[1];
66 FILE *fp = fopen(fname, "r");
67
68 // read test file
69 char buf[MAXBUF];
70 int line = 0;
71 while (fgets(buf, MAXBUF, fp)) {
72 line++;
73 // skip blanks
74 char *start = buf;
75 while (*start == ' ' || *start == '\t')
76 start++;
77 // remove '\n'
78 char *ptr = strchr(start, '\n');
79 if (ptr)
80 *ptr ='\0';
81 if (*start == '\0')
82 continue;
83
84 // skip comments
85 if (*start == '#')
86 continue;
87 ptr = strchr(start, '#');
88 if (ptr)
89 *ptr = '\0';
90
91 // extract exit status
92 int status;
93 int rv = sscanf(start, "%d\n", &status);
94 if (rv != 1) {
95 fprintf(stderr, "Error: invalid line %d in %s\n", line, fname);
96 exit(1);
97 }
98
99 // extract command
100 char *cmd = strchr(start, ' ');
101 if (!cmd) {
102 fprintf(stderr, "Error: invalid line %d in %s\n", line, fname);
103 exit(1);
104 }
105
106 // execute command
107 printf("TESTING %s\n", cmd);
108 fflush(0);
109 pid = fork();
110 if (pid == -1) {
111 perror("fork");
112 exit(1);
113 }
114
115 // child
116 if (pid == 0) {
117 char *earg[50];
118 earg[0] = "/bin/bash";
119 earg[1] = "-c";
120 earg[2] = cmd;
121 earg[3] = NULL;
122 execvp(earg[0], earg);
123 }
124 // parent
125 else {
126 int exit_status;
127
128 alarm(TIMEOUT);
129 pid = waitpid(pid, &exit_status, 0);
130 if (pid == -1) {
131 perror("waitpid");
132 exit(1);
133 }
134
135 if (WEXITSTATUS(exit_status) != status)
136 printf("ERROR TESTING: %s\n", cmd);
137 }
138
139 fflush(0);
140 }
141 fclose(fp);
142
143 return 0;
144} \ No newline at end of file