• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <gmock/gmock.h>
19 #include <gtest/gtest.h>
20 #include <netinet/in.h>
21 #include <sys/socket.h>
22 
23 #include <future>
24 
25 #include "common/init_flags.h"
26 #include "hal/snoop_logger_common.h"
27 #include "hal/snoop_logger_socket_thread.h"
28 #include "hal/syscall_wrapper_impl.h"
29 #include "hal/syscall_wrapper_mock.h"
30 #include "os/log.h"
31 #include "os/utils.h"
32 
33 static const char* test_flags[] = {
34     "INIT_logging_debug_enabled_for_all=true",
35     nullptr,
36 };
37 
38 namespace testing {
39 
40 using bluetooth::hal::SnoopLoggerCommon;
41 using bluetooth::hal::SnoopLoggerSocket;
42 using bluetooth::hal::SyscallWrapperImpl;
43 using bluetooth::hal::SyscallWrapperMock;
44 
45 static constexpr int INVALID_FD = -1;
46 
47 class SnoopLoggerSocketModuleTest : public Test {
48  protected:
SetUp()49   void SetUp() override {
50     bluetooth::common::InitFlags::Load(test_flags);
51   }
SnoopLoggerSocketModuleTest()52   SnoopLoggerSocketModuleTest() : sls(&mock) {}
53 
InitializeCommunicationsSuccess(SnoopLoggerSocket & sls,SyscallWrapperMock & mock)54   void InitializeCommunicationsSuccess(SnoopLoggerSocket& sls, SyscallWrapperMock& mock) {
55     ON_CALL(mock, Pipe2).WillByDefault(Invoke([&](int* fds, int) {
56       fds[0] = listen_fd;
57       fds[1] = write_fd;
58       return 0;
59     }));
60     ON_CALL(mock, Socket).WillByDefault((Return(fd)));
61     ON_CALL(mock, Setsockopt(Eq(fd), _, _, _, _)).WillByDefault((Return(0)));
62     ON_CALL(mock, Bind(Eq(fd), _, _)).WillByDefault((Return(0)));
63     ON_CALL(mock, Listen(Eq(fd), _)).WillByDefault((Return(0)));
64 
65     EXPECT_CALL(mock, FDZero);
66     EXPECT_CALL(mock, Pipe2(_, _));
67     EXPECT_CALL(mock, FDSet(Eq(listen_fd), _));
68     EXPECT_CALL(mock, FDSet(Eq(fd), _));
69     EXPECT_CALL(mock, Socket);
70     EXPECT_CALL(mock, Setsockopt);
71     EXPECT_CALL(mock, Bind);
72     EXPECT_CALL(mock, Listen);
73 
74     ASSERT_EQ(sls.InitializeCommunications(), 0);
75 
76     // will be called in destructor
77     EXPECT_CALL(mock, Close(Eq(fd)));
78     EXPECT_CALL(mock, FDClr(Eq(fd), _));
79     EXPECT_CALL(mock, Close(Eq(listen_fd)));
80     EXPECT_CALL(mock, FDClr(Eq(listen_fd), _));
81     EXPECT_CALL(mock, Close(Eq(write_fd)));
82     EXPECT_CALL(mock, FDClr(Eq(write_fd), _));
83   }
84 
TearDown()85   void TearDown() override {}
86 
87   int fd = 11;
88   const int listen_fd = 66;
89   const int write_fd = 99;
90 
91   StrictMock<SyscallWrapperMock> mock;
92   SnoopLoggerSocket sls;
93 };
94 
TEST_F(SnoopLoggerSocketModuleTest,test_Constructor_GetSyscallWrapperInterface)95 TEST_F(SnoopLoggerSocketModuleTest, test_Constructor_GetSyscallWrapperInterface) {
96   ASSERT_EQ(sls.GetSyscallWrapperInterface(), &mock);
97 }
98 
TEST_F(SnoopLoggerSocketModuleTest,test_Destructor_implicit_cleanup)99 TEST_F(SnoopLoggerSocketModuleTest, test_Destructor_implicit_cleanup) {}
100 
TEST_F(SnoopLoggerSocketModuleTest,test_Cleanup_explicit)101 TEST_F(SnoopLoggerSocketModuleTest, test_Cleanup_explicit) {
102   sls.Cleanup();
103 }
104 
TEST_F(SnoopLoggerSocketModuleTest,test_CreateSocket_fail_on_Socket)105 TEST_F(SnoopLoggerSocketModuleTest, test_CreateSocket_fail_on_Socket) {
106   ON_CALL(mock, Socket(_, _, _)).WillByDefault((Return(-1)));
107 
108   EXPECT_CALL(mock, Socket).Times(1);
109   EXPECT_CALL(mock, GetErrno);
110   ASSERT_EQ(sls.CreateSocket(), INVALID_FD);
111 }
112 
TEST_F(SnoopLoggerSocketModuleTest,test_CreateSocket_fail_on_Setsockopt)113 TEST_F(SnoopLoggerSocketModuleTest, test_CreateSocket_fail_on_Setsockopt) {
114   ON_CALL(mock, Socket(_, _, _)).WillByDefault((Return(fd)));
115   ON_CALL(mock, Setsockopt(_, _, _, _, _)).WillByDefault((Return(-1)));
116 
117   EXPECT_CALL(mock, Socket);
118   EXPECT_CALL(mock, Setsockopt);
119   EXPECT_CALL(mock, Close);
120   EXPECT_CALL(mock, FDClr(Eq(fd), _));
121   EXPECT_CALL(mock, FDSet(Eq(fd), _));
122   EXPECT_CALL(mock, GetErrno);
123   ASSERT_EQ(sls.CreateSocket(), INVALID_FD);
124 }
125 
TEST_F(SnoopLoggerSocketModuleTest,test_CreateSocket_fail_on_Bind)126 TEST_F(SnoopLoggerSocketModuleTest, test_CreateSocket_fail_on_Bind) {
127   ON_CALL(mock, Socket(_, _, _)).WillByDefault((Return(fd)));
128   ON_CALL(mock, Setsockopt(_, _, _, _, _)).WillByDefault((Return(0)));
129   ON_CALL(mock, Bind(_, _, _)).WillByDefault((Return(-1)));
130 
131   EXPECT_CALL(mock, Socket);
132   EXPECT_CALL(mock, Setsockopt);
133   EXPECT_CALL(mock, Bind);
134   EXPECT_CALL(mock, Close);
135   EXPECT_CALL(mock, FDSet(Eq(fd), _));
136   EXPECT_CALL(mock, FDClr(Eq(fd), _));
137   EXPECT_CALL(mock, GetErrno);
138   ASSERT_EQ(sls.CreateSocket(), INVALID_FD);
139 }
140 
TEST_F(SnoopLoggerSocketModuleTest,test_CreateSocket_fail_on_Listen)141 TEST_F(SnoopLoggerSocketModuleTest, test_CreateSocket_fail_on_Listen) {
142   ON_CALL(mock, Socket(_, _, _)).WillByDefault((Return(fd)));
143   ON_CALL(mock, Setsockopt(_, _, _, _, _)).WillByDefault((Return(0)));
144   ON_CALL(mock, Bind(_, _, _)).WillByDefault((Return(0)));
145   ON_CALL(mock, Listen(_, _)).WillByDefault((Return(-1)));
146 
147   EXPECT_CALL(mock, Socket);
148   EXPECT_CALL(mock, Setsockopt);
149   EXPECT_CALL(mock, Bind);
150   EXPECT_CALL(mock, Listen);
151   EXPECT_CALL(mock, Close);
152   EXPECT_CALL(mock, FDSet(Eq(fd), _));
153   EXPECT_CALL(mock, FDClr(Eq(fd), _));
154   EXPECT_CALL(mock, GetErrno);
155   ASSERT_EQ(sls.CreateSocket(), INVALID_FD);
156 }
157 
TEST_F(SnoopLoggerSocketModuleTest,test_CreateSocket_success)158 TEST_F(SnoopLoggerSocketModuleTest, test_CreateSocket_success) {
159   ON_CALL(mock, Socket(_, _, _)).WillByDefault((Return(fd)));
160   ON_CALL(mock, Setsockopt(_, _, _, _, _)).WillByDefault((Return(0)));
161   ON_CALL(mock, Bind(_, _, _)).WillByDefault((Return(0)));
162   ON_CALL(mock, Listen(_, _)).WillByDefault((Return(0)));
163 
164   EXPECT_CALL(mock, Socket);
165   EXPECT_CALL(mock, Setsockopt);
166   EXPECT_CALL(mock, Bind);
167   EXPECT_CALL(mock, Listen);
168   EXPECT_CALL(mock, FDSet(Eq(fd), _));
169   ASSERT_EQ(sls.CreateSocket(), fd);
170 }
171 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_fd_invalid_fd)172 TEST_F(SnoopLoggerSocketModuleTest, test_Write_fd_invalid_fd) {
173   fd = INVALID_FD;
174 
175   sls.Write(fd, NULL, 0);
176 }
177 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_fd_fail_on_Send_ECONNRESET)178 TEST_F(SnoopLoggerSocketModuleTest, test_Write_fd_fail_on_Send_ECONNRESET) {
179   char data[10];
180 
181   ON_CALL(mock, Send(_, _, _, _)).WillByDefault((Return(-1)));
182   ON_CALL(mock, GetErrno()).WillByDefault((Return(ECONNRESET)));
183 
184   EXPECT_CALL(mock, Send(Eq(fd), Eq(data), Eq(sizeof(data)), _));
185   EXPECT_CALL(mock, Close(Eq(fd)));
186   EXPECT_CALL(mock, FDClr(Eq(fd), _));
187   EXPECT_CALL(mock, GetErrno);
188 
189   sls.Write(fd, data, sizeof(data));
190 }
191 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_fd_fail_on_Send_EINVAL)192 TEST_F(SnoopLoggerSocketModuleTest, test_Write_fd_fail_on_Send_EINVAL) {
193   char data[10];
194 
195   ON_CALL(mock, Send(_, _, _, _)).WillByDefault((Return(-1)));
196   ON_CALL(mock, GetErrno()).WillByDefault((Return(EINVAL)));
197 
198   EXPECT_CALL(mock, Send(Eq(fd), Eq(data), Eq(sizeof(data)), _));
199   EXPECT_CALL(mock, GetErrno).Times(2);
200 
201   sls.Write(fd, data, sizeof(data));
202 }
203 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_fd_success)204 TEST_F(SnoopLoggerSocketModuleTest, test_Write_fd_success) {
205   int client_fd = 33;
206   char data[10];
207 
208   EXPECT_CALL(mock, Send(client_fd, _, _, _)).Times(1);
209 
210   sls.Write(client_fd, data, sizeof(data));
211 }
212 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_fail_no_client)213 TEST_F(SnoopLoggerSocketModuleTest, test_Write_fail_no_client) {
214   char data[10];
215 
216   sls.Write(data, sizeof(data));
217 }
218 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_success)219 TEST_F(SnoopLoggerSocketModuleTest, test_Write_success) {
220   int client_fd = 33;
221   char data[10];
222 
223   EXPECT_CALL(mock, Send(client_fd, _, _, _)).Times(1);
224   EXPECT_CALL(mock, Close(client_fd)).Times(1);
225 
226   sls.ClientSocketConnected(client_fd);
227 
228   sls.Write(data, sizeof(data));
229   EXPECT_CALL(mock, FDClr(Eq(client_fd), _));
230 }
231 
TEST_F(SnoopLoggerSocketModuleTest,test_Write_fd_fail_on_Send_EINTR)232 TEST_F(SnoopLoggerSocketModuleTest, test_Write_fd_fail_on_Send_EINTR) {
233   char data[10];
234   int intr_count = 5;
235 
236   ON_CALL(mock, Send).WillByDefault(Invoke([&](int fd, const void* buf, size_t n, int flags) {
237     if (intr_count > 0) {
238       intr_count--;
239       errno = EINTR;
240       return -1;
241     }
242     errno = 0;
243     return 0;
244   }));
245 
246   EXPECT_CALL(mock, Send(Eq(fd), Eq(data), Eq(sizeof(data)), _)).Times(intr_count + 1);
247 
248   sls.Write(fd, data, sizeof(data));
249 }
250 
TEST_F(SnoopLoggerSocketModuleTest,test_ClientSocketConnected)251 TEST_F(SnoopLoggerSocketModuleTest, test_ClientSocketConnected) {
252   ASSERT_FALSE(sls.IsClientSocketConnected());
253 
254   EXPECT_CALL(mock, Close(Eq(fd))).Times(1);
255   EXPECT_CALL(mock, Close(Eq(fd + 1))).Times(1);
256   EXPECT_CALL(mock, FDClr(Eq(fd), _));
257   EXPECT_CALL(mock, FDClr(Eq(fd + 1), _));
258 
259   sls.ClientSocketConnected(fd);
260 
261   ASSERT_TRUE(sls.IsClientSocketConnected());
262 
263   sls.ClientSocketConnected(fd + 1);
264 
265   ASSERT_TRUE(sls.IsClientSocketConnected());
266 }
267 
TEST_F(SnoopLoggerSocketModuleTest,test_WaitForClientSocketConnected)268 TEST_F(SnoopLoggerSocketModuleTest, test_WaitForClientSocketConnected) {
269   ASSERT_FALSE(sls.IsClientSocketConnected());
270 
271   sls.ClientSocketConnected(fd);
272 
273   ASSERT_TRUE(sls.IsClientSocketConnected());
274 
275   ASSERT_TRUE(sls.WaitForClientSocketConnected());
276 
277   EXPECT_CALL(mock, Close(Eq(fd)));
278   EXPECT_CALL(mock, FDClr(Eq(fd), _));
279 }
280 
TEST_F(SnoopLoggerSocketModuleTest,test_InitializeClientSocket)281 TEST_F(SnoopLoggerSocketModuleTest, test_InitializeClientSocket) {
282   int client_fd = 10;
283 
284   EXPECT_CALL(mock, Send(client_fd, _, _, _)).Times(1);
285 
286   sls.InitializeClientSocket(client_fd);
287 }
288 
TEST_F(SnoopLoggerSocketModuleTest,test_AcceptIncomingConnection_fail_on_accept_EINVAL)289 TEST_F(SnoopLoggerSocketModuleTest, test_AcceptIncomingConnection_fail_on_accept_EINVAL) {
290   int client_fd = 0;
291 
292   ON_CALL(mock, Accept(Eq(fd), _, _, _)).WillByDefault(Return(INVALID_FD));
293   ON_CALL(mock, GetErrno()).WillByDefault(Return(EINVAL));
294 
295   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
296   EXPECT_CALL(mock, GetErrno);
297   ASSERT_EQ(sls.AcceptIncomingConnection(fd, client_fd), EINVAL);
298 }
299 
TEST_F(SnoopLoggerSocketModuleTest,test_AcceptIncomingConnection_fail_on_accept_EBADF)300 TEST_F(SnoopLoggerSocketModuleTest, test_AcceptIncomingConnection_fail_on_accept_EBADF) {
301   int client_fd = 0;
302 
303   ON_CALL(mock, Accept(Eq(fd), _, _, _)).WillByDefault(Return(INVALID_FD));
304   ON_CALL(mock, GetErrno()).WillByDefault(Return(EBADF));
305 
306   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
307   EXPECT_CALL(mock, GetErrno);
308   ASSERT_EQ(sls.AcceptIncomingConnection(fd, client_fd), EBADF);
309 }
310 
TEST_F(SnoopLoggerSocketModuleTest,test_AcceptIncomingConnection_fail_on_accept_EINTR)311 TEST_F(SnoopLoggerSocketModuleTest, test_AcceptIncomingConnection_fail_on_accept_EINTR) {
312   int client_fd = 0;
313   int intr_count = 5;
314 
315   ON_CALL(mock, Accept(Eq(fd), _, _, _))
316       .WillByDefault(Invoke([&](int fd, struct sockaddr* addr, socklen_t* addr_len, int flags) {
317         if (intr_count > 0) {
318           intr_count--;
319           errno = EINTR;
320           return -1;
321         }
322         errno = 0;
323         return client_fd;
324       }));
325 
326   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _)).Times(intr_count + 1);  // 5 intr + 1 with errno = 0
327   EXPECT_CALL(mock, GetErrno);
328   ASSERT_EQ(sls.AcceptIncomingConnection(fd, client_fd), 0);
329 }
330 
TEST_F(SnoopLoggerSocketModuleTest,test_AcceptIncomingConnection_fail_on_accept_other_errors)331 TEST_F(SnoopLoggerSocketModuleTest, test_AcceptIncomingConnection_fail_on_accept_other_errors) {
332   int client_fd = 0;
333 
334   ON_CALL(mock, Accept(Eq(fd), _, _, _)).WillByDefault(Return(INVALID_FD));
335   ON_CALL(mock, GetErrno()).WillByDefault(Return(EAGAIN));
336 
337   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
338   EXPECT_CALL(mock, GetErrno);
339   ASSERT_EQ(sls.AcceptIncomingConnection(fd, client_fd), 0);
340   ASSERT_EQ(client_fd, -1);
341 }
342 
TEST_F(SnoopLoggerSocketModuleTest,test_AcceptIncomingConnection_success)343 TEST_F(SnoopLoggerSocketModuleTest, test_AcceptIncomingConnection_success) {
344   int client_fd = 13;
345   int client_fd_out = 0;
346 
347   ON_CALL(mock, Accept(Eq(fd), _, _, _)).WillByDefault(Return(client_fd));
348   ON_CALL(mock, GetErrno()).WillByDefault(Return(0));
349 
350   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
351 
352   ASSERT_EQ(sls.AcceptIncomingConnection(fd, client_fd_out), 0);
353   ASSERT_EQ(client_fd, client_fd_out);
354 }
355 
TEST_F(SnoopLoggerSocketModuleTest,test_InitializeCommunications_fail_on_Pipe2)356 TEST_F(SnoopLoggerSocketModuleTest, test_InitializeCommunications_fail_on_Pipe2) {
357   int ret = -9;
358 
359   ON_CALL(mock, Pipe2(_, _)).WillByDefault(Invoke([ret](int* fds, int) { return ret; }));
360   EXPECT_CALL(mock, FDZero);
361   EXPECT_CALL(mock, Pipe2(_, _));
362 
363   ASSERT_EQ(sls.InitializeCommunications(), ret);
364 }
365 
TEST_F(SnoopLoggerSocketModuleTest,test_InitializeCommunications_fail_on_CreateSocket)366 TEST_F(SnoopLoggerSocketModuleTest, test_InitializeCommunications_fail_on_CreateSocket) {
367   int ret = -9;
368 
369   ON_CALL(mock, Socket(_, _, _)).WillByDefault(Return(ret));
370   ON_CALL(mock, Pipe2).WillByDefault(Invoke([&](int* fds, int) {
371     fds[0] = listen_fd;
372     fds[1] = write_fd;
373     return 0;
374   }));
375 
376   EXPECT_CALL(mock, FDZero);
377   EXPECT_CALL(mock, Pipe2(_, _));
378   EXPECT_CALL(mock, FDSet(listen_fd, _));
379   EXPECT_CALL(mock, Socket);
380   EXPECT_CALL(mock, GetErrno);
381 
382   EXPECT_CALL(mock, Close(Eq(listen_fd)));
383   EXPECT_CALL(mock, FDClr(Eq(listen_fd), _));
384   EXPECT_CALL(mock, Close(Eq(write_fd)));
385   EXPECT_CALL(mock, FDClr(Eq(write_fd), _));
386 
387   ASSERT_EQ(sls.InitializeCommunications(), -1);
388 }
389 
TEST_F(SnoopLoggerSocketModuleTest,test_InitializeCommunications_success)390 TEST_F(SnoopLoggerSocketModuleTest, test_InitializeCommunications_success) {
391   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
392 }
393 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_fail_on_Select_EINTR)394 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_fail_on_Select_EINTR) {
395   ON_CALL(mock, Select).WillByDefault((Return(-1)));
396   ON_CALL(mock, GetErrno()).WillByDefault((Return(EINTR)));
397 
398   EXPECT_CALL(mock, Select);
399   EXPECT_CALL(mock, GetErrno).Times(2);
400   ASSERT_TRUE(sls.ProcessIncomingRequest());
401 }
402 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_fail_on_Select_EINVAL)403 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_fail_on_Select_EINVAL) {
404   ON_CALL(mock, Select).WillByDefault((Return(-1)));
405   ON_CALL(mock, GetErrno()).WillByDefault((Return(EINVAL)));
406 
407   EXPECT_CALL(mock, Select);
408   EXPECT_CALL(mock, GetErrno).Times(2);
409   ASSERT_FALSE(sls.ProcessIncomingRequest());
410 }
411 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_no_fds)412 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_no_fds) {
413   ON_CALL(mock, Select).WillByDefault((Return(0)));
414 
415   EXPECT_CALL(mock, Select);
416   ASSERT_TRUE(sls.ProcessIncomingRequest());
417 }
418 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_FDIsSet_false)419 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_FDIsSet_false) {
420   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
421 
422   ON_CALL(mock, Select).WillByDefault((Return(0)));
423   ON_CALL(mock, FDIsSet(fd, _)).WillByDefault((Return(false)));
424   ON_CALL(mock, FDIsSet(listen_fd, _)).WillByDefault((Return(false)));
425 
426   EXPECT_CALL(mock, Select);
427   EXPECT_CALL(mock, FDIsSet(Eq(fd), _));
428   EXPECT_CALL(mock, FDIsSet(Eq(listen_fd), _));
429   ASSERT_TRUE(sls.ProcessIncomingRequest());
430 }
431 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_signal_close)432 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_signal_close) {
433   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
434 
435   ON_CALL(mock, Select).WillByDefault((Return(0)));
436   ON_CALL(mock, FDIsSet(fd, _)).WillByDefault((Return(false)));
437   ON_CALL(mock, FDIsSet(listen_fd, _)).WillByDefault((Return(true)));
438 
439   EXPECT_CALL(mock, Select);
440   EXPECT_CALL(mock, FDIsSet(Eq(fd), _));
441   EXPECT_CALL(mock, FDIsSet(Eq(listen_fd), _));
442   ASSERT_FALSE(sls.ProcessIncomingRequest());
443 }
444 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_signal_incoming_connection_fail_on_accept_exit)445 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_signal_incoming_connection_fail_on_accept_exit) {
446   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
447 
448   ON_CALL(mock, Select).WillByDefault((Return(0)));
449   ON_CALL(mock, FDIsSet(fd, _)).WillByDefault((Return(true)));
450   ON_CALL(mock, FDIsSet(listen_fd, _)).WillByDefault((Return(false)));
451 
452   ON_CALL(mock, Accept(fd, _, _, _)).WillByDefault(Return(INVALID_FD));
453   ON_CALL(mock, GetErrno()).WillByDefault(Return(EINVAL));
454 
455   EXPECT_CALL(mock, Select);
456   EXPECT_CALL(mock, FDIsSet(Eq(fd), _));
457   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
458   EXPECT_CALL(mock, GetErrno);
459   ASSERT_FALSE(sls.ProcessIncomingRequest());
460 }
461 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_signal_incoming_connection_fail_on_accept_continue)462 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_signal_incoming_connection_fail_on_accept_continue) {
463   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
464 
465   ON_CALL(mock, Select).WillByDefault((Return(0)));
466   ON_CALL(mock, FDIsSet(fd, _)).WillByDefault((Return(true)));
467   ON_CALL(mock, FDIsSet(listen_fd, _)).WillByDefault((Return(false)));
468 
469   ON_CALL(mock, Accept(fd, _, _, _)).WillByDefault(Return(INVALID_FD));
470   ON_CALL(mock, GetErrno()).WillByDefault(Return(ENOMEM));
471 
472   EXPECT_CALL(mock, Select);
473   EXPECT_CALL(mock, FDIsSet(Eq(fd), _));
474   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
475   EXPECT_CALL(mock, GetErrno);
476   ASSERT_TRUE(sls.ProcessIncomingRequest());
477 }
478 
TEST_F(SnoopLoggerSocketModuleTest,test_ProcessIncomingRequest_signal_incoming_connection_success)479 TEST_F(SnoopLoggerSocketModuleTest, test_ProcessIncomingRequest_signal_incoming_connection_success) {
480   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
481 
482   int client_fd = 23;
483 
484   ON_CALL(mock, Select).WillByDefault((Return(0)));
485   ON_CALL(mock, FDIsSet(fd, _)).WillByDefault((Return(true)));
486   ON_CALL(mock, FDIsSet(listen_fd, _)).WillByDefault((Return(false)));
487 
488   ON_CALL(mock, Accept(fd, _, _, _)).WillByDefault(Return(client_fd));
489   ON_CALL(mock, GetErrno()).WillByDefault(Return(0));
490 
491   EXPECT_CALL(mock, Send(client_fd, _, _, _)).Times(1);
492 
493   EXPECT_CALL(mock, Select);
494   EXPECT_CALL(mock, FDIsSet(Eq(fd), _));
495   EXPECT_CALL(mock, Accept(Eq(fd), _, _, _));
496   ASSERT_TRUE(sls.ProcessIncomingRequest());
497 
498   EXPECT_CALL(mock, Close(Eq(client_fd)));
499   EXPECT_CALL(mock, FDClr(Eq(client_fd), _));
500 }
501 
TEST_F(SnoopLoggerSocketModuleTest,test_NotifySocketListener_no_fd)502 TEST_F(SnoopLoggerSocketModuleTest, test_NotifySocketListener_no_fd) {
503   ASSERT_EQ(sls.NotifySocketListener(), 0);
504 }
505 
TEST_F(SnoopLoggerSocketModuleTest,test_NotifySocketListener_fail_on_write)506 TEST_F(SnoopLoggerSocketModuleTest, test_NotifySocketListener_fail_on_write) {
507   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
508 
509   ON_CALL(mock, Write).WillByDefault((Return(-1)));
510   EXPECT_CALL(mock, Write(write_fd, _, _)).Times(1);
511 
512   ASSERT_EQ(sls.NotifySocketListener(), -1);
513 }
514 
TEST_F(SnoopLoggerSocketModuleTest,test_NotifySocketListener_fail_on_write_EINTR_success)515 TEST_F(SnoopLoggerSocketModuleTest, test_NotifySocketListener_fail_on_write_EINTR_success) {
516   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
517 
518   int intr_count = 5;
519 
520   ON_CALL(mock, Write).WillByDefault(Invoke([&](int, const void*, size_t count) {
521     if (intr_count > 0) {
522       intr_count--;
523       errno = EINTR;
524       return (ssize_t)-1;
525     }
526     errno = 0;
527     return (ssize_t)count;
528   }));
529 
530   EXPECT_CALL(mock, Write(write_fd, _, _)).Times(intr_count + 1);
531 
532   ASSERT_EQ(sls.NotifySocketListener(), 0);
533 }
534 
TEST_F(SnoopLoggerSocketModuleTest,test_NotifySocketListener_success)535 TEST_F(SnoopLoggerSocketModuleTest, test_NotifySocketListener_success) {
536   ASSERT_NO_FATAL_FAILURE(InitializeCommunicationsSuccess(sls, mock));
537 
538   ON_CALL(mock, Write).WillByDefault((Return(0)));
539 
540   EXPECT_CALL(mock, Write(write_fd, _, _));
541   ASSERT_EQ(sls.NotifySocketListener(), 0);
542 }
543 
544 }  // namespace testing
545