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 "chre_api/chre/wifi.h"
18 #include <cstdint>
19 #include "chre/core/event_loop_manager.h"
20 #include "chre/core/settings.h"
21 #include "chre/platform/linux/pal_nan.h"
22 #include "chre/platform/linux/pal_wifi.h"
23 #include "chre/platform/log.h"
24 #include "chre/util/system/napp_permissions.h"
25 #include "chre_api/chre/event.h"
26
27 #include "gtest/gtest.h"
28 #include "test_base.h"
29 #include "test_event.h"
30 #include "test_event_queue.h"
31 #include "test_util.h"
32
33 namespace chre {
34 namespace {
35
TEST_F(TestBase,WifiCanSubscribeAndUnsubscribeToScanMonitoring)36 TEST_F(TestBase, WifiCanSubscribeAndUnsubscribeToScanMonitoring) {
37 CREATE_CHRE_TEST_EVENT(MONITORING_REQUEST, 0);
38
39 struct MonitoringRequest {
40 bool enable;
41 uint32_t cookie;
42 };
43
44 class App : public TestNanoapp {
45 public:
46 App()
47 : TestNanoapp(
48 TestNanoappInfo{.perms = NanoappPermissions::CHRE_PERMS_WIFI}) {}
49
50 void handleEvent(uint32_t, uint16_t eventType,
51 const void *eventData) override {
52 switch (eventType) {
53 case CHRE_EVENT_WIFI_ASYNC_RESULT: {
54 auto *event = static_cast<const chreAsyncResult *>(eventData);
55 if (event->success) {
56 TestEventQueueSingleton::get()->pushEvent(
57 CHRE_EVENT_WIFI_ASYNC_RESULT,
58 *(static_cast<const uint32_t *>(event->cookie)));
59 }
60 break;
61 }
62
63 case CHRE_EVENT_TEST_EVENT: {
64 auto event = static_cast<const TestEvent *>(eventData);
65 switch (event->type) {
66 case MONITORING_REQUEST:
67 auto request =
68 static_cast<const MonitoringRequest *>(event->data);
69 mCookie = request->cookie;
70 bool success =
71 chreWifiConfigureScanMonitorAsync(request->enable, &mCookie);
72 TestEventQueueSingleton::get()->pushEvent(MONITORING_REQUEST,
73 success);
74 }
75 }
76 }
77 }
78
79 protected:
80 uint32_t mCookie;
81 };
82
83 uint64_t appId = loadNanoapp(MakeUnique<App>());
84
85 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
86
87 MonitoringRequest request{.enable = true, .cookie = 0x123};
88 sendEventToNanoapp(appId, MONITORING_REQUEST, request);
89 uint32_t cookie;
90 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
91 EXPECT_EQ(cookie, request.cookie);
92 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
93
94 request = {.enable = false, .cookie = 0x456};
95 sendEventToNanoapp(appId, MONITORING_REQUEST, request);
96 bool success;
97 waitForEvent(MONITORING_REQUEST, &success);
98 EXPECT_TRUE(success);
99 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
100 EXPECT_EQ(cookie, request.cookie);
101 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
102 }
103
TEST_F(TestBase,WifiScanMonitoringDisabledOnUnload)104 TEST_F(TestBase, WifiScanMonitoringDisabledOnUnload) {
105 CREATE_CHRE_TEST_EVENT(MONITORING_REQUEST, 1);
106
107 struct MonitoringRequest {
108 bool enable;
109 uint32_t cookie;
110 };
111
112 class App : public TestNanoapp {
113 public:
114 App()
115 : TestNanoapp(
116 TestNanoappInfo{.perms = NanoappPermissions::CHRE_PERMS_WIFI}) {}
117
118 void handleEvent(uint32_t, uint16_t eventType,
119 const void *eventData) override {
120 switch (eventType) {
121 case CHRE_EVENT_WIFI_ASYNC_RESULT: {
122 auto *event = static_cast<const chreAsyncResult *>(eventData);
123 if (event->success) {
124 TestEventQueueSingleton::get()->pushEvent(
125 CHRE_EVENT_WIFI_ASYNC_RESULT,
126 *(static_cast<const uint32_t *>(event->cookie)));
127 }
128 break;
129 }
130
131 case CHRE_EVENT_TEST_EVENT: {
132 auto event = static_cast<const TestEvent *>(eventData);
133 switch (event->type) {
134 case MONITORING_REQUEST:
135 auto request =
136 static_cast<const MonitoringRequest *>(event->data);
137 mCookie = request->cookie;
138 bool success =
139 chreWifiConfigureScanMonitorAsync(request->enable, &mCookie);
140 TestEventQueueSingleton::get()->pushEvent(MONITORING_REQUEST,
141 success);
142 }
143 }
144 }
145 }
146
147 protected:
148 uint32_t mCookie;
149 };
150
151 uint64_t appId = loadNanoapp(MakeUnique<App>());
152
153 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
154
155 MonitoringRequest request{.enable = true, .cookie = 0x123};
156 sendEventToNanoapp(appId, MONITORING_REQUEST, request);
157 bool success;
158 waitForEvent(MONITORING_REQUEST, &success);
159 EXPECT_TRUE(success);
160 uint32_t cookie;
161 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
162 EXPECT_EQ(cookie, request.cookie);
163 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
164
165 unloadNanoapp(appId);
166 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
167 }
168
TEST_F(TestBase,WifiScanMonitoringDisabledOnUnloadAndCanBeReEnabled)169 TEST_F(TestBase, WifiScanMonitoringDisabledOnUnloadAndCanBeReEnabled) {
170 CREATE_CHRE_TEST_EVENT(MONITORING_REQUEST, 1);
171
172 struct MonitoringRequest {
173 bool enable;
174 uint32_t cookie;
175 };
176
177 class App : public TestNanoapp {
178 public:
179 App()
180 : TestNanoapp(
181 TestNanoappInfo{.perms = NanoappPermissions::CHRE_PERMS_WIFI}) {}
182
183 void handleEvent(uint32_t, uint16_t eventType,
184 const void *eventData) override {
185 switch (eventType) {
186 case CHRE_EVENT_WIFI_ASYNC_RESULT: {
187 auto *event = static_cast<const chreAsyncResult *>(eventData);
188 if (event->success) {
189 TestEventQueueSingleton::get()->pushEvent(
190 CHRE_EVENT_WIFI_ASYNC_RESULT,
191 *(static_cast<const uint32_t *>(event->cookie)));
192 }
193 break;
194 }
195
196 case CHRE_EVENT_TEST_EVENT: {
197 auto event = static_cast<const TestEvent *>(eventData);
198 switch (event->type) {
199 case MONITORING_REQUEST:
200 auto request =
201 static_cast<const MonitoringRequest *>(event->data);
202 mCookie = request->cookie;
203 bool success =
204 chreWifiConfigureScanMonitorAsync(request->enable, &mCookie);
205 TestEventQueueSingleton::get()->pushEvent(MONITORING_REQUEST,
206 success);
207 }
208 }
209 }
210 }
211
212 protected:
213 uint32_t mCookie;
214 };
215
216 uint64_t appId = loadNanoapp(MakeUnique<App>());
217
218 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
219
220 MonitoringRequest request{.enable = true, .cookie = 0x123};
221 sendEventToNanoapp(appId, MONITORING_REQUEST, request);
222 bool success;
223 waitForEvent(MONITORING_REQUEST, &success);
224 EXPECT_TRUE(success);
225 uint32_t cookie;
226 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
227 EXPECT_EQ(cookie, request.cookie);
228 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
229
230 unloadNanoapp(appId);
231 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
232
233 appId = loadNanoapp(MakeUnique<App>());
234 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
235
236 request = {.enable = true, .cookie = 0x456};
237 sendEventToNanoapp(appId, MONITORING_REQUEST, request);
238 waitForEvent(MONITORING_REQUEST, &success);
239 EXPECT_TRUE(success);
240 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
241 EXPECT_EQ(cookie, request.cookie);
242 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
243 }
244
245 } // namespace
246 } // namespace chre