1 /*
2 * Copyright (c) 2025 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
17 #include <cstring>
18 #include "securec.h"
19 #include <cerrno>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <csignal>
23 #include <iostream>
24 #include <iterator>
25 #include <linux/fs.h>
26 #include <ostream>
27 #include <string>
28 #include <vector>
29 #include <termios.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <malloc.h>
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
35 #include <sys/fsuid.h>
36 #include <sys/stat.h>
37 #include <gtest/gtest.h>
38 #include <linux/if.h>
39
40 using namespace testing::ext;
41
42 class IoCtlTest : public testing::Test {
43 public:
44 static void SetUpTestCase();
45 static void TearDownTestCase();
46 void SetUp();
47 void TearDown();
48 private:
49 };
SetUp()50 void IoCtlTest::SetUp()
51 {
52 }
53
TearDown()54 void IoCtlTest::TearDown()
55 {
56 }
57
SetUpTestCase()58 void IoCtlTest::SetUpTestCase()
59 {
60 }
61
TearDownTestCase()62 void IoCtlTest::TearDownTestCase()
63 {
64 }
65
66 /*
67 * @tc.number : SUB_KERNEL_SYSCALL_IOCTL_0100
68 * @tc.name : IoctlValidFdSuccess_0001
69 * @tc.desc : Ioctl invalid fd failed.
70 * @tc.size : MediumTest
71 * @tc.type : Function
72 * @tc.level : Level 1
73 */
74 HWTEST_F(IoCtlTest, IoctlValidFdSuccess_0001, Function | MediumTest | Level1)
75 {
76 errno = 0;
77 int fd = open("/dev/null", O_RDONLY);
78 ASSERT_NE(fd, -1);
79 int result = ioctl(fd, 0xffff, 0);
80 close(fd);
81 EXPECT_TRUE(result == -1);
82 EXPECT_EQ(errno, ENOTTY);
83 }
84
85 /*
86 * @tc.number : SUB_KERNEL_SYSCALL_IOCTL_0200
87 * @tc.name : IoctlSiocgifconfSuccess_0002
88 * @tc.desc : Ioctl config SIOCGIFCONF successfully.
89 * @tc.size : MediumTest
90 * @tc.type : Function
91 * @tc.level : Level 1
92 */
93 HWTEST_F(IoCtlTest, IoctlSiocgifconfSuccess_0002, Function | MediumTest | Level1)
94 {
95 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
96 struct ifconf ifconfig;
97 int ret = ioctl(sockfd, SIOCGIFCONF, &ifconfig);
98 close(sockfd);
99 EXPECT_EQ(ret, 0);
100 }
101
102 /*
103 * @tc.number : SUB_KERNEL_SYSCALL_IOCTL_0300
104 * @tc.name : IoctlSiocgifflagsSuccess_0003
105 * @tc.desc : Ioctl config SIOCGIFFLAGS successfully.
106 * @tc.size : MediumTest
107 * @tc.type : Function
108 * @tc.level : Level 1
109 */
110 HWTEST_F(IoCtlTest, IoctlSiocgifflagsSuccess_0003, Function | MediumTest | Level1)
111 {
112 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
113 struct ifreq ifr;
114 memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr));
115 const char *strValue = "wlan0";
116 memcpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), strValue, strlen(strValue));
117 int ret = ioctl(sockfd, SIOCGIFFLAGS, &ifr);
118 close(sockfd);
119 EXPECT_EQ(ret, 0);
120 }
121
122 /*
123 * @tc.number : SUB_KERNEL_SYSCALL_IOCTL_0400
124 * @tc.name : IoctlSiocgifindexSuccess_0004
125 * @tc.desc : Ioctl config SIOCGIFINDEX successfully.
126 * @tc.size : MediumTest
127 * @tc.type : Function
128 * @tc.level : Level 1
129 */
130 HWTEST_F(IoCtlTest, IoctlSiocgifindexSuccess_0004, Function | MediumTest | Level1)
131 {
132 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
133 struct ifreq ifr;
134 memset_s(&ifr, sizeof(ifr), 0, sizeof(ifr));
135 const char *strValue = "wlan0";
136 memcpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), strValue, strlen(strValue));
137 int ret = ioctl(sockfd, SIOCGIFINDEX, &ifr);
138 close(sockfd);
139 EXPECT_EQ(ret, 0);
140 }
141