• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "dumpstate_1_1_hidl_hal_test"
18 
19 #include <fcntl.h>
20 #include <unistd.h>
21 
22 #include <functional>
23 #include <vector>
24 
25 #include <android/hardware/dumpstate/1.1/IDumpstateDevice.h>
26 #include <android/hardware/dumpstate/1.1/types.h>
27 #include <cutils/native_handle.h>
28 #include <gtest/gtest.h>
29 #include <hidl/GtestPrinter.h>
30 #include <hidl/ServiceManagement.h>
31 #include <log/log.h>
32 
33 namespace {
34 
35 using ::android::sp;
36 using ::android::hardware::Return;
37 using ::android::hardware::dumpstate::V1_1::DumpstateMode;
38 using ::android::hardware::dumpstate::V1_1::DumpstateStatus;
39 using ::android::hardware::dumpstate::V1_1::IDumpstateDevice;
40 using ::android::hardware::dumpstate::V1_1::toString;
41 
42 class DumpstateHidl1_1Test : public ::testing::TestWithParam<std::string> {
43   protected:
SetUp()44     virtual void SetUp() override { GetService(); }
45 
GetService()46     void GetService() {
47         dumpstate = IDumpstateDevice::getService(GetParam());
48         ASSERT_NE(dumpstate, nullptr) << "Could not get HIDL instance";
49     }
50 
ToggleVerboseLogging(bool enable)51     void ToggleVerboseLogging(bool enable) {
52         Return<void> status = dumpstate->setVerboseLoggingEnabled(enable);
53         ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
54 
55         if (!dumpstate->ping().isOk()) {
56             ALOGW("IDumpstateDevice service appears to have exited lazily, attempting to get "
57                   "again");
58             GetService();
59         }
60 
61         Return<bool> logging_enabled = dumpstate->getVerboseLoggingEnabled();
62         ASSERT_TRUE(logging_enabled.isOk())
63                 << "Status should be ok: " << logging_enabled.description();
64         ASSERT_EQ(logging_enabled, enable)
65                 << "Verbose logging should now be " << (enable ? "enabled" : "disabled");
66 
67         if (!dumpstate->ping().isOk()) {
68             ALOGW("IDumpstateDevice service appears to have exited lazily, attempting to get "
69                   "again");
70             GetService();
71         }
72     }
73 
EnableVerboseLogging()74     void EnableVerboseLogging() { ToggleVerboseLogging(true); }
75 
DisableVerboseLogging()76     void DisableVerboseLogging() { ToggleVerboseLogging(false); }
77 
78     sp<IDumpstateDevice> dumpstate;
79 };
80 
81 #define TEST_FOR_DUMPSTATE_MODE(name, body, mode) \
82     TEST_P(DumpstateHidl1_1Test, name##_##mode) { body(DumpstateMode::mode); }
83 
84 // We use a macro to define individual test cases instead of hidl_enum_range<> because some HAL
85 // implementations are lazy and may call exit() at the end of dumpstateBoard(), which would cause
86 // DEAD_OBJECT errors after the first iteration. Separate cases re-get the service each time as part
87 // of SetUp(), and also provide better separation of concerns when specific modes are problematic.
88 #define TEST_FOR_ALL_DUMPSTATE_MODES(name, body)       \
89     TEST_FOR_DUMPSTATE_MODE(name, body, FULL);         \
90     TEST_FOR_DUMPSTATE_MODE(name, body, INTERACTIVE);  \
91     TEST_FOR_DUMPSTATE_MODE(name, body, REMOTE);       \
92     TEST_FOR_DUMPSTATE_MODE(name, body, WEAR);         \
93     TEST_FOR_DUMPSTATE_MODE(name, body, CONNECTIVITY); \
94     TEST_FOR_DUMPSTATE_MODE(name, body, WIFI);         \
95     TEST_FOR_DUMPSTATE_MODE(name, body, DEFAULT);      \
96     TEST_FOR_DUMPSTATE_MODE(name, body, PROTO);
97 
98 constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000;  // 30 seconds
99 
100 // Will only execute additional_assertions when status == expected.
AssertStatusForMode(const DumpstateMode mode,const Return<DumpstateStatus> & status,const DumpstateStatus expected,std::function<void ()> additional_assertions=nullptr)101 void AssertStatusForMode(const DumpstateMode mode, const Return<DumpstateStatus>& status,
102                          const DumpstateStatus expected,
103                          std::function<void()> additional_assertions = nullptr) {
104     ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
105                                << status.description();
106     if (mode == DumpstateMode::DEFAULT) {
107         ASSERT_EQ(expected, status) << "Required mode (DumpstateMode::" << toString(mode)
108                                     << "): status should be DumpstateStatus::" << toString(expected)
109                                     << ", but got DumpstateStatus::" << toString(status);
110     } else {
111         // The rest of the modes are optional to support, but they MUST return either the expected
112         // value or UNSUPPORTED_MODE.
113         ASSERT_TRUE(status == expected || status == DumpstateStatus::UNSUPPORTED_MODE)
114                 << "Optional mode (DumpstateMode::" << toString(mode)
115                 << "): status should be DumpstateStatus::" << toString(expected)
116                 << " or DumpstateStatus::UNSUPPORTED_MODE, but got DumpstateStatus::"
117                 << toString(status);
118     }
119     if (status == expected && additional_assertions != nullptr) {
120         additional_assertions();
121     }
122 }
123 
124 // Negative test: make sure dumpstateBoard() doesn't crash when passed a null pointer.
__anon88e212c50202(DumpstateMode mode) 125 TEST_FOR_ALL_DUMPSTATE_MODES(TestNullHandle, [this](DumpstateMode mode) {
126     EnableVerboseLogging();
127 
128     Return<DumpstateStatus> status =
129             dumpstate->dumpstateBoard_1_1(nullptr, mode, kDefaultTimeoutMillis);
130 
131     AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT);
132 });
133 
134 // Negative test: make sure dumpstateBoard() ignores a handle with no FD.
__anon88e212c50302(DumpstateMode mode) 135 TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithNoFd, [this](DumpstateMode mode) {
136     EnableVerboseLogging();
137 
138     native_handle_t* handle = native_handle_create(0, 0);
139     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
140 
141     Return<DumpstateStatus> status =
142             dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
143 
144     AssertStatusForMode(mode, status, DumpstateStatus::ILLEGAL_ARGUMENT);
145 
146     native_handle_close(handle);
147     native_handle_delete(handle);
148 });
149 
150 // Positive test: make sure dumpstateBoard() writes something to the FD.
__anon88e212c50402(DumpstateMode mode) 151 TEST_FOR_ALL_DUMPSTATE_MODES(TestOk, [this](DumpstateMode mode) {
152     EnableVerboseLogging();
153 
154     // Index 0 corresponds to the read end of the pipe; 1 to the write end.
155     int fds[2];
156     ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
157 
158     native_handle_t* handle = native_handle_create(1, 0);
159     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
160     handle->data[0] = fds[1];
161 
162     Return<DumpstateStatus> status =
163             dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
164 
165     AssertStatusForMode(mode, status, DumpstateStatus::OK, [&fds]() {
166         // Check that at least one byte was written.
167         char buff;
168         ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
169     });
170 
171     native_handle_close(handle);
172     native_handle_delete(handle);
173 });
174 
175 // Positive test: make sure dumpstateBoard() doesn't crash with two FDs.
__anon88e212c50602(DumpstateMode mode) 176 TEST_FOR_ALL_DUMPSTATE_MODES(TestHandleWithTwoFds, [this](DumpstateMode mode) {
177     EnableVerboseLogging();
178 
179     int fds1[2];
180     int fds2[2];
181     ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno;
182     ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno;
183 
184     native_handle_t* handle = native_handle_create(2, 0);
185     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
186     handle->data[0] = fds1[1];
187     handle->data[1] = fds2[1];
188 
189     Return<DumpstateStatus> status =
190             dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
191 
192     AssertStatusForMode(mode, status, DumpstateStatus::OK, [&fds1, &fds2]() {
193         // Check that at least one byte was written to one of the FDs.
194         char buff;
195         size_t read1 = read(fds1[0], &buff, 1);
196         size_t read2 = read(fds2[0], &buff, 1);
197         // Sometimes read returns -1, so we can't just add them together and expect >= 1.
198         ASSERT_TRUE(read1 == 1 || read2 == 1) << "Dumped nothing";
199     });
200 
201     native_handle_close(handle);
202     native_handle_delete(handle);
203 });
204 
205 // Make sure dumpstateBoard_1_1 actually validates its arguments.
TEST_P(DumpstateHidl1_1Test,TestInvalidModeArgument_Negative)206 TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Negative) {
207     EnableVerboseLogging();
208 
209     int fds[2];
210     ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
211 
212     native_handle_t* handle = native_handle_create(1, 0);
213     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
214     handle->data[0] = fds[1];
215 
216     Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
217             handle, static_cast<DumpstateMode>(-100), kDefaultTimeoutMillis);
218 
219     ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
220                                << status.description();
221     ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
222             << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
223 
224     native_handle_close(handle);
225     native_handle_delete(handle);
226 }
227 
TEST_P(DumpstateHidl1_1Test,TestInvalidModeArgument_Undefined)228 TEST_P(DumpstateHidl1_1Test, TestInvalidModeArgument_Undefined) {
229     EnableVerboseLogging();
230 
231     int fds[2];
232     ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
233 
234     native_handle_t* handle = native_handle_create(1, 0);
235     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
236     handle->data[0] = fds[1];
237 
238     Return<DumpstateStatus> status = dumpstate->dumpstateBoard_1_1(
239             handle, static_cast<DumpstateMode>(9001), kDefaultTimeoutMillis);
240 
241     ASSERT_TRUE(status.isOk()) << "Status should be ok and return a more specific DumpstateStatus: "
242                                << status.description();
243     ASSERT_EQ(status, DumpstateStatus::ILLEGAL_ARGUMENT)
244             << "Should return DumpstateStatus::ILLEGAL_ARGUMENT for invalid mode param";
245 
246     native_handle_close(handle);
247     native_handle_delete(handle);
248 }
249 
250 // Positive test: make sure dumpstateBoard() from 1.0 doesn't fail.
TEST_P(DumpstateHidl1_1Test,Test1_0MethodOk)251 TEST_P(DumpstateHidl1_1Test, Test1_0MethodOk) {
252     EnableVerboseLogging();
253 
254     int fds[2];
255     ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
256 
257     native_handle_t* handle = native_handle_create(1, 0);
258     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
259     handle->data[0] = fds[1];
260 
261     Return<void> status = dumpstate->dumpstateBoard(handle);
262 
263     ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.description();
264 
265     // Check that at least one byte was written.
266     char buff;
267     ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing";
268 
269     native_handle_close(handle);
270     native_handle_delete(handle);
271 }
272 
273 // Make sure disabling verbose logging behaves correctly. Some info is still allowed to be emitted,
274 // but it can't have privacy/storage/battery impacts.
__anon88e212c50802(DumpstateMode mode) 275 TEST_FOR_ALL_DUMPSTATE_MODES(TestVerboseLoggingDisabled, [this](DumpstateMode mode) {
276     DisableVerboseLogging();
277 
278     // Index 0 corresponds to the read end of the pipe; 1 to the write end.
279     int fds[2];
280     ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno;
281 
282     native_handle_t* handle = native_handle_create(1, 0);
283     ASSERT_NE(handle, nullptr) << "Could not create native_handle";
284     handle->data[0] = fds[1];
285 
286     Return<DumpstateStatus> status =
287             dumpstate->dumpstateBoard_1_1(handle, mode, kDefaultTimeoutMillis);
288 
289     // We don't include additional assertions here about the file passed in. If verbose logging is
290     // disabled, the OEM may choose to include nothing at all, but it is allowed to include some
291     // essential information based on the mode as long as it isn't private user information.
292     AssertStatusForMode(mode, status, DumpstateStatus::OK);
293 
294     native_handle_close(handle);
295     native_handle_delete(handle);
296 });
297 
298 // Double-enable is perfectly valid, but the second call shouldn't do anything.
TEST_P(DumpstateHidl1_1Test,TestRepeatedEnable)299 TEST_P(DumpstateHidl1_1Test, TestRepeatedEnable) {
300     EnableVerboseLogging();
301     EnableVerboseLogging();
302 }
303 
304 // Double-disable is perfectly valid, but the second call shouldn't do anything.
TEST_P(DumpstateHidl1_1Test,TestRepeatedDisable)305 TEST_P(DumpstateHidl1_1Test, TestRepeatedDisable) {
306     DisableVerboseLogging();
307     DisableVerboseLogging();
308 }
309 
310 // Toggling in short order is perfectly valid.
TEST_P(DumpstateHidl1_1Test,TestRepeatedToggle)311 TEST_P(DumpstateHidl1_1Test, TestRepeatedToggle) {
312     EnableVerboseLogging();
313     DisableVerboseLogging();
314     EnableVerboseLogging();
315     DisableVerboseLogging();
316 }
317 
318 INSTANTIATE_TEST_SUITE_P(
319         PerInstance, DumpstateHidl1_1Test,
320         testing::ValuesIn(android::hardware::getAllHalInstanceNames(IDumpstateDevice::descriptor)),
321         android::hardware::PrintInstanceNameToString);
322 
323 }  // namespace
324