ap_interface_test.cpp revision 356d05a48660fb4ba18069be262146592f7a9d91
1/* 2 * Copyright (C) 2016, 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 <vector> 18 19#include <gtest/gtest.h> 20#include <utils/StrongPointer.h> 21 22#include "android/net/wifi/IApInterface.h" 23#include "android/net/wifi/IWificond.h" 24#include "wificond/tests/integration/process_utils.h" 25 26using android::net::wifi::IApInterface; 27using android::net::wifi::IWificond; 28using android::wificond::tests::integration::HostapdIsDead; 29using android::wificond::tests::integration::HostapdIsRunning; 30using android::wificond::tests::integration::ScopedDevModeWificond; 31using android::wificond::tests::integration::WaitForTrue; 32using std::vector; 33 34namespace android { 35namespace wificond { 36namespace { 37 38constexpr int kHostapdStartupTimeoutSeconds = 3; 39constexpr int kHostapdDeathTimeoutSeconds = 3; 40 41const char kValidSsid[] = "foobar"; 42const char kInvalidSsid[] = "0123456789" 43 "0123456789" 44 "0123456789" 45 "012"; // 33 bytes is too long 46const char kValidPassphrase[] = "super secret"; 47 48} // namespace 49 50TEST(ApInterfaceTest, CanCreateApInterfaces) { 51 ScopedDevModeWificond dev_mode; 52 sp<IWificond> service = dev_mode.EnterDevModeOrDie(); 53 54 // We should be able to create an AP interface. 55 sp<IApInterface> ap_interface; 56 EXPECT_TRUE(service->createApInterface(&ap_interface).isOk()); 57 EXPECT_NE(nullptr, ap_interface.get()); 58 59 // We should not be able to create two AP interfaces. 60 sp<IApInterface> ap_interface2; 61 EXPECT_TRUE(service->createApInterface(&ap_interface2).isOk()); 62 EXPECT_EQ(nullptr, ap_interface2.get()); 63 64 // We can tear down the created interface. 65 EXPECT_TRUE(service->tearDownInterfaces().isOk()); 66} 67 68// TODO: b/30311493 this test fails because hostapd fails to set the driver 69// channel every other time. 70TEST(ApInterfaceTest, CanStartStopHostapd) { 71 ScopedDevModeWificond dev_mode; 72 sp<IWificond> service = dev_mode.EnterDevModeOrDie(); 73 sp<IApInterface> ap_interface; 74 EXPECT_TRUE(service->createApInterface(&ap_interface).isOk()); 75 ASSERT_NE(nullptr, ap_interface.get()); 76 77 bool wrote_config = false; 78 EXPECT_TRUE(ap_interface->writeHostapdConfig( 79 vector<uint8_t>(kValidSsid, kValidSsid + sizeof(kValidSsid) - 1), 80 false, 81 6, 82 IApInterface::ENCRYPTION_TYPE_WPA2, 83 vector<uint8_t>(kValidPassphrase, 84 kValidPassphrase + sizeof(kValidPassphrase) - 1), 85 &wrote_config).isOk()); 86 ASSERT_TRUE(wrote_config); 87 88 for (int iteration = 0; iteration < 4; iteration++) { 89 bool hostapd_started = false; 90 EXPECT_TRUE(ap_interface->startHostapd(&hostapd_started).isOk()); 91 EXPECT_TRUE(hostapd_started); 92 93 EXPECT_TRUE(WaitForTrue(HostapdIsRunning, kHostapdStartupTimeoutSeconds)) 94 << "Failed on iteration " << iteration; 95 96 // There are two reasons to do this: 97 // 1) We look for hostapd so quickly that we miss when it dies on startup 98 // 2) If we don't give hostapd enough time to get fully up, killing it 99 // can leave the driver in a poor state. 100 // The latter points to an obvious race, where we cannot fully clean up the 101 // driver on quick transitions. 102 sleep(1); 103 EXPECT_TRUE(HostapdIsRunning()) << "Failed on iteration " << iteration; 104 105 bool hostapd_stopped = false; 106 EXPECT_TRUE(ap_interface->stopHostapd(&hostapd_stopped).isOk()); 107 EXPECT_TRUE(hostapd_stopped); 108 109 EXPECT_TRUE(WaitForTrue(HostapdIsDead, kHostapdDeathTimeoutSeconds)) 110 << "Failed on iteration " << iteration; 111 } 112} 113 114TEST(ApInterfaceTest, CanWriteHostapdConfig) { 115 ScopedDevModeWificond dev_mode; 116 sp<IWificond> service = dev_mode.EnterDevModeOrDie(); 117 sp<IApInterface> ap_interface; 118 EXPECT_TRUE(service->createApInterface(&ap_interface).isOk()); 119 ASSERT_NE(nullptr, ap_interface.get()); 120 121 bool success = false; 122 // Should be able to write out a valid configuration 123 EXPECT_TRUE(ap_interface->writeHostapdConfig( 124 vector<uint8_t>(kValidSsid, kValidSsid + sizeof(kValidSsid) - 1), 125 false, 126 2, 127 IApInterface::ENCRYPTION_TYPE_WPA2, 128 vector<uint8_t>(kValidPassphrase, 129 kValidPassphrase + sizeof(kValidPassphrase) - 1), 130 &success).isOk()); 131 EXPECT_TRUE(success) << "Expected to write out a valid config."; 132 133 // SSIDs have to be 32 bytes or less 134 EXPECT_TRUE(ap_interface->writeHostapdConfig( 135 vector<uint8_t>(kInvalidSsid, kInvalidSsid + sizeof(kInvalidSsid) - 1), 136 false, 137 2, 138 IApInterface::ENCRYPTION_TYPE_WPA2, 139 vector<uint8_t>(kValidPassphrase, 140 kValidPassphrase + sizeof(kValidPassphrase) - 1), 141 &success).isOk()); 142 EXPECT_FALSE(success) << "Did not expect to write out an invalid config."; 143} 144 145} // namespace wificond 146} // namespace android 147 148