diff options
author | Christopher Wiley <wiley@google.com> | 2016-07-14 16:40:58 -0700 |
---|---|---|
committer | Christopher Wiley <wiley@google.com> | 2016-07-18 13:58:21 -0700 |
commit | 944db7cbdafa91279b40dd8a7737307e49cf711e (patch) | |
tree | 3b6cb1aef535ccb223437a8b27a622d14de0a0d2 /libwifi_system/tests | |
parent | 3efd47ff5e926cc1fa042e288e129df10fa4ec83 (diff) | |
download | wifi-944db7cbdafa91279b40dd8a7737307e49cf711e.tar.gz |
Move hostapd management logic to libwifi-system
This is the logic that existed in netd, but moved into a mockable
C++ object and formulated to support unittesting the config file
generation.
Bug: 30040724
Test: unit tests pass, data for tests extracted from config files
generated by existing netd code.
Test: netd continues to be able to set up APs for tethering using
libwifi-system rather than previous code.
Test: No SELinux policy violations during/after setup.
Test: Can sign in to network backed by hostapd started by netd using
libwifi-system.
Change-Id: I1156c494fc889a3fdf14182b11c7697d93fdf586
Diffstat (limited to 'libwifi_system/tests')
-rw-r--r-- | libwifi_system/tests/hostapd_manager_unittest.cpp | 145 | ||||
-rw-r--r-- | libwifi_system/tests/main.cpp | 27 |
2 files changed, 172 insertions, 0 deletions
diff --git a/libwifi_system/tests/hostapd_manager_unittest.cpp b/libwifi_system/tests/hostapd_manager_unittest.cpp new file mode 100644 index 000000000..f8ebd995f --- /dev/null +++ b/libwifi_system/tests/hostapd_manager_unittest.cpp @@ -0,0 +1,145 @@ +/* + * 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; + +namespace android { +namespace wifi_system { +namespace { + +const char kTestInterfaceName[] = "foobar0"; +const char kTestSsidStr[] = "helloisitme"; +const char kTestPassphraseStr[] = "yourelookingfor"; +const int kTestChannel = 2; + +#define CONFIG_COMMON_PREFIX \ + "interface=foobar0\n" \ + "driver=nl80211\n" \ + "ctrl_interface=/data/misc/wifi/hostapd\n" \ + "ssid2=68656c6c6f" "6973" "6974" "6d65\n" \ + "channel=2\n" \ + "ieee80211n=1\n" \ + "hw_mode=g\n" + +// If you generate your config file with both the test ssid +// and the test passphrase, you'll get this line in the config. +#define CONFIG_PSK_LINE \ + "wpa_psk=dffa36815281e5a6eca1910f254717fa2528681335e3bbec5966d2aa9221a66e\n" + +#define CONFIG_WPA_SUFFIX \ + "wpa=3\n" \ + "wpa_pairwise=TKIP CCMP\n" \ + CONFIG_PSK_LINE + +#define CONFIG_WPA2_SUFFIX \ + "wpa=2\n" \ + "rsn_pairwise=CCMP\n" \ + CONFIG_PSK_LINE + +const char kExpectedOpenConfig[] = + CONFIG_COMMON_PREFIX + "ignore_broadcast_ssid=0\n" + "wowlan_triggers=any\n"; + +const char kExpectedWpaConfig[] = + CONFIG_COMMON_PREFIX + "ignore_broadcast_ssid=0\n" + "wowlan_triggers=any\n" + CONFIG_WPA_SUFFIX; + +const char kExpectedWpa2Config[] = + CONFIG_COMMON_PREFIX + "ignore_broadcast_ssid=0\n" + "wowlan_triggers=any\n" + CONFIG_WPA2_SUFFIX; + +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 + +} // namespace + +TEST_F(HostapdManagerTest, GeneratesCorrectOpenConfig) { + string config = GetConfigForEncryptionType( + HostapdManager::EncryptionType::kOpen); + EXPECT_FALSE(config.empty()); + EXPECT_EQ(kExpectedOpenConfig, config); +} + +TEST_F(HostapdManagerTest, GeneratesCorrectWpaConfig) { + string config = GetConfigForEncryptionType( + HostapdManager::EncryptionType::kWpa); + EXPECT_FALSE(config.empty()); + EXPECT_EQ(kExpectedWpaConfig, config); +} + +TEST_F(HostapdManagerTest, GeneratesCorrectWpa2Config) { + string config = GetConfigForEncryptionType( + HostapdManager::EncryptionType::kWpa2); + EXPECT_FALSE(config.empty()); + EXPECT_EQ(kExpectedWpa2Config, config); +} + +TEST_F(HostapdManagerTest, RespectsHiddenSetting) { + string config = HostapdManager().CreateHostapdConfig( + kTestInterfaceName, + cstr2vector(kTestSsidStr), + true, + kTestChannel, + HostapdManager::EncryptionType::kOpen, + vector<uint8_t>()); + EXPECT_FALSE(config.find("ignore_broadcast_ssid=1\n") == string::npos); + EXPECT_TRUE(config.find("ignore_broadcast_ssid=0\n") == string::npos); +} + +TEST_F(HostapdManagerTest, CorrectlyInfersHwMode) { + string config = HostapdManager().CreateHostapdConfig( + kTestInterfaceName, + cstr2vector(kTestSsidStr), + true, + 44, + HostapdManager::EncryptionType::kOpen, + vector<uint8_t>()); + EXPECT_FALSE(config.find("hw_mode=a\n") == string::npos); + EXPECT_TRUE(config.find("hw_mode=g\n") == string::npos); +} + + +} // namespace wifi_system +} // namespace android diff --git a/libwifi_system/tests/main.cpp b/libwifi_system/tests/main.cpp new file mode 100644 index 000000000..51e8dad29 --- /dev/null +++ b/libwifi_system/tests/main.cpp @@ -0,0 +1,27 @@ +/* + * 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 <gtest/gtest.h> + +#include <android-base/logging.h> + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + // Force ourselves to always log to stderr + android::base::InitLogging(argv, android::base::StderrLogger); + return RUN_ALL_TESTS(); +} + |