1 /*
2 * Copyright (C) 2020 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 <gtest/gtest.h>
18
19 #include <stddef.h>
20 #include <cstring>
21
22 #include "chpp/common/wifi_types.h"
23 #include "chpp/memory.h"
24 #include "chre/test/common/macros.h"
25
26 namespace {
27
validateScanResult(const ChppWifiScanResult & chppAp,const chreWifiScanResult & chreAp,bool decodeMode)28 void validateScanResult(const ChppWifiScanResult &chppAp,
29 const chreWifiScanResult &chreAp, bool decodeMode) {
30 EXPECT_EQ(chppAp.ageMs, chreAp.ageMs);
31 EXPECT_EQ(chppAp.capabilityInfo, chreAp.capabilityInfo);
32 EXPECT_EQ(chppAp.ssidLen, chreAp.ssidLen);
33 EXPECT_EQ(std::memcmp(chppAp.ssid, chreAp.ssid, sizeof(chppAp.ssid)), 0);
34 EXPECT_EQ(std::memcmp(chppAp.bssid, chreAp.bssid, sizeof(chppAp.bssid)), 0);
35 EXPECT_EQ(chppAp.flags, chreAp.flags);
36 EXPECT_EQ(chppAp.rssi, chreAp.rssi);
37 EXPECT_EQ(chppAp.band, chreAp.band);
38 EXPECT_EQ(chppAp.primaryChannel, chreAp.primaryChannel);
39 EXPECT_EQ(chppAp.centerFreqPrimary, chreAp.centerFreqPrimary);
40 EXPECT_EQ(chppAp.centerFreqSecondary, chreAp.centerFreqSecondary);
41 EXPECT_EQ(chppAp.channelWidth, chreAp.channelWidth);
42 EXPECT_EQ(chppAp.securityMode, chreAp.securityMode);
43 EXPECT_EQ(chppAp.radioChain, chreAp.radioChain);
44 EXPECT_EQ(chppAp.rssiChain0, chreAp.rssiChain0);
45 EXPECT_EQ(chppAp.rssiChain1, chreAp.rssiChain1);
46
47 for (size_t i = 0;
48 i < (decodeMode ? sizeof(chreAp.reserved) : sizeof(chppAp.reserved));
49 i++) {
50 SCOPED_TRACE(i);
51 EXPECT_EQ((decodeMode ? chreAp.reserved[i] : chppAp.reserved[i]), 0);
52 }
53 }
54
validateScanEvent(const chreWifiScanEvent & chreEvent)55 void validateScanEvent(const chreWifiScanEvent &chreEvent) {
56 ChppWifiScanEventWithHeader *chppWithHeader = nullptr;
57 size_t outputSize = 999;
58
59 // Encode
60 bool result =
61 chppWifiScanEventFromChre(&chreEvent, &chppWithHeader, &outputSize);
62 ASSERT_TRUE(result);
63 ASSERT_NE(chppWithHeader, nullptr);
64
65 size_t expectedSize = sizeof(ChppWifiScanEventWithHeader) +
66 chreEvent.scannedFreqListLen * sizeof(uint32_t) +
67 chreEvent.resultCount * sizeof(ChppWifiScanResult);
68 EXPECT_EQ(outputSize, expectedSize);
69
70 ChppWifiScanEvent *chppEvent = &chppWithHeader->payload;
71
72 // Decode
73 outputSize -= sizeof(struct ChppAppHeader);
74 chreWifiScanEvent *backEvent = chppWifiScanEventToChre(chppEvent, outputSize);
75 ASSERT_NE(backEvent, nullptr);
76
77 // Compare chreEvent against encoded (chppEvent) and decoded back (backEvent)
78
79 EXPECT_EQ(chppEvent->version, CHRE_WIFI_SCAN_EVENT_VERSION);
80 EXPECT_EQ(chppEvent->resultCount, chreEvent.resultCount);
81 EXPECT_EQ(chppEvent->resultTotal, chreEvent.resultTotal);
82 EXPECT_EQ(chppEvent->resultCount, chreEvent.resultCount);
83 EXPECT_EQ(chppEvent->eventIndex, chreEvent.eventIndex);
84 EXPECT_EQ(chppEvent->scanType, chreEvent.scanType);
85 EXPECT_EQ(chppEvent->ssidSetSize, chreEvent.ssidSetSize);
86 EXPECT_EQ(chppEvent->scannedFreqListLen, chreEvent.scannedFreqListLen);
87 EXPECT_EQ(chppEvent->referenceTime, chreEvent.referenceTime);
88 EXPECT_EQ(chppEvent->radioChainPref, chreEvent.radioChainPref);
89
90 EXPECT_EQ(backEvent->version, CHRE_WIFI_SCAN_EVENT_VERSION);
91 EXPECT_EQ(backEvent->resultCount, chreEvent.resultCount);
92 EXPECT_EQ(backEvent->resultTotal, chreEvent.resultTotal);
93 EXPECT_EQ(backEvent->resultCount, chreEvent.resultCount);
94 EXPECT_EQ(backEvent->eventIndex, chreEvent.eventIndex);
95 EXPECT_EQ(backEvent->scanType, chreEvent.scanType);
96 EXPECT_EQ(backEvent->ssidSetSize, chreEvent.ssidSetSize);
97 EXPECT_EQ(backEvent->scannedFreqListLen, chreEvent.scannedFreqListLen);
98 EXPECT_EQ(backEvent->referenceTime, chreEvent.referenceTime);
99 EXPECT_EQ(backEvent->radioChainPref, chreEvent.radioChainPref);
100
101 uint16_t baseOffset = sizeof(ChppWifiScanEvent);
102 if (chreEvent.scannedFreqListLen > 0) {
103 EXPECT_EQ(chppEvent->scannedFreqList.offset, baseOffset);
104 EXPECT_EQ(chppEvent->scannedFreqList.length,
105 chppEvent->scannedFreqListLen * sizeof(uint32_t));
106 baseOffset += chppEvent->scannedFreqList.length;
107
108 auto *chppScannedFreqList =
109 ((const uint8_t *)chppEvent + chppEvent->scannedFreqList.offset);
110 for (size_t i = 0; i < chppEvent->scannedFreqListLen; i++) {
111 uint32_t currScannedFreq;
112 memcpy(&currScannedFreq, chppScannedFreqList + (i * sizeof(uint32_t)),
113 sizeof(uint32_t));
114 SCOPED_TRACE(i);
115 EXPECT_EQ(currScannedFreq, chreEvent.scannedFreqList[i]);
116 EXPECT_EQ(currScannedFreq, backEvent->scannedFreqList[i]);
117 }
118 } else {
119 EXPECT_EQ(chppEvent->scannedFreqList.offset, 0);
120 EXPECT_EQ(chppEvent->scannedFreqList.length, 0);
121 }
122
123 if (chreEvent.resultCount > 0) {
124 EXPECT_EQ(chppEvent->results.offset, baseOffset);
125 EXPECT_EQ(chppEvent->results.length,
126 chppEvent->resultCount * sizeof(ChppWifiScanResult));
127 baseOffset += chppEvent->results.length;
128
129 const ChppWifiScanResult *chppAp =
130 (const ChppWifiScanResult *)((const uint8_t *)chppEvent +
131 chppEvent->results.offset);
132 for (size_t i = 0; i < chppEvent->resultCount; i++) {
133 SCOPED_TRACE(::testing::Message() << "Scan result index " << i);
134 validateScanResult(chppAp[i], chreEvent.results[i], /*decodeMode=*/false);
135 validateScanResult(chppAp[i], backEvent->results[i], /*decodeMode=*/true);
136 }
137 } else {
138 EXPECT_EQ(chppEvent->results.offset, 0);
139 EXPECT_EQ(chppEvent->results.length, 0);
140 }
141
142 // Handling of short input
143 chreWifiScanEvent *chreMalformed;
144 chreMalformed = chppWifiScanEventToChre(chppEvent, outputSize - 1);
145 ASSERT_EQ(chreMalformed, nullptr);
146
147 chppFree(chppWithHeader);
148 chppFree(backEvent);
149 }
150
validateScanParams(const chreWifiScanParams & chreParams)151 void validateScanParams(const chreWifiScanParams &chreParams) {
152 ChppWifiScanParamsWithHeader *chppWithHeader = nullptr;
153 size_t outputSize = 999;
154
155 // Encode
156 bool result =
157 chppWifiScanParamsFromChre(&chreParams, &chppWithHeader, &outputSize);
158 ASSERT_TRUE(result);
159 ASSERT_NE(chppWithHeader, nullptr);
160
161 size_t expectedSize = sizeof(ChppWifiScanParamsWithHeader) +
162 chreParams.frequencyListLen * sizeof(uint32_t) +
163 chreParams.ssidListLen * sizeof(ChppWifiSsidListItem);
164 EXPECT_EQ(outputSize, expectedSize);
165
166 ChppWifiScanParams *chppParams = &chppWithHeader->payload;
167
168 // Decode
169 outputSize -= sizeof(struct ChppAppHeader);
170 chreWifiScanParams *backParams =
171 chppWifiScanParamsToChre(chppParams, outputSize);
172 ASSERT_NE(backParams, nullptr);
173
174 // Compare chreEvent against encoded (chppEvent) and decoded back (backEvent)
175
176 EXPECT_EQ(chppParams->scanType, chreParams.scanType);
177 EXPECT_EQ(chppParams->maxScanAgeMs, chreParams.maxScanAgeMs);
178 EXPECT_EQ(chppParams->frequencyListLen, chreParams.frequencyListLen);
179 EXPECT_EQ(chppParams->ssidListLen, chreParams.ssidListLen);
180 EXPECT_EQ(chppParams->radioChainPref, chreParams.radioChainPref);
181
182 EXPECT_EQ(backParams->scanType, chreParams.scanType);
183 EXPECT_EQ(backParams->maxScanAgeMs, chreParams.maxScanAgeMs);
184 EXPECT_EQ(backParams->frequencyListLen, chreParams.frequencyListLen);
185 EXPECT_EQ(backParams->ssidListLen, chreParams.ssidListLen);
186 EXPECT_EQ(backParams->radioChainPref, chreParams.radioChainPref);
187
188 uint16_t baseOffset = sizeof(ChppWifiScanParams);
189 if (chreParams.frequencyListLen > 0) {
190 EXPECT_EQ(chppParams->frequencyList.offset, baseOffset);
191 EXPECT_EQ(chppParams->frequencyList.length,
192 chppParams->frequencyListLen * sizeof(uint32_t));
193 baseOffset += chppParams->frequencyList.length;
194
195 auto *chppFrequencyList =
196 ((const uint8_t *)chppParams + chppParams->frequencyList.offset);
197 for (size_t i = 0; i < chppParams->frequencyListLen; i++) {
198 uint32_t currScannedFreq;
199 memcpy(&currScannedFreq, chppFrequencyList + (i * sizeof(uint32_t)),
200 sizeof(uint32_t));
201 SCOPED_TRACE(i);
202 EXPECT_EQ(currScannedFreq, chreParams.frequencyList[i]);
203 EXPECT_EQ(currScannedFreq, backParams->frequencyList[i]);
204 }
205 } else {
206 EXPECT_EQ(chppParams->frequencyList.offset, 0);
207 EXPECT_EQ(chppParams->frequencyList.length, 0);
208 }
209
210 if (chreParams.ssidListLen > 0) {
211 EXPECT_EQ(chppParams->ssidList.offset, baseOffset);
212 EXPECT_EQ(chppParams->ssidList.length,
213 chppParams->ssidListLen * sizeof(ChppWifiSsidListItem));
214 baseOffset += chppParams->ssidList.length;
215
216 const ChppWifiSsidListItem *chppSsidList =
217 (const ChppWifiSsidListItem *)((const uint8_t *)chppParams +
218 chppParams->ssidList.offset);
219 for (size_t i = 0; i < chppParams->ssidListLen; i++) {
220 SCOPED_TRACE(i);
221 EXPECT_EQ(chppSsidList[i].ssidLen, chreParams.ssidList[i].ssidLen);
222 EXPECT_EQ(std::memcmp(chppSsidList[i].ssid, chreParams.ssidList[i].ssid,
223 sizeof(chppSsidList[i].ssid)),
224 0);
225 EXPECT_EQ(chppSsidList[i].ssidLen, backParams->ssidList[i].ssidLen);
226 EXPECT_EQ(std::memcmp(chppSsidList[i].ssid, backParams->ssidList[i].ssid,
227 sizeof(chppSsidList[i].ssid)),
228 0);
229 }
230 } else {
231 EXPECT_EQ(chppParams->ssidList.offset, 0);
232 EXPECT_EQ(chppParams->ssidList.length, 0);
233 }
234
235 // Handling of short input
236 chreWifiScanParams *chreMalformed;
237 chreMalformed = chppWifiScanParamsToChre(chppParams, outputSize - 1);
238 ASSERT_EQ(chreMalformed, nullptr);
239
240 chppFree(chppWithHeader);
241 chppFree(backParams);
242 }
243
validateRangingParams(const chreWifiRangingParams & chreParams)244 void validateRangingParams(const chreWifiRangingParams &chreParams) {
245 ChppWifiRangingParamsWithHeader *chppWithHeader = nullptr;
246 size_t outputSize = 999;
247
248 // Encode
249 bool result =
250 chppWifiRangingParamsFromChre(&chreParams, &chppWithHeader, &outputSize);
251 ASSERT_TRUE(result);
252 ASSERT_NE(chppWithHeader, nullptr);
253
254 size_t expectedSize =
255 sizeof(ChppWifiRangingParamsWithHeader) +
256 chreParams.targetListLen * sizeof(ChppWifiRangingTarget);
257 EXPECT_EQ(outputSize, expectedSize);
258
259 ChppWifiRangingParams *chppParams = &chppWithHeader->payload;
260
261 // Decode
262 outputSize -= sizeof(struct ChppAppHeader);
263 chreWifiRangingParams *backParams =
264 chppWifiRangingParamsToChre(chppParams, outputSize);
265 ASSERT_NE(backParams, nullptr);
266
267 // Compare chreEvent against encoded (chppEvent) and decoded back (backEvent)
268 EXPECT_EQ(chppParams->targetListLen, chreParams.targetListLen);
269 EXPECT_EQ(backParams->targetListLen, chreParams.targetListLen);
270
271 uint16_t baseOffset = sizeof(ChppWifiRangingParams);
272 if (chreParams.targetListLen > 0) {
273 EXPECT_EQ(chppParams->targetList.offset, baseOffset);
274 EXPECT_EQ(chppParams->targetList.length,
275 chppParams->targetListLen * sizeof(ChppWifiRangingTarget));
276 baseOffset += chppParams->targetList.length;
277
278 auto *chppRangingList =
279 ((const uint8_t *)chppParams + chppParams->targetList.offset);
280 for (size_t i = 0; i < chppParams->targetListLen; i++) {
281 ChppWifiRangingTarget currentRangingTarget;
282 memcpy(¤tRangingTarget,
283 chppRangingList + (i * sizeof(ChppWifiRangingTarget)),
284 sizeof(ChppWifiRangingTarget));
285 SCOPED_TRACE(i);
286 EXPECT_EQ(currentRangingTarget.macAddress[0],
287 chreParams.targetList[i].macAddress[0]);
288 EXPECT_EQ(currentRangingTarget.macAddress[1],
289 chreParams.targetList[i].macAddress[1]);
290 EXPECT_EQ(currentRangingTarget.macAddress[2],
291 chreParams.targetList[i].macAddress[2]);
292 EXPECT_EQ(currentRangingTarget.macAddress[3],
293 chreParams.targetList[i].macAddress[3]);
294 EXPECT_EQ(currentRangingTarget.macAddress[4],
295 chreParams.targetList[i].macAddress[4]);
296 EXPECT_EQ(currentRangingTarget.macAddress[5],
297 chreParams.targetList[i].macAddress[5]);
298 EXPECT_EQ(currentRangingTarget.primaryChannel,
299 chreParams.targetList[i].primaryChannel);
300 EXPECT_EQ(currentRangingTarget.centerFreqPrimary,
301 chreParams.targetList[i].centerFreqPrimary);
302 EXPECT_EQ(currentRangingTarget.centerFreqSecondary,
303 chreParams.targetList[i].centerFreqSecondary);
304 EXPECT_EQ(currentRangingTarget.channelWidth,
305 chreParams.targetList[i].channelWidth);
306
307 EXPECT_EQ(currentRangingTarget.macAddress[0],
308 backParams->targetList[i].macAddress[0]);
309 EXPECT_EQ(currentRangingTarget.macAddress[1],
310 backParams->targetList[i].macAddress[1]);
311 EXPECT_EQ(currentRangingTarget.macAddress[2],
312 backParams->targetList[i].macAddress[2]);
313 EXPECT_EQ(currentRangingTarget.macAddress[3],
314 backParams->targetList[i].macAddress[3]);
315 EXPECT_EQ(currentRangingTarget.macAddress[4],
316 backParams->targetList[i].macAddress[4]);
317 EXPECT_EQ(currentRangingTarget.macAddress[5],
318 backParams->targetList[i].macAddress[5]);
319 EXPECT_EQ(currentRangingTarget.primaryChannel,
320 backParams->targetList[i].primaryChannel);
321 EXPECT_EQ(currentRangingTarget.centerFreqPrimary,
322 backParams->targetList[i].centerFreqPrimary);
323 EXPECT_EQ(currentRangingTarget.centerFreqSecondary,
324 backParams->targetList[i].centerFreqSecondary);
325 EXPECT_EQ(currentRangingTarget.channelWidth,
326 backParams->targetList[i].channelWidth);
327 }
328 } else {
329 EXPECT_EQ(chppParams->targetList.offset, 0);
330 EXPECT_EQ(chppParams->targetList.length, 0);
331 }
332
333 // Handling of short input
334 chreWifiRangingParams *chreMalformed;
335 chreMalformed = chppWifiRangingParamsToChre(chppParams, outputSize - 1);
336 ASSERT_EQ(chreMalformed, nullptr);
337
338 chppFree(chppWithHeader);
339 chppFree(backParams);
340 }
341
validateNanSubscribeConfig(const chreWifiNanSubscribeConfig & config)342 void validateNanSubscribeConfig(const chreWifiNanSubscribeConfig &config) {
343 ChppWifiNanSubscribeConfigWithHeader *chppWithHeader = nullptr;
344 size_t outputSize = 999;
345
346 // Test Encode
347 bool result =
348 chppWifiNanSubscribeConfigFromChre(&config, &chppWithHeader, &outputSize);
349 ASSERT_TRUE(result);
350 ASSERT_NE(chppWithHeader, nullptr);
351
352 size_t expectedSize = sizeof(ChppWifiNanSubscribeConfigWithHeader) +
353 std::strlen(config.service) + 1 +
354 config.matchFilterLength +
355 config.serviceSpecificInfoSize;
356 EXPECT_EQ(expectedSize, outputSize);
357
358 // Test Decode
359 outputSize -= sizeof(struct ChppAppHeader);
360 ChppWifiNanSubscribeConfig *chppConfig = &chppWithHeader->payload;
361 chreWifiNanSubscribeConfig *decodedConfig =
362 chppWifiNanSubscribeConfigToChre(chppConfig, outputSize);
363 ASSERT_NE(decodedConfig, nullptr);
364
365 EXPECT_EQ(config.subscribeType, decodedConfig->subscribeType);
366 EXPECT_EQ(std::string(config.service), std::string(decodedConfig->service));
367
368 EXPECT_EQ(config.serviceSpecificInfoSize,
369 decodedConfig->serviceSpecificInfoSize);
370 EXPECT_TRUE(0 == std::memcmp(config.serviceSpecificInfo,
371 decodedConfig->serviceSpecificInfo,
372 config.serviceSpecificInfoSize));
373
374 EXPECT_EQ(config.matchFilterLength, decodedConfig->matchFilterLength);
375 EXPECT_TRUE(0 == std::memcmp(config.matchFilter, decodedConfig->matchFilter,
376 config.matchFilterLength));
377 }
378
validateNanDiscoveryEvent(const struct chreWifiNanDiscoveryEvent & event)379 void validateNanDiscoveryEvent(const struct chreWifiNanDiscoveryEvent &event) {
380 ChppWifiNanDiscoveryEventWithHeader *chppWithHeader = nullptr;
381 size_t outputSize = 999;
382
383 // Test Encode
384 bool result =
385 chppWifiNanDiscoveryEventFromChre(&event, &chppWithHeader, &outputSize);
386 ASSERT_TRUE(result);
387 ASSERT_NE(chppWithHeader, nullptr);
388
389 size_t expectedSize = sizeof(ChppWifiNanDiscoveryEventWithHeader) +
390 event.serviceSpecificInfoSize;
391 EXPECT_EQ(expectedSize, outputSize);
392
393 // Test Decode
394 outputSize -= sizeof(struct ChppAppHeader);
395 ChppWifiNanDiscoveryEvent *chppEvent = &chppWithHeader->payload;
396 chreWifiNanDiscoveryEvent *decodedEvent =
397 chppWifiNanDiscoveryEventToChre(chppEvent, outputSize);
398 ASSERT_NE(decodedEvent, nullptr);
399
400 EXPECT_EQ(event.publishId, decodedEvent->publishId);
401 EXPECT_EQ(event.subscribeId, decodedEvent->subscribeId);
402 EXPECT_TRUE(0 == std::memcmp(event.serviceSpecificInfo,
403 decodedEvent->serviceSpecificInfo,
404 event.serviceSpecificInfoSize));
405 EXPECT_TRUE(0 == std::memcmp(event.publisherAddress,
406 decodedEvent->publisherAddress,
407 CHRE_WIFI_BSSID_LEN));
408 }
409
validateNanRangingParams(const struct chreWifiNanRangingParams & params)410 void validateNanRangingParams(const struct chreWifiNanRangingParams ¶ms) {
411 ChppWifiNanRangingParamsWithHeader *chppWithHeader = nullptr;
412 size_t outputSize = 999;
413
414 // Test Encode
415 bool result =
416 chppWifiNanRangingParamsFromChre(¶ms, &chppWithHeader, &outputSize);
417 ASSERT_TRUE(result);
418 ASSERT_NE(chppWithHeader, nullptr);
419
420 size_t expectedSize = sizeof(ChppWifiNanRangingParamsWithHeader);
421 EXPECT_EQ(expectedSize, outputSize);
422
423 // Test Decode
424 outputSize -= sizeof(struct ChppAppHeader);
425 ChppWifiNanRangingParams *chppParams = &chppWithHeader->payload;
426 chreWifiNanRangingParams *decodedParams =
427 chppWifiNanRangingParamsToChre(chppParams, outputSize);
428 ASSERT_NE(decodedParams, nullptr);
429 EXPECT_TRUE(0 == std::memcmp(¶ms, decodedParams, sizeof(params)));
430 }
431
validateNanSessionLostEvent(const struct chreWifiNanSessionLostEvent & event)432 void validateNanSessionLostEvent(
433 const struct chreWifiNanSessionLostEvent &event) {
434 ChppWifiNanSessionLostEventWithHeader *chppWithHeader = nullptr;
435 size_t outputSize = 999;
436
437 // Test Encode
438 bool result =
439 chppWifiNanSessionLostEventFromChre(&event, &chppWithHeader, &outputSize);
440 ASSERT_TRUE(result);
441 ASSERT_NE(chppWithHeader, nullptr);
442
443 size_t expectedSize = sizeof(ChppWifiNanSessionLostEventWithHeader);
444 EXPECT_EQ(expectedSize, outputSize);
445
446 // Test Decode
447 outputSize -= sizeof(struct ChppAppHeader);
448 ChppWifiNanSessionLostEvent *chppEvent = &chppWithHeader->payload;
449 chreWifiNanSessionLostEvent *decodedEvent =
450 chppWifiNanSessionLostEventToChre(chppEvent, outputSize);
451 ASSERT_NE(decodedEvent, nullptr);
452 EXPECT_TRUE(0 == std::memcmp(&event, decodedEvent, sizeof(event)));
453 }
454
validateNanSessionTerminatedEvent(const struct chreWifiNanSessionTerminatedEvent & event)455 void validateNanSessionTerminatedEvent(
456 const struct chreWifiNanSessionTerminatedEvent &event) {
457 ChppWifiNanSessionTerminatedEventWithHeader *chppWithHeader = nullptr;
458 size_t outputSize = 999;
459
460 // Test Encode
461 bool result = chppWifiNanSessionTerminatedEventFromChre(
462 &event, &chppWithHeader, &outputSize);
463 ASSERT_TRUE(result);
464 ASSERT_NE(chppWithHeader, nullptr);
465
466 size_t expectedSize = sizeof(ChppWifiNanSessionTerminatedEventWithHeader);
467 EXPECT_EQ(expectedSize, outputSize);
468
469 // Test Decode
470 outputSize -= sizeof(struct ChppAppHeader);
471 ChppWifiNanSessionTerminatedEvent *chppEvent = &chppWithHeader->payload;
472 chreWifiNanSessionTerminatedEvent *decodedEvent =
473 chppWifiNanSessionTerminatedEventToChre(chppEvent, outputSize);
474 ASSERT_NE(decodedEvent, nullptr);
475 EXPECT_TRUE(0 == std::memcmp(&event, decodedEvent, sizeof(event)));
476 }
477
478 } // anonymous namespace
479
TEST(WifiConvert,EmptyScanResult)480 TEST(WifiConvert, EmptyScanResult) {
481 const chreWifiScanEvent chreEvent = {
482 .version = 200, // ignored
483 .resultCount = 0,
484 .resultTotal = 0,
485 .eventIndex = 0,
486 .scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS,
487 .ssidSetSize = 2,
488 .scannedFreqListLen = 0,
489 .referenceTime = 1234,
490 .scannedFreqList = nullptr,
491 .results = nullptr,
492 .radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_HIGH_ACCURACY,
493 };
494
495 validateScanEvent(chreEvent);
496 }
497
TEST(WifiConvert,SingleResult)498 TEST(WifiConvert, SingleResult) {
499 // clang-format off
500 const chreWifiScanResult chreAp = {
501 .ageMs = 11,
502 .capabilityInfo = 22,
503 .ssidLen = 4,
504 .ssid = {'a', 'b', 'c', 'd',},
505 .bssid = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
506 .flags = CHRE_WIFI_SCAN_RESULT_FLAGS_IS_FTM_RESPONDER,
507 .rssi = -37,
508 .band = CHRE_WIFI_BAND_2_4_GHZ,
509 .primaryChannel = 2437,
510 .centerFreqPrimary = 2442,
511 .centerFreqSecondary = 2447,
512 .channelWidth = CHRE_WIFI_CHANNEL_WIDTH_80_MHZ,
513 .securityMode = CHRE_WIFI_SECURITY_MODE_PSK,
514 .radioChain = CHRE_WIFI_RADIO_CHAIN_0 | CHRE_WIFI_RADIO_CHAIN_1,
515 .rssiChain0 = -37,
516 .rssiChain1 = -42,
517 };
518 const chreWifiScanEvent chreEvent = {
519 .version = 200, // ignored
520 .resultCount = 1,
521 .resultTotal = 5,
522 .eventIndex = 2,
523 .scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE,
524 .ssidSetSize = 0,
525 .scannedFreqListLen = 0,
526 .referenceTime = 12345,
527 .scannedFreqList = nullptr,
528 .results = &chreAp,
529 .radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT,
530 };
531 // clang-format on
532
533 validateScanEvent(chreEvent);
534 }
535
TEST(WifiConvert,TwoResultsWithFreqList)536 TEST(WifiConvert, TwoResultsWithFreqList) {
537 // clang-format off
538 const chreWifiScanResult chreAps[] = {
539 {
540 .ageMs = 11,
541 .capabilityInfo = 22,
542 .ssidLen = 4,
543 .ssid = {'a', 'b', 'c', 'd',},
544 .bssid = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff},
545 .flags = CHRE_WIFI_SCAN_RESULT_FLAGS_IS_FTM_RESPONDER,
546 .rssi = -37,
547 .band = CHRE_WIFI_BAND_2_4_GHZ,
548 .primaryChannel = 2437,
549 .centerFreqPrimary = 2442,
550 .centerFreqSecondary = 2447,
551 .channelWidth = CHRE_WIFI_CHANNEL_WIDTH_80_MHZ,
552 .securityMode = CHRE_WIFI_SECURITY_MODE_PSK,
553 .radioChain = CHRE_WIFI_RADIO_CHAIN_0 | CHRE_WIFI_RADIO_CHAIN_1,
554 .rssiChain0 = -37,
555 .rssiChain1 = -42,
556 },
557 {
558 .ageMs = 4325,
559 .capabilityInfo = 37,
560 .ssidLen = 2,
561 .ssid = {'h', 'i',},
562 .bssid = {0xab, 0xcd, 0xef, 0x01, 0x23, 0x45},
563 .flags = CHRE_WIFI_SCAN_RESULT_FLAGS_VHT_OPS_PRESENT,
564 .rssi = -52,
565 .band = CHRE_WIFI_BAND_5_GHZ,
566 .primaryChannel = 9999,
567 .centerFreqPrimary = 8888,
568 .centerFreqSecondary = 7777,
569 .channelWidth = CHRE_WIFI_CHANNEL_WIDTH_160_MHZ,
570 .securityMode = CHRE_WIFI_SECURITY_MODE_SAE,
571 .radioChain = CHRE_WIFI_RADIO_CHAIN_0,
572 .rssiChain0 = -37,
573 .rssiChain1 = 0,
574 }};
575 const uint32_t freqList[] = {0xdeadbeef, 0xc001cafe, 0xc0a1ba11};
576 const chreWifiScanEvent chreEvent = {
577 .version = 200, // ignored
578 .resultCount = 2,
579 .resultTotal = 3,
580 .eventIndex = 1,
581 .scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE,
582 .ssidSetSize = 10,
583 .scannedFreqListLen = 3,
584 .referenceTime = 56789,
585 .scannedFreqList = freqList,
586 .results = chreAps,
587 .radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_LOW_POWER,
588 };
589 // clang-format on
590
591 validateScanEvent(chreEvent);
592 }
593
TEST(WifiConvert,DefaultScanParams)594 TEST(WifiConvert, DefaultScanParams) {
595 // From chreWifiRequestScanAsyncDefault
596 struct chreWifiScanParams params = {};
597 params.scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE;
598 params.maxScanAgeMs = 5000; // 5 seconds
599 params.frequencyListLen = 0;
600 params.ssidListLen = 0;
601 params.radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT;
602
603 validateScanParams(params);
604 }
605
TEST(WifiConvert,ScanParamsWithFreqList)606 TEST(WifiConvert, ScanParamsWithFreqList) {
607 uint32_t freqList[] = {1234, 3456};
608 struct chreWifiScanParams chreParams = {
609 .scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS,
610 .maxScanAgeMs = 9999,
611 .frequencyListLen = 2,
612 .frequencyList = freqList,
613 .ssidListLen = 0,
614 .ssidList = nullptr,
615 .radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_LOW_POWER,
616 };
617
618 validateScanParams(chreParams);
619 }
620
TEST(WifiConvert,ScanParamsWithSsidList)621 TEST(WifiConvert, ScanParamsWithSsidList) {
622 // clang-format off
623 chreWifiSsidListItem ssidList[] = {
624 {.ssidLen = 4, .ssid = {0xde, 0xad, 0xbe, 0xef}},
625 {.ssidLen = 2, .ssid = {':', ')'}}
626 };
627 // clang-format on
628 struct chreWifiScanParams chreParams = {
629 .scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS,
630 .maxScanAgeMs = 9999,
631 .frequencyListLen = 0,
632 .frequencyList = nullptr,
633 .ssidListLen = 2,
634 .ssidList = ssidList,
635 .radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_LOW_POWER,
636 };
637
638 validateScanParams(chreParams);
639 }
640
TEST(WifiConvert,ScanParamsWithBothLists)641 TEST(WifiConvert, ScanParamsWithBothLists) {
642 uint32_t freqList[] = {1234, 3456, 5678};
643 // clang-format off
644 chreWifiSsidListItem ssidList[] = {
645 {.ssidLen = 4, .ssid = {0xde, 0xad, 0xbe, 0xef}},
646 {.ssidLen = 3, .ssid = {':', '-', ')'}}
647 };
648 // clang-format on
649 struct chreWifiScanParams chreParams = {
650 .scanType = CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS,
651 .maxScanAgeMs = 9999,
652 .frequencyListLen = 3,
653 .frequencyList = freqList,
654 .ssidListLen = 2,
655 .ssidList = ssidList,
656 .radioChainPref = CHRE_WIFI_RADIO_CHAIN_PREF_LOW_POWER,
657 };
658
659 validateScanParams(chreParams);
660 }
661
TEST(WifiConvert,RangingParamsEmpty)662 TEST(WifiConvert, RangingParamsEmpty) {
663 struct chreWifiRangingParams chreParams = {
664 .targetListLen = 0,
665 .targetList = NULL,
666 };
667
668 validateRangingParams(chreParams);
669 }
670
TEST(WifiConvert,RangingParamsWithTarget)671 TEST(WifiConvert, RangingParamsWithTarget) {
672 struct chreWifiRangingTarget target = {
673 .macAddress = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc},
674 .primaryChannel = 0xdef02468,
675 .centerFreqPrimary = 0xace13579,
676 .centerFreqSecondary = 0xbdf369cf,
677 .channelWidth = 0x48,
678 };
679
680 struct chreWifiRangingParams chreParams = {
681 .targetListLen = 1,
682 .targetList = &target,
683 };
684
685 validateRangingParams(chreParams);
686 }
687
TEST(WifiConvert,NanSubscribeConfig)688 TEST(WifiConvert, NanSubscribeConfig) {
689 constexpr size_t kServiceSpecificInfoSize = 4;
690 constexpr size_t kMatchFilterLength = 6;
691 uint8_t serviceSpecificInfo[kServiceSpecificInfoSize] = {0x1, 0x2, 0x3, 0x4};
692 uint8_t matchFilter[kMatchFilterLength] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
693 struct chreWifiNanSubscribeConfig config = {
694 .subscribeType = CHRE_WIFI_NAN_SUBSCRIBE_TYPE_PASSIVE,
695 .service = "Hello NAN!",
696 .serviceSpecificInfo = serviceSpecificInfo,
697 .serviceSpecificInfoSize = kServiceSpecificInfoSize,
698 .matchFilter = matchFilter,
699 .matchFilterLength = kMatchFilterLength};
700
701 validateNanSubscribeConfig(config);
702 }
703
TEST(WifiConvert,NanDiscoveryEvent)704 TEST(WifiConvert, NanDiscoveryEvent) {
705 constexpr size_t kServiceSpecificInfoSize = 4;
706 uint8_t serviceSpecificInfo[kServiceSpecificInfoSize] = {0x1, 0x2, 0x3, 0x4};
707 struct chreWifiNanDiscoveryEvent event = {
708 .subscribeId = 0xc0ffee,
709 .publishId = 0xcafe,
710 .publisherAddress = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6},
711 .serviceSpecificInfo = serviceSpecificInfo,
712 .serviceSpecificInfoSize = kServiceSpecificInfoSize};
713
714 validateNanDiscoveryEvent(event);
715 }
716
TEST(WifiConvert,NanSessionLostEvent)717 TEST(WifiConvert, NanSessionLostEvent) {
718 struct chreWifiNanSessionLostEvent event = {.id = 0xdead, .peerId = 0xbeef};
719 validateNanSessionLostEvent(event);
720 }
721
TEST(WifiConvert,NanSessionTerminatedEvent)722 TEST(WifiConvert, NanSessionTerminatedEvent) {
723 struct chreWifiNanSessionTerminatedEvent event = {.id = 0xc001,
724 .reason = CHRE_ERROR};
725 validateNanSessionTerminatedEvent(event);
726 }
727
TEST(WifiConvert,NanRangingParams)728 TEST(WifiConvert, NanRangingParams) {
729 struct chreWifiNanRangingParams params = {
730 .macAddress = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6}};
731 validateNanRangingParams(params);
732 }
733