1*696b5899Stb /* $OpenBSD: ber_test_int_i.c,v 1.6 2019/10/24 12:39:26 tb Exp $
242df20ffSrob */
342df20ffSrob /*
442df20ffSrob * Copyright (c) Rob Pierce <rob@openbsd.org>
542df20ffSrob *
642df20ffSrob * Permission to use, copy, modify, and distribute this software for any
742df20ffSrob * purpose with or without fee is hereby granted, provided that the above
842df20ffSrob * copyright notice and this permission notice appear in all copies.
942df20ffSrob *
1042df20ffSrob * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1142df20ffSrob * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1242df20ffSrob * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1342df20ffSrob * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1442df20ffSrob * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1542df20ffSrob * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1642df20ffSrob * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1742df20ffSrob */
1842df20ffSrob
1942df20ffSrob #include <sys/types.h>
2042df20ffSrob
2142df20ffSrob #include <ber.h>
2242df20ffSrob #include <errno.h>
2342df20ffSrob #include <stdio.h>
2442df20ffSrob #include <string.h>
2542df20ffSrob
2642df20ffSrob #define SUCCEED 0
2742df20ffSrob #define FAIL 1
2842df20ffSrob
2942df20ffSrob struct test_vector {
3042df20ffSrob int fail; /* 1 means test is expected to fail */
3142df20ffSrob char title[128];
32cc73a603Srob size_t input_length;
3342df20ffSrob long long value;
3442df20ffSrob unsigned char input[1024];
35cc73a603Srob size_t expect_length;
36cc73a603Srob unsigned char expect[1024];
3742df20ffSrob };
3842df20ffSrob
3942df20ffSrob struct test_vector test_vectors[] = {
4042df20ffSrob {
41272e3cb7Srob SUCCEED,
4242df20ffSrob "integer (-9223372036854775807)",
4342df20ffSrob 10,
4442df20ffSrob -9223372036854775807,
4542df20ffSrob {
4642df20ffSrob 0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
4742df20ffSrob 0x00, 0x01
4842df20ffSrob },
49cc73a603Srob 10,
50cc73a603Srob {
51cc73a603Srob 0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
52cc73a603Srob 0x00, 0x01
53cc73a603Srob },
5442df20ffSrob },
5542df20ffSrob {
56272e3cb7Srob SUCCEED,
5742df20ffSrob "integer (-9223372036854775806)",
5842df20ffSrob 10,
5942df20ffSrob -9223372036854775806,
6042df20ffSrob {
6142df20ffSrob 0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
6242df20ffSrob 0x00, 0x02
6342df20ffSrob },
64cc73a603Srob 10,
65cc73a603Srob {
66cc73a603Srob 0x02, 0x08, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
67cc73a603Srob 0x00, 0x02
68cc73a603Srob },
6942df20ffSrob },
7042df20ffSrob {
71cc73a603Srob FAIL,
72cc73a603Srob "integer (-2147483650) (expected failure)",
7342df20ffSrob 10,
7442df20ffSrob -2147483650,
7542df20ffSrob {
7642df20ffSrob 0x02, 0x08, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xff,
7742df20ffSrob 0xff, 0xfe
7842df20ffSrob },
79cc73a603Srob 7,
80cc73a603Srob {
81cc73a603Srob 0x02, 0x05, 0xff, 0x7f, 0xff, 0xff, 0xfe
82cc73a603Srob },
83cc73a603Srob },
84cc73a603Srob {
85cc73a603Srob SUCCEED,
86cc73a603Srob "integer (-2147483650)",
87cc73a603Srob 7,
88cc73a603Srob -2147483650,
89cc73a603Srob {
90cc73a603Srob 0x02, 0x05, 0xff, 0x7f, 0xff, 0xff, 0xfe
91cc73a603Srob },
92cc73a603Srob 7,
93cc73a603Srob {
94cc73a603Srob 0x02, 0x05, 0xff, 0x7f, 0xff, 0xff, 0xfe
95cc73a603Srob },
9642df20ffSrob },
9742df20ffSrob {
98272e3cb7Srob SUCCEED,
9942df20ffSrob "integer (-2147483649)",
100cc73a603Srob 7,
10142df20ffSrob -2147483649,
10242df20ffSrob {
103cc73a603Srob 0x02, 0x05, 0xff, 0x7f, 0xff, 0xff, 0xff
104cc73a603Srob },
105cc73a603Srob 7,
106cc73a603Srob {
107cc73a603Srob 0x02, 0x05, 0xff, 0x7f, 0xff, 0xff, 0xff
108cc73a603Srob },
109cc73a603Srob },
110cc73a603Srob {
111cc73a603Srob FAIL,
112cc73a603Srob "integer (-2147483648) (expected failure)",
113cc73a603Srob 8,
114cc73a603Srob -2147483648,
115cc73a603Srob {
116cc73a603Srob 0x02, 0x06, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00
117cc73a603Srob },
118cc73a603Srob 6,
119cc73a603Srob {
120cc73a603Srob 0x02, 0x04, 0x80, 0x00, 0x00, 0x00
12142df20ffSrob },
12242df20ffSrob },
12342df20ffSrob {
12442df20ffSrob SUCCEED,
12542df20ffSrob "integer (-2147483648)",
12642df20ffSrob 6,
12742df20ffSrob -2147483648,
12842df20ffSrob {
12942df20ffSrob 0x02, 0x04, 0x80, 0x00, 0x00, 0x00
13042df20ffSrob },
131cc73a603Srob 6,
132cc73a603Srob {
133cc73a603Srob 0x02, 0x04, 0x80, 0x00, 0x00, 0x00
134cc73a603Srob },
13542df20ffSrob },
13642df20ffSrob {
13742df20ffSrob SUCCEED,
13842df20ffSrob "integer (-1073741824)",
13942df20ffSrob 6,
14042df20ffSrob -1073741824,
14142df20ffSrob {
14242df20ffSrob 0x02, 0x04, 0xc0, 0x00, 0x00, 0x00
14342df20ffSrob },
144cc73a603Srob 6,
145cc73a603Srob {
146cc73a603Srob 0x02, 0x04, 0xc0, 0x00, 0x00, 0x00
147cc73a603Srob },
14842df20ffSrob },
14942df20ffSrob {
15042df20ffSrob SUCCEED,
15142df20ffSrob "integer (-536870912)",
15242df20ffSrob 6,
15342df20ffSrob -536870912,
15442df20ffSrob {
15542df20ffSrob 0x02, 0x04, 0xe0, 0x00, 0x00, 0x00
15642df20ffSrob },
157cc73a603Srob 6,
158cc73a603Srob {
159cc73a603Srob 0x02, 0x04, 0xe0, 0x00, 0x00, 0x00
160cc73a603Srob },
16142df20ffSrob },
16242df20ffSrob {
16342df20ffSrob SUCCEED,
16442df20ffSrob "integer (-268435456)",
16542df20ffSrob 6,
16642df20ffSrob -268435456,
16742df20ffSrob {
16842df20ffSrob 0x02, 0x04, 0xf0, 0x00, 0x00, 0x00
16942df20ffSrob },
170cc73a603Srob 6,
171cc73a603Srob {
172cc73a603Srob 0x02, 0x04, 0xf0, 0x00, 0x00, 0x00
173cc73a603Srob },
17442df20ffSrob },
17542df20ffSrob {
17642df20ffSrob SUCCEED,
17742df20ffSrob "integer (-1342177728)",
17842df20ffSrob 6,
17942df20ffSrob -1342177728,
18042df20ffSrob {
18142df20ffSrob 0x02, 0x04, 0xaf, 0xff, 0xfe, 0x40
18242df20ffSrob },
183cc73a603Srob 6,
184cc73a603Srob {
185cc73a603Srob 0x02, 0x04, 0xaf, 0xff, 0xfe, 0x40
186cc73a603Srob },
18742df20ffSrob },
18842df20ffSrob {
18942df20ffSrob SUCCEED,
19042df20ffSrob "integer (-67108865)",
19142df20ffSrob 6,
19242df20ffSrob -67108865,
19342df20ffSrob {
19442df20ffSrob 0x02, 0x04, 0xfb, 0xff, 0xff, 0xff
19542df20ffSrob },
196cc73a603Srob 6,
197cc73a603Srob {
198cc73a603Srob 0x02, 0x04, 0xfb, 0xff, 0xff, 0xff
199cc73a603Srob },
20042df20ffSrob },
20142df20ffSrob {
20242df20ffSrob SUCCEED,
20342df20ffSrob "integer (-67108864)",
20442df20ffSrob 6,
20542df20ffSrob -67108864,
20642df20ffSrob {
20742df20ffSrob 0x02, 0x04, 0xfc, 0x00, 0x00, 0x00
20842df20ffSrob },
209cc73a603Srob 6,
210cc73a603Srob {
211cc73a603Srob 0x02, 0x04, 0xfc, 0x00, 0x00, 0x00
212cc73a603Srob },
21342df20ffSrob },
21442df20ffSrob {
21542df20ffSrob SUCCEED,
21642df20ffSrob "integer (-67108863)",
21742df20ffSrob 6,
21842df20ffSrob -67108863,
21942df20ffSrob {
22042df20ffSrob 0x02, 0x04, 0xfc, 0x00, 0x00, 0x01
22142df20ffSrob },
222cc73a603Srob 6,
223cc73a603Srob {
224cc73a603Srob 0x02, 0x04, 0xfc, 0x00, 0x00, 0x01
225cc73a603Srob },
22642df20ffSrob },
22742df20ffSrob {
22842df20ffSrob SUCCEED,
22942df20ffSrob "integer (-3554432)",
230cc73a603Srob 5,
23142df20ffSrob -3554432,
23242df20ffSrob {
233cc73a603Srob 0x02, 0x03, 0xc9, 0xc3, 0x80
234cc73a603Srob },
235cc73a603Srob 5,
236cc73a603Srob {
237cc73a603Srob 0x02, 0x03, 0xc9, 0xc3, 0x80
23842df20ffSrob },
23942df20ffSrob },
24042df20ffSrob {
24142df20ffSrob SUCCEED,
24242df20ffSrob "integer (-65535)",
243cc73a603Srob 5,
24442df20ffSrob -65535,
24542df20ffSrob {
246cc73a603Srob 0x02, 0x03, 0xff, 0x00, 0x01,
247cc73a603Srob },
248cc73a603Srob 5,
249cc73a603Srob {
250cc73a603Srob 0x02, 0x03, 0xff, 0x00, 0x01,
25142df20ffSrob },
25242df20ffSrob },
25342df20ffSrob {
25442df20ffSrob SUCCEED,
25542df20ffSrob "integer (-32769)",
256cc73a603Srob 5,
25742df20ffSrob -32769,
25842df20ffSrob {
259cc73a603Srob 0x02, 0x03, 0xff, 0x7f, 0xff
260cc73a603Srob },
261cc73a603Srob 5,
262cc73a603Srob {
263cc73a603Srob 0x02, 0x03, 0xff, 0x7f, 0xff
26442df20ffSrob },
26542df20ffSrob },
26642df20ffSrob {
26742df20ffSrob SUCCEED,
26842df20ffSrob "integer (-32768)",
26942df20ffSrob 4,
27042df20ffSrob -32768,
27142df20ffSrob {
27242df20ffSrob 0x02, 0x02, 0x80, 0x00
27342df20ffSrob },
274cc73a603Srob 4,
275cc73a603Srob {
276cc73a603Srob 0x02, 0x02, 0x80, 0x00
277cc73a603Srob },
27842df20ffSrob },
27942df20ffSrob {
28042df20ffSrob SUCCEED,
28142df20ffSrob "integer (-128)",
282cc73a603Srob 3,
28371359a58Srob -128,
28442df20ffSrob {
285cc73a603Srob 0x02, 0x01, 0x80
286cc73a603Srob },
287cc73a603Srob 3,
288cc73a603Srob {
289cc73a603Srob 0x02, 0x01, 0x80
29042df20ffSrob },
29142df20ffSrob },
29242df20ffSrob {
29342df20ffSrob SUCCEED,
29442df20ffSrob "integer (-1)",
295cc73a603Srob 3,
29642df20ffSrob -1,
29742df20ffSrob {
298cc73a603Srob 0x02, 0x01, 0xff
299cc73a603Srob },
300cc73a603Srob 3,
301cc73a603Srob {
302cc73a603Srob 0x02, 0x01, 0xff
30342df20ffSrob },
30442df20ffSrob },
30542df20ffSrob {
30642df20ffSrob SUCCEED,
30742df20ffSrob "integer (0)",
30842df20ffSrob 3,
30942df20ffSrob 0,
31042df20ffSrob {
31142df20ffSrob 0x02, 0x01, 0x00
31242df20ffSrob },
313cc73a603Srob 3,
314cc73a603Srob {
315cc73a603Srob 0x02, 0x01, 0x00
316cc73a603Srob },
31742df20ffSrob },
31842df20ffSrob {
31942df20ffSrob SUCCEED,
32042df20ffSrob "integer (127)",
32142df20ffSrob 3,
32242df20ffSrob 127,
32342df20ffSrob {
32442df20ffSrob 0x02, 0x01, 0x7f
32542df20ffSrob },
326cc73a603Srob 3,
327cc73a603Srob {
328cc73a603Srob 0x02, 0x01, 0x7f
329cc73a603Srob },
33042df20ffSrob },
33142df20ffSrob {
33242df20ffSrob SUCCEED,
33342df20ffSrob "integer (128)",
33442df20ffSrob 4,
33542df20ffSrob 128,
33642df20ffSrob {
33742df20ffSrob 0x02, 0x02, 0x00, 0x80
33842df20ffSrob },
339cc73a603Srob 4,
340cc73a603Srob {
341cc73a603Srob 0x02, 0x02, 0x00, 0x80
342cc73a603Srob },
34342df20ffSrob },
34442df20ffSrob {
34542df20ffSrob SUCCEED,
346272e3cb7Srob "integer (32767)",
347272e3cb7Srob 4,
348272e3cb7Srob 32767,
349272e3cb7Srob {
350272e3cb7Srob 0x02, 0x02, 0x7f, 0xff
351272e3cb7Srob },
352cc73a603Srob 4,
353cc73a603Srob {
354cc73a603Srob 0x02, 0x02, 0x7f, 0xff
355cc73a603Srob },
356272e3cb7Srob },
357272e3cb7Srob {
358272e3cb7Srob SUCCEED,
359272e3cb7Srob "integer (32768)",
360272e3cb7Srob 5,
361272e3cb7Srob 32768,
362272e3cb7Srob {
363272e3cb7Srob 0x02, 0x03, 0x00, 0x80, 0x00
364272e3cb7Srob },
365cc73a603Srob 5,
366cc73a603Srob {
367cc73a603Srob 0x02, 0x03, 0x00, 0x80, 0x00
368cc73a603Srob },
369272e3cb7Srob },
370272e3cb7Srob {
371272e3cb7Srob SUCCEED,
37242df20ffSrob "integer (65535)",
37342df20ffSrob 5,
37442df20ffSrob 65535,
37542df20ffSrob {
37642df20ffSrob 0x02, 0x03, 0x00, 0xff, 0xff
37742df20ffSrob },
378cc73a603Srob 5,
379cc73a603Srob {
380cc73a603Srob 0x02, 0x03, 0x00, 0xff, 0xff
381cc73a603Srob },
38242df20ffSrob },
38342df20ffSrob {
38442df20ffSrob SUCCEED,
38542df20ffSrob "integer (65536)",
386cc73a603Srob 5,
38742df20ffSrob 65536,
38842df20ffSrob {
389cc73a603Srob 0x02, 0x03, 0x01, 0x00, 0x00
390cc73a603Srob },
391cc73a603Srob 5,
392cc73a603Srob {
393cc73a603Srob 0x02, 0x03, 0x01, 0x00, 0x00
39442df20ffSrob },
39542df20ffSrob },
39642df20ffSrob {
39742df20ffSrob SUCCEED,
39842df20ffSrob "integer (2147483647)",
39942df20ffSrob 6,
40042df20ffSrob 2147483647,
40142df20ffSrob {
40242df20ffSrob 0x02, 0x04, 0x7f, 0xff, 0xff, 0xff
40342df20ffSrob },
404cc73a603Srob 6,
405cc73a603Srob {
406cc73a603Srob 0x02, 0x04, 0x7f, 0xff, 0xff, 0xff
407cc73a603Srob },
40842df20ffSrob },
40942df20ffSrob {
41042df20ffSrob SUCCEED,
41142df20ffSrob "integer (2147483648)",
412cc73a603Srob 7,
41342df20ffSrob 2147483648,
41442df20ffSrob {
415cc73a603Srob 0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x00
416cc73a603Srob },
417cc73a603Srob 7,
418cc73a603Srob {
419cc73a603Srob 0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x00
42042df20ffSrob },
42142df20ffSrob },
42242df20ffSrob {
42342df20ffSrob SUCCEED,
42442df20ffSrob "integer (2147483649)",
425cc73a603Srob 7,
42642df20ffSrob 2147483649,
42742df20ffSrob {
428cc73a603Srob 0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x01
429cc73a603Srob },
430cc73a603Srob 7,
431cc73a603Srob {
432cc73a603Srob 0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x01
43342df20ffSrob },
43442df20ffSrob },
43542df20ffSrob {
43642df20ffSrob SUCCEED,
43742df20ffSrob "integer (4294967295)",
438cc73a603Srob 7,
43942df20ffSrob 4294967295,
44042df20ffSrob {
441cc73a603Srob 0x02, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff
442cc73a603Srob },
443cc73a603Srob 7,
444cc73a603Srob {
445cc73a603Srob 0x02, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff
44642df20ffSrob },
44742df20ffSrob },
44842df20ffSrob {
44942df20ffSrob SUCCEED,
45042df20ffSrob "integer (4294967296)",
451cc73a603Srob 7,
45242df20ffSrob 4294967296,
45342df20ffSrob {
454cc73a603Srob 0x02, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00
455cc73a603Srob },
456cc73a603Srob 7,
457cc73a603Srob {
458cc73a603Srob 0x02, 0x05, 0x01, 0x00, 0x00, 0x00, 0x00
45942df20ffSrob },
46042df20ffSrob },
46142df20ffSrob {
46242df20ffSrob SUCCEED,
46342df20ffSrob "integer (4294967297)",
464cc73a603Srob 7,
46542df20ffSrob 4294967297,
46642df20ffSrob {
467cc73a603Srob 0x02, 0x05, 0x01, 0x00, 0x00, 0x00, 0x01
468cc73a603Srob },
469cc73a603Srob 7,
470cc73a603Srob {
471cc73a603Srob 0x02, 0x05, 0x01, 0x00, 0x00, 0x00, 0x01
47242df20ffSrob },
47342df20ffSrob },
47442df20ffSrob {
47542df20ffSrob SUCCEED,
47642df20ffSrob "integer (9223372036854775806)",
47742df20ffSrob 10,
47842df20ffSrob 9223372036854775806,
47942df20ffSrob {
48042df20ffSrob 0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
48142df20ffSrob 0xff, 0xfe
48242df20ffSrob },
483cc73a603Srob 10,
484cc73a603Srob {
485cc73a603Srob 0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
486cc73a603Srob 0xff, 0xfe
487cc73a603Srob },
48842df20ffSrob },
48942df20ffSrob {
49042df20ffSrob SUCCEED,
49142df20ffSrob "integer (9223372036854775807)",
49242df20ffSrob 10,
49342df20ffSrob 9223372036854775807,
49442df20ffSrob {
49542df20ffSrob 0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
49642df20ffSrob 0xff, 0xff
49742df20ffSrob },
498cc73a603Srob 10,
499cc73a603Srob {
500cc73a603Srob 0x02, 0x08, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
501cc73a603Srob 0xff, 0xff
502cc73a603Srob },
50342df20ffSrob },
50442df20ffSrob };
50542df20ffSrob
50642df20ffSrob static void
hexdump(const unsigned char * buf,size_t len)50742df20ffSrob hexdump(const unsigned char *buf, size_t len)
50842df20ffSrob {
50942df20ffSrob size_t i;
51042df20ffSrob
51142df20ffSrob for (i = 1; i < len; i++)
51242df20ffSrob fprintf(stderr, " 0x%02hhx,%s", buf[i - 1], i % 8 ? "": "\n");
51342df20ffSrob
51442df20ffSrob fprintf(stderr, " 0x%02hhx", buf[i - 1]);
51542df20ffSrob fprintf(stderr, "\n");
51642df20ffSrob }
51742df20ffSrob
51842df20ffSrob static int
test_read_elements(int i)519cc73a603Srob test_read_elements(int i)
52042df20ffSrob {
52142df20ffSrob int pos, b;
52242df20ffSrob char *string;
52342df20ffSrob void *p = NULL;
52442df20ffSrob ssize_t len = 0;
52542df20ffSrob struct ber_element *elm = NULL, *ptr = NULL;
52642df20ffSrob struct ber ber;
52742df20ffSrob long long val;
52842df20ffSrob void *bstring = NULL;
52942df20ffSrob struct ber_oid oid;
53042df20ffSrob struct ber_octetstring ostring;
53142df20ffSrob
53242df20ffSrob bzero(&ber, sizeof(ber));
533*696b5899Stb ober_set_readbuf(&ber, test_vectors[i].input,
534cc73a603Srob test_vectors[i].input_length);
53542df20ffSrob
536*696b5899Stb elm = ober_read_elements(&ber, elm);
537cc73a603Srob if (elm != NULL && test_vectors[i].fail == FAIL) {
538*696b5899Stb printf("unexpectedly passed ober_read_elements\n");
53942df20ffSrob return 1;
540cc73a603Srob } else if (elm == NULL && test_vectors[i].fail != FAIL) {
541*696b5899Stb printf("unexpectedly failed ober_read_elements\n");
54242df20ffSrob return 1;
543cc73a603Srob } else if (elm == NULL && test_vectors[i].fail == FAIL)
544cc73a603Srob return 0;
54542df20ffSrob
546*696b5899Stb pos = ober_getpos(elm);
54742df20ffSrob if (pos != 2) {
54842df20ffSrob printf("unexpected element position within "
549cc73a603Srob "byte stream (%d)\n", pos);
55042df20ffSrob return 1;
55142df20ffSrob }
55242df20ffSrob
55342df20ffSrob switch (elm->be_encoding) {
55442df20ffSrob case BER_TYPE_INTEGER:
555*696b5899Stb if (ober_get_integer(elm, &val) == -1) {
55642df20ffSrob printf("failed (int) encoding check\n");
55742df20ffSrob return 1;
55842df20ffSrob }
55942df20ffSrob if (val != test_vectors[i].value) {
560*696b5899Stb printf("(ober_get_integer) got %lld, expected %lld\n",
56142df20ffSrob val, test_vectors[i].value);
562272e3cb7Srob return 1;
56342df20ffSrob }
564*696b5899Stb if (ober_scanf_elements(elm, "i", &val) == -1) {
565*696b5899Stb printf("(ober_scanf_elements) failed (int)"
566*696b5899Stb " ober_scanf_elements (i)\n");
56742df20ffSrob return 1;
56842df20ffSrob }
56942df20ffSrob if (val != test_vectors[i].value) {
57042df20ffSrob printf("got %lld, expected %lld\n", val,
57142df20ffSrob test_vectors[i].value);
572272e3cb7Srob return 1;
57342df20ffSrob }
57442df20ffSrob break;
57542df20ffSrob default:
576272e3cb7Srob printf("failed with unexpected encoding (%ud)\n",
57742df20ffSrob elm->be_encoding);
57842df20ffSrob return 1;
57942df20ffSrob }
58042df20ffSrob
581*696b5899Stb len = ober_calc_len(elm);
582cc73a603Srob if (len != test_vectors[i].expect_length) {
58342df20ffSrob printf("failed to calculate length\n");
584cc73a603Srob printf("was %zd want %zu\n", len,
585cc73a603Srob test_vectors[i].expect_length);
58642df20ffSrob return 1;
58742df20ffSrob }
58842df20ffSrob
58942df20ffSrob ber.br_wbuf = NULL;
590*696b5899Stb len = ober_write_elements(&ber, elm);
591cc73a603Srob if (len != test_vectors[i].expect_length) {
59242df20ffSrob printf("failed length check (was %zd want "
593cc73a603Srob "%zd)\n", len, test_vectors[i].expect_length);
59442df20ffSrob return 1;
59542df20ffSrob }
59642df20ffSrob
597cc73a603Srob if (memcmp(ber.br_wbuf, test_vectors[i].expect,
598cc73a603Srob test_vectors[i].expect_length) != 0) {
59942df20ffSrob printf("failed byte stream compare\n");
60042df20ffSrob printf("Got:\n");
60142df20ffSrob hexdump(ber.br_wbuf, len);
60242df20ffSrob printf("Expected:\n");
603cc73a603Srob hexdump(test_vectors[i].expect, test_vectors[i].expect_length);
604cc73a603Srob return 1;
605cc73a603Srob }
606*696b5899Stb ober_free(&ber);
607cc73a603Srob
608*696b5899Stb ober_free_elements(elm);
609cc73a603Srob
610cc73a603Srob return 0;
611cc73a603Srob }
612cc73a603Srob
613cc73a603Srob static int
test_printf_elements(int i)614cc73a603Srob test_printf_elements(int i)
615cc73a603Srob {
616cc73a603Srob int pos, b;
617cc73a603Srob char *string;
618cc73a603Srob void *p = NULL;
619cc73a603Srob ssize_t len = 0;
620cc73a603Srob struct ber_element *elm = NULL, *ptr = NULL;
621cc73a603Srob struct ber ber;
622cc73a603Srob long long val;
623cc73a603Srob void *bstring = NULL;
624cc73a603Srob struct ber_oid oid;
625cc73a603Srob struct ber_octetstring ostring;
626cc73a603Srob
627cc73a603Srob bzero(&ber, sizeof(ber));
628*696b5899Stb ober_set_readbuf(&ber, test_vectors[i].input,
629cc73a603Srob test_vectors[i].input_length);
630cc73a603Srob
631*696b5899Stb elm = ober_printf_elements(elm, "i", test_vectors[i].value);
632cc73a603Srob if (elm == NULL) {
633*696b5899Stb printf("unexpectedly failed ober_printf_elements\n");
634cc73a603Srob return 1;
635cc73a603Srob }
636cc73a603Srob
637cc73a603Srob switch (elm->be_encoding) {
638cc73a603Srob case BER_TYPE_INTEGER:
639*696b5899Stb if (ober_get_integer(elm, &val) == -1) {
640cc73a603Srob printf("failed (int) encoding check\n");
641cc73a603Srob return 1;
642cc73a603Srob }
643cc73a603Srob if (val != test_vectors[i].value) {
644*696b5899Stb printf("(ober_get_integer) got %lld, expected %lld\n",
645cc73a603Srob val, test_vectors[i].value);
646cc73a603Srob return 1;
647cc73a603Srob }
648*696b5899Stb if (ober_scanf_elements(elm, "i", &val) == -1) {
649*696b5899Stb printf("(ober_scanf_elements) failed (int)"
650*696b5899Stb " ober_scanf_elements (i)\n");
651cc73a603Srob return 1;
652cc73a603Srob }
653cc73a603Srob if (val != test_vectors[i].value) {
654cc73a603Srob printf("got %lld, expected %lld\n", val,
655cc73a603Srob test_vectors[i].value);
656cc73a603Srob return 1;
657cc73a603Srob }
658cc73a603Srob break;
659cc73a603Srob default:
660cc73a603Srob printf("failed with unexpected encoding (%ud)\n",
661cc73a603Srob elm->be_encoding);
662cc73a603Srob return 1;
663cc73a603Srob }
664cc73a603Srob
665*696b5899Stb len = ober_calc_len(elm);
666cc73a603Srob if (len != test_vectors[i].expect_length) {
667cc73a603Srob printf("failed to calculate length\n");
668cc73a603Srob printf("was %zd want %zu\n", len,
669cc73a603Srob test_vectors[i].expect_length);
670cc73a603Srob return 1;
671cc73a603Srob }
672cc73a603Srob
673cc73a603Srob ber.br_wbuf = NULL;
674*696b5899Stb len = ober_write_elements(&ber, elm);
675cc73a603Srob if (len != test_vectors[i].expect_length) {
676cc73a603Srob printf("failed length check (was %zd want "
677cc73a603Srob "%zd)\n", len, test_vectors[i].expect_length);
678cc73a603Srob return 1;
679cc73a603Srob }
680cc73a603Srob
681cc73a603Srob if (memcmp(ber.br_wbuf, test_vectors[i].expect,
682cc73a603Srob test_vectors[i].expect_length) != 0) {
683cc73a603Srob printf("failed byte stream compare\n");
684cc73a603Srob printf("Got:\n");
685cc73a603Srob hexdump(ber.br_wbuf, len);
686cc73a603Srob printf("Expected:\n");
687cc73a603Srob hexdump(test_vectors[i].expect, test_vectors[i].expect_length);
68842df20ffSrob return 1;
68942df20ffSrob }
690*696b5899Stb ober_free(&ber);
69142df20ffSrob
692*696b5899Stb ober_free_elements(elm);
69342df20ffSrob
69442df20ffSrob return 0;
69542df20ffSrob }
69642df20ffSrob
69742df20ffSrob int
main(void)69842df20ffSrob main(void)
69942df20ffSrob {
70042df20ffSrob extern char *__progname;
70142df20ffSrob
70242df20ffSrob ssize_t len = 0;
70342df20ffSrob int i, ret = 0;
70442df20ffSrob
70542df20ffSrob /*
70642df20ffSrob * drive test vectors for ber byte stream input validation, etc.
70742df20ffSrob */
708cc73a603Srob printf("= = = = = test_read_elements\n");
70942df20ffSrob for (i = 0; i < sizeof(test_vectors) / sizeof(test_vectors[0]); i++) {
710cc73a603Srob if (test_read_elements(i) != 0) {
711cc73a603Srob printf("FAILED: %s\n", test_vectors[i].title);
712cc73a603Srob ret = 1;
713cc73a603Srob } else
714cc73a603Srob printf("SUCCESS: %s\n", test_vectors[i].title);
715cc73a603Srob }
716cc73a603Srob
717cc73a603Srob printf("= = = = = test_printf_elements\n");
718cc73a603Srob for (i = 0; i < sizeof(test_vectors) / sizeof(test_vectors[0]); i++) {
719cc73a603Srob /*
720*696b5899Stb * skip test cases known to fail under ober_read_elements as
721*696b5899Stb * they are not applicable to ober_printf_elements
722cc73a603Srob */
723cc73a603Srob if (test_vectors[i].fail)
724cc73a603Srob continue;
725cc73a603Srob if (test_printf_elements(i) != 0) {
72642df20ffSrob printf("FAILED: %s\n", test_vectors[i].title);
72742df20ffSrob ret = 1;
72842df20ffSrob } else
72942df20ffSrob printf("SUCCESS: %s\n", test_vectors[i].title);
73042df20ffSrob }
73142df20ffSrob
73242df20ffSrob if (ret != 0) {
73342df20ffSrob printf("FAILED: %s\n", __progname);
73442df20ffSrob return 1;
73542df20ffSrob }
73642df20ffSrob
73742df20ffSrob return 0;
73842df20ffSrob }
739