1 /* Checks if a file consists of line of strictly monotonically
2 * increasing numbers. An expected start and end number may
3 * be set.
4 *
5 * Params
6 * -f<filename> file to process, if not given stdin is processed.
7 * -s<starting number> -e<ending number>
8 * default for s is 0. -e should be given (else it is also 0)
9 * -d may be specified, in which case duplicate messages are permitted.
10 * -m number of messages permitted to be missing without triggering a
11 * failure. This is necessary for some failover tests, where it is
12 * impossible to totally guard against messagt loss. By default, NO
13 * message is permitted to be lost.
14 * -T anticipate truncation (which means specified payload length may be
15 * more than actual payload (which may have been truncated)
16 * -i increment between messages (default: 1). Can be used for tests which
17 * intentionally leave consistent gaps in the message numbering.
18 *
19 * Part of the testbench for rsyslog.
20 *
21 * Copyright 2009-2018 Rainer Gerhards and Adiscon GmbH.
22 *
23 * This file is part of rsyslog.
24 *
25 * Rsyslog is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
29 *
30 * Rsyslog is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with Rsyslog. If not, see <http://www.gnu.org/licenses/>.
37 *
38 * A copy of the GPL can be found in the file "COPYING" in this distribution.
39 */
40 #include "config.h"
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #if defined(_AIX)
45 #include <unistd.h>
46 #else
47 #include <getopt.h>
48 #endif
49
main(int argc,char * argv[])50 int main(int argc, char *argv[])
51 {
52 FILE *fp;
53 int val;
54 int i;
55 int ret = 0;
56 int scanfOK;
57 int verbose = 0;
58 int bHaveExtraData = 0;
59 int bAnticipateTruncation = 0;
60 int dupsPermitted = 0;
61 int start = 0, end = 0;
62 int opt;
63 int lostok = 0; /* how many messages are OK to be lost? */
64 int nDups = 0;
65 int increment = 1;
66 int reachedEOF;
67 int edLen; /* length of extra data */
68 static char edBuf[500*1024]; /* buffer for extra data (pretty large to be on the save side...) */
69 static char ioBuf[sizeof(edBuf)+1024];
70 char *file = NULL;
71
72 while((opt = getopt(argc, argv, "i:e:f:ds:vm:ET")) != EOF) {
73 switch((char)opt) {
74 case 'f':
75 file = optarg;
76 break;
77 case 'd':
78 dupsPermitted = 1;
79 break;
80 case 'i':
81 increment = atoi(optarg);
82 break;
83 case 'e':
84 end = atoi(optarg);
85 break;
86 case 's':
87 start = atoi(optarg);
88 break;
89 case 'v':
90 ++verbose;
91 break;
92 case 'm':
93 lostok = atoi(optarg);
94 break;
95 case 'E':
96 bHaveExtraData = 1;
97 break;
98 case 'T':
99 bAnticipateTruncation = 1;
100 break;
101 default:printf("Invalid call of chkseq, optchar='%c'\n", opt);
102 printf("Usage: chkseq file -sstart -eend -d -E\n");
103 exit(1);
104 }
105 }
106
107 if(start > end) {
108 printf("start must be less than or equal end!\n");
109 exit(1);
110 }
111
112 if(verbose) {
113 printf("chkseq: start %d, end %d\n", start, end);
114 }
115
116 /* read file */
117 if(file == NULL) {
118 fp = stdin;
119 } else {
120 fp = fopen(file, "r");
121 }
122 if(fp == NULL) {
123 printf("error opening file '%s'\n", file);
124 perror(file);
125 exit(1);
126 }
127
128 for(i = start ; i <= end ; i += increment) {
129 if(bHaveExtraData) {
130 if(fgets(ioBuf, sizeof(ioBuf), fp) == NULL) {
131 scanfOK = 0;
132 } else {
133 scanfOK = sscanf(ioBuf, "%d,%d,%s\n", &val, &edLen, edBuf) == 3 ? 1 : 0;
134 }
135 if(edLen != (int) strlen(edBuf)) {
136 if (bAnticipateTruncation == 1) {
137 if (edLen < (int) strlen(edBuf)) {
138 printf("extra data length specified %d, but actually is %ld in"
139 " record %d (truncation was anticipated, but payload should"
140 " have been smaller than data-length, not larger)\n",
141 edLen, (long) strlen(edBuf), i);
142 exit(1);
143 }
144 } else {
145 printf("extra data length specified %d, but actually is %ld in record %d\n",
146 edLen, (long) strlen(edBuf), i);
147 exit(1);
148 }
149 }
150 } else {
151 if(fgets(ioBuf, sizeof(ioBuf), fp) == NULL) {
152 scanfOK = 0;
153 } else {
154 scanfOK = sscanf(ioBuf, "%d\n", &val) == 1 ? 1 : 0;
155 }
156 }
157 if(!scanfOK) {
158 printf("scanf error in index i=%d\n", i);
159 exit(1);
160 }
161 while(val > i && lostok > 0) {
162 --lostok;
163 printf("message %d missing (ok due to -m [now %d])\n", i, lostok);
164 ++i;
165 }
166 if(val != i) {
167 if(val == i - increment && dupsPermitted) {
168 --i;
169 ++nDups;
170 } else {
171 printf("read value %d, but expected value %d\n", val, i);
172 exit(1);
173 }
174 }
175 }
176
177 if(i - increment != end) {
178 printf("lastrecord does not match. file: %d, expected %d\n", i - increment, end);
179 exit(1);
180 }
181
182 int c = getc(fp);
183 if(c == EOF) {
184 reachedEOF = 1;
185 } else {
186 ungetc(c, fp);
187 /* if duplicates are permitted, we need to do a final check if we have duplicates at the
188 * end of file.
189 */
190 if(dupsPermitted) {
191 i = end;
192 while(!feof(fp)) {
193 if(bHaveExtraData) {
194 if(fgets(ioBuf, sizeof(ioBuf), fp) == NULL) {
195 scanfOK = 0;
196 } else {
197 scanfOK = sscanf(ioBuf, "%d,%d,%s\n", &val,
198 &edLen, edBuf) == 3 ? 1 : 0;
199 }
200 if(edLen != (int) strlen(edBuf)) {
201 if (bAnticipateTruncation == 1) {
202 if (edLen < (int) strlen(edBuf)) {
203 printf("extra data length specified %d, but "
204 "actually is %ld in record %d (truncation was"
205 " anticipated, but payload should have been "
206 "smaller than data-length, not larger)\n",
207 edLen, (long) strlen(edBuf), i);
208 exit(1);
209 }
210 } else {
211 printf("extra data length specified %d, but actually "
212 "is %ld in record %d\n",
213 edLen, (long) strlen(edBuf), i);
214 exit(1);
215 }
216 }
217 } else {
218 if(fgets(ioBuf, sizeof(ioBuf), fp) == NULL) {
219 scanfOK = 0;
220 } else {
221 scanfOK = sscanf(ioBuf, "%d\n", &val) == 1 ? 1 : 0;
222 }
223 }
224
225 if(val != i) {
226 reachedEOF = 0;
227 goto breakIF;
228 }
229 }
230 reachedEOF = feof(fp) ? 1 : 0;
231 } else {
232 reachedEOF = 0;
233 }
234 }
235
236 breakIF:
237 if(nDups != 0)
238 printf("info: had %d duplicates (this is no error)\n", nDups);
239
240 if(!reachedEOF) {
241 printf("end of processing, but NOT end of file! First line of extra data is:\n");
242 for(c = fgetc(fp) ; c != '\n' && c != EOF ; c = fgetc(fp))
243 putchar(c);
244 putchar('\n');
245 exit(1);
246 }
247
248 exit(ret);
249 }
250