1 /* $NetBSD: t_parsedate.c,v 1.7 2013/01/19 15:21:43 apb Exp $ */ 2 /*- 3 * Copyright (c) 2010 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 27 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __RCSID("$NetBSD: t_parsedate.c,v 1.7 2013/01/19 15:21:43 apb Exp $"); 33 34 #include <atf-c.h> 35 #include <errno.h> 36 #include <stdlib.h> 37 #include <time.h> 38 #include <util.h> 39 40 ATF_TC(dates); 41 42 ATF_TC_HEAD(dates, tc) 43 { 44 atf_tc_set_md_var(tc, "descr", "Test unambiguous dates" 45 " (PR lib/44255)"); 46 } 47 48 ATF_TC_BODY(dates, tc) 49 { 50 51 ATF_CHECK(parsedate("69-09-10", NULL, NULL) != -1); 52 ATF_CHECK(parsedate("2006-11-17", NULL, NULL) != -1); 53 ATF_CHECK(parsedate("10/1/2000", NULL, NULL) != -1); 54 ATF_CHECK(parsedate("20 Jun 1994", NULL, NULL) != -1); 55 ATF_CHECK(parsedate("23jun2001", NULL, NULL) != -1); 56 ATF_CHECK(parsedate("1-sep-06", NULL, NULL) != -1); 57 ATF_CHECK(parsedate("1/11", NULL, NULL) != -1); 58 ATF_CHECK(parsedate("1500-01-02", NULL, NULL) != -1); 59 ATF_CHECK(parsedate("9999-12-21", NULL, NULL) != -1); 60 } 61 62 ATF_TC(times); 63 64 ATF_TC_HEAD(times, tc) 65 { 66 atf_tc_set_md_var(tc, "descr", "Test times" 67 " (PR lib/44255)"); 68 } 69 70 ATF_TC_BODY(times, tc) 71 { 72 73 ATF_CHECK(parsedate("10:01", NULL, NULL) != -1); 74 ATF_CHECK(parsedate("10:12pm", NULL, NULL) != -1); 75 ATF_CHECK(parsedate("12:11:01.000012", NULL, NULL) != -1); 76 ATF_CHECK(parsedate("12:21-0500", NULL, NULL) != -1); 77 } 78 79 ATF_TC(relative); 80 81 ATF_TC_HEAD(relative, tc) 82 { 83 atf_tc_set_md_var(tc, "descr", "Test relative items" 84 " (PR lib/44255)"); 85 } 86 87 ATF_TC_BODY(relative, tc) 88 { 89 90 ATF_CHECK(parsedate("-1 month", NULL, NULL) != -1); 91 ATF_CHECK(parsedate("last friday", NULL, NULL) != -1); 92 ATF_CHECK(parsedate("one week ago", NULL, NULL) != -1); 93 ATF_CHECK(parsedate("this thursday", NULL, NULL) != -1); 94 ATF_CHECK(parsedate("next sunday", NULL, NULL) != -1); 95 ATF_CHECK(parsedate("+2 years", NULL, NULL) != -1); 96 } 97 98 ATF_TC(atsecs); 99 100 ATF_TC_HEAD(atsecs, tc) 101 { 102 atf_tc_set_md_var(tc, "descr", "Test seconds past the epoch"); 103 } 104 105 ATF_TC_BODY(atsecs, tc) 106 { 107 int tzoff; 108 109 /* "@0" -> (time_t)0, regardless of timezone */ 110 ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0); 111 putenv(__UNCONST("TZ=Europe/Berlin")); 112 tzset(); 113 ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0); 114 putenv(__UNCONST("TZ=America/New_York")); 115 tzset(); 116 ATF_CHECK(parsedate("@0", NULL, NULL) == (time_t)0); 117 tzoff = 0; 118 ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0); 119 tzoff = 3600; 120 ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0); 121 tzoff = -3600; 122 ATF_CHECK(parsedate("@0", NULL, &tzoff) == (time_t)0); 123 124 /* -1 or other negative numbers are not errors */ 125 errno = 0; 126 ATF_CHECK(parsedate("@-1", NULL, &tzoff) == (time_t)-1 && errno == 0); 127 ATF_CHECK(parsedate("@-2", NULL, &tzoff) == (time_t)-2 && errno == 0); 128 129 /* junk is an error */ 130 errno = 0; 131 ATF_CHECK(parsedate("@junk", NULL, NULL) == (time_t)-1 && errno != 0); 132 } 133 134 ATF_TP_ADD_TCS(tp) 135 { 136 ATF_TP_ADD_TC(tp, dates); 137 ATF_TP_ADD_TC(tp, times); 138 ATF_TP_ADD_TC(tp, relative); 139 ATF_TP_ADD_TC(tp, atsecs); 140 141 return atf_no_error(); 142 } 143