Debian Codegraph @

crt/s2n/tests/unit/s2n_timer_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15 #include "utils/s2n_timer.h"
16
17 #include "s2n_test.h"
18 #include "tls/s2n_config.h"
19
20 int mock_clock(void *in, uint64_t *out)
21 {
22 *out = *(uint64_t *) in;
23
24 return 0;
25 }
26
27 int main(int argc, char **argv)
28 {
29 struct s2n_config *config = NULL;
30 struct s2n_timer timer;
31 uint64_t nanoseconds = 0;
32 uint64_t mock_time = 0;
33
34 BEGIN_TEST();
35 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
36
37 EXPECT_NOT_NULL(config = s2n_config_new());
38 EXPECT_SUCCESS(s2n_config_set_monotonic_clock(config, mock_clock, &mock_time));
39
40 mock_time = 0;
41 EXPECT_OK(s2n_timer_start(config, &timer));
42
43 mock_time = 10;
44 EXPECT_OK(s2n_timer_reset(config, &timer, &nanoseconds));
45 EXPECT_EQUAL(nanoseconds, 10);
46
47 mock_time = 20;
48 EXPECT_OK(s2n_timer_elapsed(config, &timer, &nanoseconds));
49 EXPECT_EQUAL(nanoseconds, 10);
50
51 mock_time = 30;
52 EXPECT_OK(s2n_timer_reset(config, &timer, &nanoseconds));
53 EXPECT_EQUAL(nanoseconds, 20);
54
55 mock_time = 40;
56 EXPECT_OK(s2n_timer_elapsed(config, &timer, &nanoseconds));
57 EXPECT_EQUAL(nanoseconds, 10);
58 EXPECT_EQUAL(mock_time, 40); /* Work-around for cppcheck complaining that mock_time is never read after being set */
59
60 EXPECT_SUCCESS(s2n_config_free(config));
61
62 END_TEST();
63 }