t_io.c revision 314817
1/* $NetBSD: t_io.c,v 1.4 2014/01/21 00:32:16 yamt Exp $ */ 2 3/*- 4 * Copyright (c) 2013 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 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 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__COPYRIGHT("@(#) Copyright (c) 2011\ 34 The NetBSD Foundation, inc. All rights reserved."); 35__RCSID("$NetBSD: t_io.c,v 1.4 2014/01/21 00:32:16 yamt Exp $"); 36 37#include <sys/param.h> 38#include <errno.h> 39#include <locale.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <string.h> 43#include <wchar.h> 44 45#include <atf-c.h> 46 47 48ATF_TC(bad_big5_wprintf); 49ATF_TC_HEAD(bad_big5_wprintf, tc) 50{ 51 atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar wprintf"); 52} 53 54ATF_TC_BODY(bad_big5_wprintf, tc) 55{ 56#ifdef __FreeBSD__ 57 atf_tc_skip("does not fail as expected (may be implementation " 58 "specific issue with the test)"); 59#endif 60 /* XXX implementation detail knowledge (wchar_t encoding) */ 61 wchar_t ibuf[] = { 0xcf10, 0 }; 62 setlocale(LC_CTYPE, "zh_TW.Big5"); 63 ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0); 64 ATF_REQUIRE(ferror(stdout)); 65} 66 67ATF_TC(bad_big5_swprintf); 68ATF_TC_HEAD(bad_big5_swprintf, tc) 69{ 70 atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf"); 71} 72 73ATF_TC_BODY(bad_big5_swprintf, tc) 74{ 75#ifdef __FreeBSD__ 76 atf_tc_skip("does not fail as expected (may be implementation " 77 "specific issue with the test)"); 78#endif 79 /* XXX implementation detail knowledge (wchar_t encoding) */ 80 wchar_t ibuf[] = { 0xcf10, 0 }; 81 wchar_t obuf[20]; 82 setlocale(LC_CTYPE, "zh_TW.Big5"); 83 ATF_REQUIRE_ERRNO(EILSEQ, 84 swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0); 85} 86 87ATF_TC(good_big5_wprintf); 88ATF_TC_HEAD(good_big5_wprintf, tc) 89{ 90 atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf"); 91} 92 93ATF_TC_BODY(good_big5_wprintf, tc) 94{ 95 /* XXX implementation detail knowledge (wchar_t encoding) */ 96 wchar_t ibuf[] = { 0xcf40, 0 }; 97 setlocale(LC_CTYPE, "zh_TW.Big5"); 98 ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2); 99} 100 101ATF_TC(good_big5_swprintf); 102ATF_TC_HEAD(good_big5_swprintf, tc) 103{ 104 atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf"); 105} 106 107ATF_TC_BODY(good_big5_swprintf, tc) 108{ 109 /* XXX implementation detail knowledge (wchar_t encoding) */ 110 wchar_t ibuf[] = { 0xcf40, 0 }; 111 wchar_t obuf[20]; 112 setlocale(LC_CTYPE, "zh_TW.Big5"); 113 ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2); 114} 115 116struct ibuf { 117 off_t off; 118 size_t buflen; 119 const char *buf; 120}; 121 122static int 123readfn(void *vp, char *buf, int len) 124{ 125 struct ibuf *ib = vp; 126 size_t todo = MIN((size_t)len, ib->buflen - ib->off); 127 128 memcpy(buf, ib->buf + ib->off, todo); 129 ib->off += todo; 130 return todo; 131} 132 133ATF_TC(good_big5_getwc); 134ATF_TC_HEAD(good_big5_getwc, tc) 135{ 136 atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc"); 137} 138 139ATF_TC_BODY(good_big5_getwc, tc) 140{ 141 const char buf[] = { 0xcf, 0x40 }; 142 struct ibuf ib = { 143 .buf = buf, 144 .buflen = sizeof(buf), 145 }; 146 FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL); 147 148 ATF_REQUIRE(fp != NULL); 149 setlocale(LC_CTYPE, "zh_TW.Big5"); 150 /* XXX implementation detail knowledge (wchar_t encoding) */ 151 ATF_REQUIRE_EQ(getwc(fp), 0xcf40); 152 fclose(fp); 153} 154 155ATF_TC(bad_big5_getwc); 156ATF_TC_HEAD(bad_big5_getwc, tc) 157{ 158 atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc"); 159} 160 161ATF_TC_BODY(bad_big5_getwc, tc) 162{ 163 const char buf[] = { 0xcf, 0x20 }; 164 struct ibuf ib = { 165 .buf = buf, 166 .buflen = sizeof(buf), 167 }; 168 FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL); 169 170 ATF_REQUIRE(fp != NULL); 171 setlocale(LC_CTYPE, "zh_TW.Big5"); 172#ifdef __FreeBSD__ 173 atf_tc_expect_fail("does not return WEOF as expected"); 174#endif 175 ATF_REQUIRE_EQ(getwc(fp), WEOF); 176 fclose(fp); 177} 178 179ATF_TP_ADD_TCS(tp) 180{ 181 ATF_TP_ADD_TC(tp, bad_big5_wprintf); 182 ATF_TP_ADD_TC(tp, bad_big5_swprintf); 183 ATF_TP_ADD_TC(tp, good_big5_wprintf); 184 ATF_TP_ADD_TC(tp, good_big5_swprintf); 185 ATF_TP_ADD_TC(tp, good_big5_getwc); 186 ATF_TP_ADD_TC(tp, bad_big5_getwc); 187 188 return atf_no_error(); 189} 190