1 /*
2 * Copyright (C) 2022 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 #include "wifi_nan_iface.h"
23
24 #include "mock_interface_tool.h"
25 #include "mock_wifi_feature_flags.h"
26 #include "mock_wifi_iface_util.h"
27 #include "mock_wifi_legacy_hal.h"
28
29 using testing::NiceMock;
30 using testing::Return;
31 using testing::Test;
32
33 namespace {
34 constexpr char kIfaceName[] = "mockWlan0";
35 } // namespace
36
37 namespace aidl {
38 namespace android {
39 namespace hardware {
40 namespace wifi {
41
CaptureIfaceEventHandlers(const std::string &,iface_util::IfaceEventHandlers in_iface_event_handlers,iface_util::IfaceEventHandlers * out_iface_event_handlers)42 bool CaptureIfaceEventHandlers(const std::string& /* iface_name*/,
43 iface_util::IfaceEventHandlers in_iface_event_handlers,
44 iface_util::IfaceEventHandlers* out_iface_event_handlers) {
45 *out_iface_event_handlers = in_iface_event_handlers;
46 return true;
47 }
48
49 class MockNanIface : public WifiNanIface {
50 public:
MockNanIface(const std::string & ifname,bool is_dedicated_iface,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)51 MockNanIface(const std::string& ifname, bool is_dedicated_iface,
52 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
53 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)
54 : WifiNanIface(ifname, is_dedicated_iface, legacy_hal, iface_util) {}
55
createMock(const std::string & ifname,bool is_dedicated_iface,const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)56 static std::shared_ptr<MockNanIface> createMock(
57 const std::string& ifname, bool is_dedicated_iface,
58 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
59 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util) {
60 std::shared_ptr<MockNanIface> ptr = ndk::SharedRefBase::make<MockNanIface>(
61 ifname, is_dedicated_iface, legacy_hal, iface_util);
62 std::weak_ptr<MockNanIface> weak_ptr_this(ptr);
63 ptr->setWeakPtr(weak_ptr_this);
64 ptr->registerCallbackHandlers();
65 return ptr;
66 }
67
68 // Override getEventCallbacks() so that we can return a mocked callback object.
getEventCallbacks()69 std::set<std::shared_ptr<IWifiNanIfaceEventCallback>> getEventCallbacks() override {
70 return {callback_};
71 }
72
setMockCallback(std::shared_ptr<IWifiNanIfaceEventCallback> cb)73 void setMockCallback(std::shared_ptr<IWifiNanIfaceEventCallback> cb) { callback_ = cb; }
74
75 private:
76 std::shared_ptr<IWifiNanIfaceEventCallback> callback_;
77 };
78
79 class MockNanIfaceEventCallback : public IWifiNanIfaceEventCallback {
80 public:
asBinder()81 ndk::SpAIBinder asBinder() override { return ::ndk::SpAIBinder{}; }
isRemote()82 bool isRemote() override { return false; }
83
getInterfaceVersion(int32_t * _aidl_return)84 ::ndk::ScopedAStatus getInterfaceVersion(int32_t* _aidl_return) override {
85 *_aidl_return = 1;
86 return ndk::ScopedAStatus::ok();
87 }
getInterfaceHash(std::string * _aidl_return)88 ::ndk::ScopedAStatus getInterfaceHash(std::string* _aidl_return) override {
89 *_aidl_return = "some_hash";
90 return ndk::ScopedAStatus::ok();
91 }
92
93 MOCK_METHOD3(notifyCapabilitiesResponse,
94 ndk::ScopedAStatus(char16_t, const NanStatus&, const NanCapabilities&));
95 MOCK_METHOD2(notifyEnableResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
96 MOCK_METHOD2(notifyConfigResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
97 MOCK_METHOD2(notifyDisableResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
98 MOCK_METHOD3(notifyStartPublishResponse,
99 ndk::ScopedAStatus(char16_t, const NanStatus&, int8_t));
100 MOCK_METHOD2(notifyStopPublishResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
101 MOCK_METHOD3(notifyStartSubscribeResponse,
102 ndk::ScopedAStatus(char16_t, const NanStatus&, int8_t));
103 MOCK_METHOD2(notifyStopSubscribeResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
104 MOCK_METHOD2(notifyTransmitFollowupResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
105 MOCK_METHOD2(notifyCreateDataInterfaceResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
106 MOCK_METHOD2(notifyDeleteDataInterfaceResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
107 MOCK_METHOD3(notifyInitiateDataPathResponse,
108 ndk::ScopedAStatus(char16_t, const NanStatus&, int32_t));
109 MOCK_METHOD2(notifyRespondToDataPathIndicationResponse,
110 ndk::ScopedAStatus(char16_t, const NanStatus&));
111 MOCK_METHOD2(notifyTerminateDataPathResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
112 MOCK_METHOD1(eventClusterEvent, ndk::ScopedAStatus(const NanClusterEventInd&));
113 MOCK_METHOD1(eventDisabled, ndk::ScopedAStatus(const NanStatus&));
114 MOCK_METHOD2(eventPublishTerminated, ndk::ScopedAStatus(int8_t, const NanStatus&));
115 MOCK_METHOD2(eventSubscribeTerminated, ndk::ScopedAStatus(int8_t, const NanStatus&));
116 MOCK_METHOD1(eventMatch, ndk::ScopedAStatus(const NanMatchInd&));
117 MOCK_METHOD2(eventMatchExpired, ndk::ScopedAStatus(int8_t, int32_t));
118 MOCK_METHOD1(eventFollowupReceived, ndk::ScopedAStatus(const NanFollowupReceivedInd&));
119 MOCK_METHOD2(eventTransmitFollowup, ndk::ScopedAStatus(char16_t, const NanStatus&));
120 MOCK_METHOD1(eventDataPathRequest, ndk::ScopedAStatus(const NanDataPathRequestInd&));
121 MOCK_METHOD1(eventDataPathConfirm, ndk::ScopedAStatus(const NanDataPathConfirmInd&));
122 MOCK_METHOD1(eventDataPathTerminated, ndk::ScopedAStatus(int32_t));
123 MOCK_METHOD1(eventDataPathScheduleUpdate,
124 ndk::ScopedAStatus(const NanDataPathScheduleUpdateInd&));
125 MOCK_METHOD1(eventPairingConfirm, ndk::ScopedAStatus(const NanPairingConfirmInd&));
126 MOCK_METHOD1(eventPairingRequest, ndk::ScopedAStatus(const NanPairingRequestInd&));
127 MOCK_METHOD1(eventBootstrappingConfirm, ndk::ScopedAStatus(const NanBootstrappingConfirmInd&));
128 MOCK_METHOD1(eventBootstrappingRequest, ndk::ScopedAStatus(const NanBootstrappingRequestInd&));
129 MOCK_METHOD3(notifyInitiatePairingResponse,
130 ndk::ScopedAStatus(char16_t, const NanStatus&, int32_t));
131 MOCK_METHOD2(notifyRespondToPairingIndicationResponse,
132 ndk::ScopedAStatus(char16_t, const NanStatus&));
133 MOCK_METHOD3(notifyInitiateBootstrappingResponse,
134 ndk::ScopedAStatus(char16_t, const NanStatus&, int32_t));
135 MOCK_METHOD2(notifyRespondToBootstrappingIndicationResponse,
136 ndk::ScopedAStatus(char16_t, const NanStatus&));
137 MOCK_METHOD2(notifySuspendResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
138 MOCK_METHOD2(notifyResumeResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
139 MOCK_METHOD2(notifyTerminatePairingResponse, ndk::ScopedAStatus(char16_t, const NanStatus&));
140 MOCK_METHOD1(eventSuspensionModeChanged, ndk::ScopedAStatus(const NanSuspensionModeChangeInd&));
141 };
142
143 class WifiNanIfaceTest : public Test {
144 protected:
145 legacy_hal::wifi_hal_fn fake_func_table_;
146 std::shared_ptr<NiceMock<::android::wifi_system::MockInterfaceTool>> iface_tool_{
147 new NiceMock<::android::wifi_system::MockInterfaceTool>};
148 std::shared_ptr<NiceMock<legacy_hal::MockWifiLegacyHal>> legacy_hal_{
149 new NiceMock<legacy_hal::MockWifiLegacyHal>(iface_tool_, fake_func_table_, true)};
150 std::shared_ptr<NiceMock<iface_util::MockWifiIfaceUtil>> iface_util_{
151 new NiceMock<iface_util::MockWifiIfaceUtil>(iface_tool_, legacy_hal_)};
152 };
153
TEST_F(WifiNanIfaceTest,IfacEventHandlers_OnStateToggleOffOn)154 TEST_F(WifiNanIfaceTest, IfacEventHandlers_OnStateToggleOffOn) {
155 // Ensure that event handlers are registered during nan iface creation.
156 iface_util::IfaceEventHandlers captured_iface_event_handlers = {};
157 EXPECT_CALL(*legacy_hal_, nanRegisterCallbackHandlers(testing::_, testing::_))
158 .WillOnce(testing::Return(legacy_hal::WIFI_SUCCESS));
159 EXPECT_CALL(*iface_util_, registerIfaceEventHandlers(testing::_, testing::_))
160 .WillOnce(testing::Invoke(bind(CaptureIfaceEventHandlers, std::placeholders::_1,
161 std::placeholders::_2, &captured_iface_event_handlers)));
162
163 // Create nan iface and register a callback.
164 // Note: Since we can't register a callback directly (gTest fails on
165 // AIBinder_linkToDeath), simulate the registration by overriding
166 // getEventCallbacks() to return our mock callback object.
167 std::shared_ptr<MockNanIface> mock_nan_iface =
168 MockNanIface::createMock(kIfaceName, false, legacy_hal_, iface_util_);
169 std::shared_ptr<MockNanIfaceEventCallback> mock_event_callback =
170 ndk::SharedRefBase::make<MockNanIfaceEventCallback>();
171 mock_nan_iface->setMockCallback(mock_event_callback);
172
173 // Ensure that the eventDisabled() function in the mock callback will be invoked.
174 NanStatus expected_nan_status = {NanStatusCode::UNSUPPORTED_CONCURRENCY_NAN_DISABLED, ""};
175 EXPECT_CALL(*mock_event_callback, eventDisabled(expected_nan_status)).Times(1);
176
177 // Trigger the iface state toggle callback.
178 captured_iface_event_handlers.on_state_toggle_off_on(kIfaceName);
179 }
180
181 } // namespace wifi
182 } // namespace hardware
183 } // namespace android
184 } // namespace aidl
185