| /* |
| * Copyright (C) 2016, The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| #include <string> |
| #include <vector> |
| |
| #include "wifi_system/hostapd_manager.h" |
| |
| using std::string; |
| using std::vector; |
| using testing::HasSubstr; |
| using testing::Not; |
| |
| namespace android { |
| namespace wifi_system { |
| namespace { |
| |
| const char kTestInterfaceName[] = "foobar0"; |
| const char kTestSsidStr[] = "helloisitme"; |
| const char kTestPassphraseStr[] = "yourelookingfor"; |
| const int kTestChannel = 2; |
| |
| // If you generate your config file with both the test ssid |
| // and the test passphrase, you'll get this line in the config. |
| const char kConfigPskLine[] = |
| "wpa_psk=dffa36815281e5a6eca1910f254717fa2528681335e3bbec5966d2aa9221a66e\n"; |
| |
| class HostapdManagerTest : public ::testing::Test { |
| protected: |
| string GetConfigForEncryptionType( |
| HostapdManager::EncryptionType encryption_type) { |
| return HostapdManager().CreateHostapdConfig( |
| kTestInterfaceName, |
| cstr2vector(kTestSsidStr), |
| false, // not hidden |
| kTestChannel, |
| encryption_type, |
| cstr2vector(kTestPassphraseStr)); |
| } |
| |
| vector<uint8_t> cstr2vector(const char* data) { |
| return vector<uint8_t>(data, data + strlen(data)); |
| } |
| }; // class HostapdManagerTest |
| |
| // This is used to verify config string generated by test helper function |
| // |GetConfigForEncryptionType|. |
| void VerifyCommonConfigs(const string& config) { |
| EXPECT_THAT(config, HasSubstr("interface=foobar0\n")); |
| EXPECT_THAT(config, HasSubstr("driver=nl80211\n")); |
| EXPECT_THAT(config, HasSubstr("ctrl_interface=/data/misc/wifi/hostapd/ctrl\n")); |
| EXPECT_THAT(config, HasSubstr("ssid2=68656c6c6f" "6973" "6974" "6d65\n")); |
| EXPECT_THAT(config, HasSubstr("channel=2\n")); |
| EXPECT_THAT(config, HasSubstr("hw_mode=g\n")); |
| EXPECT_THAT(config, HasSubstr("wowlan_triggers=any\n")); |
| EXPECT_THAT(config, HasSubstr("ignore_broadcast_ssid=0\n")); |
| EXPECT_THAT(config, Not(HasSubstr("ignore_broadcast_ssid=1\n"))); |
| } |
| |
| } // namespace |
| |
| TEST_F(HostapdManagerTest, GeneratesCorrectOpenConfig) { |
| string config = GetConfigForEncryptionType( |
| HostapdManager::EncryptionType::kOpen); |
| VerifyCommonConfigs(config); |
| } |
| |
| TEST_F(HostapdManagerTest, GeneratesCorrectWpaConfig) { |
| string config = GetConfigForEncryptionType( |
| HostapdManager::EncryptionType::kWpa); |
| VerifyCommonConfigs(config); |
| EXPECT_THAT(config, HasSubstr("wpa=3\n")); |
| EXPECT_THAT(config, HasSubstr("wpa_pairwise=TKIP CCMP\n")); |
| EXPECT_THAT(config, HasSubstr(kConfigPskLine)); |
| } |
| |
| TEST_F(HostapdManagerTest, GeneratesCorrectWpa2Config) { |
| string config = GetConfigForEncryptionType( |
| HostapdManager::EncryptionType::kWpa2); |
| VerifyCommonConfigs(config); |
| EXPECT_THAT(config, HasSubstr("wpa=2\n")); |
| EXPECT_THAT(config, HasSubstr("rsn_pairwise=CCMP\n")); |
| EXPECT_THAT(config, HasSubstr(kConfigPskLine)); |
| } |
| |
| TEST_F(HostapdManagerTest, RespectsHiddenSetting) { |
| string config = HostapdManager().CreateHostapdConfig( |
| kTestInterfaceName, |
| cstr2vector(kTestSsidStr), |
| true, |
| kTestChannel, |
| HostapdManager::EncryptionType::kOpen, |
| vector<uint8_t>()); |
| EXPECT_THAT(config, HasSubstr("ignore_broadcast_ssid=1\n")); |
| EXPECT_THAT(config, Not(HasSubstr("ignore_broadcast_ssid=0\n"))); |
| } |
| |
| TEST_F(HostapdManagerTest, CorrectlyInfersHwMode) { |
| string config = HostapdManager().CreateHostapdConfig( |
| kTestInterfaceName, |
| cstr2vector(kTestSsidStr), |
| true, |
| 44, |
| HostapdManager::EncryptionType::kOpen, |
| vector<uint8_t>()); |
| EXPECT_THAT(config, HasSubstr("hw_mode=a\n")); |
| EXPECT_THAT(config, Not(HasSubstr("hw_mode=g\n"))); |
| } |
| |
| |
| } // namespace wifi_system |
| } // namespace android |