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 <errno.h>
17 #include <dlfcn.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdbool.h>
22 #include <ctype.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdint.h>
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <net/if.h>
32 #include <resolv.h>
33 #include <arpa/nameser.h>
34 #include <poll.h>
35 #include <pthread.h>
36 #include "functionalext.h"
37
38 typedef void (*TEST_FUN)();
39 unsigned char TEST_IOCTL_ACCESS_TOKEN_ID = 'A';
40 unsigned int GET_TOKEN_ID = 1;
41 unsigned int SET_TOKEN_ID = 2;
42 const char device_tokenid[] = "/dev/access_token_id";
43 #define TEST_TOKENID_GET_TOKENID _IOR(TEST_IOCTL_ACCESS_TOKEN_ID, GET_TOKEN_ID, unsigned long long)
44 #define TEST_TOKENID_SET_TOKENID _IOW(TEST_IOCTL_ACCESS_TOKEN_ID, SET_TOKEN_ID, unsigned long long)
GetRand64(void)45 static unsigned long long GetRand64(void)
46 {
47 unsigned long long randValue = 0;
48 int randFd = open("/dev/random", O_RDONLY);
49 if (randFd > 0) {
50 read(randFd, &randValue, sizeof(unsigned long long));
51 }
52 close(randFd);
53 return randValue;
54 }
55
56 /**
57 * @tc.name : ioctl_0100
58 * @tc.desc : Verify ioctl management of character device.
59 * @tc.level : Level 0
60 */
ioctl_0100(void)61 void ioctl_0100(void)
62 {
63 unsigned long long tokenGet;
64 unsigned long long token = GetRand64();
65 int fd = open(device_tokenid, O_RDWR);
66 if (fd < 0) {
67 printf("open %s failed\n", device_tokenid);
68 EXPECT_TRUE(ioctl_0100, false);
69 return;
70 }
71 int ret = ioctl(fd, TEST_TOKENID_GET_TOKENID, &token);
72 EXPECT_EQ("ioctl_0100", 0, ret);
73 ret = ioctl(fd, TEST_TOKENID_GET_TOKENID, &tokenGet);
74 EXPECT_EQ("ioctl_0100", 0, ret);
75 EXPECT_EQ("ioctl_0100", 0, ret);
76 }
77
78 /**
79 * @tc.name : ioctl_0200
80 * @tc.desc : Verify ioctl management of network device.
81 * @tc.level : Level 0
82 */
ioctl_0200(void)83 void ioctl_0200(void)
84 {
85 int sockFd = socket(AF_INET, SOCK_DGRAM, 0);
86 if (sockFd < 0) {
87 printf("socket failed\n");
88 EXPECT_TRUE(ioctl_0200, false);
89 close(sockFd);
90 return;
91 }
92 EXPECT_NE("ioctl_0200", -1, sockFd);
93 struct ifreq ifr[3];
94 struct ifconf ifc = {0};
95 ifc.ifc_len = 3 * sizeof(struct ifreq);
96 ifc.ifc_buf = (char *)ifr;
97 int ret = ioctl(sockFd, SIOCGIFCONF, &ifc);
98 EXPECT_EQ("ioctl_0200", 0, ret);
99 int ifrCount = ifc.ifc_len / sizeof(struct ifreq);
100 for (int i = 0; i < ifrCount; i++) {
101 printf("interface name: %s\n", ifr[i].ifr_name);
102 printf("address: %s\n", inet_ntoa(((struct sockaddr_in *)&(ifr[i].ifr_addr))->sin_addr));
103 }
104 if (ifrCount == 0) {
105 close(sockFd);
106 return;
107 }
108 char interName[128];
109 strcpy(interName, ifr[0].ifr_name);
110 char interAddr[128];
111 strcpy(interAddr, inet_ntoa(((struct sockaddr_in *)&(ifr[0].ifr_addr))->sin_addr));
112 struct ifreq ifrAddr = {0};
113 strcpy(ifrAddr.ifr_name, interName);
114 ret = ioctl(sockFd, SIOCGIFADDR, &ifrAddr);
115 EXPECT_EQ("ioctl_0200", 0, ret);
116 struct sockaddr_in *srvAddr = (struct sockaddr_in *)&ifrAddr.ifr_addr;
117 EXPECT_STREQ("ioctl_0200", interAddr, inet_ntoa(srvAddr->sin_addr));
118 close(sockFd);
119 }
120
121 TEST_FUN G_Fun_Array[] = {
122 ioctl_0100,
123 ioctl_0200,
124 };
125
main(int argc,char * argv[])126 int main(int argc, char *argv[])
127 {
128 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
129 for (int pos = 0; pos < num; ++pos) {
130 G_Fun_Array[pos]();
131 }
132
133 return t_status;
134 }
135