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