1 /*
2  * Copyright (C) 2017 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 "chre/apps/wifi_offload/chre_scan_params_safe.h"
20 #include "include/utility.h"
21 
22 class ChreScanParamsSafeTest : public testing::Test {
23  public:
24   wifi_offload_test::RandomGenerator random_gen_;
25   wifi_offload::ScanParams nanoapp_scan_params_;
26 
ConstructChreScanParamsSafeAndCompareWithOrigScanParams()27   void ConstructChreScanParamsSafeAndCompareWithOrigScanParams() {
28     wifi_offload::ChreScanParamsSafe chre_scan_params_safe(
29         nanoapp_scan_params_);
30     const chreWifiScanParams *chre_scan_params =
31         chre_scan_params_safe.GetChreWifiScanParams();
32 
33     EXPECT_EQ(CHRE_WIFI_SCAN_TYPE_ACTIVE_PLUS_PASSIVE_DFS,
34               chre_scan_params->scanType);
35     EXPECT_EQ(0, chre_scan_params->maxScanAgeMs);
36 
37     ASSERT_EQ(nanoapp_scan_params_.ssids_to_scan_.size(),
38               chre_scan_params->ssidListLen);
39     for (size_t i = 0; i < chre_scan_params->ssidListLen; i++) {
40       chreWifiSsidListItem ssid_item;
41       nanoapp_scan_params_.ssids_to_scan_[i].ToChreWifiSsidListItem(&ssid_item);
42       ASSERT_EQ(ssid_item.ssidLen, chre_scan_params->ssidList[i].ssidLen);
43       EXPECT_EQ(0,
44                 std::memcmp(ssid_item.ssid, chre_scan_params->ssidList[i].ssid,
45                             chre_scan_params->ssidList[i].ssidLen));
46     }
47     ASSERT_EQ(nanoapp_scan_params_.frequencies_to_scan_mhz_.size(),
48               chre_scan_params->frequencyListLen);
49     EXPECT_EQ(
50         0, std::memcmp(nanoapp_scan_params_.frequencies_to_scan_mhz_.data(),
51                        chre_scan_params->frequencyList,
52                        chre_scan_params->frequencyListLen * sizeof(uint32_t)));
53   }
54 };
55 
TEST_F(ChreScanParamsSafeTest,ConstructsChreScanParamsSafeAndComparesWithOriginalScanParams)56 TEST_F(ChreScanParamsSafeTest,
57        ConstructsChreScanParamsSafeAndComparesWithOriginalScanParams) {
58   init(nanoapp_scan_params_.frequencies_to_scan_mhz_, random_gen_,
59        CHRE_WIFI_FREQUENCY_LIST_MAX_LEN - 5);
60   init(nanoapp_scan_params_.ssids_to_scan_, random_gen_,
61        CHRE_WIFI_SSID_LIST_MAX_LEN - 2);
62   ConstructChreScanParamsSafeAndCompareWithOrigScanParams();
63 }
64 
TEST_F(ChreScanParamsSafeTest,ConstructsChreScanParamsSafeWithEmptyFreqList)65 TEST_F(ChreScanParamsSafeTest, ConstructsChreScanParamsSafeWithEmptyFreqList) {
66   init(nanoapp_scan_params_.ssids_to_scan_, random_gen_,
67        CHRE_WIFI_SSID_LIST_MAX_LEN - 2);
68   ConstructChreScanParamsSafeAndCompareWithOrigScanParams();
69 }
70 
TEST_F(ChreScanParamsSafeTest,ConstructsChreScanParamsSafeWithEmptySsidList)71 TEST_F(ChreScanParamsSafeTest, ConstructsChreScanParamsSafeWithEmptySsidList) {
72   init(nanoapp_scan_params_.frequencies_to_scan_mhz_, random_gen_,
73        CHRE_WIFI_FREQUENCY_LIST_MAX_LEN - 5);
74   ConstructChreScanParamsSafeAndCompareWithOrigScanParams();
75 }
76 
TEST_F(ChreScanParamsSafeTest,ChreScanParamsSafeTruncatesLongLists)77 TEST_F(ChreScanParamsSafeTest, ChreScanParamsSafeTruncatesLongLists) {
78   // initialize frequency and ssid lists to exceed limit size
79   init(nanoapp_scan_params_.frequencies_to_scan_mhz_, random_gen_,
80        CHRE_WIFI_FREQUENCY_LIST_MAX_LEN + 5);
81   init(nanoapp_scan_params_.ssids_to_scan_, random_gen_,
82        CHRE_WIFI_SSID_LIST_MAX_LEN + 2);
83 
84   wifi_offload::ChreScanParamsSafe chre_scan_params_safe(nanoapp_scan_params_);
85   const chreWifiScanParams *chre_scan_params =
86       chre_scan_params_safe.GetChreWifiScanParams();
87 
88   EXPECT_EQ(CHRE_WIFI_SSID_LIST_MAX_LEN, chre_scan_params->ssidListLen);
89   EXPECT_EQ(CHRE_WIFI_FREQUENCY_LIST_MAX_LEN,
90             chre_scan_params->frequencyListLen);
91 }
92