1 /*
2 * Copyright (C) 2016, 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 <cerrno>
18 #include <memory>
19 #include <tuple>
20 #include <utility>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24
25 #include "wifilogd/tests/mock_command_processor.h"
26 #include "wifilogd/tests/mock_os.h"
27
28 #include "wifilogd/main_loop.h"
29 #include "wifilogd/protocol.h"
30
31 namespace android {
32 namespace wifilogd {
33 namespace {
34
35 using ::testing::_;
36 using ::testing::AnyNumber;
37 using ::testing::Ge;
38 using ::testing::Return;
39 using ::testing::StrictMock;
40
41 constexpr int kControlSocketFd = 100;
42 constexpr char kFakeSocketName[] = "fake-socket";
43
44 class MainLoopTest : public ::testing::Test {
45 public:
MainLoopTest()46 MainLoopTest()
47 : os_(new StrictMock<MockOs>()),
48 command_processor_(new StrictMock<MockCommandProcessor>()) {
49 EXPECT_CALL(*os_, GetControlSocket(kFakeSocketName))
50 .WillOnce(Return(std::tuple<size_t, Os::Errno>{kControlSocketFd, 0}));
51 main_loop_ = std::make_unique<MainLoop>(
52 kFakeSocketName, std::unique_ptr<Os>{os_},
53 std::unique_ptr<CommandProcessor>{command_processor_});
54 }
55
56 protected:
57 std::unique_ptr<MainLoop> main_loop_;
58 // We use raw pointers to access the mocks, since ownership passes
59 // to |main_loop_|.
60 StrictMock<MockOs>* os_;
61 StrictMock<MockCommandProcessor>* command_processor_;
62 };
63
64 } // namespace
65
TEST_F(MainLoopTest,RunOnceReadsFromCorrectSocket)66 TEST_F(MainLoopTest, RunOnceReadsFromCorrectSocket) {
67 EXPECT_CALL(*os_, ReceiveDatagram(kControlSocketFd, _, _));
68 EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(AnyNumber());
69 main_loop_->RunOnce();
70 }
71
TEST_F(MainLoopTest,RunOnceReadsWithSufficientlyLargeBuffer)72 TEST_F(MainLoopTest, RunOnceReadsWithSufficientlyLargeBuffer) {
73 EXPECT_CALL(*os_, ReceiveDatagram(_, _, Ge(protocol::kMaxMessageSize)));
74 EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(AnyNumber());
75 main_loop_->RunOnce();
76 }
77
TEST_F(MainLoopTest,RunOncePassesSmallestValidMessageToCommandProcessor)78 TEST_F(MainLoopTest, RunOncePassesSmallestValidMessageToCommandProcessor) {
79 EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
80 .WillOnce(
81 Return(std::tuple<size_t, Os::Errno>{sizeof(protocol::Command), 0}));
82 EXPECT_CALL(*command_processor_,
83 ProcessCommand(_, sizeof(protocol::Command), _));
84 main_loop_->RunOnce();
85 }
86
TEST_F(MainLoopTest,RunOncePassesLargestValidMessageToCommandProcessor)87 TEST_F(MainLoopTest, RunOncePassesLargestValidMessageToCommandProcessor) {
88 EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
89 .WillOnce(
90 Return(std::tuple<size_t, Os::Errno>{protocol::kMaxMessageSize, 0}));
91 EXPECT_CALL(*command_processor_,
92 ProcessCommand(_, protocol::kMaxMessageSize, _));
93 main_loop_->RunOnce();
94 }
95
TEST_F(MainLoopTest,RunOncePassesRuntMessageToCommandProcessor)96 TEST_F(MainLoopTest, RunOncePassesRuntMessageToCommandProcessor) {
97 EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
98 .WillOnce(Return(std::tuple<size_t, Os::Errno>{0, 0}));
99 EXPECT_CALL(*command_processor_, ProcessCommand(_, 0, _));
100 main_loop_->RunOnce();
101 }
102
TEST_F(MainLoopTest,RunOnceLimitsMaxSizeReportedToCommandProcessor)103 TEST_F(MainLoopTest, RunOnceLimitsMaxSizeReportedToCommandProcessor) {
104 EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
105 .WillOnce(Return(
106 std::tuple<size_t, Os::Errno>{protocol::kMaxMessageSize + 1, 0}));
107 EXPECT_CALL(*command_processor_,
108 ProcessCommand(_, protocol::kMaxMessageSize, _));
109 main_loop_->RunOnce();
110 }
111
TEST_F(MainLoopTest,RunOnceSleepsAndDoesNotPassDataToCommandProcessorOnError)112 TEST_F(MainLoopTest, RunOnceSleepsAndDoesNotPassDataToCommandProcessorOnError) {
113 EXPECT_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize))
114 .WillOnce(Return(std::tuple<size_t, Os::Errno>{0, EINTR}));
115 EXPECT_CALL(*os_, Nanosleep(_));
116 EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(0);
117 main_loop_->RunOnce();
118 }
119
120 // Per
121 // github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests,
122 // death tests should be specially named.
123 using MainLoopDeathTest = MainLoopTest;
124
TEST_F(MainLoopDeathTest,CtorFailureToFetchControlSocketCausesDeath)125 TEST_F(MainLoopDeathTest, CtorFailureToFetchControlSocketCausesDeath) {
126 auto os = std::make_unique<StrictMock<MockOs>>();
127 auto command_processor = std::make_unique<StrictMock<MockCommandProcessor>>();
128 ON_CALL(*os, GetControlSocket(kFakeSocketName))
129 .WillByDefault(Return(std::tuple<size_t, Os::Errno>{-1, ERANGE}));
130 EXPECT_DEATH(
131 MainLoop(kFakeSocketName, std::move(os), std::move(command_processor)),
132 "Failed to get control socket");
133 }
134
TEST_F(MainLoopDeathTest,RunOnceTerminatesOnUnexpectedError)135 TEST_F(MainLoopDeathTest, RunOnceTerminatesOnUnexpectedError) {
136 ON_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize))
137 .WillByDefault(Return(std::tuple<size_t, Os::Errno>{0, EFAULT}));
138 EXPECT_DEATH(main_loop_->RunOnce(), "Unexpected error");
139 }
140
141 } // namespace wifilogd
142 } // namespace android
143