1 /*	$NetBSD: parse_time-test.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <config.h>
37 
38 #include <krb5/roken.h>
39 #include <krb5/parse_time.h>
40 #include "test-mem.h"
41 #include "err.h"
42 
43 static struct testcase {
44     size_t size;
45     int    val;
46     char  *str;
47 } tests[] = {
48     { 8, 1,		"1 second" },
49     { 17, 61,		"1 minute 1 second" },
50     { 18, 62,		"1 minute 2 seconds" },
51     { 8, 60,		"1 minute" },
52     { 6, 3600,	 	"1 hour" },
53     { 15, 3601,	 	"1 hour 1 second" },
54     { 16, 3602,	 	"1 hour 2 seconds" }
55 };
56 
57 int
main(int argc,char ** argv)58 main(int argc, char **argv)
59 {
60     size_t sz;
61     size_t buf_sz;
62     int i, j;
63 
64     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); ++i) {
65 	char *buf;
66 
67 	sz = unparse_time(tests[i].val, NULL, 0);
68 	if  (sz != tests[i].size)
69 	    errx(1, "sz (%lu) != tests[%d].size (%lu)",
70 		 (unsigned long)sz, i, (unsigned long)tests[i].size);
71 
72 	for (buf_sz = 0; buf_sz < tests[i].size + 2; buf_sz++) {
73 
74 	    buf = rk_test_mem_alloc(RK_TM_OVERRUN, "overrun",
75 				    NULL, buf_sz);
76 	    sz = unparse_time(tests[i].val, buf, buf_sz);
77 	    if (sz != tests[i].size)
78 		errx(1, "sz (%lu) != tests[%d].size (%lu) with in size %lu",
79 		     (unsigned long)sz, i,
80 		     (unsigned long)tests[i].size,
81 		     (unsigned long)buf_sz);
82 	    if (buf_sz > 0 && memcmp(buf, tests[i].str, buf_sz - 1) != 0)
83 		errx(1, "test %i wrong result %s vs %s", i, buf, tests[i].str);
84 	    if (buf_sz > 0 && buf[buf_sz - 1] != '\0')
85 		errx(1, "test %i not zero terminated", i);
86 	    rk_test_mem_free("overrun");
87 
88 	    buf = rk_test_mem_alloc(RK_TM_UNDERRUN, "underrun",
89 				    NULL, tests[i].size);
90 	    sz = unparse_time(tests[i].val, buf, min(buf_sz, tests[i].size));
91 	    if (sz != tests[i].size)
92 		errx(1, "sz (%lu) != tests[%d].size (%lu) with insize %lu",
93 		     (unsigned long)sz, i,
94 		     (unsigned long)tests[i].size,
95 		     (unsigned long)buf_sz);
96 	    if (buf_sz > 0 && strncmp(buf, tests[i].str, min(buf_sz, tests[i].size) - 1) != 0)
97 		errx(1, "test %i wrong result %s vs %s", i, buf, tests[i].str);
98 	    if (buf_sz > 0 && buf[min(buf_sz, tests[i].size) - 1] != '\0')
99 		errx(1, "test %i not zero terminated", i);
100 	    rk_test_mem_free("underrun");
101 	}
102 
103 	buf = rk_test_mem_alloc(RK_TM_OVERRUN, "overrun",
104 				tests[i].str, tests[i].size + 1);
105 	j = parse_time(buf, "s");
106 	if (j != tests[i].val)
107 	    errx(1, "parse_time failed for test %d", i);
108 	rk_test_mem_free("overrun");
109 
110 	buf = rk_test_mem_alloc(RK_TM_UNDERRUN, "underrun",
111 				tests[i].str, tests[i].size + 1);
112 	j = parse_time(buf, "s");
113 	if (j != tests[i].val)
114 	    errx(1, "parse_time failed for test %d", i);
115 	rk_test_mem_free("underrun");
116 
117     }
118     return 0;
119 }
120