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 #include <wifi_system/interface_tool.h>
22 
23 #include "android/net/wifi/IApInterface.h"
24 #include "android/net/wifi/IWificond.h"
25 #include "wificond/tests/integration/process_utils.h"
26 #include "wificond/tests/mock_ap_interface_event_callback.h"
27 
28 using android::net::wifi::IApInterface;
29 using android::net::wifi::IWificond;
30 using android::wifi_system::InterfaceTool;
31 using android::wificond::MockApInterfaceEventCallback;
32 using android::wificond::tests::integration::HostapdIsDead;
33 using android::wificond::tests::integration::HostapdIsRunning;
34 using android::wificond::tests::integration::ScopedDevModeWificond;
35 using android::wificond::tests::integration::WaitForTrue;
36 using std::string;
37 using std::vector;
38 
39 namespace android {
40 namespace wificond {
41 namespace {
42 const char kInterfaceName[] = "wlan0";
43 constexpr int kHostapdStartupTimeoutSeconds = 3;
44 constexpr int kHostapdDeathTimeoutSeconds = 3;
45 }  // namespace
46 
47 TEST(ApInterfaceTest, CanCreateApInterfaces) {
48   ScopedDevModeWificond dev_mode;
49   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
50 
51   // We should be able to create an AP interface.
52   sp<IApInterface> ap_interface;
53   EXPECT_TRUE(service->createApInterface(kInterfaceName, &ap_interface).isOk());
54   EXPECT_NE(nullptr, ap_interface.get());
55 
56   // The interface should start out down.
57   string if_name;
58   EXPECT_TRUE(ap_interface->getInterfaceName(&if_name).isOk());
59   EXPECT_TRUE(!if_name.empty());
60   InterfaceTool if_tool;
61   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
62 
63   // Mark the interface as up, just to test that we mark it down on tearDown.
64   EXPECT_TRUE(if_tool.SetUpState(if_name.c_str(), true));
65   EXPECT_TRUE(if_tool.GetUpState(if_name.c_str()));
66 
67   // We should not be able to create two AP interfaces.
68   sp<IApInterface> ap_interface2;
69   EXPECT_TRUE(service->createApInterface(
70       kInterfaceName, &ap_interface2).isOk());
71   EXPECT_EQ(nullptr, ap_interface2.get());
72 
73   // We can tear down the created interface.
74   bool success = false;
75   EXPECT_TRUE(service->tearDownApInterface(kInterfaceName, &success).isOk());
76   EXPECT_TRUE(success);
77   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
78 
79   // Teardown everything at the end of the test.
80   EXPECT_TRUE(service->tearDownInterfaces().isOk());
81 }
82 
83 // TODO: b/30311493 this test fails because hostapd fails to set the driver
84 //       channel every other time.
85 TEST(ApInterfaceTest, CanStartStopHostapd) {
86   ScopedDevModeWificond dev_mode;
87   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
88   sp<IApInterface> ap_interface;
89   EXPECT_TRUE(service->createApInterface(kInterfaceName, &ap_interface).isOk());
90   ASSERT_NE(nullptr, ap_interface.get());
91 
92   // Interface should start out down.
93   string if_name;
94   EXPECT_TRUE(ap_interface->getInterfaceName(&if_name).isOk());
95   EXPECT_TRUE(!if_name.empty());
96   InterfaceTool if_tool;
97   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
98 
99   sp<MockApInterfaceEventCallback> ap_interface_event_callback(
100       new MockApInterfaceEventCallback());
101 
102   for (int iteration = 0; iteration < 4; iteration++) {
103     bool hostapd_started = false;
104     EXPECT_TRUE(
105         ap_interface
106             ->startHostapd(ap_interface_event_callback, &hostapd_started)
107             .isOk());
108     EXPECT_TRUE(hostapd_started);
109 
110     EXPECT_TRUE(WaitForTrue(HostapdIsRunning, kHostapdStartupTimeoutSeconds))
111         << "Failed on iteration " << iteration;
112 
113     // There are two reasons to do this:
114     //   1) We look for hostapd so quickly that we miss when it dies on startup
115     //   2) If we don't give hostapd enough time to get fully up, killing it
116     //      can leave the driver in a poor state.
117     // The latter points to an obvious race, where we cannot fully clean up the
118     // driver on quick transitions.
119     auto InterfaceIsUp = [&if_tool, &if_name] () {
120       return if_tool.GetUpState(if_name.c_str());
121     };
122     EXPECT_TRUE(WaitForTrue(InterfaceIsUp, kHostapdStartupTimeoutSeconds))
123         << "Failed on iteration " << iteration;
124     EXPECT_TRUE(HostapdIsRunning()) << "Failed on iteration " << iteration;
125 
126     bool hostapd_stopped = false;
127     EXPECT_TRUE(ap_interface->stopHostapd(&hostapd_stopped).isOk());
128     EXPECT_TRUE(hostapd_stopped);
129     EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
130 
131 
132     EXPECT_TRUE(WaitForTrue(HostapdIsDead, kHostapdDeathTimeoutSeconds))
133         << "Failed on iteration " << iteration;
134   }
135 }
136 }  // namespace wificond
137 }  // namespace android
138