ksgi

Форк
0
/
test-epoch2str.c 
118 строк · 3.2 Кб
1
/*	$Id$ */
2
/*
3
 * Copyright (c) 2016, 2020 Kristaps Dzonsons <kristaps@bsd.lv>
4
 *
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.
8
 *
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.
16
 */
17
#include "../config.h"
18

19
#if HAVE_ERR
20
# include <err.h>
21
#endif
22

23
#include <stdarg.h>
24
#include <stdint.h>
25
#include <stdlib.h>
26
#include <string.h>
27
#include <time.h>
28
#include <unistd.h>
29

30
#include <curl/curl.h>
31

32
#include "../kcgi.h"
33
#include "regress.h"
34

35
int
36
main(int argc, char *argv[])
37
{
38
	size_t	 	 i;
39
	time_t	 	 v;
40
	int64_t		 vv;
41
	struct tm	*tm;
42
	char	 	 buf[64], testbuf[64];
43

44
	/* 
45
	 * Test a lot of positive and negative numbers.
46
	 * We want the full range of time_t to work.
47
	 */
48

49
	for (i = 0; i < 100000; i++) {
50
#if HAVE_ARC4RANDOM
51
		v = (int32_t)arc4random();
52
#else
53
		v = (int32_t)(random() + random());
54
#endif
55
		tm = gmtime(&v);
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);
61
	}
62

63
	/* Truncate test. */
64

65
	v = 10000; /* whatever */
66
	tm = gmtime(&v);
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);
74

75
	/* Now test for time_t > int32_t. */
76

77
	vv = 100000000000;
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);
83

84
	/* Similarly, but for >4 digit years. */
85

86
	vv = INT64_MAX;
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);
92

93
	/* And time_t < int32_t (also tests for negative year). */
94

95
	vv = INT64_MIN;
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);
101

102
	/* Truncate to zero test. */
103

104
	tm = gmtime(&v);
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");
109

110
	/* No NULL or zero-length values. */
111

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");
116

117
	return 0;
118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.