Debian Codegraph @

testing/fulltests/snmpv3/T050etimetest_capp.c

raw
1 /*
2 * etimetest.c
3 *
4 * HEADER Testing engineID hashing and timing
5 *
6 * Expected SUCCESSes for all tests: 3
7 *
8 * Returns:
9 * Number of FAILUREs.
10 *
11 * Test of hash_engineID(). SUCCESSes: 0
12 * Test of LCD Engine ID and Time List. SUCCESSes: 3
13 *
14 * FIX Devise a test for {set,get}_enginetime(..., FALSE).
15 */
16
17 #include <net-snmp/net-snmp-config.h>
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #ifdef HAVE_STDLIB_H
22 #include <stdlib.h>
23 #endif
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #endif
27
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
30 #endif
31
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35
36 #include <net-snmp/library/asn1.h>
37 #include <net-snmp/library/snmp_api.h>
38 #include <net-snmp/library/scapi.h>
39 #include <net-snmp/library/tools.h>
40 #include <net-snmp/library/lcd_time.h>
41 #include <net-snmp/library/snmp_debug.h>
42 #include <net-snmp/library/callback.h>
43 #include <net-snmp/library/getopt.h>
44
45 static u_int dummy_etime, dummy_eboot; /* For ISENGINEKNOWN(). */
46
47 int testcount=0;
48
49 /*
50 * Globals, &c...
51 */
52 char *local_progname;
53
54 #define USAGE "Usage: %s [-h][-s <seconds>][-aeH]"
55 #define OPTIONLIST "aehHs:"
56
57 int doalltests = 0, dohashindex = 0, doetimetest = 0;
58
59 #define ALLOPTIONS (doalltests + dohashindex + doetimetest)
60
61
62
63 #define LOCAL_MAXBUF (1024 * 8)
64 #define NL "\n"
65
66 #define OUTPUT(o) fprintf(stdout, "# %s\n", o);
67
68 #define TEST_SUCCEEDED(s) do { printf("# Done with %s\n", (s)); } while (0)
69
70 #define TEST_FAILED(e, f) \
71 do { \
72 if (e != SNMPERR_SUCCESS) { \
73 fprintf(stdout, "not ok: %d - %s\n", ++testcount, (f)); \
74 failcount += 1; \
75 } else { \
76 fprintf(stdout, "ok: %d - %s\n", ++testcount, (f)); \
77 } \
78 fflush(stdout); \
79 } while (0)
80
81 #define DETAILINT(s, i) \
82 fprintf(stdout, "# %s: %d\n", s, i);
83
84 /*
85 * Global variables.
86 */
87 int sleeptime = 2;
88
89 #define BLAT "alk;djf;an riu;alicenmrul;aiknglksajhe1 adcfalcenrco2"
90
91 static void usage(FILE *);
92 static int test_hashindex(void);
93 static int test_etime(void);
94
95 int
96 main(int argc, char **argv)
97 {
98 int rval = SNMPERR_SUCCESS;
99 int failcount NETSNMP_ATTRIBUTE_UNUSED = 0;
100 int ch;
101
102 local_progname = argv[0];
103
104 /*
105 * Parse.
106 */
107 while ((ch = getopt(argc, argv, OPTIONLIST)) != EOF) {
108 switch (ch) {
109 case 'a':
110 doalltests = 1;
111 break;
112 case 'e':
113 doetimetest = 1;
114 break;
115 case 'H':
116 dohashindex = 1;
117 break;
118 case 's':
119 sleeptime = atoi(optarg);
120 if (sleeptime < 0) {
121 usage(stderr);
122 exit(1000);
123 }
124 break;
125 break;
126 case 'h':
127 rval = 0;
128 /* fall through */
129 default:
130 usage(stdout);
131 exit(rval);
132 }
133
134 argc -= 1;
135 argv += 1;
136 if (optarg) {
137 argc -= 1;
138 argv += 1;
139 optarg = NULL;
140 }
141 optind = 1;
142 } /* endwhile getopt */
143
144 if ((argc > 1)) {
145 usage(stdout);
146 exit(1000);
147
148 } else if (ALLOPTIONS != 1) {
149 doalltests = 1;
150 }
151
152 init_snmp("testing");
153
154 /*
155 * test stuff.
156 */
157 rval = sc_init();
158 TEST_FAILED(rval, "sc_init()");
159
160
161 if (dohashindex || doalltests) {
162 failcount += test_hashindex();
163 }
164 if (doetimetest || doalltests) {
165 failcount += test_etime();
166 }
167
168
169 fprintf(stdout, "1..%d\n", testcount);
170 return 0;
171 } /* end main() */
172
173
174
175
176
177 void
178 usage(FILE * ofp)
179 {
180 fprintf(ofp,
181 USAGE
182 "" NL
183 " -a All tests." NL
184 " -e Exercise the list of enginetimes."
185 NL
186 " -h Help."
187 NL
188 " -H Test hash_engineID()."
189 NL
190 " -s <seconds> Seconds to pause. (Default: 0.)"
191 NL NL, local_progname);
192
193 } /* end usage() */
194
195
196
197
198 #ifdef EXAMPLE
199 /*******************************************************************-o-******
200 * test_dosomething
201 *
202 * Returns:
203 * Number of failures.
204 *
205 *
206 * Test template.
207 */
208 int
209 test_dosomething(void)
210 {
211 int rval = SNMPERR_SUCCESS, failcount = 0;
212
213 test_dosomething_quit:
214 return failcount;
215
216 } /* end test_dosomething() */
217 #endif /* EXAMPLE */
218
219
220
221
222
223 /*******************************************************************-o-******
224 * test_hashindex
225 *
226 * Returns:
227 * Number of failures.
228 *
229 *
230 * Test hash_engineID().
231 */
232 int
233 test_hashindex(void)
234 {
235 int /* rval = SNMPERR_SUCCESS, */
236 failcount = 0;
237 const u_char *s;
238
239 OUTPUT("Visual spot check of hash index outputs. "
240 "(Success or failure not noted.)");
241
242 s = (const u_char *) " A";
243 fprintf(stdout, "# %s = %d\n", s, hash_engineID(s, strlen((const char *) s)));
244
245 s = (const u_char *) " BB";
246 fprintf(stdout, "# %s = %d\n", s, hash_engineID(s, strlen((const char *) s)));
247
248 s = (const u_char *) " CCC";
249 fprintf(stdout, "# %s = %d\n", s, hash_engineID(s, strlen((const char *) s)));
250
251 s = (const u_char *) " DDDD";
252 fprintf(stdout, "# %s = %d\n", s, hash_engineID(s, strlen((const char *) s)));
253
254 s = (const u_char *) " EEEEE";
255 fprintf(stdout, "# %s = %d\n", s, hash_engineID(s, strlen((const char *) s)));
256
257 s = (const u_char *) BLAT;
258 fprintf(stdout, "# %s = %d\n", s, hash_engineID(s, strlen((const char *) s)));
259
260
261 OUTPUT("Visual spot check -- DONE.");
262
263 return failcount;
264
265 } /* end test_hashindex() */
266
267
268
269
270
271 /*******************************************************************-o-******
272 * test_etime
273 *
274 * Returns:
275 * Number of failures.
276 *
277 * Test of LCD Engine ID and Time List.
278 */
279 int
280 test_etime(void)
281 {
282 int rval = SNMPERR_SUCCESS, failcount = 0;
283 u_int etime, eboot;
284
285 /*
286 * ------------------------------------ -o-
287 */
288 OUTPUT("Query of empty list, two set actions.");
289
290
291 rval = ISENGINEKNOWN((const u_char *) "A", 1);
292 if (rval == TRUE) {
293 TEST_FAILED(SNMPERR_GENERR, "Query of empty list returned TRUE.");
294 }
295
296
297 rval = set_enginetime((const u_char *) "BB", 2, 2, 20, TRUE);
298 TEST_FAILED(rval, "set_enginetime()");
299
300
301 rval = set_enginetime((const u_char *) "CCC", 3, 31, 90127, TRUE);
302 TEST_FAILED(rval, "set_enginetime()");
303
304
305 TEST_SUCCEEDED("Check of empty list, and two additions.");
306
307
308
309 /*
310 * ------------------------------------ -o-
311 */
312 OUTPUT("Add entries using macros, test for existence with macros.");
313
314
315 rval = ENSURE_ENGINE_RECORD((const u_char *) "DDDD", 4);
316 TEST_FAILED(rval, "ENSURE_ENGINE_RECORD()");
317
318
319 rval = MAKENEW_ENGINE_RECORD((const u_char *) "EEEEE", 5);
320 if (rval == SNMPERR_SUCCESS) {
321 TEST_FAILED(rval,
322 "MAKENEW_ENGINE_RECORD returned success for "
323 "missing record.");
324 }
325
326
327 rval = MAKENEW_ENGINE_RECORD((const u_char *) "BB", 2);
328 TEST_FAILED(rval, "MAKENEW_ENGINE_RECORD().");
329
330
331 TEST_SUCCEEDED("Added entries with macros, tested for existence with"
332 " macros.");
333
334
335
336 /*
337 * ------------------------------------ -o-
338 */
339 OUTPUT("Dump the list and then sleep.");
340
341 #ifdef NETSNMP_ENABLE_TESTING_CODE
342 dump_etimelist();
343 #endif
344
345 fprintf(stdout, "# Sleeping for %d second%s... ",
346 sleeptime, (sleeptime == 1) ? "" : "s");
347
348 sleep(sleeptime);
349
350
351 /*
352 * ------------------------------------ -o-
353 */
354 OUTPUT
355 ("Retrieve data from real/stubbed records, update real/stubbed.");
356
357
358
359 rval = get_enginetime((const u_char *) "BB", 2, &eboot, &etime, TRUE);
360 TEST_FAILED(rval, "get_enginetime().");
361
362 fprintf(stdout, "# BB = <%d,%d>\n", eboot, etime);
363 if ((etime < 20) || (eboot < 2)) {
364 TEST_FAILED(SNMPERR_GENERR,
365 "get_enginetime() returned bad values. (1)");
366 }
367
368 rval = get_enginetime((const u_char *) "DDDD", 4, &eboot, &etime, FALSE);
369 TEST_FAILED(rval, "get_enginetime().");
370
371 fprintf(stdout, "# DDDD = <%d,%d>\n", eboot, etime);
372 if ((etime < sleeptime) || (eboot != 0)) {
373 TEST_FAILED(SNMPERR_GENERR,
374 "get_enginetime() returned bad values. (2)");
375 }
376
377
378 rval = set_enginetime((const u_char *) "CCC", 3, 234, 10000, TRUE);
379 TEST_FAILED(rval, "set_enginetime().");
380
381
382 rval = set_enginetime((const u_char *) "EEEEE", 5, 9876, 55555, TRUE);
383 TEST_FAILED(rval, "set_enginetime().");
384
385
386 TEST_SUCCEEDED("Retrieval and updates.");
387
388
389
390 /*
391 * ------------------------------------ -o-
392 */
393 OUTPUT("Sleep again, then dump the list one last time.");
394
395 fprintf(stdout, "# Sleeping for %d second%s...\n",
396 sleeptime, (sleeptime == 1) ? "" : "s");
397
398 sleep(sleeptime);
399
400 #ifdef NETSNMP_ENABLE_TESTING_CODE
401 dump_etimelist();
402 #endif
403
404 return failcount;
405
406 } /* end test_etime() */