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 struct App : public TestNanoapp {
45 uint32_t perms = NanoappPermissions::CHRE_PERMS_WIFI;
46
47 decltype(nanoappHandleEvent) *handleEvent =
48 [](uint32_t, uint16_t eventType, const void *eventData) {
49 static uint32_t cookie;
50
51 switch (eventType) {
52 case CHRE_EVENT_WIFI_ASYNC_RESULT: {
53 auto *event = static_cast<const chreAsyncResult *>(eventData);
54 if (event->success) {
55 TestEventQueueSingleton::get()->pushEvent(
56 CHRE_EVENT_WIFI_ASYNC_RESULT,
57 *(static_cast<const uint32_t *>(event->cookie)));
58 }
59 break;
60 }
61
62 case CHRE_EVENT_TEST_EVENT: {
63 auto event = static_cast<const TestEvent *>(eventData);
64 switch (event->type) {
65 case MONITORING_REQUEST:
66 auto request =
67 static_cast<const MonitoringRequest *>(event->data);
68 cookie = request->cookie;
69 bool success = chreWifiConfigureScanMonitorAsync(
70 request->enable, &cookie);
71 TestEventQueueSingleton::get()->pushEvent(MONITORING_REQUEST,
72 success);
73 }
74 }
75 }
76 };
77 };
78
79 auto app = loadNanoapp<App>();
80 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
81
82 MonitoringRequest request{.enable = true, .cookie = 0x123};
83 sendEventToNanoapp(app, MONITORING_REQUEST, request);
84 uint32_t cookie;
85 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
86 EXPECT_EQ(cookie, request.cookie);
87 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
88
89 request = {.enable = false, .cookie = 0x456};
90 sendEventToNanoapp(app, MONITORING_REQUEST, request);
91 bool success;
92 waitForEvent(MONITORING_REQUEST, &success);
93 EXPECT_TRUE(success);
94 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
95 EXPECT_EQ(cookie, request.cookie);
96 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
97 }
98
TEST_F(TestBase,WifiScanMonitoringDisabledOnUnload)99 TEST_F(TestBase, WifiScanMonitoringDisabledOnUnload) {
100 CREATE_CHRE_TEST_EVENT(MONITORING_REQUEST, 1);
101
102 struct MonitoringRequest {
103 bool enable;
104 uint32_t cookie;
105 };
106
107 struct App : public TestNanoapp {
108 uint32_t perms = NanoappPermissions::CHRE_PERMS_WIFI;
109
110 decltype(nanoappHandleEvent) *handleEvent =
111 [](uint32_t, uint16_t eventType, const void *eventData) {
112 static uint32_t cookie;
113
114 switch (eventType) {
115 case CHRE_EVENT_WIFI_ASYNC_RESULT: {
116 auto *event = static_cast<const chreAsyncResult *>(eventData);
117 if (event->success) {
118 TestEventQueueSingleton::get()->pushEvent(
119 CHRE_EVENT_WIFI_ASYNC_RESULT,
120 *(static_cast<const uint32_t *>(event->cookie)));
121 }
122 break;
123 }
124
125 case CHRE_EVENT_TEST_EVENT: {
126 auto event = static_cast<const TestEvent *>(eventData);
127 switch (event->type) {
128 case MONITORING_REQUEST:
129 auto request =
130 static_cast<const MonitoringRequest *>(event->data);
131 cookie = request->cookie;
132 bool success = chreWifiConfigureScanMonitorAsync(
133 request->enable, &cookie);
134 TestEventQueueSingleton::get()->pushEvent(MONITORING_REQUEST,
135 success);
136 }
137 }
138 }
139 };
140 };
141
142 auto app = loadNanoapp<App>();
143 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
144
145 MonitoringRequest request{.enable = true, .cookie = 0x123};
146 sendEventToNanoapp(app, MONITORING_REQUEST, request);
147 bool success;
148 waitForEvent(MONITORING_REQUEST, &success);
149 EXPECT_TRUE(success);
150 uint32_t cookie;
151 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
152 EXPECT_EQ(cookie, request.cookie);
153 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
154
155 unloadNanoapp(app);
156 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
157 }
158
TEST_F(TestBase,WifiScanMonitoringDisabledOnUnloadAndCanBeReEnabled)159 TEST_F(TestBase, WifiScanMonitoringDisabledOnUnloadAndCanBeReEnabled) {
160 CREATE_CHRE_TEST_EVENT(MONITORING_REQUEST, 1);
161
162 struct MonitoringRequest {
163 bool enable;
164 uint32_t cookie;
165 };
166
167 struct App : public TestNanoapp {
168 uint32_t perms = NanoappPermissions::CHRE_PERMS_WIFI;
169
170 decltype(nanoappHandleEvent) *handleEvent =
171 [](uint32_t, uint16_t eventType, const void *eventData) {
172 static uint32_t cookie;
173
174 switch (eventType) {
175 case CHRE_EVENT_WIFI_ASYNC_RESULT: {
176 auto *event = static_cast<const chreAsyncResult *>(eventData);
177 if (event->success) {
178 TestEventQueueSingleton::get()->pushEvent(
179 CHRE_EVENT_WIFI_ASYNC_RESULT,
180 *(static_cast<const uint32_t *>(event->cookie)));
181 }
182 break;
183 }
184
185 case CHRE_EVENT_TEST_EVENT: {
186 auto event = static_cast<const TestEvent *>(eventData);
187 switch (event->type) {
188 case MONITORING_REQUEST:
189 auto request =
190 static_cast<const MonitoringRequest *>(event->data);
191 cookie = request->cookie;
192 bool success = chreWifiConfigureScanMonitorAsync(
193 request->enable, &cookie);
194 TestEventQueueSingleton::get()->pushEvent(MONITORING_REQUEST,
195 success);
196 }
197 }
198 }
199 };
200 };
201
202 auto app = loadNanoapp<App>();
203 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
204
205 MonitoringRequest request{.enable = true, .cookie = 0x123};
206 sendEventToNanoapp(app, MONITORING_REQUEST, request);
207 bool success;
208 waitForEvent(MONITORING_REQUEST, &success);
209 EXPECT_TRUE(success);
210 uint32_t cookie;
211 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
212 EXPECT_EQ(cookie, request.cookie);
213 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
214
215 unloadNanoapp(app);
216 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
217
218 app = loadNanoapp<App>();
219 EXPECT_FALSE(chrePalWifiIsScanMonitoringActive());
220
221 request = {.enable = true, .cookie = 0x456};
222 sendEventToNanoapp(app, MONITORING_REQUEST, request);
223 waitForEvent(MONITORING_REQUEST, &success);
224 EXPECT_TRUE(success);
225 waitForEvent(CHRE_EVENT_WIFI_ASYNC_RESULT, &cookie);
226 EXPECT_EQ(cookie, request.cookie);
227 EXPECT_TRUE(chrePalWifiIsScanMonitoringActive());
228 }
229
230 } // namespace
231 } // namespace chre