1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
/*
* Copyright (C) 2016, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "wificond/tests/integration/process_utils.h"
#include <unistd.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <binder/IBinder.h>
#include <binder/IServiceManager.h>
#include <utils/String16.h>
#include <utils/StrongPointer.h>
#include "wificond/ipc_constants.h"
#include "wificond/tests/shell_utils.h"
using android::String16;
using android::base::StringPrintf;
using android::base::Trim;
using android::net::wifi::nl80211::IWificond;
using android::sp;
using android::wificond::ipc_constants::kServiceName;
namespace android {
namespace wificond {
namespace tests {
namespace integration {
namespace {
bool IsProcessRunning(const char* process_name) {
std::string output;
RunShellCommand(StringPrintf("pgrep -c ^%s$", process_name), &output);
output = Trim(output);
if (output == "0") {
return false;
}
return true;
}
}
const uint32_t ScopedDevModeWificond::kSystemServerDeathTimeoutSeconds = 10;
const uint32_t ScopedDevModeWificond::kSystemServerStartTimeoutSeconds = 10;
const uint32_t ScopedDevModeWificond::kWificondDeathTimeoutSeconds = 10;
const uint32_t ScopedDevModeWificond::kWificondStartTimeoutSeconds = 10;
ScopedDevModeWificond::~ScopedDevModeWificond() {
if (in_dev_mode_) {
ExitDevMode();
}
}
sp<IWificond> ScopedDevModeWificond::EnterDevModeOrDie() {
sp<IWificond> service;
RunShellCommand("stop wificond");
CHECK(WaitForTrue(WificondIsDead, kWificondDeathTimeoutSeconds));
RunShellCommand("stop");
CHECK(WaitForTrue(SystemServerIsDead, kSystemServerDeathTimeoutSeconds));
RunShellCommand("start wificond");
auto wificond_restarted = std::bind(IsBinderServiceRegistered, kServiceName);
CHECK(WaitForTrue(wificond_restarted, kWificondStartTimeoutSeconds));
status_t status = getService(String16(kServiceName), &service);
if (status != NO_ERROR || service.get() == nullptr) {
LOG(FATAL) << "Failed to restart wificond in dev mode";
}
in_dev_mode_ = true;
return service;
}
void ScopedDevModeWificond::ExitDevMode() {
RunShellCommand("stop wificond");
CHECK(WaitForTrue(WificondIsDead, kWificondDeathTimeoutSeconds));
RunShellCommand("start");
CHECK(WaitForTrue(SystemServerIsRunning, kSystemServerStartTimeoutSeconds));
RunShellCommand("start wificond");
CHECK(WaitForTrue(WificondIsRunning, kWificondStartTimeoutSeconds));
in_dev_mode_ = false;
}
bool WaitForTrue(std::function<bool()> condition, time_t timeout_seconds) {
time_t start_time_seconds = time(nullptr);
do {
if (condition()) {
return true;
}
usleep(1000);
} while ((time(nullptr) - start_time_seconds) < timeout_seconds);
return false;
}
bool IsBinderServiceRegistered(const char* service_name) {
sp<IBinder> service =
defaultServiceManager()->checkService(String16(service_name));
return service.get() != nullptr;
}
bool SystemServerIsRunning() {
return IsProcessRunning("system_server");
}
bool SystemServerIsDead() {
return !IsProcessRunning("system_server");
}
bool WificondIsRunning() {
return IsProcessRunning("wificond");
}
bool WificondIsDead() { return !WificondIsRunning(); }
bool HostapdIsRunning() {
return IsProcessRunning("hostapd");
}
bool HostapdIsDead() { return !HostapdIsRunning(); }
bool SupplicantIsRunning() {
return IsProcessRunning("wpa_supplicant");
}
bool SupplicantIsDead() { return !SupplicantIsRunning(); }
} // namespace integration
} // namespace tests
} // namespace android
} // namespace wificond
|