1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <sys/time.h>
17 #include <stdlib.h>
18 #include <time.h>
19 #include <unistd.h>
20 #include "functionalext.h"
21
22 const int SUCCESS = 0;
23
24 extern int __gettimeofday_time64(struct timeval *__restrict, void *__restrict);
25
26 /**
27 * @tc.name : gettimeofday_0100
28 * @tc.desc : Each parameter is valid, and the current system time can be obtained.
29 * @tc.level : Level 0
30 */
gettimeofday_0100(void)31 void gettimeofday_0100(void)
32 {
33 struct timeval tv;
34 char str[11];
35 system("date +%s > ./time.txt");
36 FILE *fptr = fopen("time.txt", "r");
37 fflush(fptr);
38 fread(str, sizeof(str), 1, fptr);
39 str[sizeof(str) - 1] = '\0';
40
41 int sec = atoi(str);
42 int returnflag = gettimeofday(&tv, NULL);
43 remove("time.txt");
44 EXPECT_EQ("gettimeofday_0100", returnflag, SUCCESS);
45 EXPECT_EQ("gettimeofday_0100", (sec == tv.tv_sec || (sec+1) == tv.tv_sec), true);
46 }
47
48 /**
49 * @tc.name : gettimeofday_0200
50 * @tc.desc : Each parameter is invalid, and the current system time can be obtained.
51 * @tc.level : Level 2
52 */
gettimeofday_0200(void)53 void gettimeofday_0200(void)
54 {
55 int returnflag = gettimeofday(NULL, NULL);
56 EXPECT_EQ("gettimeofday_0200", returnflag, SUCCESS);
57 }
58
59 /**
60 * @tc.name : gettimeofday_time64_0100
61 * @tc.desc : Each parameter is valid, and the current system time can be obtained.
62 * @tc.level : Level 0
63 */
gettimeofday_time64_0100(void)64 void gettimeofday_time64_0100(void)
65 {
66 struct timeval tv;
67 char str[11];
68 system("date +%s > ./time.txt");
69 FILE *fptr = fopen("time.txt", "r");
70 fflush(fptr);
71 fread(str, sizeof(str), 1, fptr);
72 str[sizeof(str) - 1] = '\0';
73
74 int sec = atoi(str);
75 int returnflag = __gettimeofday_time64(&tv, NULL);
76 remove("time.txt");
77 EXPECT_EQ("gettimeofday_time64_0100", returnflag, SUCCESS);
78 EXPECT_EQ("gettimeofday_time64_0100", (sec == tv.tv_sec || (sec+1) == tv.tv_sec), true);
79 }
80
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 gettimeofday_0100();
84 gettimeofday_0200();
85 gettimeofday_time64_0100();
86 return t_status;
87 }