1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
/*
Timelib
test_timelib.c
Copyright (C) Jakub Hrozek <jhrozek@redhat.com> 2009
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _XOPEN_SOURCE /* strptime */
#include <check.h>
#include <popt.h>
#include <time.h>
#include <errno.h>
#include <stdlib.h>
#include <stdbool.h>
#include "providers/ipa/ipa_timerules.h"
#include "util/util.h"
#include "tests/common.h"
#define CHECK_TIME_RULE_LEAK(ctx, tctx, str, now, result) do { \
check_leaks_push(ctx); \
ret = check_time_rule(ctx, tctx, str, now, result); \
check_leaks_pop(ctx); \
} while (0)
static TALLOC_CTX *ctx;
static void usage(poptContext pc, const char *error)
{
poptPrintUsage(pc, stderr, 0);
if (error) fprintf(stderr, "%s", error);
}
int str2time_t(const char *fmt, const char *str, time_t *out)
{
char *err;
struct tm stm;
memset(&stm, 0, sizeof(struct tm));
err = strptime(str, fmt, &stm);
if(!err || err[0] != '\0')
return EINVAL;
DEBUG(9, ("after strptime: %s\n", asctime(&stm)));
stm.tm_isdst = -1;
*out = mktime(&stm);
DEBUG(9, ("after mktime: %s\n", ctime(out)));
return (*out == -1) ? EINVAL : EOK;
}
/* Fixtures - open the time library before every test, close it afterwards */
void setup(void)
{
leak_check_setup();
ctx = talloc_new(NULL);
fail_if(ctx == NULL);
}
void teardown(void)
{
leak_check_teardown();
}
/* Test that timelib detects a time rule inside the absolute range */
START_TEST(test_timelib_absolute_in_range)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%F", "2000-1-1", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 199412161032.5 ~ 200512161032,5", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error");
talloc_free(tctx);
}
END_TEST
/* Test that timelib detects a time rule outside the absolute range */
START_TEST(test_timelib_absolute_out_of_range)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%F", "2007-1-1", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 199412161032.5 ~ 200512161032,5", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
talloc_free(tctx);
}
END_TEST
/* Test that absolute timeranges work OK with only minimal data supplied */
START_TEST(test_timelib_absolute_minimal)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%F", "2000-1-1", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 19941216 ~ 20051216", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error");
talloc_free(tctx);
}
END_TEST
/* Test a time value "right off the edge" of the time specifier */
START_TEST(test_timelib_absolute_one_off)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M-%S", "1994-12-16-10-32-29", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 19941216103230 ~ 19941216103231", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M-%S", "1994-12-16-10-32-32", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 19941216103230 ~ 19941216103231", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
talloc_free(tctx);
}
END_TEST
/* Test a time value "right on the edge" of the time specifier */
START_TEST(test_timelib_absolute_one_on)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M-%S", "1994-12-16-10-32-30", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 19941216103230 ~ 19941216103231", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M-%S", "1994-12-16-10-32-31", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "absolute 19941216103230 ~ 19941216103231", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_daily_in)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error");
/* test edges */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-09-30", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 0930 ~ 1830", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == true, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-18-30", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 0930 ~ 1830", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge2)");
fail_unless(result == true, "Range check error");
/* test wrap around */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-16-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 1500 ~ 0600", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == true, "Range check error1");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-15-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 1500 ~ 0600", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == true, "Range check error1");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-06-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 1500 ~ 0600", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == true, "Range check error1");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_daily_out)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-21-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
/* test one-off errors */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-09-29", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 0930 ~ 1830", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (one-off 1)");
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-18-31", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 0930 ~ 1830", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (one-off 2)");
fail_unless(result == false, "Range check error");
/* test wrap around */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-10-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 1500 ~ 0600", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-14-59", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 1500 ~ 0600", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == false, "Range check error1");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-03-30-06-01", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic daily 1500 ~ 0600", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (edge1)");
fail_unless(result == false, "Range check error2");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_weekly_in)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-02-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Mon-Fri 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error1");
/* test edges - monday */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-22-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Mon-Fri 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error2");
/* test edges - friday */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-26-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Mon-Fri 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error3");
/* test wrap around */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-11-03-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Fri-Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error2");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-11-06-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Fri-Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == true, "Range check error3");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_weekly_out)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-04-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Mon-Fri 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
/* test one-off error - monday */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-22-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Tue-Thu 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
/* test one-off error - friday */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-26-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Tue-Thu 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error");
/* test wrap around */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-11-04-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Fri-Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error2");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-11-05-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic weekly day Fri-Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule");
fail_unless(result == false, "Range check error3");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_monthly_in)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-07-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly week 1,2 day Mon,Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule1 (ret = %d: %s)", ret, strerror(ret));
fail_unless(result == true, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-05-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly day 1-5,10,15,20-25 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule2 (ret = %d: %s)", ret, strerror(ret));
fail_unless(result == true, "Range check error");
/* edges - week in */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-13-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly week 1,2 day Sat 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (week edge 1)");
fail_unless(result == true, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-29-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly week 5 day Mon,Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (week edge 2)");
fail_unless(result == true, "Range check error");
/* edges - day in */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-01-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (day edge 1)");
fail_unless(result == true, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-10-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (day edge 2)");
fail_unless(result == true, "Range check error");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_monthly_out)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-03-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly week 1,2 day Mon,Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (ret = %d)", ret);
fail_unless(result == false, "Range check error");
/* one-off error - week out */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-15-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly week 1 day Sun-Sat 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (week edge 1)");
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-28-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly week 5 day Mon,Tue 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (week edge 2)");
fail_unless(result == false, "Range check error");
/* one-off error - day out */
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-01-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly day 2-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (day edge 1)");
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-04-11-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic monthly day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (day edge 2)");
fail_unless(result == false, "Range check error");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_yearly_in)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-08-03-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly month 1,7-8 day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (ret = %d)", ret);
fail_unless(result == true, "Range check error1");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-01-01-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly month 1,7-8 day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (ret = %d)", ret);
fail_unless(result == true, "Range check error2");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-01-01-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly week 1 day 1-7 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (ret = %d)", ret);
fail_unless(result == true, "Range check error3");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-07-10-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly month 1,7-8 day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule (ret = %d)", ret);
fail_unless(result == true, "Range check error4");
talloc_free(tctx);
}
END_TEST
START_TEST(test_timelib_periodic_yearly_out)
{
time_t now;
int ret;
bool result;
static struct time_rules_ctx *tctx = NULL;
ret = init_time_rules_parser(ctx, &tctx);
fail_if(ret != EOK);
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-06-13-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly month 7-8 day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule1 (ret = %d)", ret);
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-09-13-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly month 7-8 day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule2 (ret = %d)", ret);
fail_unless(result == false, "Range check error");
fail_unless(str2time_t("%Y-%m-%d-%H-%M", "2009-01-11-12-00", &now) == 0, "Cannot parse time spec");
CHECK_TIME_RULE_LEAK(ctx, tctx, "periodic yearly month 1,7-8 day 1-10 0900 ~ 1800", now, &result);
fail_unless(ret == EOK, "Fail to check the time rule3 (ret = %d)", ret);
fail_unless(result == false, "Range check error");
talloc_free(tctx);
}
END_TEST
Suite *create_timelib_suite(void)
{
Suite *s = suite_create("timelib");
TCase *tc_timelib = tcase_create("Timelib Tests");
/* Add setup() and teardown() methods */
tcase_add_checked_fixture(tc_timelib, setup, teardown);
/* Do some testing */
tcase_add_test(tc_timelib, test_timelib_absolute_in_range);
tcase_add_test(tc_timelib, test_timelib_absolute_out_of_range);
tcase_add_test(tc_timelib, test_timelib_absolute_minimal);
tcase_add_test(tc_timelib, test_timelib_absolute_one_off);
tcase_add_test(tc_timelib, test_timelib_absolute_one_on);
tcase_add_test(tc_timelib, test_timelib_periodic_daily_in);
tcase_add_test(tc_timelib, test_timelib_periodic_daily_out);
tcase_add_test(tc_timelib, test_timelib_periodic_weekly_in);
tcase_add_test(tc_timelib, test_timelib_periodic_weekly_out);
tcase_add_test(tc_timelib, test_timelib_periodic_monthly_in);
tcase_add_test(tc_timelib, test_timelib_periodic_monthly_out);
tcase_add_test(tc_timelib, test_timelib_periodic_yearly_in);
tcase_add_test(tc_timelib, test_timelib_periodic_yearly_out);
/* Add all test cases to the test suite */
suite_add_tcase(s, tc_timelib);
return s;
}
int main(int argc, const char *argv[])
{
int ret;
poptContext pc;
int failure_count;
Suite *timelib_suite;
SRunner *sr;
int debug = 0;
struct poptOption long_options[] = {
POPT_AUTOHELP
{ "debug-level", 'd', POPT_ARG_INT, &debug, 0, "Set debug level", NULL },
POPT_TABLEEND
};
pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
if((ret = poptGetNextOpt(pc)) < -1) {
usage(pc, poptStrerror(ret));
return EXIT_FAILURE;
}
debug_level = debug;
tests_set_cwd();
timelib_suite = create_timelib_suite();
sr = srunner_create(timelib_suite);
/* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */
srunner_run_all(sr, CK_ENV);
failure_count = srunner_ntests_failed(sr);
srunner_free(sr);
return (failure_count==0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
|