xref: /openssl/test/time_test.c (revision 039119a0)
1 /*
2  * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include "testutil.h"
11 #include "internal/time.h"
12 
test_time_to_timeval(void)13 static int test_time_to_timeval(void)
14 {
15     OSSL_TIME a;
16     struct timeval tv;
17 
18     a = ossl_time_zero();
19 
20     tv = ossl_time_to_timeval(a);
21     if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 0))
22         return 0;
23 
24     /* Test that zero round trips */
25     if (!TEST_true(ossl_time_is_zero(ossl_time_from_timeval(tv))))
26         return 0;
27 
28     /* We should round up nano secs to the next usec */
29     a = ossl_ticks2time(1);
30     tv = ossl_time_to_timeval(a);
31     if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1))
32         return 0;
33     a = ossl_ticks2time(999);
34     tv = ossl_time_to_timeval(a);
35     if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1))
36         return 0;
37     a = ossl_ticks2time(1000);
38     tv = ossl_time_to_timeval(a);
39     if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 1))
40         return 0;
41     a = ossl_ticks2time(1001);
42     tv = ossl_time_to_timeval(a);
43     if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 2))
44         return 0;
45     a = ossl_ticks2time(999000);
46     tv = ossl_time_to_timeval(a);
47     if (!TEST_long_eq(tv.tv_sec, 0) || !TEST_long_eq(tv.tv_usec, 999))
48         return 0;
49     a = ossl_ticks2time(999999001);
50     tv = ossl_time_to_timeval(a);
51     if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0))
52         return 0;
53     a = ossl_ticks2time(999999999);
54     tv = ossl_time_to_timeval(a);
55     if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0))
56         return 0;
57     a = ossl_ticks2time(1000000000);
58     tv = ossl_time_to_timeval(a);
59     if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 0))
60         return 0;
61     a = ossl_ticks2time(1000000001);
62     tv = ossl_time_to_timeval(a);
63     if (!TEST_long_eq(tv.tv_sec, 1) || !TEST_long_eq(tv.tv_usec, 1))
64         return 0;
65 
66     /*
67      * Note that we don't currently support infinity round tripping. Instead
68      * callers need to explicitly test for infinity.
69      */
70 
71     return 1;
72 }
73 
setup_tests(void)74 int setup_tests(void)
75 {
76     ADD_TEST(test_time_to_timeval);
77 
78     return 1;
79 }
80