This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_clock_gettime_settime.c
120 lines (102 loc) · 3.22 KB
/
test_clock_gettime_settime.c
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
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/timex.h>
#include "tests.h"
static void test_clock_getres(struct timespec *tv, time_t sec)
{
int result = clock_getres(CLOCK_REALTIME, tv);
if (result)
test_failure(1, "clock_getres returned %d", result);
else if (tv->tv_sec != sec)
test_failure(0, "clock_getres returned %lld instead of %lld", (long long) (tv->tv_sec), (long long) sec);
else
test_success();
}
static void test_clock_gettime(struct timespec *tv, time_t sec)
{
int result = clock_gettime(CLOCK_REALTIME, tv);
if (result)
test_failure(1, "clock_gettime returned %d", result);
else if (tv->tv_sec != sec)
test_failure(0, "clock_gettime returned %lld instead of %lld", (long long) (tv->tv_sec), (long long) sec);
else
test_success();
}
static void test_ntp_gettime(time_t sec)
{
struct ntptimeval ntv;
int result = ntp_gettime(&ntv);
if (result == -1)
test_failure(1, "ntp_gettime returned %d", result);
else if (ntv.time.tv_sec != sec)
test_failure(0, "ntp_gettime returned %lld instead of %lld",
(long long) (ntv.time.tv_sec), (long long) sec);
else
test_success();
}
static void test_ntp_gettimex(time_t sec)
{
struct ntptimeval ntv;
int result = ntp_gettimex(&ntv);
if (result == -1)
test_failure(1, "ntp_gettimex returned %d", result);
else if (ntv.time.tv_sec != sec)
test_failure(0, "ntp_gettimex returned %lld instead of %lld",
(long long) (ntv.time.tv_sec), (long long) sec);
else
test_success();
}
static int test_clock_settime(struct timespec *tv)
{
int result = clock_settime(CLOCK_REALTIME, tv);
if (result)
test_failure(1, "clock_settime returned %d", result);
else
test_success();
return result;
}
void test_clock_gettime_settime(void)
{
struct timespec tv0, tv;
test_begin("Get clock real time");
memset(&tv0, 0x55, sizeof(tv0));
int result = clock_gettime(CLOCK_REALTIME, &tv0);
if (result) test_failure(1, "clock_gettime returned %d", result); else test_success();
test_begin("Get clock resolution");
tv.tv_sec = 0;
tv.tv_nsec = 0;
test_clock_getres(&tv, 0);
test_begin("Set clock real time to Y2038 minus 60 seconds");
tv.tv_sec = 0x7FFFFFFF;
tv.tv_sec -= 59;
tv.tv_nsec = 0;
result = test_clock_settime(&tv);
if (result == 0)
{
test_begin("Get clock real time");
test_clock_gettime(&tv, tv.tv_sec);
test_begin("Get clock real time via NTP (ntp_gettime)");
test_ntp_gettime(tv.tv_sec);
test_begin("Get clock real time via NTP (ntp_gettimex)");
test_ntp_gettimex(tv.tv_sec);
}
test_begin("Set clock real time to Y2038 plus 60 seconds");
tv.tv_sec = 0x7FFFFFFF;
tv.tv_sec += 61;
tv.tv_nsec = 0;
result = test_clock_settime(&tv);
if (result == 0)
{
test_begin("Get clock real time");
test_clock_gettime(&tv, tv.tv_sec);
test_begin("Get clock real time via NTP (ntp_gettime)");
test_ntp_gettime(tv.tv_sec);
test_begin("Get clock real time via NTP (ntp_gettimex)");
test_ntp_gettimex(tv.tv_sec);
}
test_begin("Restore clock real time");
result = clock_settime(CLOCK_REALTIME, &tv0);
if (result) test_failure(1, "closk_settime returned %d", result); else test_success();
}