xref: /freebsd/crypto/krb5/src/lib/krb5/krb/t_valid_times.c (revision f1c4c3daccbaf3820f0e2224de53df12fc952fcc)
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/t_valid_times.c - test program for krb5int_validate_times() */
3 /*
4  * Copyright (C) 2017 by the Massachusetts Institute of Technology.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in
16  *   the documentation and/or other materials provided with the
17  *   distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "k5-int.h"
34 #include "int-proto.h"
35 
36 #define BOUNDARY (uint32_t)INT32_MIN
37 
38 int
main(void)39 main(void)
40 {
41     krb5_error_code ret;
42     krb5_context context;
43     krb5_ticket_times times = { 0, 0, 0, 0 };
44 
45     ret = krb5_init_context(&context);
46     assert(!ret);
47 
48     /* Current time is within authtime and end time. */
49     ret = krb5_set_debugging_time(context, 1000, 0);
50     times.authtime = 500;
51     times.endtime = 1500;
52     ret = krb5int_validate_times(context, &times);
53     assert(!ret);
54 
55     /* Current time is before starttime, but within clock skew. */
56     times.starttime = 1100;
57     ret = krb5int_validate_times(context, &times);
58     assert(!ret);
59 
60     /* Current time is before starttime by more than clock skew. */
61     times.starttime = 1400;
62     ret = krb5int_validate_times(context, &times);
63     assert(ret == KRB5KRB_AP_ERR_TKT_NYV);
64 
65     /* Current time is after end time, but within clock skew. */
66     times.starttime = 500;
67     times.endtime = 800;
68     ret = krb5int_validate_times(context, &times);
69     assert(!ret);
70 
71     /* Current time is after end time by more than clock skew. */
72     times.endtime = 600;
73     ret = krb5int_validate_times(context, &times);
74     assert(ret == KRB5KRB_AP_ERR_TKT_EXPIRED);
75 
76     /* Current time is within starttime and endtime; current time and
77      * endtime are across y2038 boundary. */
78     ret = krb5_set_debugging_time(context, BOUNDARY - 100, 0);
79     assert(!ret);
80     times.starttime = BOUNDARY - 200;
81     times.endtime = BOUNDARY + 500;
82     ret = krb5int_validate_times(context, &times);
83     assert(!ret);
84 
85     /* Current time is before starttime, but by less than clock skew. */
86     times.starttime = BOUNDARY + 100;
87     ret = krb5int_validate_times(context, &times);
88     assert(!ret);
89 
90     /* Current time is before starttime by more than clock skew. */
91     times.starttime = BOUNDARY + 250;
92     ret = krb5int_validate_times(context, &times);
93     assert(ret == KRB5KRB_AP_ERR_TKT_NYV);
94 
95     /* Current time is after endtime, but by less than clock skew. */
96     ret = krb5_set_debugging_time(context, BOUNDARY + 100, 0);
97     assert(!ret);
98     times.starttime = BOUNDARY - 1000;
99     times.endtime = BOUNDARY - 100;
100     ret = krb5int_validate_times(context, &times);
101     assert(!ret);
102 
103     /* Current time is after endtime by more than clock skew. */
104     times.endtime = BOUNDARY - 300;
105     ret = krb5int_validate_times(context, &times);
106     assert(ret == KRB5KRB_AP_ERR_TKT_EXPIRED);
107 
108     krb5_free_context(context);
109 
110     return 0;
111 }
112