nis: Fix leak on realloc failure in nis_getnames [BZ #28150]
[glibc.git] / time / tst-getdate.c
blob3bb0e967070a9c1a666aef2b420443f7cfd52197
1 /* Test for getdate.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Andreas Jaeger <aj@suse.de>, 2000.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <array_length.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <support/check.h>
26 #include <support/temp_file.h>
27 #include <support/xunistd.h>
28 #include <time.h>
30 static const struct
32 const char *str;
33 const char *tz;
34 struct tm tm;
35 bool time64;
36 } tests [] =
38 {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
39 false },
40 {"21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
41 false },
42 {" 21:01:10 1999-1-31", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
43 false },
44 {"21:01:10 1999-1-31 ", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
45 false },
46 {" 21:01:10 1999-1-31 ", "Universal", {10, 1, 21, 31, 0, 99, 0, 0, 0},
47 false },
48 {"21:01:10 1999-2-28", "Universal", {10, 1, 21, 28, 1, 99, 0, 0, 0},
49 false },
50 {"16:30:46 2000-2-29", "Universal", {46, 30,16, 29, 1, 100, 0, 0, 0},
51 false },
52 {"01-08-2000 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 100, 0, 0, 0},
53 false },
55 /* 64 bit time_t tests. */
56 {"21:01:10 2038-1-31", "Universal", {10, 1, 21, 31, 0, 138, 0, 0, 0},
57 true },
58 {"22:01:10 2048-5-20", "Universal", {10, 1, 22, 20, 4, 148, 0, 0, 0},
59 true },
60 {"01-08-2038 05:06:07", "Europe/Berlin", {7, 6, 5, 1, 7, 138, 0, 0, 0},
61 true },
62 {"20-03-2050 21:30:08", "Europe/Berlin", {8, 30, 21, 20, 2, 150, 0, 0, 0},
63 true }
66 static const char *
67 report_date_error (void)
69 switch (getdate_err)
71 case 1:
72 return "The environment variable DATEMSK is not defined or null.";
73 case 2:
74 return "The template file denoted by the DATEMSK environment variable "
75 "cannot be opened.";
76 case 3:
77 return "Information about the template file cannot retrieved.";
78 case 4:
79 return "The template file is not a regular file.\n";
80 case 5:
81 return "An I/O error occurred while reading the template file.";
82 case 6:
83 return "Not enough memory available to execute the function.";
84 case 7:
85 return "The template file contains no matching template.";
86 case 8:
87 return "The input date is invalid, but would match a template "
88 "otherwise.";
89 default:
90 return "Unknown error code.";
94 static char *datemsk;
95 static const char datemskstr[] =
96 "%H:%M:%S %F\n"
97 "%d-%m-%Y %T\n";
99 static void
100 do_prepare (int argc, char **argv)
102 int fd = create_temp_file ("tst-chk1.", &datemsk);
103 xwrite (fd, datemskstr, sizeof (datemskstr) - 1);
105 setenv ("DATEMSK", datemsk, 1);
107 #define PREPARE do_prepare
109 static int
110 do_test (void)
112 struct tm *tm;
114 for (int i = 0; i < array_length (tests); ++i)
116 setenv ("TZ", tests[i].tz, 1);
118 tm = getdate (tests[i].str);
119 TEST_COMPARE (getdate_err, 0);
120 if (getdate_err != 0)
122 support_record_failure ();
123 printf ("%s\n", report_date_error ());
125 else
127 TEST_COMPARE (tests[i].tm.tm_mon, tm->tm_mon);
128 TEST_COMPARE (tests[i].tm.tm_year, tm->tm_year);
129 TEST_COMPARE (tests[i].tm.tm_mday, tm->tm_mday);
130 TEST_COMPARE (tests[i].tm.tm_hour, tm->tm_hour);
131 TEST_COMPARE (tests[i].tm.tm_min, tm->tm_min);
132 TEST_COMPARE (tests[i].tm.tm_sec, tm->tm_sec);
135 struct tm tms;
136 TEST_COMPARE (getdate_r (tests[i].str, &tms), 0);
137 if (getdate_err == 0)
139 TEST_COMPARE (tests[i].tm.tm_mon, tms.tm_mon);
140 TEST_COMPARE (tests[i].tm.tm_year, tms.tm_year);
141 TEST_COMPARE (tests[i].tm.tm_mday, tms.tm_mday);
142 TEST_COMPARE (tests[i].tm.tm_hour, tms.tm_hour);
143 TEST_COMPARE (tests[i].tm.tm_min, tms.tm_min);
144 TEST_COMPARE (tests[i].tm.tm_sec, tms.tm_sec);
148 return 0;
151 #include <support/test-driver.c>