blob: b6c11414fc1970d282cacda7e7b4fd8121589c76 [file] [log] [blame]
mukesh agrawal3b1d20a2016-10-27 14:19:12 -07001/*
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
31namespace android {
32namespace wifilogd {
33namespace {
34
35using ::testing::_;
36using ::testing::AnyNumber;
37using ::testing::Ge;
38using ::testing::Return;
39using ::testing::StrictMock;
40
41constexpr int kControlSocketFd = 100;
42constexpr char kFakeSocketName[] = "fake-socket";
43
44class MainLoopTest : public ::testing::Test {
45 public:
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
66TEST_F(MainLoopTest, RunOnceReadsFromCorrectSocket) {
67 EXPECT_CALL(*os_, ReceiveDatagram(kControlSocketFd, _, _));
68 EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(AnyNumber());
69 main_loop_->RunOnce();
70}
71
72TEST_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
78TEST_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
87TEST_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
96TEST_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
103TEST_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
mukesh agrawalfd592212016-11-11 16:20:15 -0800112TEST_F(MainLoopTest, RunOnceSleepsAndDoesNotPassDataToCommandProcessorOnError) {
mukesh agrawal3b1d20a2016-10-27 14:19:12 -0700113 EXPECT_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize))
114 .WillOnce(Return(std::tuple<size_t, Os::Errno>{0, EINTR}));
mukesh agrawalfd592212016-11-11 16:20:15 -0800115 EXPECT_CALL(*os_, Nanosleep(_));
mukesh agrawal3b1d20a2016-10-27 14:19:12 -0700116 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.
123using MainLoopDeathTest = MainLoopTest;
124
125TEST_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
mukesh agrawalfd592212016-11-11 16:20:15 -0800135TEST_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
mukesh agrawal3b1d20a2016-10-27 14:19:12 -0700141} // namespace wifilogd
142} // namespace android