Home | History | Annotate | Line # | Download | only in locale
      1 /* $NetBSD: t_io.c,v 1.5 2017/07/12 17:32:51 perseant 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.5 2017/07/12 17:32:51 perseant 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 
     48 ATF_TC(bad_big5_wprintf);
     49 ATF_TC_HEAD(bad_big5_wprintf, tc)
     50 {
     51 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar wprintf");
     52 }
     53 
     54 ATF_TC_BODY(bad_big5_wprintf, tc)
     55 {
     56 	wchar_t ibuf[] = {
     57 #ifdef __STDC_ISO_10646__
     58 		0x0978, 0  /* An arbitrarily chosen Devangari symbol */
     59 #else
     60 		/* XXX implementation detail knowledge (wchar_t encoding) */
     61 		0xcf10, 0
     62 #endif
     63 	};
     64 	setlocale(LC_CTYPE, "zh_TW.Big5");
     65 	ATF_REQUIRE_ERRNO(EILSEQ, wprintf(L"%ls\n", ibuf) < 0);
     66 	ATF_REQUIRE(ferror(stdout));
     67 }
     68 
     69 ATF_TC(bad_big5_swprintf);
     70 ATF_TC_HEAD(bad_big5_swprintf, tc)
     71 {
     72 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
     73 }
     74 
     75 ATF_TC_BODY(bad_big5_swprintf, tc)
     76 {
     77 	wchar_t ibuf[] = {
     78 #ifdef __STDC_ISO_10646__
     79 		0x0978, 0  /* An arbitrarily chosen Devangari symbol */
     80 #else
     81 		/* XXX implementation detail knowledge (wchar_t encoding) */
     82 		0xcf10, 0
     83 #endif
     84 	};
     85 	wchar_t obuf[20];
     86 	setlocale(LC_CTYPE, "zh_TW.Big5");
     87 	ATF_REQUIRE_ERRNO(EILSEQ,
     88 			  swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf) < 0);
     89 }
     90 
     91 ATF_TC(good_big5_wprintf);
     92 ATF_TC_HEAD(good_big5_wprintf, tc)
     93 {
     94 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
     95 }
     96 
     97 ATF_TC_BODY(good_big5_wprintf, tc)
     98 {
     99 	wchar_t ibuf[] = {
    100 #ifdef __STDC_ISO_10646__
    101 		0x67DC, 0
    102 #else
    103 		/* XXX implementation detail knowledge (wchar_t encoding) */
    104 		0xcf40, 0
    105 #endif
    106 	};
    107 	setlocale(LC_CTYPE, "zh_TW.Big5");
    108 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
    109 }
    110 
    111 ATF_TC(good_big5_swprintf);
    112 ATF_TC_HEAD(good_big5_swprintf, tc)
    113 {
    114 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
    115 }
    116 
    117 ATF_TC_BODY(good_big5_swprintf, tc)
    118 {
    119 	wchar_t ibuf[] = {
    120 #ifdef __STDC_ISO_10646__
    121 		0x67DC, 0
    122 #else
    123 		/* XXX implementation detail knowledge (wchar_t encoding) */
    124 		0xcf40, 0
    125 #endif
    126 	};
    127 	wchar_t obuf[20];
    128 	setlocale(LC_CTYPE, "zh_TW.Big5");
    129 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
    130 }
    131 
    132 struct ibuf {
    133 	off_t off;
    134 	size_t buflen;
    135 	const char *buf;
    136 };
    137 
    138 static int
    139 readfn(void *vp, char *buf, int len)
    140 {
    141 	struct ibuf *ib = vp;
    142 	size_t todo = MIN((size_t)len, ib->buflen - ib->off);
    143 
    144 	memcpy(buf, ib->buf + ib->off, todo);
    145 	ib->off += todo;
    146 	return todo;
    147 }
    148 
    149 ATF_TC(good_big5_getwc);
    150 ATF_TC_HEAD(good_big5_getwc, tc)
    151 {
    152 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
    153 }
    154 
    155 ATF_TC_BODY(good_big5_getwc, tc)
    156 {
    157 	const char buf[] = { 0xcf, 0x40 };
    158 	struct ibuf ib = {
    159 		.buf = buf,
    160 		.buflen = sizeof(buf),
    161 	};
    162 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
    163 
    164 	ATF_REQUIRE(fp != NULL);
    165 	setlocale(LC_CTYPE, "zh_TW.Big5");
    166 	ATF_REQUIRE_EQ(getwc(fp),
    167 #ifdef __STDC_ISO_10646__
    168 		       0x67DC
    169 #else
    170 		       /* XXX implementation detail knowledge (wchar_t encoding) */
    171 		       0xcf40
    172 #endif
    173 		);
    174 	fclose(fp);
    175 }
    176 
    177 ATF_TC(bad_big5_getwc);
    178 ATF_TC_HEAD(bad_big5_getwc, tc)
    179 {
    180 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
    181 }
    182 
    183 ATF_TC_BODY(bad_big5_getwc, tc)
    184 {
    185 	const char buf[] = { 0xcf, 0x20 };
    186 	struct ibuf ib = {
    187 		.buf = buf,
    188 		.buflen = sizeof(buf),
    189 	};
    190 	FILE *fp = funopen(&ib, readfn, NULL, NULL, NULL);
    191 
    192 	ATF_REQUIRE(fp != NULL);
    193 	setlocale(LC_CTYPE, "zh_TW.Big5");
    194 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
    195 	fclose(fp);
    196 }
    197 
    198 ATF_TP_ADD_TCS(tp)
    199 {
    200 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
    201 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
    202 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
    203 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
    204 	ATF_TP_ADD_TC(tp, good_big5_getwc);
    205 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
    206 
    207 	return atf_no_error();
    208 }
    209