1
/* Runs a test suite on the rsyslog (and later potentially
4
* The name of the test suite must be given as argv[1]. In this config,
5
* rsyslogd is loaded with config ./testsuites/<name>.conf and then
6
* test cases ./testsuites/ *.<name> are executed on it. This test driver is
7
* suitable for testing cases where a message sent (via UDP) results in
8
* exactly one response. It can not be used in cases where no response
9
* is expected (that would result in a hang of the test driver).
10
* Note: each test suite can contain many tests, but they all need to work
11
* with the same rsyslog configuration.
13
* Part of the testbench for rsyslog.
15
* Copyright 2009 Rainer Gerhards and Adiscon GmbH.
17
* This file is part of rsyslog.
19
* Rsyslog is free software: you can redistribute it and/or modify
20
* it under the terms of the GNU General Public License as published by
21
* the Free Software Foundation, either version 3 of the License, or
22
* (at your option) any later version.
24
* Rsyslog is distributed in the hope that it will be useful,
25
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
* GNU General Public License for more details.
29
* You should have received a copy of the GNU General Public License
30
* along with Rsyslog. If not, see <http://www.gnu.org/licenses/>.
32
* A copy of the GPL can be found in the file "COPYING" in this distribution.
37
#include <sys/types.h>
38
#include <sys/socket.h>
41
#include <arpa/inet.h>
46
#include <netinet/in.h>
48
#define EXIT_FAILURE 1
50
static char *srcdir; /* global $srcdir, set so that we can run outside of "make check" */
51
static char *testSuite; /* name of current test suite */
54
void readLine(int fd, char *ln)
58
lenRead = read(fd, &c, 1);
59
while(lenRead == 1 && c != '\n') {
61
lenRead = read(fd, &c, 1);
67
/* send a message via UDP
68
* returns 0 if ok, something else otherwise.
71
udpSend(char *buf, int lenBuf)
73
struct sockaddr_in si_other;
74
int s, slen=sizeof(si_other);
76
if((s=socket(AF_INET, SOCK_DGRAM, 0))==-1) {
81
memset((char *) &si_other, 0, sizeof(si_other));
82
si_other.sin_family = AF_INET;
83
si_other.sin_port = htons(12514);
84
if(inet_aton("127.0.0.1", &si_other.sin_addr)==0) {
85
fprintf(stderr, "inet_aton() failed\n");
89
if(sendto(s, buf, lenBuf, 0, (struct sockaddr*) &si_other, slen)==-1) {
91
fprintf(stderr, "sendto() failed\n");
100
/* open pipe to test candidate - so far, this is
101
* always rsyslogd and with a fixed config. Later, we may
102
* change this. Returns 0 if ok, something else otherwise.
103
* rgerhards, 2009-03-31
105
int openPipe(char *configFile, pid_t *pid, int *pfd)
109
char *newargv[] = {"../tools/rsyslogd", "dummy", "-c4", "-u2", "-n", "-irsyslog.pid",
110
"-M../runtime//.libs", NULL };
112
char *newenviron[] = { NULL };
115
sprintf(confFile, "-f%s/testsuites/%s.conf", srcdir, configFile);
116
newargv[1] = confFile;
118
if (pipe(pipefd) == -1) {
129
if(cpid == 0) { /* Child reads from pipe */
135
execve("../tools/rsyslogd", newargv, newenviron);
146
/* Process a specific test case. File name is provided.
147
* Needs to return 0 if all is OK, something else otherwise.
150
processTestFile(int fd, char *pszFileName)
153
char *testdata = NULL;
154
char *expected = NULL;
159
if((fp = fopen((char*)pszFileName, "r")) == NULL) {
160
perror((char*)pszFileName);
164
/* skip comments at start of file */
166
getline(&testdata, &lenLn, fp);
169
getline(&testdata, &lenLn, fp);
171
break; /* first non-comment */
175
testdata[strlen(testdata)-1] = '\0'; /* remove \n */
176
/* now we have the test data to send */
177
if(udpSend(testdata, strlen(testdata)) != 0)
180
/* next line is expected output
181
* we do not care about EOF here, this will lead to a failure and thus
182
* draw enough attention. -- rgerhards, 2009-03-31
184
getline(&expected, &lenLn, fp);
185
expected[strlen(expected)-1] = '\0'; /* remove \n */
187
/* pull response from server and then check if it meets our expectation */
189
if(strcmp(expected, buf)) {
190
printf("\nExpected Response:\n'%s'\nActual Response:\n'%s'\n",
202
/* carry out all tests. Tests are specified via a file name
203
* wildcard. Each of the files is read and the test carried
205
* Returns the number of tests that failed. Zero means all
209
doTests(int fd, char *files)
217
struct stat fileInfo;
219
glob(files, GLOB_MARK, NULL, &testFiles);
221
for(i = 0; i < testFiles.gl_pathc; i++) {
222
testFile = testFiles.gl_pathv[i];
224
if(stat((char*) testFile, &fileInfo) != 0)
225
continue; /* continue with the next file if we can't stat() the file */
228
/* all regular files are run through the test logic. Symlinks don't work. */
229
if(S_ISREG(fileInfo.st_mode)) { /* config file */
230
printf("processing test case '%s' ... ", testFile);
231
ret = processTestFile(fd, testFile);
233
printf("successfully completed\n");
240
globfree(&testFiles);
243
printf("Error: no test cases found, no tests executed.\n");
246
printf("Number of tests run: %d, number of failures: %d\n", iTests, iFailed);
253
/* Run the test suite. This must be called with exactly one parameter, the
254
* name of the test suite. For details, see file header comment at the top
256
* rgerhards, 2009-04-03
258
int main(int argc, char *argv[])
265
char testcases[4096];
268
printf("Invalid call of udptester\n");
269
printf("Usage: udptester testsuite-name\n");
275
if((srcdir = getenv("srcdir")) == NULL)
278
printf("Start of udptester run ($srcdir=%s, testsuite=%s)\n", srcdir, testSuite);
280
openPipe(argv[1], &pid, &fd);
283
/* generate filename */
284
sprintf(testcases, "%s/testsuites/*.%s", srcdir, testSuite);
285
if(doTests(fd, testcases) != 0)
290
waitpid(pid, &status, 0); /* wait until instance terminates */
291
printf("End of udptester run.\n");