summaryrefslogtreecommitdiff
path: root/tests/main_loop_unittest.cpp
blob: b6c11414fc1970d282cacda7e7b4fd8121589c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cerrno>
#include <memory>
#include <tuple>
#include <utility>

#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "wifilogd/tests/mock_command_processor.h"
#include "wifilogd/tests/mock_os.h"

#include "wifilogd/main_loop.h"
#include "wifilogd/protocol.h"

namespace android {
namespace wifilogd {
namespace {

using ::testing::_;
using ::testing::AnyNumber;
using ::testing::Ge;
using ::testing::Return;
using ::testing::StrictMock;

constexpr int kControlSocketFd = 100;
constexpr char kFakeSocketName[] = "fake-socket";

class MainLoopTest : public ::testing::Test {
 public:
  MainLoopTest()
      : os_(new StrictMock<MockOs>()),
        command_processor_(new StrictMock<MockCommandProcessor>()) {
    EXPECT_CALL(*os_, GetControlSocket(kFakeSocketName))
        .WillOnce(Return(std::tuple<size_t, Os::Errno>{kControlSocketFd, 0}));
    main_loop_ = std::make_unique<MainLoop>(
        kFakeSocketName, std::unique_ptr<Os>{os_},
        std::unique_ptr<CommandProcessor>{command_processor_});
  }

 protected:
  std::unique_ptr<MainLoop> main_loop_;
  // We use raw pointers to access the mocks, since ownership passes
  // to |main_loop_|.
  StrictMock<MockOs>* os_;
  StrictMock<MockCommandProcessor>* command_processor_;
};

}  // namespace

TEST_F(MainLoopTest, RunOnceReadsFromCorrectSocket) {
  EXPECT_CALL(*os_, ReceiveDatagram(kControlSocketFd, _, _));
  EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(AnyNumber());
  main_loop_->RunOnce();
}

TEST_F(MainLoopTest, RunOnceReadsWithSufficientlyLargeBuffer) {
  EXPECT_CALL(*os_, ReceiveDatagram(_, _, Ge(protocol::kMaxMessageSize)));
  EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(AnyNumber());
  main_loop_->RunOnce();
}

TEST_F(MainLoopTest, RunOncePassesSmallestValidMessageToCommandProcessor) {
  EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
      .WillOnce(
          Return(std::tuple<size_t, Os::Errno>{sizeof(protocol::Command), 0}));
  EXPECT_CALL(*command_processor_,
              ProcessCommand(_, sizeof(protocol::Command), _));
  main_loop_->RunOnce();
}

TEST_F(MainLoopTest, RunOncePassesLargestValidMessageToCommandProcessor) {
  EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
      .WillOnce(
          Return(std::tuple<size_t, Os::Errno>{protocol::kMaxMessageSize, 0}));
  EXPECT_CALL(*command_processor_,
              ProcessCommand(_, protocol::kMaxMessageSize, _));
  main_loop_->RunOnce();
}

TEST_F(MainLoopTest, RunOncePassesRuntMessageToCommandProcessor) {
  EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
      .WillOnce(Return(std::tuple<size_t, Os::Errno>{0, 0}));
  EXPECT_CALL(*command_processor_, ProcessCommand(_, 0, _));
  main_loop_->RunOnce();
}

TEST_F(MainLoopTest, RunOnceLimitsMaxSizeReportedToCommandProcessor) {
  EXPECT_CALL(*os_, ReceiveDatagram(_, _, _))
      .WillOnce(Return(
          std::tuple<size_t, Os::Errno>{protocol::kMaxMessageSize + 1, 0}));
  EXPECT_CALL(*command_processor_,
              ProcessCommand(_, protocol::kMaxMessageSize, _));
  main_loop_->RunOnce();
}

TEST_F(MainLoopTest, RunOnceSleepsAndDoesNotPassDataToCommandProcessorOnError) {
  EXPECT_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize))
      .WillOnce(Return(std::tuple<size_t, Os::Errno>{0, EINTR}));
  EXPECT_CALL(*os_, Nanosleep(_));
  EXPECT_CALL(*command_processor_, ProcessCommand(_, _, _)).Times(0);
  main_loop_->RunOnce();
}

// Per
// github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests,
// death tests should be specially named.
using MainLoopDeathTest = MainLoopTest;

TEST_F(MainLoopDeathTest, CtorFailureToFetchControlSocketCausesDeath) {
  auto os = std::make_unique<StrictMock<MockOs>>();
  auto command_processor = std::make_unique<StrictMock<MockCommandProcessor>>();
  ON_CALL(*os, GetControlSocket(kFakeSocketName))
      .WillByDefault(Return(std::tuple<size_t, Os::Errno>{-1, ERANGE}));
  EXPECT_DEATH(
      MainLoop(kFakeSocketName, std::move(os), std::move(command_processor)),
      "Failed to get control socket");
}

TEST_F(MainLoopDeathTest, RunOnceTerminatesOnUnexpectedError) {
  ON_CALL(*os_, ReceiveDatagram(_, _, protocol::kMaxMessageSize))
      .WillByDefault(Return(std::tuple<size_t, Os::Errno>{0, EFAULT}));
  EXPECT_DEATH(main_loop_->RunOnce(), "Unexpected error");
}

}  // namespace wifilogd
}  // namespace android