~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to testing/etimetest.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

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