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 <gtest/gtest.h>
18 #include <utils/StrongPointer.h>
19 
20 #include "android/net/wifi/IClientInterface.h"
21 #include "android/net/wifi/IWificond.h"
22 #include "wificond/tests/integration/process_utils.h"
23 #include "wificond/tests/shell_utils.h"
24 
25 using android::net::wifi::IClientInterface;
26 using android::net::wifi::IWificond;
27 using android::sp;
28 using android::wificond::tests::integration::RunShellCommand;
29 using android::wificond::tests::integration::ScopedDevModeWificond;
30 using android::wificond::tests::integration::SupplicantIsDead;
31 using android::wificond::tests::integration::SupplicantIsRunning;
32 using android::wificond::tests::integration::WaitForTrue;
33 using android::wificond::tests::integration::WificondIsDead;
34 
35 namespace android {
36 namespace wificond {
37 namespace {
38 
39 constexpr int kTimeoutSeconds = 3;
40 }  // namespace
41 
42 TEST(ServiceTest, ShouldTearDownSystemOnStartup) {
43   // Simulate doing normal connectivity things by startup supplicant.
44   ScopedDevModeWificond dev_mode;
45   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
46 
47   bool supplicant_started = false;
48   EXPECT_TRUE(service->enableSupplicant(&supplicant_started).isOk());
49   EXPECT_TRUE(supplicant_started);
50 
51   EXPECT_TRUE(WaitForTrue(SupplicantIsRunning, kTimeoutSeconds));
52 
53   // Kill wificond abruptly.  It should not clean up on the way out.
54   RunShellCommand("stop wificond");
55   EXPECT_TRUE(WaitForTrue(WificondIsDead, kTimeoutSeconds));
56 
57   // Supplicant should still be up.
58   EXPECT_TRUE(SupplicantIsRunning());
59 
60   // Restart wificond, which should kill supplicant on startup.
61   service = dev_mode.EnterDevModeOrDie();
62   EXPECT_TRUE(WaitForTrue(SupplicantIsDead, kTimeoutSeconds));
63 }
64 
65 TEST(ServiceTest, CanStartStopSupplicant) {
66   ScopedDevModeWificond dev_mode;
67   sp<IWificond> service = dev_mode.EnterDevModeOrDie();
68 
69   for (int iteration = 0; iteration < 4; iteration++) {
70     bool supplicant_started = false;
71     EXPECT_TRUE(service->enableSupplicant(&supplicant_started).isOk());
72     EXPECT_TRUE(supplicant_started);
73 
74     EXPECT_TRUE(WaitForTrue(SupplicantIsRunning,
75                             kTimeoutSeconds))
76         << "Failed on iteration " << iteration;
77 
78     // We look for supplicant so quickly that we miss when it dies on startup
79     sleep(1);
80     EXPECT_TRUE(SupplicantIsRunning()) << "Failed on iteration " << iteration;
81 
82     bool supplicant_stopped = false;
83     EXPECT_TRUE(
84         service->disableSupplicant(&supplicant_stopped).isOk());
85     EXPECT_TRUE(supplicant_stopped);
86 
87     EXPECT_TRUE(WaitForTrue(SupplicantIsDead, kTimeoutSeconds))
88         << "Failed on iteration " << iteration;
89   }
90 }
91 
92 }  // namespace wificond
93 }  // namespace android
94