summary refs log tree commit diff
path: root/localedata/tst-setlocale3.c
blob: 6f1ddbd038e1293a208e55f7d42afd630a3d540c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Regression test for setlocale invalid environment variable handling.
   Copyright (C) 2014-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* The result of setlocale may be overwritten by subsequent calls, so
   this wrapper makes a copy.  */
static char *
setlocale_copy (int category, const char *locale)
{
  const char *result = setlocale (category, locale);
  if (result == NULL)
    return NULL;
  return strdup (result);
}

static char *de_locale;

static void
setlocale_fail (const char *envstring)
{
  setenv ("LC_CTYPE", envstring, 1);
  if (setlocale (LC_CTYPE, "") != NULL)
    {
      printf ("unexpected setlocale success for \"%s\" locale\n", envstring);
      exit (1);
    }
  const char *newloc = setlocale (LC_CTYPE, NULL);
  if (strcmp (newloc, de_locale) != 0)
    {
      printf ("failed setlocale call \"%s\" changed locale to \"%s\"\n",
	      envstring, newloc);
      exit (1);
    }
}

static void
setlocale_success (const char *envstring)
{
  setenv ("LC_CTYPE", envstring, 1);
  char *newloc = setlocale_copy (LC_CTYPE, "");
  if (newloc == NULL)
    {
      printf ("setlocale for \"%s\": %m\n", envstring);
      exit (1);
    }
  if (strcmp (newloc, de_locale) == 0)
    {
      printf ("setlocale with LC_CTYPE=\"%s\" left locale at \"%s\"\n",
	      envstring, de_locale);
      exit (1);
    }
  if (setlocale (LC_CTYPE, de_locale) == NULL)
    {
      printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
	      de_locale, envstring);
      exit (1);
    }
  char *newloc2 = setlocale_copy (LC_CTYPE, newloc);
  if (newloc2 == NULL)
    {
      printf ("restoring locale \"%s\" following \"%s\": %m\n",
	      newloc, envstring);
      exit (1);
    }
  if (strcmp (newloc, newloc2) != 0)
    {
      printf ("representation of locale \"%s\" changed from \"%s\" to \"%s\"",
	      envstring, newloc, newloc2);
      exit (1);
    }
  free (newloc);
  free (newloc2);

  if (setlocale (LC_CTYPE, de_locale) == NULL)
    {
      printf ("restoring locale \"%s\" with LC_CTYPE=\"%s\": %m\n",
	      de_locale, envstring);
      exit (1);
    }
}

/* Checks that a known-good locale still works if LC_ALL contains a
   value which should be ignored.  */
static void
setlocale_ignore (const char *to_ignore)
{
  const char *fr_locale = "fr_FR.UTF-8";
  setenv ("LC_CTYPE", fr_locale, 1);
  char *expected_locale = setlocale_copy (LC_CTYPE, "");
  if (expected_locale == NULL)
    {
      printf ("setlocale with LC_CTYPE=\"%s\" failed: %m\n", fr_locale);
      exit (1);
    }
  if (setlocale (LC_CTYPE, de_locale) == NULL)
    {
      printf ("failed to restore locale: %m\n");
      exit (1);
    }
  unsetenv ("LC_CTYPE");

  setenv ("LC_ALL", to_ignore, 1);
  setenv ("LC_CTYPE", fr_locale, 1);
  const char *actual_locale = setlocale (LC_CTYPE, "");
  if (actual_locale == NULL)
    {
      printf ("setlocale with LC_ALL, LC_CTYPE=\"%s\" failed: %m\n",
	      fr_locale);
      exit (1);
    }
  if (strcmp (actual_locale, expected_locale) != 0)
    {
      printf ("setlocale under LC_ALL failed: got \"%s\", expected \"%s\"\n",
	      actual_locale, expected_locale);
      exit (1);
    }
  unsetenv ("LC_CTYPE");
  setlocale_success (fr_locale);
  unsetenv ("LC_ALL");
  free (expected_locale);
}

static int
do_test (void)
{
  /* The glibc test harness sets this environment variable
     uncondionally.  */
  unsetenv ("LC_ALL");

  de_locale = setlocale_copy (LC_CTYPE, "de_DE.UTF-8");
  if (de_locale == NULL)
    {
      printf ("setlocale (LC_CTYPE, \"de_DE.UTF-8\"): %m\n");
      return 1;
    }
  setlocale_success ("C");
  setlocale_success ("en_US.UTF-8");
  setlocale_success ("/en_US.UTF-8");
  setlocale_success ("//en_US.UTF-8");
  setlocale_ignore ("");

  setlocale_fail ("does-not-exist");
  setlocale_fail ("/");
  setlocale_fail ("/../localedata/en_US.UTF-8");
  setlocale_fail ("en_US.UTF-8/");
  setlocale_fail ("en_US.UTF-8/..");
  setlocale_fail ("en_US.UTF-8/../en_US.UTF-8");
  setlocale_fail ("../localedata/en_US.UTF-8");
  {
    size_t large_length = 1024;
    char *large_name = malloc (large_length + 1);
    if (large_name == NULL)
      {
	puts ("malloc failure");
	return 1;
      }
    memset (large_name, '/', large_length);
    const char *suffix = "en_US.UTF-8";
    strcpy (large_name + large_length - strlen (suffix), suffix);
    setlocale_fail (large_name);
    free (large_name);
  }
  {
    size_t huge_length = 64 * 1024 * 1024;
    char *huge_name = malloc (huge_length + 1);
    if (huge_name == NULL)
      {
	puts ("malloc failure");
	return 1;
      }
    memset (huge_name, 'X', huge_length);
    huge_name[huge_length] = '\0';
    /* Construct a composite locale specification. */
    const char *prefix = "LC_CTYPE=de_DE.UTF-8;LC_TIME=";
    memcpy (huge_name, prefix, strlen (prefix));
    setlocale_fail (huge_name);
    free (huge_name);
  }

  return 0;
}

#define TEST_FUNCTION do_test ()
#include "../test-skeleton.c"