3
* Copyright (c) 2016, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
5
* Permission to use, copy, modify, and distribute this software for any
6
* purpose with or without fee is hereby granted, provided that the above
7
* copyright notice and this permission notice appear in all copies.
9
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
36
main(int argc, char *argv[])
42
char buf[64], testbuf[64];
45
* Test a lot of positive and negative numbers.
46
* We want the full range of time_t to work.
49
for (i = 0; i < 100000; i++) {
51
v = (int32_t)arc4random();
53
v = (int32_t)(random() + random());
56
strftime(buf, sizeof(buf), "%a, %d %b %Y %T GMT", tm);
57
khttp_epoch2str(v, testbuf, sizeof(testbuf));
58
if (strcmp(buf, testbuf))
59
errx(1, "khttp_epoch2str: "
60
"have %s, want %s", testbuf, buf);
65
v = 10000; /* whatever */
67
strftime(buf, sizeof(buf), "%a, %d %b %Y %T GMT", tm);
68
khttp_epoch2str(v, testbuf, 10);
69
if (strlen(testbuf) != 9)
70
errx(1, "khttp_epoch2str: bad string length");
71
if (strncmp(buf, testbuf, 9))
72
errx(1, "khttp_epoch2str: have %s, "
73
"want %s", testbuf, buf);
75
/* Now test for time_t > int32_t. */
78
strlcpy(buf, "Wed, 16 Nov 5138 09:46:40 GMT", sizeof(buf));
79
khttp_epoch2str(vv, testbuf, sizeof(testbuf));
80
if (strcmp(buf, testbuf))
81
errx(1, "khttp_epoch2str: "
82
"have %s, want %s", testbuf, buf);
84
/* Similarly, but for >4 digit years. */
87
strlcpy(buf, "Sun, 04 Dec 292277026596 15:30:07 GMT", sizeof(buf));
88
khttp_epoch2str(vv, testbuf, sizeof(testbuf));
89
if (strcmp(buf, testbuf))
90
errx(1, "khttp_epoch2str: "
91
"have %s, want %s", testbuf, buf);
93
/* And time_t < int32_t (also tests for negative year). */
96
strlcpy(buf, "Sun, 27 Jan -292277022657 08:29:52 GMT", sizeof(buf));
97
khttp_epoch2str(vv, testbuf, sizeof(testbuf));
98
if (strcmp(buf, testbuf))
99
errx(1, "khttp_epoch2str: "
100
"have %s, want %s", testbuf, buf);
102
/* Truncate to zero test. */
105
strftime(buf, sizeof(buf), "%a, %d %b %Y %T GMT", tm);
106
khttp_epoch2str(v, testbuf, 1);
107
if (strlen(testbuf) != 0)
108
errx(1, "khttp_epoch2str: bad string length");
110
/* No NULL or zero-length values. */
112
if (khttp_epoch2str(v, NULL, 1) != NULL)
113
errx(1, "khttp_epoch2str: should return NULL");
114
if (khttp_epoch2str(v, testbuf, 0) != NULL)
115
errx(1, "khttp_epoch2str: should return NULL");