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 
27 using android::net::wifi::IApInterface;
28 using android::net::wifi::IWificond;
29 using android::wifi_system::InterfaceTool;
30 using android::wificond::tests::integration::HostapdIsDead;
31 using android::wificond::tests::integration::HostapdIsRunning;
32 using android::wificond::tests::integration::ScopedDevModeWificond;
33 using android::wificond::tests::integration::WaitForTrue;
34 using std::string;
35 using std::vector;
36 
37 namespace android {
38 namespace wificond {
39 namespace {
40 
41 constexpr int kHostapdStartupTimeoutSeconds = 3;
42 constexpr int kHostapdDeathTimeoutSeconds = 3;
43 
44 const char kValidSsid[] = "foobar";
45 const char kInvalidSsid[] = "0123456789"
46                             "0123456789"
47                             "0123456789"
48                             "012";  // 33 bytes is too long
49 const char kValidPassphrase[] = "super secret";
50 
51 }  // namespace
52 
TEST(ApInterfaceTest,CanCreateApInterfaces)53 TEST(ApInterfaceTest, CanCreateApInterfaces) {
54   ScopedDevModeWificond dev_mode;
55   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
56 
57   // We should be able to create an AP interface.
58   sp<IApInterface> ap_interface;
59   EXPECT_TRUE(service->createApInterface(&ap_interface).isOk());
60   EXPECT_NE(nullptr, ap_interface.get());
61 
62   // The interface should start out down.
63   string if_name;
64   EXPECT_TRUE(ap_interface->getInterfaceName(&if_name).isOk());
65   EXPECT_TRUE(!if_name.empty());
66   InterfaceTool if_tool;
67   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
68 
69   // Mark the interface as up, just to test that we mark it down on teardown.
70   EXPECT_TRUE(if_tool.SetUpState(if_name.c_str(), true));
71   EXPECT_TRUE(if_tool.GetUpState(if_name.c_str()));
72 
73   // We should not be able to create two AP interfaces.
74   sp<IApInterface> ap_interface2;
75   EXPECT_TRUE(service->createApInterface(&ap_interface2).isOk());
76   EXPECT_EQ(nullptr, ap_interface2.get());
77 
78   // We can tear down the created interface.
79   EXPECT_TRUE(service->tearDownInterfaces().isOk());
80   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
81 }
82 
83 // TODO: b/30311493 this test fails because hostapd fails to set the driver
84 //       channel every other time.
TEST(ApInterfaceTest,CanStartStopHostapd)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(&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   bool wrote_config = false;
100   EXPECT_TRUE(ap_interface->writeHostapdConfig(
101       vector<uint8_t>(kValidSsid, kValidSsid + sizeof(kValidSsid) - 1),
102       false,
103       6,
104       IApInterface::ENCRYPTION_TYPE_WPA2,
105       vector<uint8_t>(kValidPassphrase,
106                       kValidPassphrase + sizeof(kValidPassphrase) - 1),
107       &wrote_config).isOk());
108   ASSERT_TRUE(wrote_config);
109 
110   for (int iteration = 0; iteration < 4; iteration++) {
111     bool hostapd_started = false;
112     EXPECT_TRUE(ap_interface->startHostapd(&hostapd_started).isOk());
113     EXPECT_TRUE(hostapd_started);
114 
115     EXPECT_TRUE(WaitForTrue(HostapdIsRunning, kHostapdStartupTimeoutSeconds))
116         << "Failed on iteration " << iteration;
117 
118     // There are two reasons to do this:
119     //   1) We look for hostapd so quickly that we miss when it dies on startup
120     //   2) If we don't give hostapd enough time to get fully up, killing it
121     //      can leave the driver in a poor state.
122     // The latter points to an obvious race, where we cannot fully clean up the
123     // driver on quick transitions.
124     auto InterfaceIsUp = [&if_tool, &if_name] () {
125       return if_tool.GetUpState(if_name.c_str());
126     };
127     EXPECT_TRUE(WaitForTrue(InterfaceIsUp, kHostapdStartupTimeoutSeconds))
128         << "Failed on iteration " << iteration;
129     EXPECT_TRUE(HostapdIsRunning()) << "Failed on iteration " << iteration;
130 
131     bool hostapd_stopped = false;
132     EXPECT_TRUE(ap_interface->stopHostapd(&hostapd_stopped).isOk());
133     EXPECT_TRUE(hostapd_stopped);
134     EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
135 
136 
137     EXPECT_TRUE(WaitForTrue(HostapdIsDead, kHostapdDeathTimeoutSeconds))
138         << "Failed on iteration " << iteration;
139   }
140 }
141 
TEST(ApInterfaceTest,CanWriteHostapdConfig)142 TEST(ApInterfaceTest, CanWriteHostapdConfig) {
143   ScopedDevModeWificond dev_mode;
144   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
145   sp<IApInterface> ap_interface;
146   EXPECT_TRUE(service->createApInterface(&ap_interface).isOk());
147   ASSERT_NE(nullptr, ap_interface.get());
148 
149   bool success = false;
150   // Should be able to write out a valid configuration
151   EXPECT_TRUE(ap_interface->writeHostapdConfig(
152       vector<uint8_t>(kValidSsid, kValidSsid + sizeof(kValidSsid) - 1),
153       false,
154       2,
155       IApInterface::ENCRYPTION_TYPE_WPA2,
156       vector<uint8_t>(kValidPassphrase,
157                       kValidPassphrase + sizeof(kValidPassphrase) - 1),
158       &success).isOk());
159   EXPECT_TRUE(success) << "Expected to write out a valid config.";
160 
161   // SSIDs have to be 32 bytes or less
162   EXPECT_TRUE(ap_interface->writeHostapdConfig(
163       vector<uint8_t>(kInvalidSsid, kInvalidSsid + sizeof(kInvalidSsid) - 1),
164       false,
165       2,
166       IApInterface::ENCRYPTION_TYPE_WPA2,
167       vector<uint8_t>(kValidPassphrase,
168                       kValidPassphrase + sizeof(kValidPassphrase) - 1),
169       &success).isOk());
170   EXPECT_FALSE(success) << "Did not expect to write out an invalid config.";
171 }
172 
173 }  // namespace wificond
174 }  // namespace android
175