1 /*- 2 * Copyright (c) 2002 Tim J. Robbins 3 * All rights reserved. 4 * 5 * Copyright (c) 2013 Ed Schouten <ed@FreeBSD.org> 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 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 /* 30 * Test program for c16rtomb() as specified by ISO/IEC 9899:2011. 31 */ 32 33 #include <sys/cdefs.h> 34 #include <errno.h> 35 #include <limits.h> 36 #include <locale.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <uchar.h> 40 41 #include <atf-c.h> 42 43 static void 44 require_lc_ctype(const char *locale_name) 45 { 46 char *lc_ctype_set; 47 48 lc_ctype_set = setlocale(LC_CTYPE, locale_name); 49 if (lc_ctype_set == NULL) 50 atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d", 51 locale_name, errno); 52 53 ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0); 54 } 55 56 static mbstate_t s; 57 static char buf[MB_LEN_MAX + 1]; 58 59 ATF_TC_WITHOUT_HEAD(c16rtomb_c_locale_test); 60 ATF_TC_BODY(c16rtomb_c_locale_test, tc) 61 { 62 63 require_lc_ctype("C"); 64 65 /* 66 * If the buffer argument is NULL, c16 is implicitly 0, 67 * c16rtomb() resets its internal state. 68 */ 69 ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1); 70 ATF_REQUIRE(c16rtomb(NULL, 0xdc00, NULL) == 1); 71 72 /* Null wide character. */ 73 memset(&s, 0, sizeof(s)); 74 memset(buf, 0xcc, sizeof(buf)); 75 ATF_REQUIRE(c16rtomb(buf, 0, &s) == 1); 76 ATF_REQUIRE((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); 77 78 /* Latin letter A, internal state. */ 79 ATF_REQUIRE(c16rtomb(NULL, L'\0', NULL) == 1); 80 ATF_REQUIRE(c16rtomb(NULL, L'A', NULL) == 1); 81 82 /* Latin letter A. */ 83 memset(&s, 0, sizeof(s)); 84 memset(buf, 0xcc, sizeof(buf)); 85 ATF_REQUIRE(c16rtomb(buf, L'A', &s) == 1); 86 ATF_REQUIRE((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); 87 88 /* Unicode character 'Pile of poo'. */ 89 memset(&s, 0, sizeof(s)); 90 memset(buf, 0xcc, sizeof(buf)); 91 ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0); 92 ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1); 93 ATF_REQUIRE(errno == EILSEQ); 94 ATF_REQUIRE((unsigned char)buf[0] == 0xcc); 95 } 96 97 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_1_test); 98 ATF_TC_BODY(c16rtomb_iso_8859_1_test, tc) 99 { 100 101 require_lc_ctype("en_US.ISO8859-1"); 102 103 /* Unicode character 'Euro sign'. */ 104 memset(&s, 0, sizeof(s)); 105 memset(buf, 0xcc, sizeof(buf)); 106 ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == (size_t)-1); 107 ATF_REQUIRE(errno == EILSEQ); 108 ATF_REQUIRE((unsigned char)buf[0] == 0xcc); 109 } 110 111 ATF_TC_WITHOUT_HEAD(c16rtomb_iso_8859_15_test); 112 ATF_TC_BODY(c16rtomb_iso_8859_15_test, tc) 113 { 114 115 require_lc_ctype("en_US.ISO8859-15"); 116 117 /* Unicode character 'Euro sign'. */ 118 memset(&s, 0, sizeof(s)); 119 memset(buf, 0xcc, sizeof(buf)); 120 ATF_REQUIRE(c16rtomb(buf, 0x20ac, &s) == 1); 121 ATF_REQUIRE((unsigned char)buf[0] == 0xa4 && (unsigned char)buf[1] == 0xcc); 122 } 123 124 ATF_TC_WITHOUT_HEAD(c16rtomb_utf_8_test); 125 ATF_TC_BODY(c16rtomb_utf_8_test, tc) 126 { 127 128 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false)) 129 atf_tc_skip("https://bugs.freebsd.org/265871"); 130 131 require_lc_ctype("en_US.UTF-8"); 132 133 /* Unicode character 'Pile of poo'. */ 134 memset(&s, 0, sizeof(s)); 135 memset(buf, 0xcc, sizeof(buf)); 136 ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0); 137 ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == 4); 138 ATF_REQUIRE((unsigned char)buf[0] == 0xf0 && (unsigned char)buf[1] == 0x9f && 139 (unsigned char)buf[2] == 0x92 && (unsigned char)buf[3] == 0xa9 && 140 (unsigned char)buf[4] == 0xcc); 141 142 /* Invalid code; 'Pile of poo' without the trail surrogate. */ 143 memset(&s, 0, sizeof(s)); 144 memset(buf, 0xcc, sizeof(buf)); 145 ATF_REQUIRE(c16rtomb(buf, 0xd83d, &s) == 0); 146 ATF_REQUIRE(c16rtomb(buf, L'A', &s) == (size_t)-1); 147 ATF_REQUIRE(errno == EILSEQ); 148 ATF_REQUIRE((unsigned char)buf[0] == 0xcc); 149 150 /* Invalid code; 'Pile of poo' without the lead surrogate. */ 151 memset(&s, 0, sizeof(s)); 152 memset(buf, 0xcc, sizeof(buf)); 153 ATF_REQUIRE(c16rtomb(buf, 0xdca9, &s) == (size_t)-1); 154 ATF_REQUIRE(errno == EILSEQ); 155 ATF_REQUIRE((unsigned char)buf[0] == 0xcc); 156 } 157 158 ATF_TP_ADD_TCS(tp) 159 { 160 161 ATF_TP_ADD_TC(tp, c16rtomb_c_locale_test); 162 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_1_test); 163 ATF_TP_ADD_TC(tp, c16rtomb_iso_8859_15_test); 164 ATF_TP_ADD_TC(tp, c16rtomb_utf_8_test); 165 166 return (atf_no_error()); 167 } 168