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 <string>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 #include <utils/StrongPointer.h>
22 #include <wifi_system/interface_tool.h>
23 
24 #include "android/net/wifi/IClientInterface.h"
25 #include "android/net/wifi/IWificond.h"
26 #include "wificond/tests/integration/process_utils.h"
27 
28 using android::net::wifi::IClientInterface;
29 using android::net::wifi::IWificond;
30 using android::wifi_system::InterfaceTool;
31 using android::wificond::tests::integration::ScopedDevModeWificond;
32 using android::wificond::tests::integration::SupplicantIsDead;
33 using android::wificond::tests::integration::SupplicantIsRunning;
34 using android::wificond::tests::integration::WaitForTrue;
35 using std::string;
36 using std::vector;
37 
38 namespace android {
39 namespace wificond {
40 namespace {
41 
42 constexpr int kSupplicantStartupTimeoutSeconds = 3;
43 constexpr int kSupplicantDeathTimeoutSeconds = 3;
44 
45 }  // namespace
46 
TEST(ClientInterfaceTest,CanCreateClientInterfaces)47 TEST(ClientInterfaceTest, CanCreateClientInterfaces) {
48   ScopedDevModeWificond dev_mode;
49   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
50 
51   // We should be able to create an client interface.
52   sp<IClientInterface> client_interface;
53   EXPECT_TRUE(service->createClientInterface(&client_interface).isOk());
54   EXPECT_NE(nullptr, client_interface.get());
55 
56   // The interface should start out down.
57   string if_name;
58   EXPECT_TRUE(client_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 client interfaces.
68   sp<IClientInterface> client_interface2;
69   EXPECT_TRUE(service->createClientInterface(&client_interface2).isOk());
70   EXPECT_EQ(nullptr, client_interface2.get());
71 
72   // We can tear down the created interface.
73   EXPECT_TRUE(service->tearDownInterfaces().isOk());
74   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
75 }
76 
TEST(ClientInterfaceTest,CanStartStopSupplicant)77 TEST(ClientInterfaceTest, CanStartStopSupplicant) {
78   ScopedDevModeWificond dev_mode;
79   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
80   sp<IClientInterface> client_interface;
81   EXPECT_TRUE(service->createClientInterface(&client_interface).isOk());
82   ASSERT_NE(nullptr, client_interface.get());
83 
84   for (int iteration = 0; iteration < 4; iteration++) {
85     bool supplicant_started = false;
86     EXPECT_TRUE(client_interface->enableSupplicant(&supplicant_started).isOk());
87     EXPECT_TRUE(supplicant_started);
88 
89     EXPECT_TRUE(WaitForTrue(SupplicantIsRunning,
90                             kSupplicantStartupTimeoutSeconds))
91         << "Failed on iteration " << iteration;
92 
93     // We look for supplicant so quickly that we miss when it dies on startup
94     sleep(1);
95     EXPECT_TRUE(SupplicantIsRunning()) << "Failed on iteration " << iteration;
96 
97     bool supplicant_stopped = false;
98     EXPECT_TRUE(
99         client_interface->disableSupplicant(&supplicant_stopped).isOk());
100     EXPECT_TRUE(supplicant_stopped);
101 
102     EXPECT_TRUE(WaitForTrue(SupplicantIsDead, kSupplicantDeathTimeoutSeconds))
103         << "Failed on iteration " << iteration;
104   }
105 }
106 
TEST(ClientInterfaceTest,CanGetMacAddress)107 TEST(ClientInterfaceTest, CanGetMacAddress) {
108   ScopedDevModeWificond dev_mode;
109   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
110   sp<IClientInterface> client_interface;
111   EXPECT_TRUE(service->createClientInterface(&client_interface).isOk());
112   ASSERT_NE(nullptr, client_interface.get());
113   vector<uint8_t> mac_address;
114   EXPECT_TRUE(client_interface->getMacAddress(&mac_address).isOk());
115   EXPECT_TRUE(mac_address.size() == 6);
116 }
117 
118 }  // namespace wificond
119 }  // namespace android
120