Home | History | Annotate | Line # | Download | only in locale
t_io.c revision 1.2.4.2
      1 /* $NetBSD: t_io.c,v 1.2.4.2 2013/06/23 06:28:56 tls 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.2.4.2 2013/06/23 06:28:56 tls 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[] = { 0xcf10, 0 };
     57 	setlocale(LC_CTYPE, "zh_TW.Big5");
     58 	atf_tc_expect_fail("PR lib/47660");
     59 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), -1);
     60 }
     61 
     62 ATF_TC(bad_big5_swprintf);
     63 ATF_TC_HEAD(bad_big5_swprintf, tc)
     64 {
     65 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar swprintf");
     66 }
     67 
     68 ATF_TC_BODY(bad_big5_swprintf, tc)
     69 {
     70 	wchar_t ibuf[] = { 0xcf10, 0 };
     71 	wchar_t obuf[20];
     72 	setlocale(LC_CTYPE, "zh_TW.Big5");
     73 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), -1);
     74 }
     75 
     76 ATF_TC(good_big5_wprintf);
     77 ATF_TC_HEAD(good_big5_wprintf, tc)
     78 {
     79 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar wprintf");
     80 }
     81 
     82 ATF_TC_BODY(good_big5_wprintf, tc)
     83 {
     84 	wchar_t ibuf[] = { 0xcf40, 0 };
     85 	setlocale(LC_CTYPE, "zh_TW.Big5");
     86 	// WTF? swprintf() fails, wprintf succeeds?
     87 	ATF_REQUIRE_EQ(wprintf(L"%ls\n", ibuf), 2);
     88 }
     89 
     90 ATF_TC(good_big5_swprintf);
     91 ATF_TC_HEAD(good_big5_swprintf, tc)
     92 {
     93 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar swprintf");
     94 }
     95 
     96 ATF_TC_BODY(good_big5_swprintf, tc)
     97 {
     98 	wchar_t ibuf[] = { 0xcf40, 0 };
     99 	wchar_t obuf[20];
    100 	setlocale(LC_CTYPE, "zh_TW.Big5");
    101 	ATF_REQUIRE_EQ(swprintf(obuf, sizeof(obuf), L"%ls\n", ibuf), 2);
    102 }
    103 
    104 static int readfn(void *p, char *buf, int len) {
    105 	memcpy(buf, p, MIN(len, 2));
    106 	return 2;
    107 }
    108 
    109 ATF_TC(good_big5_getwc);
    110 ATF_TC_HEAD(good_big5_getwc, tc)
    111 {
    112 	atf_tc_set_md_var(tc, "descr", "Test good big5 wchar getwc");
    113 }
    114 
    115 ATF_TC_BODY(good_big5_getwc, tc)
    116 {
    117 	char ibuf[] = { 0xcf, 0x40 };
    118 	FILE *fp = funopen(ibuf, readfn, NULL, NULL, NULL);
    119 
    120 	ATF_REQUIRE(fp != NULL);
    121 	setlocale(LC_CTYPE, "zh_TW.Big5");
    122 	ATF_REQUIRE_EQ(getwc(fp), 0xcf40);
    123 	fclose(fp);
    124 }
    125 
    126 ATF_TC(bad_big5_getwc);
    127 ATF_TC_HEAD(bad_big5_getwc, tc)
    128 {
    129 	atf_tc_set_md_var(tc, "descr", "Test bad big5 wchar getwc");
    130 }
    131 
    132 ATF_TC_BODY(bad_big5_getwc, tc)
    133 {
    134 	char ibuf[] = { 0xcf, 0x20 };
    135 	FILE *fp = funopen(ibuf, readfn, NULL, NULL, NULL);
    136 
    137 	ATF_REQUIRE(fp != NULL);
    138 	setlocale(LC_CTYPE, "zh_TW.Big5");
    139 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
    140 	fclose(fp);
    141 }
    142 
    143 ATF_TC(bad_eucJP_getwc);
    144 ATF_TC_HEAD(bad_eucJP_getwc, tc)
    145 {
    146 	atf_tc_set_md_var(tc, "descr", "Test bad eucJP wchar getwc");
    147 }
    148 
    149 ATF_TC_BODY(bad_eucJP_getwc, tc)
    150 {
    151 	char ibuf[] = { 0xcf, 0x20 };
    152 	FILE *fp = funopen(ibuf, readfn, NULL, NULL, NULL);
    153 
    154 	ATF_REQUIRE(fp != NULL);
    155 	setlocale(LC_CTYPE, "ja_JP.eucJP");
    156 	// WTF? Not even returning what it read?
    157 	ATF_CHECK_EQ(getwc(fp), 0xcf20);
    158 	atf_tc_expect_fail("PR lib/47660");
    159 	ATF_REQUIRE_EQ(getwc(fp), WEOF);
    160 	fclose(fp);
    161 }
    162 
    163 ATF_TP_ADD_TCS(tp)
    164 {
    165 	ATF_TP_ADD_TC(tp, bad_big5_wprintf);
    166 	ATF_TP_ADD_TC(tp, bad_big5_swprintf);
    167 	ATF_TP_ADD_TC(tp, good_big5_wprintf);
    168 	ATF_TP_ADD_TC(tp, good_big5_swprintf);
    169 	ATF_TP_ADD_TC(tp, good_big5_getwc);
    170 	ATF_TP_ADD_TC(tp, bad_big5_getwc);
    171 	ATF_TP_ADD_TC(tp, bad_eucJP_getwc);
    172 
    173 	return atf_no_error();
    174 }
    175