diff options
author | Kedar Chitnis <kedarc@google.com> | 2021-11-16 09:08:19 +0000 |
---|---|---|
committer | Kedar Chitnis <kedarc@google.com> | 2021-11-25 07:44:46 +0000 |
commit | bc09321ae24f69b0a44e9d5bb1e2734df72ac3a6 (patch) | |
tree | 9fca0e0984839ea8facb4e4f7eb591f52f6904fd /dumpstate | |
parent | 2edfc467f11543d76fd37f7a7716c8a658c01fce (diff) |
dumpstate: Add dumpstate device AIDL HAL
- Add interface AIDL files
- Add default example service. Example service is setup as a
lazy service.
- Add VTS test having same coverage as dumpstate device HIDL
1.1 VTS tests
Bug: 205760700
Test: VtsHalDumpstateTargetTest, dumpstate, dumpstate_test, dumpsys
Change-Id: I63f177af5e1e16cad392ce594f1b438e5610d757
Diffstat (limited to 'dumpstate')
-rw-r--r-- | dumpstate/aidl/Android.bp | 43 | ||||
-rw-r--r-- | dumpstate/aidl/aidl_api/android.hardware.dumpstate/current/android/hardware/dumpstate/IDumpstateDevice.aidl | 53 | ||||
-rw-r--r-- | dumpstate/aidl/android/hardware/dumpstate/IDumpstateDevice.aidl | 137 | ||||
-rw-r--r-- | dumpstate/aidl/default/Android.bp | 46 | ||||
-rw-r--r-- | dumpstate/aidl/default/Dumpstate.cpp | 101 | ||||
-rw-r--r-- | dumpstate/aidl/default/Dumpstate.h | 46 | ||||
-rw-r--r-- | dumpstate/aidl/default/dumpstate-default.rc | 7 | ||||
-rw-r--r-- | dumpstate/aidl/default/dumpstate-default.xml | 8 | ||||
-rw-r--r-- | dumpstate/aidl/default/main.cpp | 36 | ||||
-rw-r--r-- | dumpstate/aidl/vts/functional/Android.bp | 41 | ||||
-rw-r--r-- | dumpstate/aidl/vts/functional/VtsHalDumpstateTargetTest.cpp | 295 |
11 files changed, 813 insertions, 0 deletions
diff --git a/dumpstate/aidl/Android.bp b/dumpstate/aidl/Android.bp new file mode 100644 index 0000000000..e18eadeb45 --- /dev/null +++ b/dumpstate/aidl/Android.bp @@ -0,0 +1,43 @@ +// Copyright (C) 2021 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. + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "hardware_interfaces_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["hardware_interfaces_license"], +} + +aidl_interface { + name: "android.hardware.dumpstate", + vendor_available: true, + srcs: ["android/hardware/dumpstate/*.aidl"], + stability: "vintf", + backend: { + cpp: { + enabled: false, + }, + java: { + enabled: false, + }, + ndk: { + separate_platform_variant: false, + vndk: { + enabled: true, + }, + }, + }, +} diff --git a/dumpstate/aidl/aidl_api/android.hardware.dumpstate/current/android/hardware/dumpstate/IDumpstateDevice.aidl b/dumpstate/aidl/aidl_api/android.hardware.dumpstate/current/android/hardware/dumpstate/IDumpstateDevice.aidl new file mode 100644 index 0000000000..4d78a4c09c --- /dev/null +++ b/dumpstate/aidl/aidl_api/android.hardware.dumpstate/current/android/hardware/dumpstate/IDumpstateDevice.aidl @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2021 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. + */ +/////////////////////////////////////////////////////////////////////////////// +// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE. // +/////////////////////////////////////////////////////////////////////////////// + +// This file is a snapshot of an AIDL file. Do not edit it manually. There are +// two cases: +// 1). this is a frozen version file - do not edit this in any case. +// 2). this is a 'current' file. If you make a backwards compatible change to +// the interface (from the latest frozen version), the build system will +// prompt you to update this file with `m <name>-update-api`. +// +// You must not make a backward incompatible change to any AIDL file built +// with the aidl_interface module type with versions property set. The module +// type is used to build AIDL files in a way that they can be used across +// independently updatable components of the system. If a device is shipped +// with such a backward incompatible change, it has a high risk of breaking +// later when a module using the interface is updated, e.g., Mainline modules. + +package android.hardware.dumpstate; +@VintfStability +interface IDumpstateDevice { + void dumpstateBoard(in ParcelFileDescriptor[] fd, in android.hardware.dumpstate.IDumpstateDevice.DumpstateMode mode, in long timeoutMillis); + boolean getVerboseLoggingEnabled(); + void setVerboseLoggingEnabled(in boolean enable); + const int ERROR_UNSUPPORTED_MODE = 1; + const int ERROR_DEVICE_LOGGING_NOT_ENABLED = 2; + @Backing(type="int") @VintfStability + enum DumpstateMode { + FULL = 0, + INTERACTIVE = 1, + REMOTE = 2, + WEAR = 3, + CONNECTIVITY = 4, + WIFI = 5, + DEFAULT = 6, + PROTO = 7, + } +} diff --git a/dumpstate/aidl/android/hardware/dumpstate/IDumpstateDevice.aidl b/dumpstate/aidl/android/hardware/dumpstate/IDumpstateDevice.aidl new file mode 100644 index 0000000000..3b42546a46 --- /dev/null +++ b/dumpstate/aidl/android/hardware/dumpstate/IDumpstateDevice.aidl @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2021 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. + */ + +package android.hardware.dumpstate; + +import android.os.ParcelFileDescriptor; + +@VintfStability +interface IDumpstateDevice { + /** + * Constants that define the type of bug report being taken to restrict content appropriately. + */ + @VintfStability + @Backing(type="int") + enum DumpstateMode { + /** + * Takes a bug report without user interference. + */ + FULL = 0, + /** + * Interactive bug report, i.e. triggered by the user. + */ + INTERACTIVE = 1, + /** + * Remote bug report triggered by DevicePolicyManager, for example. + */ + REMOTE = 2, + /** + * Bug report triggered on a wear device. + */ + WEAR = 3, + /** + * Bug report limited to only connectivity info (cellular, wifi, and networking). Sometimes + * called "telephony" in legacy contexts. + * + * All reported information MUST directly relate to connectivity debugging or customer + * support and MUST NOT contain unrelated private information. This information MUST NOT + * identify user-installed packages (UIDs are OK, package names are not), and MUST NOT + * contain logs of user application traffic. + */ + CONNECTIVITY = 4, + /** + * Bug report limited to only wifi info. + */ + WIFI = 5, + /** + * Default mode, This mode MUST be supported if the + * dumpstate HAL is implemented. + */ + DEFAULT = 6, + /** + * Takes a report in protobuf. + * + * The content, if implemented, must be a binary protobuf message written to the first file + * descriptor of the native handle. The protobuf schema shall be defined by the vendor. + */ + PROTO = 7, + } + + /** + * Returned for cases where the device doesn't support the given DumpstateMode (e.g. a phone + * trying to use DumpstateMode::WEAR). + */ + const int ERROR_UNSUPPORTED_MODE = 1; + /** + * Returned when device logging is not enabled. + */ + const int ERROR_DEVICE_LOGGING_NOT_ENABLED = 2; + + /** + * Dump device-specific state into the given file descriptors. + * + * One file descriptor must be passed to this method but two may be passed: + * the first descriptor must be used to dump device-specific state in text + * format, the second descriptor is optional and may be used to dump + * device-specific state in binary format. + * + * DumpstateMode can be used to limit the information that is output. + * For an example of when this is relevant, consider a bug report being generated with + * DumpstateMode::CONNECTIVITY - there is no reason to include camera or USB logs in this type + * of report. + * + * When verbose logging is disabled, getVerboseLoggingEnabled returns false, and this + * API is called, it may still output essential information but must not include + * information that identifies the user. + * + * @param fd array of file descriptors, with one or two valid file descriptors. The first FD is + * for text output, the second (if present) is for binary output. + * @param mode A mode value to restrict dumped content. + * @param timeoutMillis An approximate "budget" for how much time this call has been allotted. + * If execution runs longer than this, the IDumpstateDevice service may be killed and only + * partial information will be included in the report. + * @return If error, return service specific error with code + * ERROR_UNSUPPORTED_MODE or ERROR_DEVICE_LOGGING_NOT_ENABLED + */ + void dumpstateBoard(in ParcelFileDescriptor[] fd, in DumpstateMode mode, in long timeoutMillis); + + /** + * Queries the current state of verbose device logging. Primarily for UI and informative + * purposes. + * + * Even if verbose logging has been disabled, dumpstateBoard may still be called by the + * dumpstate routine, and essential information that does not identify the user may be included. + * + * @return Whether or not verbose vendor logging is currently enabled. + */ + boolean getVerboseLoggingEnabled(); + + /** + * Turns verbose device vendor logging on or off. + * + * The setting should be persistent across reboots. Underlying implementations may need to start + * vendor logging daemons, set system properties, or change logging masks, for example. Given + * that many vendor logs contain significant amounts of private information and may come with + * memory/storage/battery impacts, calling this method on a user build should only be done after + * user consent has been obtained, e.g. from a toggle in developer settings. + * + * Even if verbose logging has been disabled, dumpstateBoard may still be called by the + * dumpstate routine, and essential information that does not identify the user may be included. + * + * @param enable Whether to enable or disable verbose vendor logging. + */ + void setVerboseLoggingEnabled(in boolean enable); +} diff --git a/dumpstate/aidl/default/Android.bp b/dumpstate/aidl/default/Android.bp new file mode 100644 index 0000000000..45fdc17d31 --- /dev/null +++ b/dumpstate/aidl/default/Android.bp @@ -0,0 +1,46 @@ +// Copyright (C) 2021 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. + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "hardware_interfaces_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["hardware_interfaces_license"], +} + +cc_binary { + name: "android.hardware.dumpstate-service.example", + relative_install_path: "hw", + init_rc: ["dumpstate-default.rc"], + vintf_fragments: ["dumpstate-default.xml"], + vendor: true, + shared_libs: [ + "libbase", + "libbinder_ndk", + "libcutils", + "libdumpstateutil", + "liblog", + "libutils", + "android.hardware.dumpstate-V1-ndk", + ], + srcs: [ + "main.cpp", + "Dumpstate.cpp", + ], + cflags: [ + "-DLOG_TAG=\"android.hardware.dumpstate-service.example\"", + ], +} diff --git a/dumpstate/aidl/default/Dumpstate.cpp b/dumpstate/aidl/default/Dumpstate.cpp new file mode 100644 index 0000000000..a0730fb521 --- /dev/null +++ b/dumpstate/aidl/default/Dumpstate.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2021 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 <android-base/properties.h> +#include <log/log.h> +#include "DumpstateUtil.h" + +#include "Dumpstate.h" + +using android::os::dumpstate::DumpFileToFd; + +namespace aidl { +namespace android { +namespace hardware { +namespace dumpstate { + +const char kVerboseLoggingProperty[] = "persist.dumpstate.verbose_logging.enabled"; + +ndk::ScopedAStatus Dumpstate::dumpstateBoard(const std::vector<::ndk::ScopedFileDescriptor>& in_fds, + IDumpstateDevice::DumpstateMode in_mode, + int64_t in_timeoutMillis) { + (void)in_timeoutMillis; + + if (in_fds.size() < 1) { + return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, + "No file descriptor"); + } + + int fd = in_fds[0].get(); + if (fd < 0) { + return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, + "Invalid file descriptor"); + } + + switch (in_mode) { + case IDumpstateDevice::DumpstateMode::FULL: + return dumpstateBoardImpl(fd, true); + + case IDumpstateDevice::DumpstateMode::DEFAULT: + return dumpstateBoardImpl(fd, false); + + case IDumpstateDevice::DumpstateMode::INTERACTIVE: + case IDumpstateDevice::DumpstateMode::REMOTE: + case IDumpstateDevice::DumpstateMode::WEAR: + case IDumpstateDevice::DumpstateMode::CONNECTIVITY: + case IDumpstateDevice::DumpstateMode::WIFI: + case IDumpstateDevice::DumpstateMode::PROTO: + return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(ERROR_UNSUPPORTED_MODE, + "Unsupported mode"); + + default: + return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT, + "Invalid mode"); + } + + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus Dumpstate::getVerboseLoggingEnabled(bool* _aidl_return) { + *_aidl_return = getVerboseLoggingEnabledImpl(); + return ndk::ScopedAStatus::ok(); +} + +ndk::ScopedAStatus Dumpstate::setVerboseLoggingEnabled(bool in_enable) { + ::android::base::SetProperty(kVerboseLoggingProperty, in_enable ? "true" : "false"); + return ndk::ScopedAStatus::ok(); +} + +bool Dumpstate::getVerboseLoggingEnabledImpl() { + return ::android::base::GetBoolProperty(kVerboseLoggingProperty, false); +} + +ndk::ScopedAStatus Dumpstate::dumpstateBoardImpl(const int fd, const bool full) { + ALOGD("DumpstateDevice::dumpstateBoard() FD: %d\n", fd); + + dprintf(fd, "verbose logging: %s\n", getVerboseLoggingEnabledImpl() ? "enabled" : "disabled"); + dprintf(fd, "[%s] %s\n", (full ? "full" : "default"), "Hello, world!"); + + // Shows an example on how to use the libdumpstateutil API. + DumpFileToFd(fd, "cmdline", "/proc/self/cmdline"); + + return ndk::ScopedAStatus::ok(); +} + +} // namespace dumpstate +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/dumpstate/aidl/default/Dumpstate.h b/dumpstate/aidl/default/Dumpstate.h new file mode 100644 index 0000000000..0629831557 --- /dev/null +++ b/dumpstate/aidl/default/Dumpstate.h @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2021 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. + */ + +#pragma once + +#include <aidl/android/hardware/dumpstate/BnDumpstateDevice.h> +#include <aidl/android/hardware/dumpstate/IDumpstateDevice.h> +#include <android/binder_status.h> + +namespace aidl { +namespace android { +namespace hardware { +namespace dumpstate { + +class Dumpstate : public BnDumpstateDevice { + private: + bool getVerboseLoggingEnabledImpl(); + ::ndk::ScopedAStatus dumpstateBoardImpl(const int fd, const bool full); + + public: + ::ndk::ScopedAStatus dumpstateBoard(const std::vector<::ndk::ScopedFileDescriptor>& in_fds, + IDumpstateDevice::DumpstateMode in_mode, + int64_t in_timeoutMillis) override; + + ::ndk::ScopedAStatus getVerboseLoggingEnabled(bool* _aidl_return) override; + + ::ndk::ScopedAStatus setVerboseLoggingEnabled(bool in_enable) override; +}; + +} // namespace dumpstate +} // namespace hardware +} // namespace android +} // namespace aidl diff --git a/dumpstate/aidl/default/dumpstate-default.rc b/dumpstate/aidl/default/dumpstate-default.rc new file mode 100644 index 0000000000..4d011dd1c3 --- /dev/null +++ b/dumpstate/aidl/default/dumpstate-default.rc @@ -0,0 +1,7 @@ +service vendor.dumpstate-default /vendor/bin/hw/android.hardware.dumpstate-service.example + class hal + user nobody + group nobody + interface aidl android.hardware.dumpstate.IDumpstateDevice/default + oneshot + disabled diff --git a/dumpstate/aidl/default/dumpstate-default.xml b/dumpstate/aidl/default/dumpstate-default.xml new file mode 100644 index 0000000000..877aeed408 --- /dev/null +++ b/dumpstate/aidl/default/dumpstate-default.xml @@ -0,0 +1,8 @@ +<manifest version="1.0" type="device"> + <hal format="aidl"> + <name>android.hardware.dumpstate</name> + <version>1</version> + <fqname>IDumpstateDevice/default</fqname> + </hal> +</manifest> + diff --git a/dumpstate/aidl/default/main.cpp b/dumpstate/aidl/default/main.cpp new file mode 100644 index 0000000000..245175291c --- /dev/null +++ b/dumpstate/aidl/default/main.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021 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 "Dumpstate.h" + +#include <android-base/logging.h> +#include <android/binder_manager.h> +#include <android/binder_process.h> + +using aidl::android::hardware::dumpstate::Dumpstate; + +int main() { + ABinderProcess_setThreadPoolMaxThreadCount(0); + std::shared_ptr<Dumpstate> dumpstate = ndk::SharedRefBase::make<Dumpstate>(); + + const std::string instance = std::string() + Dumpstate::descriptor + "/default"; + binder_status_t status = + AServiceManager_registerLazyService(dumpstate->asBinder().get(), instance.c_str()); + CHECK(status == STATUS_OK); + + ABinderProcess_joinThreadPool(); + return EXIT_FAILURE; // Unreachable +} diff --git a/dumpstate/aidl/vts/functional/Android.bp b/dumpstate/aidl/vts/functional/Android.bp new file mode 100644 index 0000000000..5e516cf4b2 --- /dev/null +++ b/dumpstate/aidl/vts/functional/Android.bp @@ -0,0 +1,41 @@ +// Copyright (C) 2021 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. + +package { + // See: http://go/android-license-faq + // A large-scale-change added 'default_applicable_licenses' to import + // all of the 'license_kinds' from "hardware_interfaces_license" + // to get the below license kinds: + // SPDX-license-identifier-Apache-2.0 + default_applicable_licenses: ["hardware_interfaces_license"], +} + +cc_test { + name: "VtsHalDumpstateTargetTest", + defaults: [ + "VtsHalTargetTestDefaults", + "use_libaidlvintf_gtest_helper_static", + ], + srcs: ["VtsHalDumpstateTargetTest.cpp"], + shared_libs: [ + "libbinder_ndk", + "libvintf", + ], + static_libs: [ + "android.hardware.dumpstate-V1-ndk", + ], + test_suites: [ + "vts", + ], +} diff --git a/dumpstate/aidl/vts/functional/VtsHalDumpstateTargetTest.cpp b/dumpstate/aidl/vts/functional/VtsHalDumpstateTargetTest.cpp new file mode 100644 index 0000000000..442b0b067c --- /dev/null +++ b/dumpstate/aidl/vts/functional/VtsHalDumpstateTargetTest.cpp @@ -0,0 +1,295 @@ +/* + * Copyright (C) 2021 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 <fcntl.h> +#include <unistd.h> + +#include <functional> +#include <tuple> +#include <vector> + +#include <aidl/Gtest.h> +#include <aidl/Vintf.h> + +#include <aidl/android/hardware/dumpstate/IDumpstateDevice.h> +#include <android/binder_manager.h> +#include <android/binder_process.h> + +using aidl::android::hardware::dumpstate::IDumpstateDevice; + +// Base class common to all dumpstate HAL AIDL tests. +template <typename T> +class DumpstateAidlTestBase : public ::testing::TestWithParam<T> { + protected: + bool CheckStatus(const ndk::ScopedAStatus& status, const binder_exception_t expected_ex_code, + const int32_t expected_service_specific) { + binder_exception_t ex_code = status.getExceptionCode(); + if (ex_code != expected_ex_code) { + return false; + } + if (ex_code == EX_SERVICE_SPECIFIC) { + int32_t service_specific = status.getServiceSpecificError(); + if (service_specific != expected_service_specific) { + return false; + } + } + return true; + } + + public: + virtual void SetUp() override { GetService(); } + + virtual std::string GetInstanceName() = 0; + + void GetService() { + const std::string instance_name = GetInstanceName(); + + ASSERT_TRUE(AServiceManager_isDeclared(instance_name.c_str())); + auto dumpstateBinder = + ndk::SpAIBinder(AServiceManager_waitForService(instance_name.c_str())); + dumpstate = IDumpstateDevice::fromBinder(dumpstateBinder); + ASSERT_NE(dumpstate, nullptr) << "Could not get AIDL instance " << instance_name; + } + + void ToggleVerboseLogging(bool enable) { + ndk::ScopedAStatus status; + bool logging_enabled = false; + + status = dumpstate->setVerboseLoggingEnabled(enable); + ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.getDescription(); + + status = dumpstate->getVerboseLoggingEnabled(&logging_enabled); + ASSERT_TRUE(status.isOk()) << "Status should be ok: " << status.getDescription(); + ASSERT_EQ(logging_enabled, enable) + << "Verbose logging should now be " << (enable ? "enabled" : "disabled"); + } + + void EnableVerboseLogging() { ToggleVerboseLogging(true); } + + void DisableVerboseLogging() { ToggleVerboseLogging(false); } + + std::shared_ptr<IDumpstateDevice> dumpstate; +}; + +// Tests that don't need to iterate every single DumpstateMode value for dumpstateBoard_1_1. +class DumpstateAidlGeneralTest : public DumpstateAidlTestBase<std::string> { + protected: + virtual std::string GetInstanceName() override { return GetParam(); } +}; + +// Tests that iterate every single DumpstateMode value for dumpstateBoard_1_1. +class DumpstateAidlPerModeTest + : public DumpstateAidlTestBase<std::tuple<std::string, IDumpstateDevice::DumpstateMode>> { + protected: + virtual std::string GetInstanceName() override { return std::get<0>(GetParam()); } + + IDumpstateDevice::DumpstateMode GetMode() { return std::get<1>(GetParam()); } + + // Will only execute additional_assertions when status == expected. + void AssertStatusForMode(const ::ndk::ScopedAStatus& status, + binder_exception_t expected_ex_code, int32_t expected_service_specific, + std::function<void()> additional_assertions = nullptr) { + if (GetMode() == IDumpstateDevice::DumpstateMode::DEFAULT) { + ASSERT_TRUE(CheckStatus(status, expected_ex_code, expected_ex_code)); + } else { + // The rest of the modes are optional to support, but they MUST return either the + // expected value or UNSUPPORTED_MODE. + ASSERT_TRUE(CheckStatus(status, expected_ex_code, expected_service_specific) || + CheckStatus(status, EX_SERVICE_SPECIFIC, + IDumpstateDevice::ERROR_UNSUPPORTED_MODE)); + } + if (CheckStatus(status, expected_ex_code, expected_service_specific) && + additional_assertions != nullptr) { + additional_assertions(); + } + } +}; + +constexpr uint64_t kDefaultTimeoutMillis = 30 * 1000; // 30 seconds + +// Negative test: make sure dumpstateBoard() doesn't crash when passed a empty file descriptor +// array. +TEST_P(DumpstateAidlPerModeTest, TestNullHandle) { + EnableVerboseLogging(); + + std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; // empty file descriptor vector + + auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis); + AssertStatusForMode(status, EX_ILLEGAL_ARGUMENT, 0); +} + +// Positive test: make sure dumpstateBoard() writes something to the FD. +TEST_P(DumpstateAidlPerModeTest, TestOk) { + EnableVerboseLogging(); + + // Index 0 corresponds to the read end of the pipe; 1 to the write end. + int fds[2]; + ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; + + std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; + dumpstateFds.emplace_back(fds[1]); + + auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis); + + AssertStatusForMode(status, EX_NONE, 0, [&fds]() { + // Check that at least one byte was written. + char buff; + ASSERT_EQ(1, read(fds[0], &buff, 1)) << "Dumped nothing"; + }); + + close(fds[1]); + close(fds[0]); +} + +// Positive test: make sure dumpstateBoard() doesn't crash with two FDs. +TEST_P(DumpstateAidlPerModeTest, TestHandleWithTwoFds) { + EnableVerboseLogging(); + + int fds1[2]; + int fds2[2]; + ASSERT_EQ(0, pipe2(fds1, O_NONBLOCK)) << errno; + ASSERT_EQ(0, pipe2(fds2, O_NONBLOCK)) << errno; + + std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; + dumpstateFds.emplace_back(fds1[1]); + dumpstateFds.emplace_back(fds2[1]); + + auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis); + + AssertStatusForMode(status, EX_NONE, 0, [&fds1, &fds2]() { + // Check that at least one byte was written to one of the FDs. + char buff; + size_t read1 = read(fds1[0], &buff, 1); + size_t read2 = read(fds2[0], &buff, 1); + // Sometimes read returns -1, so we can't just add them together and expect >= 1. + ASSERT_TRUE(read1 == 1 || read2 == 1) << "Dumped nothing"; + }); + + close(fds1[1]); + close(fds1[0]); + close(fds2[1]); + close(fds2[0]); +} + +// Make sure dumpstateBoard actually validates its arguments. +TEST_P(DumpstateAidlGeneralTest, TestInvalidModeArgument_Negative) { + EnableVerboseLogging(); + + int fds[2]; + ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; + + std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; + dumpstateFds.emplace_back(fds[1]); + + auto status = dumpstate->dumpstateBoard(dumpstateFds, + static_cast<IDumpstateDevice::DumpstateMode>(-100), + kDefaultTimeoutMillis); + ASSERT_TRUE(CheckStatus(status, EX_ILLEGAL_ARGUMENT, 0)); + + close(fds[1]); + close(fds[0]); +} + +TEST_P(DumpstateAidlGeneralTest, TestInvalidModeArgument_Undefined) { + EnableVerboseLogging(); + + int fds[2]; + ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; + + std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; + dumpstateFds.emplace_back(fds[1]); + + auto status = dumpstate->dumpstateBoard(dumpstateFds, + static_cast<IDumpstateDevice::DumpstateMode>(9001), + kDefaultTimeoutMillis); + ASSERT_TRUE(CheckStatus(status, EX_ILLEGAL_ARGUMENT, 0)); + + close(fds[1]); + close(fds[0]); +} + +// Make sure disabling verbose logging behaves correctly. Some info is still allowed to be emitted, +// but it can't have privacy/storage/battery impacts. +TEST_P(DumpstateAidlPerModeTest, TestDeviceLoggingDisabled) { + DisableVerboseLogging(); + + // Index 0 corresponds to the read end of the pipe; 1 to the write end. + int fds[2]; + ASSERT_EQ(0, pipe2(fds, O_NONBLOCK)) << errno; + + std::vector<::ndk::ScopedFileDescriptor> dumpstateFds; + dumpstateFds.emplace_back(fds[1]); + + auto status = dumpstate->dumpstateBoard(dumpstateFds, GetMode(), kDefaultTimeoutMillis); + + // We don't include additional assertions here about the file passed in. If verbose logging is + // disabled, the OEM may choose to include nothing at all, but it is allowed to include some + // essential information based on the mode as long as it isn't private user information. + AssertStatusForMode(status, EX_NONE, 0); + + close(fds[1]); + close(fds[0]); +} + +// Double-enable is perfectly valid, but the second call shouldn't do anything. +TEST_P(DumpstateAidlGeneralTest, TestRepeatedEnable) { + EnableVerboseLogging(); + EnableVerboseLogging(); +} + +// Double-disable is perfectly valid, but the second call shouldn't do anything. +TEST_P(DumpstateAidlGeneralTest, TestRepeatedDisable) { + DisableVerboseLogging(); + DisableVerboseLogging(); +} + +// Toggling in short order is perfectly valid. +TEST_P(DumpstateAidlGeneralTest, TestRepeatedToggle) { + EnableVerboseLogging(); + DisableVerboseLogging(); + EnableVerboseLogging(); + DisableVerboseLogging(); +} + +GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DumpstateAidlGeneralTest); +INSTANTIATE_TEST_SUITE_P( + PerInstance, DumpstateAidlGeneralTest, + testing::ValuesIn(android::getAidlHalInstanceNames(IDumpstateDevice::descriptor)), + android::PrintInstanceNameToString); + +// Includes the mode's name as part of the description string. +static inline std::string PrintInstanceNameToStringWithMode( + const testing::TestParamInfo<std::tuple<std::string, IDumpstateDevice::DumpstateMode>>& + info) { + return android::PrintInstanceNameToString( + testing::TestParamInfo(std::get<0>(info.param), info.index)) + + "_" + toString(std::get<1>(info.param)); +} + +GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(DumpstateAidlPerModeTest); +INSTANTIATE_TEST_SUITE_P( + PerInstanceAndMode, DumpstateAidlPerModeTest, + testing::Combine( + testing::ValuesIn(android::getAidlHalInstanceNames(IDumpstateDevice::descriptor)), + testing::ValuesIn(ndk::internal::enum_values<IDumpstateDevice::DumpstateMode>)), + PrintInstanceNameToStringWithMode); + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + ABinderProcess_setThreadPoolMaxThreadCount(1); + ABinderProcess_startThreadPool(); + return RUN_ALL_TESTS(); +} |