[HEIMDAL-646] malloc(0) checks for AIX
[heimdal.git] / lib / wind / test-rw.c
blob62f68e2f36f9a29d95cfe0d6a679ed1159796403
1 /*
2 * Copyright (c) 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "windlocl.h"
35 #include <stdio.h>
36 #include <err.h>
37 #include <assert.h>
39 #define MAX_LENGTH 10
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[] = {
52 WIND_RW_BOM,
53 4, "\xff\xfe\x20\x00",
55 1, { 0x0020 },
56 WIND_RW_LE
59 WIND_RW_BOM,
60 4, "\xfe\xff\x00\x20",
62 1, { 0x0020 },
63 WIND_RW_BE
65 /* only BE BOM */
67 WIND_RW_BOM,
68 2, "\xfe\xff",
70 0, { },
71 WIND_RW_BE
73 /* no input */
75 WIND_RW_BOM,
76 0, "",
78 0, { },
79 WIND_RW_BOM
81 /* BOM only */
83 WIND_RW_BOM,
84 2, "\xff\xfe",
86 0, { },
87 WIND_RW_LE
89 /* water + z */
91 WIND_RW_BOM|WIND_RW_LE,
92 4, "\x34\x6C\x7A\x00",
94 2, { 0x6C34, 0x7a },
95 WIND_RW_LE
97 /* water + z */
99 WIND_RW_LE,
100 4, "\x34\x6C\x7A\x00",
102 2, { 0x6C34, 0x7a },
103 WIND_RW_LE
105 /* BOM + water + z */
107 WIND_RW_BOM,
108 6, "\xFF\xFE\x34\x6C\x7A\x00",
110 2, { 0x6C34, 0x7a },
111 WIND_RW_LE
113 /* BOM + water + z */
115 WIND_RW_BOM,
116 6, "\xFE\xFF\x6C\x34\x00\x7A",
118 2, { 0x6C34, 0x7a },
119 WIND_RW_BE
121 /* error, odd length */
123 WIND_RW_BOM,
124 1, "\xfe",
125 WIND_ERR_LENGTH_NOT_MOD2,
126 0, { },
127 WIND_RW_BOM
129 /* error, missing BOM */
131 WIND_RW_BOM,
132 2, "\x00\x20",
133 WIND_ERR_NO_BOM,
134 0, { },
135 WIND_RW_BOM
137 /* error, overrun */
139 WIND_RW_BE,
140 4, "\x00\x20\x00\x20",
141 WIND_ERR_OVERRUN,
142 1, { 0x20 },
143 WIND_RW_BOM
149 main(void)
151 unsigned int n, m, flags;
152 uint16_t data[MAX_LENGTH];
153 size_t datalen;
154 int ret;
156 for (n = 0; n < sizeof(testcases)/sizeof(testcases[0]); n++) {
157 flags = testcases[n].in_flags;
159 datalen = testcases[n].ucs2_len;
160 assert(datalen < sizeof(data));
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);
170 /* on error, skip all other tests */
171 if (ret)
172 continue;
174 if (flags != testcases[n].out_flags)
175 errx(1, "testcases %u: flags wrong", n);
177 if (datalen != testcases[n].ucs2_len)
178 errx(1, "testcases %u: ucs len wrong", n);
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);
185 return 0;