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_6 {
42 namespace implementation {
43
44 using android::hardware::wifi::V1_2::NanDataPathConfirmInd;
45
CaptureIfaceEventHandlers(const std::string &,iface_util::IfaceEventHandlers in_iface_event_handlers,iface_util::IfaceEventHandlers * out_iface_event_handlers)46 bool CaptureIfaceEventHandlers(const std::string& /* iface_name*/,
47 iface_util::IfaceEventHandlers in_iface_event_handlers,
48 iface_util::IfaceEventHandlers* out_iface_event_handlers) {
49 *out_iface_event_handlers = in_iface_event_handlers;
50 return true;
51 }
52
53 class MockNanIfaceEventCallback : public V1_5::IWifiNanIfaceEventCallback {
54 public:
55 MockNanIfaceEventCallback() = default;
56
57 MOCK_METHOD3(notifyCapabilitiesResponse,
58 Return<void>(uint16_t, const WifiNanStatus&,
59 const android::hardware::wifi::V1_0::NanCapabilities&));
60 MOCK_METHOD2(notifyEnableResponse, Return<void>(uint16_t, const WifiNanStatus&));
61 MOCK_METHOD2(notifyConfigResponse, Return<void>(uint16_t, const WifiNanStatus&));
62 MOCK_METHOD2(notifyDisableResponse, Return<void>(uint16_t, const WifiNanStatus&));
63 MOCK_METHOD3(notifyStartPublishResponse, Return<void>(uint16_t, const WifiNanStatus&, uint8_t));
64 MOCK_METHOD2(notifyStopPublishResponse, Return<void>(uint16_t, const WifiNanStatus&));
65 MOCK_METHOD3(notifyStartSubscribeResponse,
66 Return<void>(uint16_t, const WifiNanStatus&, uint8_t));
67 MOCK_METHOD2(notifyStopSubscribeResponse, Return<void>(uint16_t, const WifiNanStatus&));
68 MOCK_METHOD2(notifyTransmitFollowupResponse, Return<void>(uint16_t, const WifiNanStatus&));
69 MOCK_METHOD2(notifyCreateDataInterfaceResponse, Return<void>(uint16_t, const WifiNanStatus&));
70 MOCK_METHOD2(notifyDeleteDataInterfaceResponse, Return<void>(uint16_t, const WifiNanStatus&));
71 MOCK_METHOD3(notifyInitiateDataPathResponse,
72 Return<void>(uint16_t, const WifiNanStatus&, uint32_t));
73 MOCK_METHOD2(notifyRespondToDataPathIndicationResponse,
74 Return<void>(uint16_t, const WifiNanStatus&));
75 MOCK_METHOD2(notifyTerminateDataPathResponse, Return<void>(uint16_t, const WifiNanStatus&));
76 MOCK_METHOD1(eventClusterEvent, Return<void>(const NanClusterEventInd&));
77 MOCK_METHOD1(eventDisabled, Return<void>(const WifiNanStatus&));
78 MOCK_METHOD2(eventPublishTerminated, Return<void>(uint8_t, const WifiNanStatus&));
79 MOCK_METHOD2(eventSubscribeTerminated, Return<void>(uint8_t, const WifiNanStatus&));
80 MOCK_METHOD1(eventMatch, Return<void>(const V1_0::NanMatchInd&));
81 MOCK_METHOD1(eventMatch_1_6, Return<void>(const NanMatchInd&));
82 MOCK_METHOD2(eventMatchExpired, Return<void>(uint8_t, uint32_t));
83 MOCK_METHOD1(eventFollowupReceived, Return<void>(const NanFollowupReceivedInd&));
84 MOCK_METHOD2(eventTransmitFollowup, Return<void>(uint16_t, const WifiNanStatus&));
85 MOCK_METHOD1(eventDataPathRequest, Return<void>(const NanDataPathRequestInd&));
86 MOCK_METHOD1(eventDataPathConfirm,
87 Return<void>(const android::hardware::wifi::V1_0::NanDataPathConfirmInd&));
88 MOCK_METHOD1(eventDataPathTerminated, Return<void>(uint32_t));
89 MOCK_METHOD1(eventDataPathConfirm_1_2,
90 Return<void>(const android::hardware::wifi::V1_2::NanDataPathConfirmInd&));
91 MOCK_METHOD1(eventDataPathConfirm_1_6, Return<void>(const NanDataPathConfirmInd&));
92 MOCK_METHOD1(eventDataPathScheduleUpdate,
93 Return<void>(const android::hardware::wifi::V1_2::NanDataPathScheduleUpdateInd&));
94 MOCK_METHOD1(eventDataPathScheduleUpdate_1_6,
95 Return<void>(const NanDataPathScheduleUpdateInd&));
96 MOCK_METHOD3(notifyCapabilitiesResponse_1_5,
97 Return<void>(uint16_t, const WifiNanStatus&, const V1_5::NanCapabilities&));
98 };
99
100 class WifiNanIfaceTest : public Test {
101 protected:
102 legacy_hal::wifi_hal_fn fake_func_table_;
103 std::shared_ptr<NiceMock<wifi_system::MockInterfaceTool>> iface_tool_{
104 new NiceMock<wifi_system::MockInterfaceTool>};
105 std::shared_ptr<NiceMock<legacy_hal::MockWifiLegacyHal>> legacy_hal_{
106 new NiceMock<legacy_hal::MockWifiLegacyHal>(iface_tool_, fake_func_table_, true)};
107 std::shared_ptr<NiceMock<iface_util::MockWifiIfaceUtil>> iface_util_{
108 new NiceMock<iface_util::MockWifiIfaceUtil>(iface_tool_, legacy_hal_)};
109 };
110
TEST_F(WifiNanIfaceTest,IfacEventHandlers_OnStateToggleOffOn)111 TEST_F(WifiNanIfaceTest, IfacEventHandlers_OnStateToggleOffOn) {
112 iface_util::IfaceEventHandlers captured_iface_event_handlers = {};
113 EXPECT_CALL(*legacy_hal_, nanRegisterCallbackHandlers(testing::_, testing::_))
114 .WillOnce(testing::Return(legacy_hal::WIFI_SUCCESS));
115 EXPECT_CALL(*iface_util_, registerIfaceEventHandlers(testing::_, testing::_))
116 .WillOnce(testing::Invoke(bind(CaptureIfaceEventHandlers, std::placeholders::_1,
117 std::placeholders::_2, &captured_iface_event_handlers)));
118 sp<WifiNanIface> nan_iface = new WifiNanIface(kIfaceName, false, legacy_hal_, iface_util_);
119
120 // Register a mock nan event callback.
121 sp<NiceMock<MockNanIfaceEventCallback>> mock_event_callback{
122 new NiceMock<MockNanIfaceEventCallback>};
123 nan_iface->registerEventCallback(mock_event_callback, [](const WifiStatus& status) {
124 ASSERT_EQ(WifiStatusCode::SUCCESS, status.code);
125 });
126 // Ensure that the eventDisabled() function in mock callback will be
127 // invoked.
128 WifiNanStatus expected_nan_status = {NanStatusType::UNSUPPORTED_CONCURRENCY_NAN_DISABLED, ""};
129 EXPECT_CALL(*mock_event_callback, eventDisabled(expected_nan_status)).Times(1);
130
131 // Trigger the iface state toggle callback.
132 captured_iface_event_handlers.on_state_toggle_off_on(kIfaceName);
133 }
134 } // namespace implementation
135 } // namespace V1_6
136 } // namespace wifi
137 } // namespace hardware
138 } // namespace android
139