• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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/socket.h>
17 #include <gtest/gtest.h>
18 #include "server.h"
19 #include "evloop.h"
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace Wifi {
25 static constexpr int MAX_SIZE = 10;
26 static constexpr int MASK = 0x02;
27 static constexpr int MASK_2 = 0x05;
28 static constexpr int MASK_3 = 0x06;
29 static constexpr int MASK_4 = 0x07;
30 
31 class ServerTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {}
TearDownTestCase()35     static void TearDownTestCase()
36     {}
SetUp()37     virtual void SetUp()
38     {}
TearDown()39     virtual void TearDown()
40     {}
41 };
42 
43 HWTEST_F(ServerTest, CreateEventLoopTest, TestSize.Level1)
44 {
45     EventLoop *loop = nullptr;
46     loop = CreateEventLoop(1);
47     EXPECT_TRUE(loop->setSize == 1);
48     EXPECT_TRUE(CreateEventLoop(-1) == nullptr);
49 }
50 
51 HWTEST_F(ServerTest, AddFdEventFail, TestSize.Level1)
52 {
53     int fd = 0;
54     unsigned int addMas = 0x01;
55     EventLoop loop;
56     StopEventLoop(nullptr);
57     StopEventLoop(&loop);
58     EXPECT_TRUE(AddFdEvent(nullptr, fd, addMas) == -1);
59 }
60 
61 HWTEST_F(ServerTest, AddFdEventFail2, TestSize.Level1)
62 {
63     int fd = 0;
64     unsigned int addMas = 0x01;
65     EventLoop loop;
66     loop.setSize = -1;
67     EXPECT_TRUE(AddFdEvent(&loop, fd, addMas) == -1);
68 }
69 
70 HWTEST_F(ServerTest, AddFdEventFail3, TestSize.Level1)
71 {
72     int fd = 0;
73     unsigned int addMas = 0x01;
74     FdMask mask;
75     mask.mask = 0x01;
76     EventLoop loop;
77     loop.setSize = 1;
78     loop.fdMasks = &mask;
79     EXPECT_TRUE(AddFdEvent(&loop, fd, addMas) == 0);
80 }
81 
82 HWTEST_F(ServerTest, AddFdEventFail4, TestSize.Level1)
83 {
84     int fd = 0;
85     unsigned int addMas = MASK;
86     FdMask mask;
87     mask.mask = 0x01;
88     EventLoop loop;
89     loop.setSize = 1;
90     loop.fdMasks = &mask;
91     int ret = AddFdEvent(&loop, fd, addMas);
92     EXPECT_TRUE(ret == -1 || ret == 0);
93 }
94 
95 HWTEST_F(ServerTest, AddFdEventSuccess, TestSize.Level1)
96 {
97     int fd = 1;
98     unsigned int addMas = MASK;
99     FdMask mask;
100     mask.mask = MASK;
101     EventLoop loop;
102     loop.setSize = 2;
103     loop.fdMasks = &mask;
104     int ret = AddFdEvent(&loop, fd, addMas);
105     EXPECT_TRUE(ret == -1 || ret == 0);
106 }
107 
108 HWTEST_F(ServerTest, DelFdEventFail, TestSize.Level1)
109 {
110     int fd = 0;
111     unsigned int delMask = 0x01;
112     EXPECT_TRUE(DelFdEvent(nullptr, fd, delMask) == -1);
113 }
114 
115 HWTEST_F(ServerTest, DelFdEventFail2, TestSize.Level1)
116 {
117     EventLoop loop;
118     loop.setSize = -1;
119     int fd = 0;
120     unsigned int delMask = 0x01;
121     EXPECT_TRUE(DelFdEvent(&loop, fd, delMask) == 0);
122 }
123 
124 HWTEST_F(ServerTest, DelFdEventFail3, TestSize.Level1)
125 {
126     int fd = 0;
127     unsigned int delMask = 0x01;
128     FdMask mask;
129     mask.mask = 0x00;
130     EventLoop loop;
131     loop.setSize = 1;
132     loop.fdMasks = &mask;
133     EXPECT_TRUE(DelFdEvent(&loop, fd, delMask) == 0);
134 }
135 
136 HWTEST_F(ServerTest, DelFdEventFail4, TestSize.Level1)
137 {
138     int fd = 0;
139     unsigned int delMask = MASK;
140     FdMask mask;
141     mask.mask = 0x01;
142     EventLoop loop;
143     loop.setSize = 1;
144     loop.fdMasks = &mask;
145     EXPECT_TRUE(DelFdEvent(&loop, fd, delMask) == 0);
146 }
147 
148 HWTEST_F(ServerTest, DelFdEventFail5, TestSize.Level1)
149 {
150     int fd = 0;
151     unsigned int delMask = MASK_3;
152     FdMask mask;
153     mask.mask = MASK_4;
154     EventLoop loop;
155     loop.setSize = 1;
156     loop.fdMasks = &mask;
157     int ret = DelFdEvent(&loop, fd, delMask);
158     EXPECT_TRUE(ret == -1 || ret == 0);
159     DelFdEvent(&loop, fd, MASK_2);
160 }
161 
162 HWTEST_F(ServerTest, CreateRpcServerTest, TestSize.Level1)
163 {
164     RpcServer *server = nullptr;
165     char path[] = "./unix_sock_test.sock";
166     EXPECT_TRUE(CreateRpcServer(nullptr) == nullptr);
167     server = CreateRpcServer(path);
168 }
169 
170 HWTEST_F(ServerTest, RegisterCallbackTest, TestSize.Level1)
171 {
172     EventLoop loop;
173     loop.stop = 0;
174     RpcServer server;
175     server.loop = &loop;
176     Context context;
177     EXPECT_TRUE(RegisterCallback(nullptr, MAX_SIZE, nullptr) == -1);
178     EXPECT_TRUE(RegisterCallback(&server, MAX_SIZE, nullptr) == -1);
179     EXPECT_TRUE(RegisterCallback(&server, MAX_SIZE, &context) == 0);
180 }
181 
182 HWTEST_F(ServerTest, UnRegisterCallbackTest, TestSize.Level1)
183 {
184     EventLoop loop;
185     loop.stop = 0;
186     RpcServer server;
187     server.loop = &loop;
188     Context context;
189     EXPECT_TRUE(UnRegisterCallback(nullptr, MAX_SIZE, nullptr) == -1);
190     EXPECT_TRUE(UnRegisterCallback(&server, MAX_SIZE, nullptr) == -1);
191     EXPECT_TRUE(UnRegisterCallback(&server, MAX_SIZE, &context) == 0);
192 }
193 }  // namespace Wifi
194 }  // namespace OHOS
195