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