main_loop_unittest.cpp 4.9 KB

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