1 /*
2 * Copyright (C) 2019, 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 <android-base/logging.h>
18 #include <android-base/macros.h>
19 #include <cutils/properties.h>
20 #include <gmock/gmock.h>
21
22 #undef NAN // This is weird, NAN is defined in bionic/libc/include/math.h:38
23 #include "wifi_nan_iface.h"
24
25 #include "mock_interface_tool.h"
26 #include "mock_wifi_feature_flags.h"
27 #include "mock_wifi_iface_util.h"
28 #include "mock_wifi_legacy_hal.h"
29
30 using testing::NiceMock;
31 using testing::Return;
32 using testing::Test;
33
34 namespace {
35 constexpr char kIfaceName[] = "mockWlan0";
36 } // namespace
37
38 namespace android {
39 namespace hardware {
40 namespace wifi {
41 namespace V1_3 {
42 namespace implementation {
43
CaptureIfaceEventHandlers(const std::string &,iface_util::IfaceEventHandlers in_iface_event_handlers,iface_util::IfaceEventHandlers * out_iface_event_handlers)44 bool CaptureIfaceEventHandlers(
45 const std::string& /* iface_name*/,
46 iface_util::IfaceEventHandlers in_iface_event_handlers,
47 iface_util::IfaceEventHandlers* out_iface_event_handlers) {
48 *out_iface_event_handlers = in_iface_event_handlers;
49 return true;
50 }
51
52 class MockNanIfaceEventCallback : public IWifiNanIfaceEventCallback {
53 public:
54 MockNanIfaceEventCallback() = default;
55
56 MOCK_METHOD3(notifyCapabilitiesResponse,
57 Return<void>(uint16_t, const WifiNanStatus&,
58 const NanCapabilities&));
59 MOCK_METHOD2(notifyEnableResponse,
60 Return<void>(uint16_t, const WifiNanStatus&));
61 MOCK_METHOD2(notifyConfigResponse,
62 Return<void>(uint16_t, const WifiNanStatus&));
63 MOCK_METHOD2(notifyDisableResponse,
64 Return<void>(uint16_t, const WifiNanStatus&));
65 MOCK_METHOD3(notifyStartPublishResponse,
66 Return<void>(uint16_t, const WifiNanStatus&, uint8_t));
67 MOCK_METHOD2(notifyStopPublishResponse,
68 Return<void>(uint16_t, const WifiNanStatus&));
69 MOCK_METHOD3(notifyStartSubscribeResponse,
70 Return<void>(uint16_t, const WifiNanStatus&, uint8_t));
71 MOCK_METHOD2(notifyStopSubscribeResponse,
72 Return<void>(uint16_t, const WifiNanStatus&));
73 MOCK_METHOD2(notifyTransmitFollowupResponse,
74 Return<void>(uint16_t, const WifiNanStatus&));
75 MOCK_METHOD2(notifyCreateDataInterfaceResponse,
76 Return<void>(uint16_t, const WifiNanStatus&));
77 MOCK_METHOD2(notifyDeleteDataInterfaceResponse,
78 Return<void>(uint16_t, const WifiNanStatus&));
79 MOCK_METHOD3(notifyInitiateDataPathResponse,
80 Return<void>(uint16_t, const WifiNanStatus&, uint32_t));
81 MOCK_METHOD2(notifyRespondToDataPathIndicationResponse,
82 Return<void>(uint16_t, const WifiNanStatus&));
83 MOCK_METHOD2(notifyTerminateDataPathResponse,
84 Return<void>(uint16_t, const WifiNanStatus&));
85 MOCK_METHOD1(eventClusterEvent, Return<void>(const NanClusterEventInd&));
86 MOCK_METHOD1(eventDisabled, Return<void>(const WifiNanStatus&));
87 MOCK_METHOD2(eventPublishTerminated,
88 Return<void>(uint8_t, const WifiNanStatus&));
89 MOCK_METHOD2(eventSubscribeTerminated,
90 Return<void>(uint8_t, const WifiNanStatus&));
91 MOCK_METHOD1(eventMatch, Return<void>(const NanMatchInd&));
92 MOCK_METHOD2(eventMatchExpired, Return<void>(uint8_t, uint32_t));
93 MOCK_METHOD1(eventFollowupReceived,
94 Return<void>(const NanFollowupReceivedInd&));
95 MOCK_METHOD2(eventTransmitFollowup,
96 Return<void>(uint16_t, const WifiNanStatus&));
97 MOCK_METHOD1(eventDataPathRequest,
98 Return<void>(const NanDataPathRequestInd&));
99 MOCK_METHOD1(eventDataPathConfirm,
100 Return<void>(const NanDataPathConfirmInd&));
101 MOCK_METHOD1(eventDataPathTerminated, Return<void>(uint32_t));
102 };
103
104 class WifiNanIfaceTest : public Test {
105 protected:
106 std::shared_ptr<NiceMock<wifi_system::MockInterfaceTool>> iface_tool_{
107 new NiceMock<wifi_system::MockInterfaceTool>};
108 std::shared_ptr<NiceMock<legacy_hal::MockWifiLegacyHal>> legacy_hal_{
109 new NiceMock<legacy_hal::MockWifiLegacyHal>(iface_tool_)};
110 std::shared_ptr<NiceMock<iface_util::MockWifiIfaceUtil>> iface_util_{
111 new NiceMock<iface_util::MockWifiIfaceUtil>(iface_tool_)};
112 };
113
TEST_F(WifiNanIfaceTest,IfacEventHandlers_OnStateToggleOffOn)114 TEST_F(WifiNanIfaceTest, IfacEventHandlers_OnStateToggleOffOn) {
115 iface_util::IfaceEventHandlers captured_iface_event_handlers = {};
116 EXPECT_CALL(*legacy_hal_,
117 nanRegisterCallbackHandlers(testing::_, testing::_))
118 .WillOnce(testing::Return(legacy_hal::WIFI_SUCCESS));
119 EXPECT_CALL(*iface_util_,
120 registerIfaceEventHandlers(testing::_, testing::_))
121 .WillOnce(testing::Invoke(
122 bind(CaptureIfaceEventHandlers, std::placeholders::_1,
123 std::placeholders::_2, &captured_iface_event_handlers)));
124 sp<WifiNanIface> nan_iface =
125 new WifiNanIface(kIfaceName, legacy_hal_, iface_util_);
126
127 // Register a mock nan event callback.
128 sp<NiceMock<MockNanIfaceEventCallback>> mock_event_callback{
129 new NiceMock<MockNanIfaceEventCallback>};
130 nan_iface->registerEventCallback(
131 mock_event_callback, [](const WifiStatus& status) {
132 ASSERT_EQ(WifiStatusCode::SUCCESS, status.code);
133 });
134 // Ensure that the eventDisabled() function in mock callback will be
135 // invoked.
136 WifiNanStatus expected_nan_status = {
137 NanStatusType::UNSUPPORTED_CONCURRENCY_NAN_DISABLED, ""};
138 EXPECT_CALL(*mock_event_callback, eventDisabled(expected_nan_status))
139 .Times(1);
140
141 // Trigger the iface state toggle callback.
142 captured_iface_event_handlers.on_state_toggle_off_on(kIfaceName);
143 }
144 } // namespace implementation
145 } // namespace V1_3
146 } // namespace wifi
147 } // namespace hardware
148 } // namespace android
149