Path: blob/main/lib/libc/tests/locale/newlocale_test.c
39491 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2023 Yuri Pankov <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>2829#include <locale.h>3031#include <atf-c.h>3233struct {34int lpmask;35const char *lpname;36} lparts[] = {37{ LC_COLLATE_MASK, "LC_COLLATE" },38{ LC_CTYPE_MASK, "LC_CTYPE" },39{ LC_MONETARY_MASK, "LC_MONETARY" },40{ LC_NUMERIC_MASK, "LC_NUMERIC" },41{ LC_TIME_MASK, "LC_TIME" },42{ LC_MESSAGES_MASK, "LC_MESSAGES" },43};4445static void46check_lparts(const char *expected)47{48int i;4950for (i = 0; i < nitems(lparts); i++) {51const char *actual;5253actual = querylocale(lparts[i].lpmask, uselocale(NULL));54ATF_CHECK_STREQ_MSG(expected, actual, "wrong value for %s",55lparts[i].lpname);56}57}5859static void60do_locale_switch(const char *loc1, const char *loc2)61{62locale_t l1, l2;6364/* Create and use the first locale */65l1 = newlocale(LC_ALL_MASK, loc1, NULL);66ATF_REQUIRE(l1 != NULL);67ATF_REQUIRE(uselocale(l1) != NULL);68check_lparts(loc1);69/*70* Create and use second locale, creation deliberately done only after71* the first locale check as newlocale() call would previously clobber72* the first locale contents.73*/74l2 = newlocale(LC_ALL_MASK, loc2, NULL);75ATF_REQUIRE(l2 != NULL);76ATF_REQUIRE(uselocale(l2) != NULL);77check_lparts(loc2);78/* Switch back to first locale */79ATF_REQUIRE(uselocale(l1) != NULL);80check_lparts(loc1);8182freelocale(l1);83freelocale(l2);84}8586/*87* PR 255646, 269375: Check that newlocale()/uselocale() used to switch between88* C, POSIX, and C.UTF-8 locales (and only these) do not stomp on other locale89* contents (collate part specifically).90* The issue is cosmetic only as all three have empty collate parts, but we need91* to correctly report the one in use in any case.92*/9394ATF_TC_WITHOUT_HEAD(newlocale_c_posix_cu8_test);95ATF_TC_BODY(newlocale_c_posix_cu8_test, tc)96{97do_locale_switch("C", "POSIX");98do_locale_switch("C", "C.UTF-8");99do_locale_switch("POSIX", "C");100do_locale_switch("POSIX", "C.UTF-8");101do_locale_switch("C.UTF-8", "C");102do_locale_switch("C.UTF-8", "POSIX");103}104105ATF_TP_ADD_TCS(tp)106{107ATF_TP_ADD_TC(tp, newlocale_c_posix_cu8_test);108109return (atf_no_error());110}111112113