1226031Sstas/*
2226031Sstas * Copyright (c) 2008 Kungliga Tekniska H��gskolan
3226031Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4226031Sstas * All rights reserved.
5226031Sstas *
6226031Sstas * Redistribution and use in source and binary forms, with or without
7226031Sstas * modification, are permitted provided that the following conditions
8226031Sstas * are met:
9226031Sstas *
10226031Sstas * 1. Redistributions of source code must retain the above copyright
11226031Sstas *    notice, this list of conditions and the following disclaimer.
12226031Sstas *
13226031Sstas * 2. Redistributions in binary form must reproduce the above copyright
14226031Sstas *    notice, this list of conditions and the following disclaimer in the
15226031Sstas *    documentation and/or other materials provided with the distribution.
16226031Sstas *
17226031Sstas * 3. Neither the name of the Institute nor the names of its contributors
18226031Sstas *    may be used to endorse or promote products derived from this software
19226031Sstas *    without specific prior written permission.
20226031Sstas *
21226031Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22226031Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23226031Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24226031Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25226031Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26226031Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27226031Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28226031Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29226031Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30226031Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31226031Sstas * SUCH DAMAGE.
32226031Sstas */
33226031Sstas
34226031Sstas#include "windlocl.h"
35226031Sstas#include <stdio.h>
36226031Sstas#include <err.h>
37226031Sstas#include <assert.h>
38226031Sstas
39226031Sstas#define MAX_LENGTH 10
40226031Sstas
41226031Sstas
42226031Sstasstruct testcase {
43226031Sstas    unsigned int in_flags;
44226031Sstas    size_t in_len;
45226031Sstas    const char *in_ptr;
46226031Sstas    int ret;
47226031Sstas    size_t ucs2_len;
48226031Sstas    uint16_t ucs2[MAX_LENGTH];
49226031Sstas    unsigned int out_flags;
50226031Sstas} testcases[] = {
51226031Sstas    {
52226031Sstas	WIND_RW_BOM,
53226031Sstas	4, "\xff\xfe\x20\x00",
54226031Sstas	0,
55226031Sstas	1, { 0x0020 },
56226031Sstas	WIND_RW_LE
57226031Sstas    },
58226031Sstas    {
59226031Sstas	WIND_RW_BOM,
60226031Sstas	4, "\xfe\xff\x00\x20",
61226031Sstas	0,
62226031Sstas	1, { 0x0020 },
63226031Sstas	WIND_RW_BE
64226031Sstas    },
65226031Sstas    /* only BE BOM */
66226031Sstas    {
67226031Sstas	WIND_RW_BOM,
68226031Sstas	2, "\xfe\xff",
69226031Sstas	0,
70226031Sstas	0, { 0 },
71226031Sstas	WIND_RW_BE
72226031Sstas    },
73226031Sstas    /* no input */
74226031Sstas    {
75226031Sstas	WIND_RW_BOM,
76226031Sstas	0, "",
77226031Sstas	0,
78226031Sstas	0, { 0 },
79226031Sstas	WIND_RW_BOM
80226031Sstas    },
81226031Sstas    /* BOM only */
82226031Sstas    {
83226031Sstas	WIND_RW_BOM,
84226031Sstas	2, "\xff\xfe",
85226031Sstas	0,
86226031Sstas	0, { 0 },
87226031Sstas	WIND_RW_LE
88226031Sstas    },
89226031Sstas    /* water + z */
90226031Sstas    {
91226031Sstas	WIND_RW_BOM|WIND_RW_LE,
92226031Sstas	4, "\x34\x6C\x7A\x00",
93226031Sstas	0,
94226031Sstas	2, { 0x6C34, 0x7a },
95226031Sstas	WIND_RW_LE
96226031Sstas    },
97226031Sstas    /* water + z */
98226031Sstas    {
99226031Sstas	WIND_RW_LE,
100226031Sstas	4, "\x34\x6C\x7A\x00",
101226031Sstas	0,
102226031Sstas	2, { 0x6C34, 0x7a },
103226031Sstas	WIND_RW_LE
104226031Sstas    },
105226031Sstas    /* BOM + water + z */
106226031Sstas    {
107226031Sstas	WIND_RW_BOM,
108226031Sstas	6, "\xFF\xFE\x34\x6C\x7A\x00",
109226031Sstas	0,
110226031Sstas	2, { 0x6C34, 0x7a },
111226031Sstas	WIND_RW_LE
112226031Sstas    },
113226031Sstas    /* BOM + water + z */
114226031Sstas    {
115226031Sstas	WIND_RW_BOM,
116226031Sstas	6, "\xFE\xFF\x6C\x34\x00\x7A",
117226031Sstas	0,
118226031Sstas	2, { 0x6C34, 0x7a },
119226031Sstas	WIND_RW_BE
120226031Sstas    },
121226031Sstas    /* error, odd length */
122226031Sstas    {
123226031Sstas	WIND_RW_BOM,
124226031Sstas	1, "\xfe",
125226031Sstas	WIND_ERR_LENGTH_NOT_MOD2,
126226031Sstas	0, { 0 },
127226031Sstas	WIND_RW_BOM
128226031Sstas    },
129226031Sstas    /* error, missing BOM */
130226031Sstas    {
131226031Sstas	WIND_RW_BOM,
132226031Sstas	2, "\x00\x20",
133226031Sstas	WIND_ERR_NO_BOM,
134226031Sstas	0, { 0 },
135226031Sstas	WIND_RW_BOM
136226031Sstas    },
137226031Sstas    /* error, overrun */
138226031Sstas    {
139226031Sstas	WIND_RW_BE,
140226031Sstas	4, "\x00\x20\x00\x20",
141226031Sstas	WIND_ERR_OVERRUN,
142226031Sstas	1, { 0x20 },
143226031Sstas	WIND_RW_BOM
144226031Sstas    }
145226031Sstas
146226031Sstas};
147226031Sstas
148226031Sstasint
149226031Sstasmain(void)
150226031Sstas{
151226031Sstas    unsigned int n, m, flags;
152226031Sstas    uint16_t data[MAX_LENGTH];
153226031Sstas    size_t datalen;
154226031Sstas    int ret;
155226031Sstas
156226031Sstas    for (n = 0; n < sizeof(testcases)/sizeof(testcases[0]); n++) {
157226031Sstas	flags = testcases[n].in_flags;
158226031Sstas
159226031Sstas	datalen = testcases[n].ucs2_len;
160226031Sstas	assert(datalen < sizeof(data));
161226031Sstas
162226031Sstas	ret = wind_ucs2read(testcases[n].in_ptr,
163226031Sstas			    testcases[n].in_len,
164226031Sstas			    &flags,
165226031Sstas			    data,
166226031Sstas			    &datalen);
167226031Sstas	if (ret != testcases[n].ret)
168226031Sstas	    errx(1, "testcases %u: wind_ucs2read: %d", n, ret);
169226031Sstas
170226031Sstas	/* on error, skip all other tests */
171226031Sstas	if (ret)
172226031Sstas	    continue;
173226031Sstas
174226031Sstas	if (flags != testcases[n].out_flags)
175226031Sstas	    errx(1, "testcases %u: flags wrong", n);
176226031Sstas
177226031Sstas	if (datalen != testcases[n].ucs2_len)
178226031Sstas	    errx(1, "testcases %u: ucs len wrong", n);
179226031Sstas
180226031Sstas	for (m = 0; m < datalen; m++)
181226031Sstas	    if (testcases[n].ucs2[m] != data[m])
182226031Sstas		errx(1, "testcases %u: char %u wrong", n, m);
183226031Sstas    }
184226031Sstas
185226031Sstas    return 0;
186226031Sstas}
187