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::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 const char kInterfaceName[] = "wlan0";
41 }  // namespace
42 
TEST(ApInterfaceTest,CanCreateApInterfaces)43 TEST(ApInterfaceTest, CanCreateApInterfaces) {
44   ScopedDevModeWificond dev_mode;
45   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
46 
47   // We should be able to create an AP interface.
48   sp<IApInterface> ap_interface;
49   EXPECT_TRUE(service->createApInterface(kInterfaceName, &ap_interface).isOk());
50   EXPECT_NE(nullptr, ap_interface.get());
51 
52   // The interface should start out down.
53   string if_name;
54   EXPECT_TRUE(ap_interface->getInterfaceName(&if_name).isOk());
55   EXPECT_TRUE(!if_name.empty());
56   InterfaceTool if_tool;
57   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
58 
59   // Mark the interface as up, just to test that we mark it down on tearDown.
60   EXPECT_TRUE(if_tool.SetUpState(if_name.c_str(), true));
61   EXPECT_TRUE(if_tool.GetUpState(if_name.c_str()));
62 
63   // We should not be able to create two AP interfaces.
64   sp<IApInterface> ap_interface2;
65   EXPECT_TRUE(service->createApInterface(
66       kInterfaceName, &ap_interface2).isOk());
67   EXPECT_EQ(nullptr, ap_interface2.get());
68 
69   // We can tear down the created interface.
70   bool success = false;
71   EXPECT_TRUE(service->tearDownApInterface(kInterfaceName, &success).isOk());
72   EXPECT_TRUE(success);
73   EXPECT_FALSE(if_tool.GetUpState(if_name.c_str()));
74 
75   // Teardown everything at the end of the test.
76   EXPECT_TRUE(service->tearDownInterfaces().isOk());
77 }
78 }  // namespace wificond
79 }  // namespace android
80