1 /*
2 * Copyright (C) 2021 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 <fcntl.h>
18 #include <unistd.h>
19
20 #include <functional>
21 #include <tuple>
22 #include <vector>
23
24 #include <aidl/Gtest.h>
25 #include <aidl/Vintf.h>
26
27 #include <aidl/android/hardware/dumpstate/IDumpstateDevice.h>
28 #include <android/binder_manager.h>
29 #include <android/binder_process.h>
30
31 using aidl::android::hardware::dumpstate::IDumpstateDevice;
32
33 // Base class common to all dumpstate HAL AIDL tests.
34 template <typename T>
35 class DumpstateAidlTestBase : public ::testing::TestWithParam<T> {
36 protected:
CheckStatus(const ndk::ScopedAStatus & status,const binder_exception_t expected_ex_code,const int32_t expected_service_specific)37 bool CheckStatus(const ndk::ScopedAStatus& status, const binder_exception_t expected_ex_code,
38 const int32_t expected_service_specific) {
39 binder_exception_t ex_code = status.getExceptionCode();
40 if (ex_code != expected_ex_code) {
41 return false;
42 }
43 if (ex_code == EX_SERVICE_SPECIFIC) {
44 int32_t service_specific = status.getServiceSpecificError();
45 if (service_specific != expected_service_specific) {
46 return false;
47 }
48 }
49 return true;
50 }
51
52 public:
SetUp()53 virtual void SetUp() override { GetService(); }
54
55 virtual std::string GetInstanceName() = 0;
56
GetService()57 void GetService() {
58 const std::string instance_name = GetInstanceName();
59
60 ASSERT_TRUE(AServiceManager_isDeclared(instance_name.c_str()));
61 auto dumpstateBinder =
62 ndk::SpAIBinder(AServiceManager_waitForService(instance_name.c_str()));
63 dumpstate = IDumpstateDevice::fromBinder(dumpstateBinder);
64 ASSERT_NE(dumpstate, nullptr) << "Could not get AIDL instance " << instance_name;
65 }
66
ToggleVerboseLogging(bool enable)67 void ToggleVerboseLogging(bool enable) {
68 ndk::ScopedAStatus status;
69 bool logging_enabled = false;
70
71 status = dumpstate->setVerboseLoggingEnabled(enable);
72 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.getDescription();
73
74 status = dumpstate->getVerboseLoggingEnabled(&logging_enabled);
75 ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.getDescription();
76 ASSERT_EQ(logging_enabled, enable)
77 << "Verbose logging should now be " << (enable ? "enabled" : "disabled");
78 }
79
EnableVerboseLogging()80 void EnableVerboseLogging() { ToggleVerboseLogging(true); }
81
DisableVerboseLogging()82 void DisableVerboseLogging() { ToggleVerboseLogging(false); }
83
84 std::shared_ptr<IDumpstateDevice> dumpstate;
85 };
86
87 // Tests that don't need to iterate every single DumpstateMode value for dumpstateBoard_1_1.
88 class DumpstateAidlGeneralTest : public DumpstateAidlTestBase<std::string> {
89 protected:
GetInstanceName()90 virtual std::string GetInstanceName() override { return GetParam(); }
91 };
92
93 // Tests that iterate every single DumpstateMode value for dumpstateBoard_1_1.
94 class DumpstateAidlPerModeTest
95 : public DumpstateAidlTestBase<std::tuple<std::string, IDumpstateDevice::DumpstateMode>> {
96 protected:
GetInstanceName()97 virtual std::string GetInstanceName() override { return std::get<0>(GetParam()); }
98
GetMode()99 IDumpstateDevice::DumpstateMode GetMode() { return std::get<1>(GetParam()); }
100
101 // Will only execute additional_assertions when status == expected.
AssertStatusForMode(const::ndk::ScopedAStatus & status,binder_exception_t expected_ex_code,int32_t expected_service_specific,std::function<void ()> additional_assertions=nullptr)102 void AssertStatusForMode(const ::ndk::ScopedAStatus& status,
103 binder_exception_t expected_ex_code, int32_t expected_service_specific,
104 std::function<void()> additional_assertions = nullptr) {
105 if (GetMode() == IDumpstateDevice::DumpstateMode::DEFAULT) {
106 ASSERT_TRUE(CheckStatus(status, expected_ex_code, expected_ex_code));
107 } else {
108 // The rest of the modes are optional to support, but they MUST return either the
109 // expected value or UNSUPPORTED_MODE.
110 ASSERT_TRUE(CheckStatus(status, expected_ex_code, expected_service_specific) ||
111 CheckStatus(status, EX_SERVICE_SPECIFIC,
112 IDumpstateDevice::ERROR_UNSUPPORTED_MODE));
113 }
114 if (CheckStatus(status, expected_ex_code, expected_service_specific) &&
115 additional_assertions != nullptr) {
116 additional_assertions();
117 }
118 }
119 };
120
121 constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds
122
123 // Negative test: make sure dumpstateBoard() doesn't crash when passed a empty file descriptor
124 // array.
TEST_P(DumpstateAidlPerModeTest,TestNullHandle)125 TEST_P(DumpstateAidlPerModeTest, TestNullHandle) {
126 EnableVerboseLogging();
127
128 std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; // empty file descriptor vector
129
130 auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis);
131 AssertStatusForMode(status, EX_ILLEGAL_ARGUMENT, 0);
132 }
133
134 // Positive test: make sure dumpstateBoard() writes something to the FD.
TEST_P(DumpstateAidlPerModeTest,TestOk)135 TEST_P(DumpstateAidlPerModeTest, TestOk) {
136 EnableVerboseLogging();
137
138 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
139 int fds[2];
140 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
141
142 std::vector<::ndk::ScopedFileDescriptor> dumpstateFds;
143 dumpstateFds.emplace_back(fds[1]);
144
145 auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis);
146
147 AssertStatusForMode(status, EX_NONE, 0, [&fds]() {
148 // Check that at least one byte was written.
149 char buff;
150 ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
151 });
152
153 close(fds[1]);
154 close(fds[0]);
155 }
156
157 // Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
TEST_P(DumpstateAidlPerModeTest,TestHandleWithTwoFds)158 TEST_P(DumpstateAidlPerModeTest, TestHandleWithTwoFds) {
159 EnableVerboseLogging();
160
161 int fds1[2];
162 int fds2[2];
163 ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
164 ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
165
166 std::vector<::ndk::ScopedFileDescriptor> dumpstateFds;
167 dumpstateFds.emplace_back(fds1[1]);
168 dumpstateFds.emplace_back(fds2[1]);
169
170 auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis);
171
172 AssertStatusForMode(status, EX_NONE, 0, [&fds1, &fds2]() {
173 // Check that at least one byte was written to one of the FDs.
174 char buff;
175 size_t read1 = read(fds1[0], &buff, 1);
176 size_t read2 = read(fds2[0], &buff, 1);
177 // Sometimes read returns -1, so we can't just add them together and expect >= 1.
178 ASSERT_TRUE(read1 == 1 || read2 == 1) << "Dumped nothing";
179 });
180
181 close(fds1[1]);
182 close(fds1[0]);
183 close(fds2[1]);
184 close(fds2[0]);
185 }
186
187 // Make sure dumpstateBoard actually validates its arguments.
TEST_P(DumpstateAidlGeneralTest,TestInvalidModeArgument_Negative)188 TEST_P(DumpstateAidlGeneralTest, TestInvalidModeArgument_Negative) {
189 EnableVerboseLogging();
190
191 int fds[2];
192 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
193
194 std::vector<::ndk::ScopedFileDescriptor> dumpstateFds;
195 dumpstateFds.emplace_back(fds[1]);
196
197 auto status = dumpstate->dumpstateBoard(dumpstateFds,
198 static_cast<IDumpstateDevice::DumpstateMode>(-100),
199 kDefaultTimeoutMillis);
200 ASSERT_TRUE(CheckStatus(status, EX_ILLEGAL_ARGUMENT, 0));
201
202 close(fds[1]);
203 close(fds[0]);
204 }
205
TEST_P(DumpstateAidlGeneralTest,TestInvalidModeArgument_Undefined)206 TEST_P(DumpstateAidlGeneralTest, TestInvalidModeArgument_Undefined) {
207 EnableVerboseLogging();
208
209 int fds[2];
210 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
211
212 std::vector<::ndk::ScopedFileDescriptor> dumpstateFds;
213 dumpstateFds.emplace_back(fds[1]);
214
215 auto status = dumpstate->dumpstateBoard(dumpstateFds,
216 static_cast<IDumpstateDevice::DumpstateMode>(9001),
217 kDefaultTimeoutMillis);
218 ASSERT_TRUE(CheckStatus(status, EX_ILLEGAL_ARGUMENT, 0));
219
220 close(fds[1]);
221 close(fds[0]);
222 }
223
224 // Make sure disabling verbose logging behaves correctly. Some info is still allowed to be emitted,
225 // but it can't have privacy/storage/battery impacts.
TEST_P(DumpstateAidlPerModeTest,TestDeviceLoggingDisabled)226 TEST_P(DumpstateAidlPerModeTest, TestDeviceLoggingDisabled) {
227 DisableVerboseLogging();
228
229 // Index 0 corresponds to the read end of the pipe; 1 to the write end.
230 int fds[2];
231 ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
232
233 std::vector<::ndk::ScopedFileDescriptor> dumpstateFds;
234 dumpstateFds.emplace_back(fds[1]);
235
236 auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis);
237
238 // We don't include additional assertions here about the file passed in. If verbose logging is
239 // disabled, the OEM may choose to include nothing at all, but it is allowed to include some
240 // essential information based on the mode as long as it isn't private user information.
241 AssertStatusForMode(status, EX_NONE, 0);
242
243 close(fds[1]);
244 close(fds[0]);
245 }
246
247 // Double-enable is perfectly valid, but the second call shouldn't do anything.
TEST_P(DumpstateAidlGeneralTest,TestRepeatedEnable)248 TEST_P(DumpstateAidlGeneralTest, TestRepeatedEnable) {
249 EnableVerboseLogging();
250 EnableVerboseLogging();
251 }
252
253 // Double-disable is perfectly valid, but the second call shouldn't do anything.
TEST_P(DumpstateAidlGeneralTest,TestRepeatedDisable)254 TEST_P(DumpstateAidlGeneralTest, TestRepeatedDisable) {
255 DisableVerboseLogging();
256 DisableVerboseLogging();
257 }
258
259 // Toggling in short order is perfectly valid.
TEST_P(DumpstateAidlGeneralTest,TestRepeatedToggle)260 TEST_P(DumpstateAidlGeneralTest, TestRepeatedToggle) {
261 EnableVerboseLogging();
262 DisableVerboseLogging();
263 EnableVerboseLogging();
264 DisableVerboseLogging();
265 }
266
267 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DumpstateAidlGeneralTest);
268 INSTANTIATE_TEST_SUITE_P(
269 PerInstance, DumpstateAidlGeneralTest,
270 testing::ValuesIn(android::getAidlHalInstanceNames(IDumpstateDevice::descriptor)),
271 android::PrintInstanceNameToString);
272
273 // Includes the mode's name as part of the description string.
PrintInstanceNameToStringWithMode(const testing::TestParamInfo<std::tuple<std::string,IDumpstateDevice::DumpstateMode>> & info)274 static inline std::string PrintInstanceNameToStringWithMode(
275 const testing::TestParamInfo<std::tuple<std::string, IDumpstateDevice::DumpstateMode>>&
276 info) {
277 return android::PrintInstanceNameToString(
278 testing::TestParamInfo(std::get<0>(info.param), info.index)) +
279 "_" + toString(std::get<1>(info.param));
280 }
281
282 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DumpstateAidlPerModeTest);
283 INSTANTIATE_TEST_SUITE_P(
284 PerInstanceAndMode, DumpstateAidlPerModeTest,
285 testing::Combine(
286 testing::ValuesIn(android::getAidlHalInstanceNames(IDumpstateDevice::descriptor)),
287 testing::ValuesIn(ndk::internal::enum_values<IDumpstateDevice::DumpstateMode>)),
288 PrintInstanceNameToStringWithMode);
289
main(int argc,char ** argv)290 int main(int argc, char** argv) {
291 ::testing::InitGoogleTest(&argc, argv);
292 ABinderProcess_setThreadPoolMaxThreadCount(1);
293 ABinderProcess_startThreadPool();
294 return RUN_ALL_TESTS();
295 }
296