xref: /minix3/crypto/external/bsd/heimdal/dist/lib/wind/test-rw.c (revision ebfedea0ce5bbe81e252ddf32d732e40fb633fae)
1*ebfedea0SLionel Sambuc /*	$NetBSD: test-rw.c,v 1.1.1.1 2011/04/13 18:16:00 elric Exp $	*/
2*ebfedea0SLionel Sambuc 
3*ebfedea0SLionel Sambuc /*
4*ebfedea0SLionel Sambuc  * Copyright (c) 2008 Kungliga Tekniska Högskolan
5*ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6*ebfedea0SLionel Sambuc  * All rights reserved.
7*ebfedea0SLionel Sambuc  *
8*ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9*ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10*ebfedea0SLionel Sambuc  * are met:
11*ebfedea0SLionel Sambuc  *
12*ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc  *
15*ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16*ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17*ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18*ebfedea0SLionel Sambuc  *
19*ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20*ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21*ebfedea0SLionel Sambuc  *    without specific prior written permission.
22*ebfedea0SLionel Sambuc  *
23*ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24*ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27*ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34*ebfedea0SLionel Sambuc  */
35*ebfedea0SLionel Sambuc 
36*ebfedea0SLionel Sambuc #include "windlocl.h"
37*ebfedea0SLionel Sambuc #include <stdio.h>
38*ebfedea0SLionel Sambuc #include <err.h>
39*ebfedea0SLionel Sambuc #include <assert.h>
40*ebfedea0SLionel Sambuc 
41*ebfedea0SLionel Sambuc #define MAX_LENGTH 10
42*ebfedea0SLionel Sambuc 
43*ebfedea0SLionel Sambuc 
44*ebfedea0SLionel Sambuc struct testcase {
45*ebfedea0SLionel Sambuc     unsigned int in_flags;
46*ebfedea0SLionel Sambuc     size_t in_len;
47*ebfedea0SLionel Sambuc     const char *in_ptr;
48*ebfedea0SLionel Sambuc     int ret;
49*ebfedea0SLionel Sambuc     size_t ucs2_len;
50*ebfedea0SLionel Sambuc     uint16_t ucs2[MAX_LENGTH];
51*ebfedea0SLionel Sambuc     unsigned int out_flags;
52*ebfedea0SLionel Sambuc } testcases[] = {
53*ebfedea0SLionel Sambuc     {
54*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
55*ebfedea0SLionel Sambuc 	4, "\xff\xfe\x20\x00",
56*ebfedea0SLionel Sambuc 	0,
57*ebfedea0SLionel Sambuc 	1, { 0x0020 },
58*ebfedea0SLionel Sambuc 	WIND_RW_LE
59*ebfedea0SLionel Sambuc     },
60*ebfedea0SLionel Sambuc     {
61*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
62*ebfedea0SLionel Sambuc 	4, "\xfe\xff\x00\x20",
63*ebfedea0SLionel Sambuc 	0,
64*ebfedea0SLionel Sambuc 	1, { 0x0020 },
65*ebfedea0SLionel Sambuc 	WIND_RW_BE
66*ebfedea0SLionel Sambuc     },
67*ebfedea0SLionel Sambuc     /* only BE BOM */
68*ebfedea0SLionel Sambuc     {
69*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
70*ebfedea0SLionel Sambuc 	2, "\xfe\xff",
71*ebfedea0SLionel Sambuc 	0,
72*ebfedea0SLionel Sambuc 	0, { 0 },
73*ebfedea0SLionel Sambuc 	WIND_RW_BE
74*ebfedea0SLionel Sambuc     },
75*ebfedea0SLionel Sambuc     /* no input */
76*ebfedea0SLionel Sambuc     {
77*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
78*ebfedea0SLionel Sambuc 	0, "",
79*ebfedea0SLionel Sambuc 	0,
80*ebfedea0SLionel Sambuc 	0, { 0 },
81*ebfedea0SLionel Sambuc 	WIND_RW_BOM
82*ebfedea0SLionel Sambuc     },
83*ebfedea0SLionel Sambuc     /* BOM only */
84*ebfedea0SLionel Sambuc     {
85*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
86*ebfedea0SLionel Sambuc 	2, "\xff\xfe",
87*ebfedea0SLionel Sambuc 	0,
88*ebfedea0SLionel Sambuc 	0, { 0 },
89*ebfedea0SLionel Sambuc 	WIND_RW_LE
90*ebfedea0SLionel Sambuc     },
91*ebfedea0SLionel Sambuc     /* water + z */
92*ebfedea0SLionel Sambuc     {
93*ebfedea0SLionel Sambuc 	WIND_RW_BOM|WIND_RW_LE,
94*ebfedea0SLionel Sambuc 	4, "\x34\x6C\x7A\x00",
95*ebfedea0SLionel Sambuc 	0,
96*ebfedea0SLionel Sambuc 	2, { 0x6C34, 0x7a },
97*ebfedea0SLionel Sambuc 	WIND_RW_LE
98*ebfedea0SLionel Sambuc     },
99*ebfedea0SLionel Sambuc     /* water + z */
100*ebfedea0SLionel Sambuc     {
101*ebfedea0SLionel Sambuc 	WIND_RW_LE,
102*ebfedea0SLionel Sambuc 	4, "\x34\x6C\x7A\x00",
103*ebfedea0SLionel Sambuc 	0,
104*ebfedea0SLionel Sambuc 	2, { 0x6C34, 0x7a },
105*ebfedea0SLionel Sambuc 	WIND_RW_LE
106*ebfedea0SLionel Sambuc     },
107*ebfedea0SLionel Sambuc     /* BOM + water + z */
108*ebfedea0SLionel Sambuc     {
109*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
110*ebfedea0SLionel Sambuc 	6, "\xFF\xFE\x34\x6C\x7A\x00",
111*ebfedea0SLionel Sambuc 	0,
112*ebfedea0SLionel Sambuc 	2, { 0x6C34, 0x7a },
113*ebfedea0SLionel Sambuc 	WIND_RW_LE
114*ebfedea0SLionel Sambuc     },
115*ebfedea0SLionel Sambuc     /* BOM + water + z */
116*ebfedea0SLionel Sambuc     {
117*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
118*ebfedea0SLionel Sambuc 	6, "\xFE\xFF\x6C\x34\x00\x7A",
119*ebfedea0SLionel Sambuc 	0,
120*ebfedea0SLionel Sambuc 	2, { 0x6C34, 0x7a },
121*ebfedea0SLionel Sambuc 	WIND_RW_BE
122*ebfedea0SLionel Sambuc     },
123*ebfedea0SLionel Sambuc     /* error, odd length */
124*ebfedea0SLionel Sambuc     {
125*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
126*ebfedea0SLionel Sambuc 	1, "\xfe",
127*ebfedea0SLionel Sambuc 	WIND_ERR_LENGTH_NOT_MOD2,
128*ebfedea0SLionel Sambuc 	0, { 0 },
129*ebfedea0SLionel Sambuc 	WIND_RW_BOM
130*ebfedea0SLionel Sambuc     },
131*ebfedea0SLionel Sambuc     /* error, missing BOM */
132*ebfedea0SLionel Sambuc     {
133*ebfedea0SLionel Sambuc 	WIND_RW_BOM,
134*ebfedea0SLionel Sambuc 	2, "\x00\x20",
135*ebfedea0SLionel Sambuc 	WIND_ERR_NO_BOM,
136*ebfedea0SLionel Sambuc 	0, { 0 },
137*ebfedea0SLionel Sambuc 	WIND_RW_BOM
138*ebfedea0SLionel Sambuc     },
139*ebfedea0SLionel Sambuc     /* error, overrun */
140*ebfedea0SLionel Sambuc     {
141*ebfedea0SLionel Sambuc 	WIND_RW_BE,
142*ebfedea0SLionel Sambuc 	4, "\x00\x20\x00\x20",
143*ebfedea0SLionel Sambuc 	WIND_ERR_OVERRUN,
144*ebfedea0SLionel Sambuc 	1, { 0x20 },
145*ebfedea0SLionel Sambuc 	WIND_RW_BOM
146*ebfedea0SLionel Sambuc     }
147*ebfedea0SLionel Sambuc 
148*ebfedea0SLionel Sambuc };
149*ebfedea0SLionel Sambuc 
150*ebfedea0SLionel Sambuc int
main(void)151*ebfedea0SLionel Sambuc main(void)
152*ebfedea0SLionel Sambuc {
153*ebfedea0SLionel Sambuc     unsigned int n, m, flags;
154*ebfedea0SLionel Sambuc     uint16_t data[MAX_LENGTH];
155*ebfedea0SLionel Sambuc     size_t datalen;
156*ebfedea0SLionel Sambuc     int ret;
157*ebfedea0SLionel Sambuc 
158*ebfedea0SLionel Sambuc     for (n = 0; n < sizeof(testcases)/sizeof(testcases[0]); n++) {
159*ebfedea0SLionel Sambuc 	flags = testcases[n].in_flags;
160*ebfedea0SLionel Sambuc 
161*ebfedea0SLionel Sambuc 	datalen = testcases[n].ucs2_len;
162*ebfedea0SLionel Sambuc 	assert(datalen < sizeof(data));
163*ebfedea0SLionel Sambuc 
164*ebfedea0SLionel Sambuc 	ret = wind_ucs2read(testcases[n].in_ptr,
165*ebfedea0SLionel Sambuc 			    testcases[n].in_len,
166*ebfedea0SLionel Sambuc 			    &flags,
167*ebfedea0SLionel Sambuc 			    data,
168*ebfedea0SLionel Sambuc 			    &datalen);
169*ebfedea0SLionel Sambuc 	if (ret != testcases[n].ret)
170*ebfedea0SLionel Sambuc 	    errx(1, "testcases %u: wind_ucs2read: %d", n, ret);
171*ebfedea0SLionel Sambuc 
172*ebfedea0SLionel Sambuc 	/* on error, skip all other tests */
173*ebfedea0SLionel Sambuc 	if (ret)
174*ebfedea0SLionel Sambuc 	    continue;
175*ebfedea0SLionel Sambuc 
176*ebfedea0SLionel Sambuc 	if (flags != testcases[n].out_flags)
177*ebfedea0SLionel Sambuc 	    errx(1, "testcases %u: flags wrong", n);
178*ebfedea0SLionel Sambuc 
179*ebfedea0SLionel Sambuc 	if (datalen != testcases[n].ucs2_len)
180*ebfedea0SLionel Sambuc 	    errx(1, "testcases %u: ucs len wrong", n);
181*ebfedea0SLionel Sambuc 
182*ebfedea0SLionel Sambuc 	for (m = 0; m < datalen; m++)
183*ebfedea0SLionel Sambuc 	    if (testcases[n].ucs2[m] != data[m])
184*ebfedea0SLionel Sambuc 		errx(1, "testcases %u: char %u wrong", n, m);
185*ebfedea0SLionel Sambuc     }
186*ebfedea0SLionel Sambuc 
187*ebfedea0SLionel Sambuc     return 0;
188*ebfedea0SLionel Sambuc }
189