ksgi
1/* $Id$ */
2/*
3* Copyright (c) 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_ERR20# include <err.h>21#endif22
23#include <inttypes.h>24#include <stdarg.h>25#include <stdint.h>26#include <stdlib.h>27#include <string.h>28#include <time.h>29#include <unistd.h>30
31#include <curl/curl.h>32
33#include "../kcgi.h"34#include "regress.h"35
36int
37main(int argc, char *argv[])38{
39size_t i;40struct tm test;41int64_t v, res;42
43/*44* Test a lot of positive and negative numbers.
45* We want the full range of time_t to work.
46*/
47
48for (i = 0; i < 100000; i++) {49#if HAVE_ARC4RANDOM50v = arc4random();51#else52v = random();53#endif54if (!KHTTP_EPOCH2TM(v, &test))55errx(1, "KHTTP_EPOCH2TM");56if (!khttp_datetime2epoch(&res,57test.tm_mday,58test.tm_mon + 1,59test.tm_year + 1900,60test.tm_hour,61test.tm_min,62test.tm_sec))63errx(1, "khttp_datetime2epoch: %" PRId64, v);64if (res != v)65errx(1, "date cross-check: have "66"%" PRId64 ", want %" PRId64, res, v);67}68
69for (i = 0; i < 100000; i++) {70#if HAVE_ARC4RANDOM71v = (int64_t)arc4random() * -1;72#else73v = (int64_t)random() * -1;74#endif75if (!KHTTP_EPOCH2TM(v, &test))76errx(1, "KHTTP_EPOCH2TM");77if (!khttp_datetime2epoch(&res,78test.tm_mday,79test.tm_mon + 1,80test.tm_year + 1900,81test.tm_hour,82test.tm_min,83test.tm_sec))84errx(1, "khttp_datetime2epoch: %" PRId64, v);85if (res != v)86errx(1, "date cross-check: have "87"%" PRId64 ", want %" PRId64, res, v);88}89
90/* Test specifically for -1 and 0. */91
92v = -1;93if (!KHTTP_EPOCH2TM(v, &test))94errx(1, "KHTTP_EPOCH2TM");95if (!khttp_datetime2epoch(&res,96test.tm_mday,97test.tm_mon + 1,98test.tm_year + 1900,99test.tm_hour,100test.tm_min,101test.tm_sec))102errx(1, "khttp_datetime2epoch: %" PRId64, v);103if (res != v)104errx(1, "date cross-check: have "105"%" PRId64 ", want %" PRId64, res, v);106
107v = 0;108if (!KHTTP_EPOCH2TM(v, &test))109errx(1, "KHTTP_EPOCH2TM");110if (!khttp_datetime2epoch(&res,111test.tm_mday,112test.tm_mon + 1,113test.tm_year + 1900,114test.tm_hour,115test.tm_min,116test.tm_sec))117errx(1, "khttp_datetime2epoch: %" PRId64, v);118if (res != v)119errx(1, "date cross-check: have "120"%" PRId64 ", want %" PRId64, res, v);121
122/* Leap year is not ok. */123
124if (khttp_datetime2epoch(&res,12529,1262,1272019,1280,1290,1300))131errx(1, "khttp_datetime2epoch should fail");132
133/* Leap year is ok. */134
135if (!khttp_datetime2epoch(&res,13629,1372,1382020,1390,1400,1410))142errx(1, "khttp_datetime2epoch");143
144/* Bad day of month. */145
146if (khttp_datetime2epoch(&res,14731,1484,1492020,1500,1510,1520))153errx(1, "khttp_datetime2epoch should fail");154
155/* Zeroes everywhere fails (month, day). */156
157if (khttp_datetime2epoch(&res,1580,1590,1600,1610,1620,1630))164errx(1, "khttp_datetime2epoch should fail");165
166/* Zero hour. */167
168if (!khttp_datetime2epoch(&res,1691,1701,1710,1720,1730,1740))175errx(1, "khttp_datetime2epoch");176
177return 0;178}
179