1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/t_deltat.c */
3 /*
4 * Copyright 1999 by the Massachusetts Institute of Technology.
5 * All Rights Reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26
27 #include "k5-int.h"
28
29 int
main(void)30 main (void)
31 {
32 struct {
33 char *string;
34 krb5_deltat expected;
35 int is_error;
36 #define GOOD(STR,VAL) { STR, VAL, 0 }
37 #define BAD(STR) { STR, 0, 1 }
38 #define DAY (24 * 3600)
39 #define HOUR 3600
40 #ifdef MIN
41 #undef MIN
42 #endif
43 #define MIN 60
44 } values[] = {
45 /* d-h-m-s patterns */
46 GOOD ("3d", 3*DAY),
47 GOOD ("3h", 3*HOUR),
48 GOOD ("3m", 3*MIN),
49 GOOD ("3s", 3),
50 BAD ("3dd"),
51 GOOD ("3d4m 42s", 3 * DAY + 4 * MIN + 42),
52 GOOD ("3d-1h", 3 * DAY - 1 * HOUR),
53 GOOD ("3d -1h", 3 * DAY - HOUR),
54 GOOD ("3d4h5m6s", 3 * DAY + 4 * HOUR + 5 * MIN + 6),
55 BAD ("3d4m5h"),
56 GOOD ("12345s", 12345),
57 GOOD ("1m 12345s", MIN + 12345),
58 GOOD ("1m12345s", MIN + 12345),
59 GOOD ("3d 0m", 3 * DAY),
60 GOOD ("3d 0m ", 3 * DAY),
61 GOOD ("3d \n\t 0m ", 3 * DAY),
62 /* colon patterns */
63 GOOD ("42-13:42:47", 42 * DAY + 13 * HOUR + 42 * MIN + 47),
64 BAD ("3: 4"),
65 BAD ("13:0003"),
66 GOOD ("12:34", 12 * HOUR + 34 * MIN),
67 GOOD ("1:02:03", 1 * HOUR + 2 * MIN + 3),
68 BAD ("3:-4"),
69 /* XX We might want to require exactly two digits after a colon? */
70 GOOD ("3:4", 3 * HOUR + 4 * MIN),
71 /* misc */
72 GOOD ("42", 42),
73 BAD ("1-2"),
74 /* Test overflow limitations */
75 GOOD ("2147483647s", 2147483647),
76 BAD ("2147483648s"),
77 GOOD ("24855d", 24855 * DAY),
78 BAD ("24856d"),
79 BAD ("24855d 100000000h"),
80 GOOD ("24855d 3h", 24855 * DAY + 3 * HOUR),
81 BAD ("24855d 4h"),
82 GOOD ("24855d 11647s", 24855 * DAY + 11647),
83 BAD ("24855d 11648s"),
84 GOOD ("24855d 194m 7s", 24855 * DAY + 194 * MIN + 7),
85 BAD ("24855d 194m 8s"),
86 BAD ("24855d 195m"),
87 BAD ("24855d 19500000000m"),
88 GOOD ("24855d 3h 14m 7s", 24855 * DAY + 3 * HOUR + 14 * MIN + 7),
89 BAD ("24855d 3h 14m 8s"),
90 GOOD ("596523h", 596523 * HOUR),
91 BAD ("596524h"),
92 GOOD ("596523h 847s", 596523 * HOUR + 847),
93 BAD ("596523h 848s"),
94 GOOD ("596523h 14m 7s", 596523 * HOUR + 14 * MIN + 7),
95 BAD ("596523h 14m 8s"),
96 GOOD ("35791394m", 35791394 * MIN),
97 GOOD ("35791394m7s", 35791394 * MIN + 7),
98 BAD ("35791394m8s"),
99 /* Test underflow */
100 GOOD ("-2147483647s", -2147483647),
101 /* This should be valid, but isn't */
102 /*BAD ("-2147483648s"),*/
103 GOOD ("-24855d", -24855 * DAY),
104 BAD ("-24856d"),
105 BAD ("-24855d -100000000h"),
106 GOOD ("-24855d -3h", -24855 * DAY - 3 * HOUR),
107 BAD ("-24855d -4h"),
108 GOOD ("-24855d -11647s", -24855 * DAY - 11647),
109 BAD ("-24855d -11649s"),
110 GOOD ("-24855d -194m -7s", -24855 * DAY - 194 * MIN - 7),
111 BAD ("-24855d -194m -9s"),
112 BAD ("-24855d -195m"),
113 BAD ("-24855d -19500000000m"),
114 GOOD ("-24855d -3h -14m -7s", -24855 * DAY - 3 * HOUR - 14 * MIN - 7),
115 BAD ("-24855d -3h -14m -9s"),
116 GOOD ("-596523h", -596523 * HOUR),
117 BAD ("-596524h"),
118 GOOD ("-596523h -847s", -596523 * HOUR - 847),
119 GOOD ("-596523h -848s", -596523 * HOUR - 848),
120 BAD ("-596523h -849s"),
121 GOOD ("-596523h -14m -8s", -596523 * HOUR - 14 * MIN - 8),
122 BAD ("-596523h -14m -9s"),
123 GOOD ("-35791394m", -35791394 * MIN),
124 GOOD ("-35791394m7s", -35791394 * MIN + 7),
125 BAD ("-35791394m-9s"),
126
127 };
128 int fail = 0;
129 size_t i;
130
131 for (i = 0; i < sizeof(values)/sizeof(values[0]); i++) {
132 krb5_deltat result;
133 krb5_error_code code;
134
135 code = krb5_string_to_deltat (values[i].string, &result);
136 if (code && !values[i].is_error) {
137 fprintf (stderr, "unexpected error for `%s'\n", values[i].string);
138 fail++;
139 } else if (!code && values[i].is_error) {
140 fprintf (stderr, "expected but didn't get error for `%s'\n",
141 values[i].string);
142 fail++;
143 } else if (code && values[i].is_error) {
144 /* do nothing */
145 } else if (result != values[i].expected) {
146 fprintf (stderr, "got %ld instead of expected %ld for `%s'\n",
147 (long) result, (long) values[i].expected,
148 values[i].string);
149 fail++;
150 }
151 }
152 if (fail == 0)
153 printf ("Passed all %d tests.\n", (int)i);
154 else
155 printf ("Failed %d of %d tests.\n", fail, (int)i);
156 return fail;
157 }
158