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