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