Fix libnldbl_nonshared.a references to internal libm symbols (bug 23735).
[glibc.git] / timezone / tst-tzset.c
blob744c583f7ed25ab28763ecd019afef6541150d95
1 /* tzset tests with crafted time zone data.
2 Copyright (C) 2015-2018 Free Software Foundation, Inc.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #define _GNU_SOURCE 1
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/resource.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <support/check.h>
29 #define TIMEOUT 5
30 static int do_test (void);
31 #define TEST_FUNCTION do_test ()
32 #include "../test-skeleton.c"
34 /* Returns the name of a large TZ file. */
35 static char *
36 create_tz_file (off64_t size)
38 char *path;
39 int fd = create_temp_file ("tst-tzset-", &path);
40 if (fd < 0)
41 exit (1);
42 if (!support_descriptor_supports_holes (fd))
43 FAIL_UNSUPPORTED ("File %s does not support holes", path);
45 // Reopen for large-file support.
46 close (fd);
47 fd = open64 (path, O_WRONLY);
48 if (fd < 0)
50 printf ("open64 (%s) failed: %m\n", path);
51 exit (1);
54 static const char data[] = {
55 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
58 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
60 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00,
61 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00, 0x00,
62 0x54, 0x5a, 0x69, 0x66, 0x32, 0x00, 0x00, 0x00,
63 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
65 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
67 0x00, 0x00, 0x00, 0x04, 0xf8, 0x00, 0x00, 0x00,
68 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x58, 0x54, 0x47, 0x00, 0x00,
70 0x00, 0x0a, 0x58, 0x54, 0x47, 0x30, 0x0a
72 ssize_t ret = write (fd, data, sizeof (data));
73 if (ret < 0)
75 printf ("write failed: %m\n");
76 exit (1);
78 if ((size_t) ret != sizeof (data))
80 printf ("Short write\n");
81 exit (1);
83 if (lseek64 (fd, size, SEEK_CUR) < 0)
85 printf ("lseek failed: %m\n");
86 close (fd);
87 return NULL;
89 if (write (fd, "", 1) != 1)
91 printf ("Single-byte write failed\n");
92 close (fd);
93 return NULL;
95 if (close (fd) != 0)
97 printf ("close failed: %m\n");
98 exit (1);
100 return path;
103 static void
104 test_tz_file (off64_t size)
106 char *path = create_tz_file (size);
107 if (setenv ("TZ", path, 1) < 0)
109 printf ("setenv failed: %m\n");
110 exit (1);
112 tzset ();
113 free (path);
116 static int
117 do_test (void)
119 /* Limit the size of the process. Otherwise, some of the tests will
120 consume a lot of resources. */
122 struct rlimit limit;
123 if (getrlimit (RLIMIT_AS, &limit) != 0)
125 printf ("getrlimit (RLIMIT_AS) failed: %m\n");
126 return 1;
128 long target = 512 * 1024 * 1024;
129 if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > target)
131 limit.rlim_cur = 512 * 1024 * 1024;
132 if (setrlimit (RLIMIT_AS, &limit) != 0)
134 printf ("setrlimit (RLIMIT_AS) failed: %m\n");
135 return 1;
140 int errors = 0;
141 for (int i = 1; i <= 4; ++i)
143 char tz[16];
144 snprintf (tz, sizeof (tz), "XT%d", i);
145 if (setenv ("TZ", tz, 1) < 0)
147 printf ("setenv failed: %m\n");
148 return 1;
150 tzset ();
151 if (strcmp (tzname[0], tz) == 0)
153 printf ("Unexpected success for %s\n", tz);
154 ++errors;
158 /* Large TZ files. */
160 /* This will succeed on 64-bit architectures, and fail on 32-bit
161 architectures. It used to crash on 32-bit. */
162 test_tz_file (64 * 1024 * 1024);
164 /* This will fail on 64-bit and 32-bit architectures. It used to
165 cause a test timeout on 64-bit and crash on 32-bit if the TZ file
166 open succeeded for some reason (it does not use O_LARGEFILE in
167 regular builds). */
168 test_tz_file (4LL * 1024 * 1024 * 1024 - 6);
170 /* Large TZ variables. */
172 size_t length = 64 * 1024 * 1024;
173 char *value = malloc (length + 1);
174 if (value == NULL)
176 puts ("malloc failed: %m");
177 return 1;
179 value[length] = '\0';
181 memset (value, ' ', length);
182 value[0] = 'U';
183 value[1] = 'T';
184 value[2] = 'C';
185 if (setenv ("TZ", value, 1) < 0)
187 printf ("setenv failed: %m\n");
188 return 1;
190 tzset ();
192 memset (value, '0', length);
193 value[0] = '<';
194 value[length - 1] = '>';
195 if (setenv ("TZ", value, 1) < 0)
197 printf ("setenv failed: %m\n");
198 return 1;
200 tzset ();
203 return errors > 0;