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