Migrate fake user hal.
This CL mostly copies the existing user hal library with minor
modifications to use new AIDL type.
Test: atest FakeUserHalTest.
Bug: 201830716
Change-Id: I2bf4e96fb9fd71242e01f89c92cf743f7287ef64
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/Android.bp b/automotive/vehicle/aidl/impl/fake_impl/userhal/Android.bp
new file mode 100644
index 0000000..2e95531
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/Android.bp
@@ -0,0 +1,32 @@
+/*
+ * 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 {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_library {
+ name: "FakeUserHal",
+ vendor: true,
+ srcs: ["src/*.cpp"],
+ local_include_dirs: ["include"],
+ export_include_dirs: ["include"],
+ defaults: ["VehicleHalDefaults"],
+ static_libs: [
+ "VehicleHalUtils",
+ ],
+ export_static_lib_headers: ["VehicleHalUtils"],
+}
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h
new file mode 100644
index 0000000..1424c81
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/FakeUserHal.h
@@ -0,0 +1,132 @@
+/*
+ * 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.
+ */
+
+#ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_
+#define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_
+
+#include <android-base/format.h>
+#include <android-base/result.h>
+#include <android-base/thread_annotations.h>
+
+#include <VehicleHalTypes.h>
+#include <VehicleObjectPool.h>
+
+#include <memory>
+#include <mutex>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace fake {
+
+constexpr char kUserHalDumpOption[] = "--user-hal";
+
+// Class used to emulate a real User HAL behavior through lshal debug requests.
+class FakeUserHal final {
+ public:
+ explicit FakeUserHal(std::shared_ptr<VehiclePropValuePool> valuePool) : mValuePool(valuePool) {}
+
+ ~FakeUserHal() = default;
+
+ // Checks if the emulator can handle the property.
+ static bool isSupported(int32_t prop);
+
+ // Lets the emulator set the property.
+ //
+ // @return updated property and StatusCode
+ android::base::Result<VehiclePropValuePool::RecyclableType> onSetProperty(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
+
+ // Gets the property value from the emulator.
+ //
+ // @return property value and StatusCode
+ android::base::Result<VehiclePropValuePool::RecyclableType> onGetProperty(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;
+
+ // Shows the User HAL emulation help.
+ std::string showDumpHelp() const;
+
+ // Dump its contents.
+ std::string dump(std::string indent) const;
+
+ private:
+ const std::shared_ptr<VehiclePropValuePool> mValuePool;
+ mutable std::mutex mLock;
+ VehiclePropValuePool::RecyclableType mInitialUserResponseFromCmd GUARDED_BY(mLock);
+ VehiclePropValuePool::RecyclableType mSwitchUserResponseFromCmd GUARDED_BY(mLock);
+ VehiclePropValuePool::RecyclableType mCreateUserResponseFromCmd GUARDED_BY(mLock);
+ VehiclePropValuePool::RecyclableType mSetUserIdentificationAssociationResponseFromCmd
+ GUARDED_BY(mLock);
+
+ // INITIAL_USER_INFO is called by Android when it starts, and it's expecting a property change
+ // indicating what the initial user should be.
+ //
+ // During normal circumstances, the emulator will reply right away, passing a response if
+ // InitialUserInfoResponseAction::DEFAULT (so Android could use its own logic to decide which
+ // user to boot).
+ //
+ // But during development / testing, the behavior can be changed using lshal dump, which must
+ // use the areaId to indicate what should happen next.
+ //
+ // So, the behavior of set(INITIAL_USER_INFO) is:
+ //
+ // - if it has an areaId, store the property into mInitialUserResponseFromCmd (as it was called
+ // by lshal).
+ // - else if mInitialUserResponseFromCmd is not set, return a response with the same request id
+ // and InitialUserInfoResponseAction::DEFAULT
+ // - else the behavior is defined by the areaId on mInitialUserResponseFromCmd:
+ // - if it's 1, reply with mInitialUserResponseFromCmd and the right request id
+ // - if it's 2, reply with mInitialUserResponseFromCmd but a wrong request id (so Android can
+ // test this error scenario)
+ // - if it's 3, then don't send a property change (so Android can emulate a timeout)
+ android::base::Result<VehiclePropValuePool::RecyclableType> onSetInitialUserInfoResponse(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
+
+ // Used to emulate SWITCH_USER - see onSetInitialUserInfoResponse() for usage.
+ android::base::Result<VehiclePropValuePool::RecyclableType> onSetSwitchUserResponse(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
+
+ // Used to emulate CREATE_USER - see onSetInitialUserInfoResponse() for usage.
+ android::base::Result<VehiclePropValuePool::RecyclableType> onSetCreateUserResponse(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
+
+ // Used to emulate set USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
+ // usage.
+ android::base::Result<VehiclePropValuePool::RecyclableType> onSetUserIdentificationAssociation(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value);
+
+ // Used to emulate get USER_IDENTIFICATION_ASSOCIATION - see onSetInitialUserInfoResponse() for
+ // usage.
+ android::base::Result<VehiclePropValuePool::RecyclableType> onGetUserIdentificationAssociation(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& value) const;
+
+ // Creates a default USER_IDENTIFICATION_ASSOCIATION when it was not set by lshal.
+ static android::base::Result<VehiclePropValuePool::RecyclableType>
+ defaultUserIdentificationAssociation(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& request);
+
+ android::base::Result<VehiclePropValuePool::RecyclableType> sendUserHalResponse(
+ VehiclePropValuePool::RecyclableType response, int32_t requestId);
+};
+
+} // namespace fake
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android
+
+#endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_FakeUserHal_H_
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/include/UserHalHelper.h b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/UserHalHelper.h
new file mode 100644
index 0000000..5be13be
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/UserHalHelper.h
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+
+#ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalHelper_H_
+#define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalHelper_H_
+
+#include <UserHalTypes.h>
+#include <VehicleHalTypes.h>
+#include <VehicleObjectPool.h>
+#include <android-base/result.h>
+
+#include <functional>
+#include <memory>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace fake {
+namespace user_hal_helper {
+
+// Verify whether the |value| can be casted to the type |T| and return the casted value on success.
+// Otherwise, return the error.
+template <typename T>
+::android::base::Result<T> verifyAndCast(int32_t value);
+
+// Below functions parse VehiclePropValues to the respective User HAL request structs. On success,
+// these functions return the User HAL struct. Otherwise, they return the error.
+::android::base::Result<::aidl::android::hardware::automotive::vehicle::InitialUserInfoRequest>
+toInitialUserInfoRequest(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+::android::base::Result<::aidl::android::hardware::automotive::vehicle::SwitchUserRequest>
+toSwitchUserRequest(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+::android::base::Result<::aidl::android::hardware::automotive::vehicle::CreateUserRequest>
+toCreateUserRequest(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+::android::base::Result<::aidl::android::hardware::automotive::vehicle::RemoveUserRequest>
+toRemoveUserRequest(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+::android::base::Result<
+ ::aidl::android::hardware::automotive::vehicle::UserIdentificationGetRequest>
+toUserIdentificationGetRequest(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+::android::base::Result<
+ ::aidl::android::hardware::automotive::vehicle::UserIdentificationSetRequest>
+toUserIdentificationSetRequest(
+ const ::aidl::android::hardware::automotive::vehicle::VehiclePropValue& propValue);
+
+// Below functions convert the User HAL structs to VehiclePropValues. On success, these functions
+// return the pointer to VehiclePropValue. Otherwise, they return the error.
+::android::base::Result<VehiclePropValuePool::RecyclableType> toVehiclePropValue(
+ VehiclePropValuePool& pool,
+ const ::aidl::android::hardware::automotive::vehicle::SwitchUserRequest& request);
+VehiclePropValuePool::RecyclableType toVehiclePropValue(
+ VehiclePropValuePool& pool,
+ const ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponse& response);
+VehiclePropValuePool::RecyclableType toVehiclePropValue(
+ VehiclePropValuePool& pool,
+ const ::aidl::android::hardware::automotive::vehicle::SwitchUserResponse& response);
+VehiclePropValuePool::RecyclableType toVehiclePropValue(
+ VehiclePropValuePool& pool,
+ const ::aidl::android::hardware::automotive::vehicle::CreateUserResponse& response);
+VehiclePropValuePool::RecyclableType toVehiclePropValue(
+ VehiclePropValuePool& pool,
+ const ::aidl::android::hardware::automotive::vehicle::UserIdentificationResponse& response);
+
+} // namespace user_hal_helper
+} // namespace fake
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android
+
+#endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalHelper_H_
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/include/UserHalTypes.h b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/UserHalTypes.h
new file mode 100644
index 0000000..b5e17b3
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/include/UserHalTypes.h
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+
+#ifndef android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalTypes_H_
+#define android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalTypes_H_
+
+#include <aidl/android/hardware/automotive/vehicle/CreateUserRequest.h>
+#include <aidl/android/hardware/automotive/vehicle/CreateUserResponse.h>
+#include <aidl/android/hardware/automotive/vehicle/CreateUserStatus.h>
+#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoRequest.h>
+#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoRequestType.h>
+#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoResponse.h>
+#include <aidl/android/hardware/automotive/vehicle/RemoveUserRequest.h>
+#include <aidl/android/hardware/automotive/vehicle/SwitchUserMessageType.h>
+#include <aidl/android/hardware/automotive/vehicle/SwitchUserRequest.h>
+#include <aidl/android/hardware/automotive/vehicle/SwitchUserResponse.h>
+#include <aidl/android/hardware/automotive/vehicle/SwitchUserStatus.h>
+#include <aidl/android/hardware/automotive/vehicle/UserIdentificationGetRequest.h>
+#include <aidl/android/hardware/automotive/vehicle/UserIdentificationResponse.h>
+#include <aidl/android/hardware/automotive/vehicle/UserIdentificationSetRequest.h>
+#include <aidl/android/hardware/automotive/vehicle/UserInfo.h>
+#include <aidl/android/hardware/automotive/vehicle/UsersInfo.h>
+
+#endif // android_hardware_automotive_vehicle_aidl_impl_fake_impl_userhal_include_UserHalTypes_H_
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp b/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp
new file mode 100644
index 0000000..9b60053
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/src/FakeUserHal.cpp
@@ -0,0 +1,374 @@
+/*
+ * Copyright (C) 2020 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.
+ */
+#define LOG_TAG "FakeUserHal"
+
+#include "FakeUserHal.h"
+
+#include "UserHalHelper.h"
+
+#include <VehicleUtils.h>
+#include <utils/Log.h>
+#include <utils/SystemClock.h>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace fake {
+
+namespace {
+
+using ::aidl::android::hardware::automotive::vehicle::CreateUserResponse;
+using ::aidl::android::hardware::automotive::vehicle::CreateUserStatus;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponse;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponseAction;
+using ::aidl::android::hardware::automotive::vehicle::StatusCode;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserMessageType;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserResponse;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserStatus;
+using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
+using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
+using ::android::base::Error;
+using ::android::base::Result;
+
+constexpr int32_t INITIAL_USER_INFO = toInt(VehicleProperty::INITIAL_USER_INFO);
+constexpr int32_t SWITCH_USER = toInt(VehicleProperty::SWITCH_USER);
+constexpr int32_t CREATE_USER = toInt(VehicleProperty::CREATE_USER);
+constexpr int32_t REMOVE_USER = toInt(VehicleProperty::REMOVE_USER);
+constexpr int32_t USER_IDENTIFICATION_ASSOCIATION =
+ toInt(VehicleProperty::USER_IDENTIFICATION_ASSOCIATION);
+
+Result<int32_t> getRequestId(const VehiclePropValue& value) {
+ if (value.value.int32Values.size() < 1) {
+ return Error(toInt(StatusCode::INVALID_ARG))
+ << "no int32Values on property: " << value.toString();
+ }
+ return value.value.int32Values[0];
+}
+
+Result<SwitchUserMessageType> getSwitchUserMessageType(const VehiclePropValue& value) {
+ if (value.value.int32Values.size() < 2) {
+ return Error(toInt(StatusCode::INVALID_ARG))
+ << "missing switch user message type on property: " << value.toString();
+ }
+ return user_hal_helper::verifyAndCast<SwitchUserMessageType>(value.value.int32Values[1]);
+}
+
+} // namespace
+
+bool FakeUserHal::isSupported(int32_t prop) {
+ switch (prop) {
+ case INITIAL_USER_INFO:
+ case SWITCH_USER:
+ case CREATE_USER:
+ case REMOVE_USER:
+ case USER_IDENTIFICATION_ASSOCIATION:
+ return true;
+ default:
+ return false;
+ }
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetProperty(
+ const VehiclePropValue& value) {
+ ALOGV("onSetProperty(): %s", value.toString().c_str());
+
+ switch (value.prop) {
+ case INITIAL_USER_INFO:
+ return onSetInitialUserInfoResponse(value);
+ case SWITCH_USER:
+ return onSetSwitchUserResponse(value);
+ case CREATE_USER:
+ return onSetCreateUserResponse(value);
+ case REMOVE_USER:
+ ALOGI("REMOVE_USER is FYI only, nothing to do...");
+ return nullptr;
+ case USER_IDENTIFICATION_ASSOCIATION:
+ return onSetUserIdentificationAssociation(value);
+ default:
+ return Error(toInt(StatusCode::INVALID_ARG))
+ << "Unsupported property: " << value.toString();
+ }
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onGetProperty(
+ const VehiclePropValue& value) const {
+ ALOGV("onGetProperty(%s)", value.toString().c_str());
+ switch (value.prop) {
+ case INITIAL_USER_INFO:
+ case SWITCH_USER:
+ case CREATE_USER:
+ case REMOVE_USER:
+ ALOGE("onGetProperty(): %d is only supported on SET", value.prop);
+ return Error(toInt(StatusCode::INVALID_ARG)) << "only supported on SET";
+ case USER_IDENTIFICATION_ASSOCIATION:
+ return onGetUserIdentificationAssociation(value);
+ default:
+ ALOGE("onGetProperty(): %d is not supported", value.prop);
+ return Error(toInt(StatusCode::INVALID_ARG)) << "not supported by User HAL";
+ }
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onGetUserIdentificationAssociation(
+ const VehiclePropValue& value) const {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+
+ if (mSetUserIdentificationAssociationResponseFromCmd == nullptr) {
+ return defaultUserIdentificationAssociation(value);
+ }
+ ALOGI("get(USER_IDENTIFICATION_ASSOCIATION): returning %s",
+ mSetUserIdentificationAssociationResponseFromCmd->toString().c_str());
+ auto newValue = mValuePool->obtain(*mSetUserIdentificationAssociationResponseFromCmd);
+ auto requestId = getRequestId(value);
+ if (requestId.ok()) {
+ // Must use the same requestId
+ newValue->value.int32Values[0] = *requestId;
+ } else {
+ ALOGE("get(USER_IDENTIFICATION_ASSOCIATION): no requestId on %s", value.toString().c_str());
+ return requestId.error();
+ }
+ return newValue;
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetInitialUserInfoResponse(
+ const VehiclePropValue& value) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+
+ auto requestId = getRequestId(value);
+ if (!requestId.ok()) {
+ ALOGE("Failed to get requestId on set(INITIAL_USER_INFO): %s",
+ requestId.error().message().c_str());
+ return requestId.error();
+ }
+
+ if (value.areaId != 0) {
+ ALOGD("set(INITIAL_USER_INFO) called from lshal; storing it: %s", value.toString().c_str());
+ mInitialUserResponseFromCmd = mValuePool->obtain(value);
+ return nullptr;
+ }
+
+ ALOGD("set(INITIAL_USER_INFO) called from Android: %s", value.toString().c_str());
+ if (mInitialUserResponseFromCmd != nullptr) {
+ ALOGI("replying INITIAL_USER_INFO with lshal value: %s",
+ mInitialUserResponseFromCmd->toString().c_str());
+ return sendUserHalResponse(std::move(mInitialUserResponseFromCmd), *requestId);
+ }
+
+ // Returns default response
+ auto updatedValue = user_hal_helper::toVehiclePropValue(
+ *mValuePool, InitialUserInfoResponse{
+ .requestId = *requestId,
+ .action = InitialUserInfoResponseAction::DEFAULT,
+ });
+ ALOGI("no lshal response; replying with InitialUserInfoResponseAction::DEFAULT: %s",
+ updatedValue->toString().c_str());
+ return updatedValue;
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetSwitchUserResponse(
+ const VehiclePropValue& value) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+
+ auto requestId = getRequestId(value);
+ if (!requestId.ok()) {
+ ALOGE("Failed to get requestId on set(SWITCH_USER): %s",
+ requestId.error().message().c_str());
+ return requestId.error();
+ }
+
+ auto messageType = getSwitchUserMessageType(value);
+ if (!messageType.ok()) {
+ ALOGE("Failed to get messageType on set(SWITCH_USER): %s",
+ messageType.error().message().c_str());
+ return messageType.error();
+ }
+
+ if (value.areaId != 0) {
+ if (*messageType == SwitchUserMessageType::VEHICLE_REQUEST) {
+ // User HAL can also request a user switch, so we need to check it first
+ ALOGD("set(SWITCH_USER) called from lshal to emulate a vehicle request: %s",
+ value.toString().c_str());
+ return mValuePool->obtain(value);
+ }
+ // Otherwise, we store it
+ ALOGD("set(SWITCH_USER) called from lshal; storing it: %s", value.toString().c_str());
+ mSwitchUserResponseFromCmd = mValuePool->obtain(value);
+ return nullptr;
+ }
+ ALOGD("set(SWITCH_USER) called from Android: %s", value.toString().c_str());
+
+ if (mSwitchUserResponseFromCmd != nullptr) {
+ ALOGI("replying SWITCH_USER with lshal value: %s",
+ mSwitchUserResponseFromCmd->toString().c_str());
+ return sendUserHalResponse(std::move(mSwitchUserResponseFromCmd), *requestId);
+ }
+
+ if (*messageType == SwitchUserMessageType::LEGACY_ANDROID_SWITCH ||
+ *messageType == SwitchUserMessageType::ANDROID_POST_SWITCH) {
+ ALOGI("request is %s; ignoring it", toString(*messageType).c_str());
+ return nullptr;
+ }
+
+ // Returns default response
+ auto updatedValue = user_hal_helper::toVehiclePropValue(
+ *mValuePool, SwitchUserResponse{
+ .requestId = *requestId,
+ .messageType = SwitchUserMessageType::VEHICLE_RESPONSE,
+ .status = SwitchUserStatus::SUCCESS,
+ });
+ ALOGI("no lshal response; replying with VEHICLE_RESPONSE / SUCCESS: %s",
+ updatedValue->toString().c_str());
+ return updatedValue;
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetCreateUserResponse(
+ const VehiclePropValue& value) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+
+ auto requestId = getRequestId(value);
+ if (!requestId.ok()) {
+ ALOGE("Failed to get requestId on set(CREATE_USER): %s",
+ requestId.error().message().c_str());
+ return requestId.error();
+ }
+
+ if (value.areaId != 0) {
+ ALOGD("set(CREATE_USER) called from lshal; storing it: %s", value.toString().c_str());
+ mCreateUserResponseFromCmd = mValuePool->obtain(value);
+ return nullptr;
+ }
+ ALOGD("set(CREATE_USER) called from Android: %s", value.toString().c_str());
+
+ if (mCreateUserResponseFromCmd != nullptr) {
+ ALOGI("replying CREATE_USER with lshal value: %s",
+ mCreateUserResponseFromCmd->toString().c_str());
+ return sendUserHalResponse(std::move(mCreateUserResponseFromCmd), *requestId);
+ }
+
+ // Returns default response
+ auto updatedValue = user_hal_helper::toVehiclePropValue(
+ *mValuePool, CreateUserResponse{
+ .requestId = *requestId,
+ .status = CreateUserStatus::SUCCESS,
+ });
+ ALOGI("no lshal response; replying with SUCCESS: %s", updatedValue->toString().c_str());
+ return updatedValue;
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::onSetUserIdentificationAssociation(
+ const VehiclePropValue& value) {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+
+ auto requestId = getRequestId(value);
+ if (!requestId.ok()) {
+ ALOGE("Failed to get requestId on set(USER_IDENTIFICATION_ASSOCIATION): %s",
+ requestId.error().message().c_str());
+ return requestId.error();
+ }
+
+ if (value.areaId != 0) {
+ ALOGD("set(USER_IDENTIFICATION_ASSOCIATION) called from lshal; storing it: %s",
+ value.toString().c_str());
+ mSetUserIdentificationAssociationResponseFromCmd = mValuePool->obtain(value);
+ return nullptr;
+ }
+ ALOGD("set(USER_IDENTIFICATION_ASSOCIATION) called from Android: %s", value.toString().c_str());
+
+ if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) {
+ ALOGI("replying USER_IDENTIFICATION_ASSOCIATION with lshal value: %s",
+ mSetUserIdentificationAssociationResponseFromCmd->toString().c_str());
+ // Not moving response so it can be used on GET requests
+ auto copy = mValuePool->obtain(*mSetUserIdentificationAssociationResponseFromCmd);
+ return sendUserHalResponse(std::move(copy), *requestId);
+ }
+ // Returns default response
+ return defaultUserIdentificationAssociation(value);
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::defaultUserIdentificationAssociation(
+ const VehiclePropValue& request) {
+ // TODO(b/159498909): return a response with NOT_ASSOCIATED_ANY_USER for all requested types
+ ALOGE("no lshal response for %s; replying with NOT_AVAILABLE", request.toString().c_str());
+ return Error(toInt(StatusCode::NOT_AVAILABLE)) << "not set by lshal";
+}
+
+Result<VehiclePropValuePool::RecyclableType> FakeUserHal::sendUserHalResponse(
+ VehiclePropValuePool::RecyclableType response, int32_t requestId) {
+ switch (response->areaId) {
+ case 1:
+ ALOGD("returning response with right request id");
+ response->value.int32Values[0] = requestId;
+ break;
+ case 2:
+ ALOGD("returning response with wrong request id");
+ response->value.int32Values[0] = -requestId;
+ break;
+ case 3:
+ ALOGD("not generating a property change event because of lshal prop: %s",
+ response->toString().c_str());
+ return Error(toInt(StatusCode::NOT_AVAILABLE))
+ << "not generating a property change event because of lshal prop: "
+ << response->toString();
+ default:
+ ALOGE("invalid action on lshal response: %s", response->toString().c_str());
+ return Error(toInt(StatusCode::INTERNAL_ERROR))
+ << "invalid action on lshal response: " << response->toString();
+ }
+
+ ALOGD("updating property to: %s", response->toString().c_str());
+ return response;
+}
+
+std::string FakeUserHal::showDumpHelp() const {
+ return fmt::format("{}: dumps state used for user management\n", kUserHalDumpOption);
+}
+
+std::string FakeUserHal::dump(std::string indent) const {
+ std::scoped_lock<std::mutex> lockGuard(mLock);
+
+ std::string info;
+ if (mInitialUserResponseFromCmd != nullptr) {
+ info += fmt::format("{}InitialUserInfo response: {}\n", indent,
+ mInitialUserResponseFromCmd->toString());
+ } else {
+ info += fmt::format("{}No InitialUserInfo response\n", indent);
+ }
+ if (mSwitchUserResponseFromCmd != nullptr) {
+ info += fmt::format("{}SwitchUser response: {}\n", indent,
+ mSwitchUserResponseFromCmd->toString());
+ } else {
+ info += fmt::format("{}No SwitchUser response\n", indent);
+ }
+ if (mCreateUserResponseFromCmd != nullptr) {
+ info += fmt::format("{}CreateUser response: {}\n", indent,
+ mCreateUserResponseFromCmd->toString());
+ } else {
+ info += fmt::format("{}No CreateUser response\n", indent);
+ }
+ if (mSetUserIdentificationAssociationResponseFromCmd != nullptr) {
+ info += fmt::format("{}SetUserIdentificationAssociation response: {}\n", indent,
+ mSetUserIdentificationAssociationResponseFromCmd->toString());
+ } else {
+ info += fmt::format("{}No SetUserIdentificationAssociation response\n", indent);
+ }
+ return info;
+}
+
+} // namespace fake
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/src/UserHalHelper.cpp b/automotive/vehicle/aidl/impl/fake_impl/userhal/src/UserHalHelper.cpp
new file mode 100644
index 0000000..4fd93ae
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/src/UserHalHelper.cpp
@@ -0,0 +1,403 @@
+/*
+ * 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.
+ */
+
+#define LOG_TAG "UserHalHelper"
+
+#include "UserHalHelper.h"
+
+#include <VehicleUtils.h>
+#include <log/log.h>
+#include <utils/SystemClock.h>
+
+#include <vector>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace fake {
+namespace user_hal_helper {
+
+namespace {
+
+using ::aidl::android::hardware::automotive::vehicle::CreateUserRequest;
+using ::aidl::android::hardware::automotive::vehicle::CreateUserResponse;
+using ::aidl::android::hardware::automotive::vehicle::CreateUserStatus;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoRequest;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoRequestType;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponse;
+using ::aidl::android::hardware::automotive::vehicle::RemoveUserRequest;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserMessageType;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserRequest;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserResponse;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserStatus;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationAssociationSetValue;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationAssociationType;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationGetRequest;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationResponse;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationSetAssociation;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationSetRequest;
+using ::aidl::android::hardware::automotive::vehicle::UserInfo;
+using ::aidl::android::hardware::automotive::vehicle::UsersInfo;
+using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
+using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
+using ::android::base::Error;
+using ::android::base::Result;
+
+constexpr const char kSeparator[] = "||";
+constexpr size_t kNumFieldsPerUserInfo = 2;
+constexpr size_t kNumFieldsPerSetAssociation = 2;
+
+Result<void> verifyPropValue(const VehiclePropValue& propValue, VehicleProperty vehicleProperty,
+ size_t minInt32Values) {
+ auto prop = verifyAndCast<VehicleProperty>(propValue.prop);
+ if (!prop.ok()) {
+ return Error() << "Invalid vehicle property: " << prop.error();
+ }
+ if (*prop != vehicleProperty) {
+ return Error() << "Mismatching " << toString(vehicleProperty) << " request, received "
+ << toString(*prop) << " property";
+ }
+ if (propValue.value.int32Values.size() < minInt32Values) {
+ return Error() << "Int32Values must have at least " << minInt32Values
+ << " values, received " << propValue.value.int32Values.size();
+ }
+ return {};
+}
+
+Result<void> parseUserInfo(const std::vector<int32_t>& int32Values, size_t startPos,
+ UserInfo* userInfo) {
+ if (int32Values.size() < startPos + kNumFieldsPerUserInfo) {
+ return Error() << "Int32Values must have at least " << startPos + 2 << " values, received "
+ << int32Values.size();
+ }
+ userInfo->userId = int32Values[startPos];
+ int32_t intUserFlags = int32Values[startPos + 1];
+ const int32_t combinedFlags = UserInfo::USER_FLAG_SYSTEM | UserInfo::USER_FLAG_GUEST |
+ UserInfo::USER_FLAG_EPHEMERAL | UserInfo::USER_FLAG_ADMIN |
+ UserInfo::USER_FLAG_DISABLED | UserInfo::USER_FLAG_PROFILE;
+
+ if ((intUserFlags & ~combinedFlags) != 0) {
+ return Error() << "Invalid user flags: " << intUserFlags << ", must be '|' of UserFlags";
+ }
+ userInfo->flags = intUserFlags;
+ return {};
+}
+
+Result<void> parseUsersInfo(const std::vector<int32_t>& int32Values, size_t startPos,
+ UsersInfo* usersInfo) {
+ if (int32Values.size() < startPos + 3) {
+ return Error() << "Int32Values must have at least " << startPos + 3 << " values, received "
+ << int32Values.size();
+ }
+ auto ret = parseUserInfo(int32Values, startPos, &usersInfo->currentUser);
+ if (!ret.ok()) {
+ return ret;
+ }
+ usersInfo->numberUsers = int32Values[startPos + 2];
+ usersInfo->existingUsers.resize(usersInfo->numberUsers);
+ for (size_t i = 0; i < static_cast<size_t>(usersInfo->numberUsers); ++i) {
+ ret = parseUserInfo(int32Values, startPos + 3 + (kNumFieldsPerUserInfo * i),
+ &usersInfo->existingUsers[i]);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse existing user '" << i << "' info: " << ret.error();
+ }
+ }
+ return {};
+}
+
+Result<void> parseUserAssociationTypes(
+ const std::vector<int32_t>& int32Values, size_t startPos, size_t numberAssociationTypes,
+ std::vector<UserIdentificationAssociationType>* associationTypes) {
+ size_t minInt32Values = startPos + numberAssociationTypes;
+ if (int32Values.size() < minInt32Values) {
+ return Error() << "Int32Values must have at least " << minInt32Values
+ << " values, received " << int32Values.size();
+ }
+ associationTypes->resize(numberAssociationTypes);
+ for (size_t i = 0; i < static_cast<size_t>(numberAssociationTypes); ++i) {
+ size_t pos = startPos + i;
+ auto type = verifyAndCast<UserIdentificationAssociationType>(int32Values[pos]);
+ if (!type.ok()) {
+ return Error() << "Invalid association type in query '" << i << "': " << type.error();
+ }
+ (*associationTypes)[i] = *type;
+ }
+ return {};
+}
+
+Result<void> parseUserAssociations(const std::vector<int32_t>& int32Values, size_t startPos,
+ size_t numberAssociations,
+ std::vector<UserIdentificationSetAssociation>* associations) {
+ size_t minInt32Values = startPos + (numberAssociations * kNumFieldsPerSetAssociation);
+ if (int32Values.size() < minInt32Values) {
+ return Error() << "Int32Values must have at least " << minInt32Values
+ << " values, received " << int32Values.size();
+ }
+ associations->resize(numberAssociations);
+ for (size_t i = 0; i < static_cast<size_t>(numberAssociations); ++i) {
+ size_t pos = startPos + (kNumFieldsPerSetAssociation * i);
+ auto type = verifyAndCast<UserIdentificationAssociationType>(int32Values[pos]);
+ if (!type.ok()) {
+ return Error() << "Invalid association type in request '" << i << "': " << type.error();
+ }
+ (*associations)[i].type = *type;
+ auto value = verifyAndCast<UserIdentificationAssociationSetValue>(int32Values[pos + 1]);
+ if (!value.ok()) {
+ return Error() << "Invalid association set value in request '" << i
+ << "': " << value.error();
+ }
+ (*associations)[i].value = *value;
+ }
+ return {};
+}
+
+} // namespace
+
+template <typename T>
+Result<T> verifyAndCast(int32_t value) {
+ T castValue = static_cast<T>(value);
+ for (const auto& v : ::ndk::enum_range<T>()) {
+ if (castValue == v) {
+ return castValue;
+ }
+ }
+
+ return Error() << "Value " << value << " not in enum values";
+}
+
+Result<InitialUserInfoRequest> toInitialUserInfoRequest(const VehiclePropValue& propValue) {
+ auto ret = verifyPropValue(propValue, VehicleProperty::INITIAL_USER_INFO, /*minInt32Values=*/2);
+ if (!ret.ok()) {
+ return ret.error();
+ }
+ InitialUserInfoRequest request;
+ request.requestId = propValue.value.int32Values[0];
+ auto requestType = verifyAndCast<InitialUserInfoRequestType>(propValue.value.int32Values[1]);
+ if (!requestType.ok()) {
+ return Error() << "Invalid InitialUserInfoRequestType: " << requestType.error();
+ }
+ request.requestType = *requestType;
+ ret = parseUsersInfo(propValue.value.int32Values, 2, &request.usersInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse users info: " << ret.error();
+ }
+ return request;
+}
+
+Result<SwitchUserRequest> toSwitchUserRequest(const VehiclePropValue& propValue) {
+ auto ret = verifyPropValue(propValue, VehicleProperty::SWITCH_USER, /*minInt32Values=*/2);
+ if (!ret.ok()) {
+ return ret.error();
+ }
+ SwitchUserRequest request;
+ auto messageType = verifyAndCast<SwitchUserMessageType>(propValue.value.int32Values[1]);
+ if (!messageType.ok()) {
+ return Error() << "Invalid SwitchUserMessageType: " << messageType.error();
+ }
+ if (*messageType != SwitchUserMessageType::LEGACY_ANDROID_SWITCH &&
+ *messageType != SwitchUserMessageType::ANDROID_SWITCH &&
+ *messageType != SwitchUserMessageType::ANDROID_POST_SWITCH) {
+ return Error() << "Invalid " << toString(*messageType)
+ << " message type from Android System";
+ }
+ request.requestId = propValue.value.int32Values[0];
+ request.messageType = *messageType;
+ ret = parseUserInfo(propValue.value.int32Values, /*startPos=*/2, &request.targetUser);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse target user info: " << ret.error();
+ }
+ ret = parseUsersInfo(propValue.value.int32Values, /*startPos=*/4, &request.usersInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse users info: " << ret.error();
+ }
+ return request;
+}
+
+Result<CreateUserRequest> toCreateUserRequest(const VehiclePropValue& propValue) {
+ auto ret = verifyPropValue(propValue, VehicleProperty::CREATE_USER, /*minInt32Values=*/1);
+ if (!ret.ok()) {
+ return ret.error();
+ }
+ CreateUserRequest request;
+ request.requestId = propValue.value.int32Values[0];
+ ret = parseUserInfo(propValue.value.int32Values, /*startPos=*/1, &request.newUserInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse new user info: " << ret.error();
+ }
+ request.newUserName = propValue.value.stringValue;
+ ret = parseUsersInfo(propValue.value.int32Values, /*startPos=*/3, &request.usersInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse users info: " << ret.error();
+ }
+ return request;
+}
+
+Result<RemoveUserRequest> toRemoveUserRequest(const VehiclePropValue& propValue) {
+ auto ret = verifyPropValue(propValue, VehicleProperty::REMOVE_USER, /*minInt32Values=*/1);
+ if (!ret.ok()) {
+ return ret.error();
+ }
+ RemoveUserRequest request;
+ request.requestId = propValue.value.int32Values[0];
+ ret = parseUserInfo(propValue.value.int32Values, /*startPos=*/1, &request.removedUserInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse removed user info: " << ret.error();
+ }
+ ret = parseUsersInfo(propValue.value.int32Values, /*startPos=*/3, &request.usersInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse users info: " << ret.error();
+ }
+ return request;
+}
+
+Result<UserIdentificationGetRequest> toUserIdentificationGetRequest(
+ const VehiclePropValue& propValue) {
+ auto ret = verifyPropValue(propValue, VehicleProperty::USER_IDENTIFICATION_ASSOCIATION,
+ /*minInt32Values=*/4);
+ if (!ret.ok()) {
+ return ret.error();
+ }
+ UserIdentificationGetRequest request;
+ request.requestId = propValue.value.int32Values[0];
+ ret = parseUserInfo(propValue.value.int32Values, /*startPos=*/1, &request.userInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse user info: " << ret.error();
+ }
+ request.numberAssociationTypes = propValue.value.int32Values[3];
+ ret = parseUserAssociationTypes(propValue.value.int32Values, 4, request.numberAssociationTypes,
+ &request.associationTypes);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse UserIdentificationAssociationType: " << ret.error();
+ }
+ return request;
+}
+
+Result<UserIdentificationSetRequest> toUserIdentificationSetRequest(
+ const VehiclePropValue& propValue) {
+ auto ret = verifyPropValue(propValue, VehicleProperty::USER_IDENTIFICATION_ASSOCIATION,
+ /*minInt32Values=*/4);
+ if (!ret.ok()) {
+ return ret.error();
+ }
+ UserIdentificationSetRequest request;
+ request.requestId = propValue.value.int32Values[0];
+ ret = parseUserInfo(propValue.value.int32Values, /*startPos=*/1, &request.userInfo);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse user info: " << ret.error();
+ }
+ request.numberAssociations = propValue.value.int32Values[3];
+ ret = parseUserAssociations(propValue.value.int32Values, 4, request.numberAssociations,
+ &request.associations);
+ if (!ret.ok()) {
+ return Error() << "Failed to parse UserIdentificationSetAssociation: " << ret.error();
+ }
+ return request;
+}
+
+Result<VehiclePropValuePool::RecyclableType> toVehiclePropValue(VehiclePropValuePool& pool,
+ const SwitchUserRequest& request) {
+ if (request.messageType != SwitchUserMessageType::VEHICLE_REQUEST) {
+ return Errorf("Invalid %s message type %s from HAL",
+ toString(VehicleProperty::SWITCH_USER).c_str(),
+ toString(request.messageType).c_str());
+ }
+ int32_t switchUserProp = toInt(VehicleProperty::SWITCH_USER);
+ auto propValue = pool.obtain(getPropType(switchUserProp));
+ propValue->prop = switchUserProp;
+ propValue->timestamp = elapsedRealtimeNano();
+ propValue->value.int32Values.resize(3);
+ propValue->value.int32Values[0] = static_cast<int32_t>(request.requestId);
+ propValue->value.int32Values[1] = static_cast<int32_t>(request.messageType);
+ propValue->value.int32Values[2] = static_cast<int32_t>(request.targetUser.userId);
+ return propValue;
+}
+
+VehiclePropValuePool::RecyclableType toVehiclePropValue(VehiclePropValuePool& pool,
+ const InitialUserInfoResponse& response) {
+ int32_t initialUserInfoProp = toInt(VehicleProperty::INITIAL_USER_INFO);
+ auto propValue = pool.obtain(getPropType(initialUserInfoProp));
+ propValue->prop = initialUserInfoProp;
+ propValue->timestamp = elapsedRealtimeNano();
+ propValue->value.int32Values.resize(4);
+ propValue->value.int32Values[0] = static_cast<int32_t>(response.requestId);
+ propValue->value.int32Values[1] = static_cast<int32_t>(response.action);
+ propValue->value.int32Values[2] = static_cast<int32_t>(response.userToSwitchOrCreate.userId);
+ propValue->value.int32Values[3] = response.userToSwitchOrCreate.flags;
+ propValue->value.stringValue = std::string(response.userLocales) + std::string(kSeparator) +
+ std::string(response.userNameToCreate);
+ return propValue;
+}
+
+VehiclePropValuePool::RecyclableType toVehiclePropValue(VehiclePropValuePool& pool,
+ const SwitchUserResponse& response) {
+ int32_t switchUserProp = toInt(VehicleProperty::SWITCH_USER);
+ auto propValue = pool.obtain(getPropType(switchUserProp));
+ propValue->prop = switchUserProp;
+ propValue->timestamp = elapsedRealtimeNano();
+ propValue->value.int32Values.resize(3);
+ propValue->value.int32Values[0] = static_cast<int32_t>(response.requestId);
+ propValue->value.int32Values[1] = static_cast<int32_t>(response.messageType);
+ propValue->value.int32Values[2] = static_cast<int32_t>(response.status);
+ if (response.status == SwitchUserStatus::FAILURE) {
+ propValue->value.stringValue = response.errorMessage;
+ }
+ return propValue;
+}
+
+VehiclePropValuePool::RecyclableType toVehiclePropValue(VehiclePropValuePool& pool,
+ const CreateUserResponse& response) {
+ int32_t createUserProp = toInt(VehicleProperty::CREATE_USER);
+ auto propValue = pool.obtain(getPropType(createUserProp));
+ propValue->prop = createUserProp;
+ propValue->timestamp = elapsedRealtimeNano();
+ propValue->value.int32Values.resize(2);
+ propValue->value.int32Values[0] = static_cast<int32_t>(response.requestId);
+ propValue->value.int32Values[1] = static_cast<int32_t>(response.status);
+ if (response.status == CreateUserStatus::FAILURE) {
+ propValue->value.stringValue = response.errorMessage;
+ }
+ return propValue;
+}
+
+VehiclePropValuePool::RecyclableType toVehiclePropValue(
+ VehiclePropValuePool& pool, const UserIdentificationResponse& response) {
+ int32_t userIdAssocProp = toInt(VehicleProperty::USER_IDENTIFICATION_ASSOCIATION);
+ auto propValue = pool.obtain(getPropType(userIdAssocProp));
+ propValue->prop = userIdAssocProp;
+ propValue->timestamp = elapsedRealtimeNano();
+ propValue->value.int32Values.resize(2 + (response.numberAssociation * 2));
+ propValue->value.int32Values[0] = static_cast<int32_t>(response.requestId);
+ propValue->value.int32Values[1] = static_cast<int32_t>(response.numberAssociation);
+ for (size_t i = 0; i < static_cast<size_t>(response.numberAssociation); ++i) {
+ size_t int32ValuesPos = 2 + (2 * i);
+ propValue->value.int32Values[int32ValuesPos] =
+ static_cast<int32_t>(response.associations[i].type);
+ propValue->value.int32Values[int32ValuesPos + 1] =
+ static_cast<int32_t>(response.associations[i].value);
+ }
+ if (!response.errorMessage.empty()) {
+ propValue->value.stringValue = response.errorMessage;
+ }
+ return propValue;
+}
+
+} // namespace user_hal_helper
+} // namespace fake
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/test/Android.bp b/automotive/vehicle/aidl/impl/fake_impl/userhal/test/Android.bp
new file mode 100644
index 0000000..7d0a534
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/test/Android.bp
@@ -0,0 +1,33 @@
+/*
+ * 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 {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_test {
+ name: "FakeUserHalTest",
+ vendor: true,
+ srcs: ["*.cpp"],
+ defaults: ["VehicleHalDefaults"],
+ static_libs: [
+ "FakeUserHal",
+ "VehicleHalUtils",
+ "libgtest",
+ "libgmock",
+ ],
+ test_suites: ["device-tests"],
+}
diff --git a/automotive/vehicle/aidl/impl/fake_impl/userhal/test/UserHalHelper_test.cpp b/automotive/vehicle/aidl/impl/fake_impl/userhal/test/UserHalHelper_test.cpp
new file mode 100644
index 0000000..2afb2e3
--- /dev/null
+++ b/automotive/vehicle/aidl/impl/fake_impl/userhal/test/UserHalHelper_test.cpp
@@ -0,0 +1,715 @@
+/*
+ * 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 "UserHalHelper.h"
+
+#include <VehicleUtils.h>
+#include <aidl/android/hardware/automotive/vehicle/InitialUserInfoResponseAction.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <cstdint>
+
+namespace android {
+namespace hardware {
+namespace automotive {
+namespace vehicle {
+namespace fake {
+namespace user_hal_helper {
+
+namespace {
+
+using ::testing::Eq;
+using ::testing::Gt;
+using ::testing::IsNull;
+using ::testing::NotNull;
+using ::testing::Pointee;
+
+using ::aidl::android::hardware::automotive::vehicle::CreateUserRequest;
+using ::aidl::android::hardware::automotive::vehicle::CreateUserResponse;
+using ::aidl::android::hardware::automotive::vehicle::CreateUserStatus;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoRequest;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoRequestType;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponse;
+using ::aidl::android::hardware::automotive::vehicle::InitialUserInfoResponseAction;
+using ::aidl::android::hardware::automotive::vehicle::RemoveUserRequest;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserMessageType;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserRequest;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserResponse;
+using ::aidl::android::hardware::automotive::vehicle::SwitchUserStatus;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationAssociationSetValue;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationAssociationType;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationAssociationValue;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationGetRequest;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationResponse;
+using ::aidl::android::hardware::automotive::vehicle::UserIdentificationSetRequest;
+using ::aidl::android::hardware::automotive::vehicle::UserInfo;
+using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
+using ::aidl::android::hardware::automotive::vehicle::VehiclePropValue;
+
+constexpr int32_t INITIAL_USER_INFO = toInt(VehicleProperty::INITIAL_USER_INFO);
+constexpr int32_t SWITCH_USER = toInt(VehicleProperty::SWITCH_USER);
+constexpr int32_t CREATE_USER = toInt(VehicleProperty::CREATE_USER);
+constexpr int32_t REMOVE_USER = toInt(VehicleProperty::REMOVE_USER);
+constexpr int32_t USER_IDENTIFICATION_ASSOCIATION =
+ toInt(VehicleProperty::USER_IDENTIFICATION_ASSOCIATION);
+
+constexpr int32_t FIRST_BOOT_AFTER_OTA = toInt(InitialUserInfoRequestType::FIRST_BOOT_AFTER_OTA);
+constexpr int32_t LEGACY_ANDROID_SWITCH = toInt(SwitchUserMessageType::LEGACY_ANDROID_SWITCH);
+constexpr int32_t VEHICLE_REQUEST = toInt(SwitchUserMessageType::VEHICLE_REQUEST);
+
+constexpr int32_t NONE_USER = 0;
+constexpr int32_t GUEST_USER = toInt(UserInfo::USER_FLAG_GUEST);
+constexpr int32_t SYSTEM_USER = toInt(UserInfo::USER_FLAG_SYSTEM);
+constexpr int32_t SYSTEM_ADMIN_USER = SYSTEM_USER | toInt(UserInfo::USER_FLAG_ADMIN);
+// 0x1111 is not a valid UserFlags combination.
+constexpr int32_t INVALID_USER_FLAG = 0x1111;
+
+constexpr int32_t USER_ID_ASSOC_KEY_FOB = toInt(UserIdentificationAssociationType::KEY_FOB);
+constexpr int32_t USER_ID_ASSOC_CUSTOM_1 = toInt(UserIdentificationAssociationType::CUSTOM_1);
+
+constexpr int32_t USER_ID_ASSOC_SET_CURRENT_USER =
+ toInt(UserIdentificationAssociationSetValue::ASSOCIATE_CURRENT_USER);
+constexpr int32_t USER_ID_ASSOC_UNSET_CURRENT_USER =
+ toInt(UserIdentificationAssociationSetValue::DISASSOCIATE_CURRENT_USER);
+
+constexpr int32_t USER_ID_ASSOC_CURRENT_USER =
+ toInt(UserIdentificationAssociationValue::ASSOCIATED_CURRENT_USER);
+constexpr int32_t USER_ID_ASSOC_NO_USER =
+ toInt(UserIdentificationAssociationValue::NOT_ASSOCIATED_ANY_USER);
+
+} // namespace
+
+TEST(UserHalHelperTest, TestToInitialUserInfoRequest) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0, SYSTEM_USER,
+ 10, NONE_USER}},
+ };
+ InitialUserInfoRequest expected{
+ .requestId = 23,
+ .requestType = InitialUserInfoRequestType::FIRST_BOOT_AFTER_OTA,
+ .usersInfo = {{10, 0}, 2, {{0, SYSTEM_USER}, {10, 0}}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestToInitialUserInfoRequestUserFlagsBitCombination) {
+ // SYSTEM_ADMIN_USER is two UserFlags combined.
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0,
+ SYSTEM_ADMIN_USER, 10, NONE_USER}},
+ };
+ InitialUserInfoRequest expected{
+ .requestId = 23,
+ .requestType = InitialUserInfoRequestType::FIRST_BOOT_AFTER_OTA,
+ .usersInfo = {{10, 0}, 2, {{0, SYSTEM_ADMIN_USER}, {10, 0}}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestToInitialUserInfoRequestUserInvalidUserFlag) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0,
+ INVALID_USER_FLAG, 10, NONE_USER}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on invalid user flags";
+}
+
+TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithMismatchingPropType) {
+ VehiclePropValue propValue{
+ .prop = INT32_MAX,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0, SYSTEM_USER,
+ 10, NONE_USER}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
+}
+
+TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithInvalidRequestType) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, INT32_MAX, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
+ NONE_USER}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on invalid request type";
+}
+
+TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithInvalidUserFlag) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0, SYSTEM_USER,
+ 10, INT32_MAX}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on invalid user flags";
+}
+
+TEST(UserHalHelperTest, TestFailsToInitialUserInfoRequestWithIncompleteUsersInfo) {
+ VehiclePropValue propValueMissingSecondUserInfo{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, 10, NONE_USER, 2, 0,
+ SYSTEM_USER /*Missing 2nd UserInfo*/}},
+ };
+
+ auto actual = toInitialUserInfoRequest(propValueMissingSecondUserInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
+
+ VehiclePropValue propValueMissingUsersInfo{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, FIRST_BOOT_AFTER_OTA, /*Missing UsersInfo*/}},
+ };
+
+ actual = toInitialUserInfoRequest(propValueMissingUsersInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
+}
+
+TEST(UserHalHelperTest, TestToSwitchUserRequest) {
+ VehiclePropValue propValue{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER, 10, NONE_USER, 2,
+ 0, SYSTEM_USER, 10, NONE_USER}},
+ };
+ SwitchUserRequest expected{
+ .requestId = 23,
+ .messageType = SwitchUserMessageType::LEGACY_ANDROID_SWITCH,
+ .targetUser = {0, SYSTEM_USER},
+ .usersInfo = {{10, 0}, 2, {{0, SYSTEM_USER}, {10, 0}}},
+ };
+
+ auto actual = toSwitchUserRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestFailsToSwitchUserRequestWithMismatchingPropType) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER, 10, NONE_USER, 2,
+ 0, SYSTEM_USER, 10, NONE_USER}},
+ };
+
+ auto actual = toSwitchUserRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
+}
+
+TEST(UserHalHelperTest, TestFailsToSwitchUserRequestWithInvalidMessageType) {
+ VehiclePropValue propValueIncompatibleMessageType{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23, VEHICLE_REQUEST, 0, SYSTEM_USER, 10, NONE_USER, 2, 0,
+ SYSTEM_USER, 10, NONE_USER}},
+ };
+
+ auto actual = toSwitchUserRequest(propValueIncompatibleMessageType);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on incompatible message type";
+
+ VehiclePropValue propValueInvalidMessageType{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23, INT32_MAX, 0, SYSTEM_USER, 10, NONE_USER, 2, 0,
+ SYSTEM_USER, 10, NONE_USER}},
+ };
+
+ actual = toSwitchUserRequest(propValueInvalidMessageType);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on invalid message type";
+}
+
+TEST(UserHalHelperTest, TestFailsToSwitchUserRequestWithIncompleteUsersInfo) {
+ VehiclePropValue propValueMissingSecondUserInfo{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER, 10, NONE_USER, 2,
+ 0, SYSTEM_USER,
+ /*Missing 2nd UserInfo*/}},
+ };
+
+ auto actual = toSwitchUserRequest(propValueMissingSecondUserInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
+
+ VehiclePropValue propValueMissingUsersInfo{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, 0, SYSTEM_USER,
+ /*Missing UsersInfo*/}},
+ };
+
+ actual = toSwitchUserRequest(propValueMissingUsersInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
+
+ VehiclePropValue propValueMissingTargetUser{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23, LEGACY_ANDROID_SWITCH, /*Missing target UserInfo*/}},
+ };
+
+ actual = toSwitchUserRequest(propValueMissingTargetUser);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing target user info";
+}
+
+TEST(UserHalHelperTest, TestToCreateUserRequest) {
+ VehiclePropValue propValue{
+ .prop = CREATE_USER,
+ .value = {.int32Values = {23, 11, GUEST_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
+ NONE_USER},
+ .stringValue = "Guest11"},
+ };
+ CreateUserRequest expected{
+ .requestId = 23,
+ .newUserInfo = {11, GUEST_USER},
+ .newUserName = "Guest11",
+ .usersInfo = {{10, 0}, 2, {{0, SYSTEM_USER}, {10, 0}}},
+ };
+
+ auto actual = toCreateUserRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestFailsToCreateUserRequestWithMismatchingPropType) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, 11, GUEST_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
+ NONE_USER},
+ .stringValue = "Guest11"},
+ };
+
+ auto actual = toCreateUserRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
+}
+
+TEST(UserHalHelperTest, TestFailsToCreateUserRequestWithIncompleteUsersInfo) {
+ VehiclePropValue propValueMissingSecondUserInfo{
+ .prop = CREATE_USER,
+ .value = {.int32Values = {23, 11, GUEST_USER, 10, NONE_USER, 2, 0,
+ SYSTEM_USER /*Missing 2nd UserInfo*/},
+ .stringValue = "Guest11"},
+ };
+
+ auto actual = toCreateUserRequest(propValueMissingSecondUserInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
+
+ VehiclePropValue propValueMissingUsersInfo{
+ .prop = CREATE_USER,
+ .value = {.int32Values = {23, 11, GUEST_USER, /*Missing UsersInfo*/},
+ .stringValue = "Guest11"},
+ };
+
+ actual = toCreateUserRequest(propValueMissingUsersInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
+
+ VehiclePropValue propValueMissingCreateUserInfo{
+ .prop = CREATE_USER,
+ .value = {.int32Values = {23, /*Missing create UserInfo*/}, .stringValue = "Guest11"},
+ };
+
+ actual = toCreateUserRequest(propValueMissingCreateUserInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing create user info";
+}
+
+TEST(UserHalHelperTest, TestToRemoveUserRequest) {
+ VehiclePropValue propValue{
+ .prop = REMOVE_USER,
+ .value = {.int32Values = {23, 10, NONE_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
+ NONE_USER}},
+ };
+ RemoveUserRequest expected{
+ .requestId = 23,
+ .removedUserInfo = {10, 0},
+ .usersInfo = {{10, 0}, 2, {{0, SYSTEM_USER}, {10, 0}}},
+ };
+
+ auto actual = toRemoveUserRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestFailsToRemoveUserRequestWithMismatchingPropType) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, 10, NONE_USER, 10, NONE_USER, 2, 0, SYSTEM_USER, 10,
+ NONE_USER}},
+ };
+
+ auto actual = toRemoveUserRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
+}
+
+TEST(UserHalHelperTest, TestFailsToRemoveUserRequestWithIncompleteUsersInfo) {
+ VehiclePropValue propValueMissingSecondUserInfo{
+ .prop = REMOVE_USER,
+ .value = {.int32Values = {23, 10, NONE_USER, 10, NONE_USER, 2, 0,
+ SYSTEM_USER /*Missing 2nd UserInfo*/}},
+ };
+
+ auto actual = toRemoveUserRequest(propValueMissingSecondUserInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing second user info";
+
+ VehiclePropValue propValueMissingUsersInfo{
+ .prop = REMOVE_USER,
+ .value = {.int32Values = {23, 10, NONE_USER, /*Missing UsersInfo*/}},
+ };
+
+ actual = toRemoveUserRequest(propValueMissingUsersInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing users info";
+
+ VehiclePropValue propValueMissingRemoveUserInfo{
+ .prop = REMOVE_USER,
+ .value = {.int32Values = {23, /*Missing remove UserInfo*/}},
+ };
+
+ actual = toRemoveUserRequest(propValueMissingRemoveUserInfo);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing remove user info";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequest) {
+ VehiclePropValue propValue{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
+ USER_ID_ASSOC_CUSTOM_1}},
+ };
+ UserIdentificationGetRequest expected{
+ .requestId = 23,
+ .userInfo = {10, 0},
+ .numberAssociationTypes = 2,
+ .associationTypes = {UserIdentificationAssociationType::KEY_FOB,
+ UserIdentificationAssociationType::CUSTOM_1},
+ };
+
+ auto actual = toUserIdentificationGetRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithMismatchingPropType) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
+ USER_ID_ASSOC_CUSTOM_1}},
+ };
+
+ auto actual = toUserIdentificationGetRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithInvalidAssociationTypes) {
+ VehiclePropValue propValue{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, 1, INT32_MAX}},
+ };
+
+ auto actual = toUserIdentificationGetRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on invalid association type";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithIncompleteAssociationTypes) {
+ VehiclePropValue propValueMissingSecondAssociationType{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, 2,
+ USER_ID_ASSOC_KEY_FOB /*Missing 2nd association type*/}},
+ };
+
+ auto actual = toUserIdentificationGetRequest(propValueMissingSecondAssociationType);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing second association type";
+
+ VehiclePropValue propValueMissingNumberAssociationTypes{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, /*Missing number association types*/}},
+ };
+
+ actual = toUserIdentificationGetRequest(propValueMissingNumberAssociationTypes);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing number association types";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationGetRequestWithMissingUserInfo) {
+ VehiclePropValue propValue{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, /*Missing user info*/}},
+ };
+
+ auto actual = toUserIdentificationGetRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing UserInfo";
+}
+
+TEST(UserHalHelperTest, TestToUserIdentificationSetRequest) {
+ VehiclePropValue propValue{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
+ USER_ID_ASSOC_SET_CURRENT_USER, USER_ID_ASSOC_CUSTOM_1,
+ USER_ID_ASSOC_UNSET_CURRENT_USER}},
+ };
+ UserIdentificationSetRequest expected{
+ .requestId = 23,
+ .userInfo = {10, 0},
+ .numberAssociations = 2,
+ .associations = {{UserIdentificationAssociationType::KEY_FOB,
+ UserIdentificationAssociationSetValue::ASSOCIATE_CURRENT_USER},
+ {UserIdentificationAssociationType::CUSTOM_1,
+ UserIdentificationAssociationSetValue::DISASSOCIATE_CURRENT_USER}},
+ };
+
+ auto actual = toUserIdentificationSetRequest(propValue);
+
+ ASSERT_TRUE(actual.ok()) << actual.error().message();
+ EXPECT_THAT(actual.value(), Eq(expected));
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithMismatchingPropType) {
+ VehiclePropValue propValue{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
+ USER_ID_ASSOC_SET_CURRENT_USER, USER_ID_ASSOC_CUSTOM_1,
+ USER_ID_ASSOC_UNSET_CURRENT_USER}},
+ };
+
+ auto actual = toUserIdentificationSetRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on mismatching property type";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithInvalidAssociations) {
+ VehiclePropValue propValueInvalidAssociationType{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, 1, INT32_MAX,
+ USER_ID_ASSOC_SET_CURRENT_USER}},
+ };
+
+ auto actual = toUserIdentificationSetRequest(propValueInvalidAssociationType);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on invalid association type";
+
+ VehiclePropValue propValueInvalidAssociationValue{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, USER_ID_ASSOC_KEY_FOB, INT32_MAX}},
+ };
+
+ actual = toUserIdentificationSetRequest(propValueInvalidAssociationValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing number association types";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithIncompleteAssociations) {
+ VehiclePropValue propValueMissingSecondAssociationType{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, 2, USER_ID_ASSOC_KEY_FOB,
+ USER_ID_ASSOC_SET_CURRENT_USER,
+ /*Missing 2nd association*/}},
+ };
+
+ auto actual = toUserIdentificationSetRequest(propValueMissingSecondAssociationType);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing second association type";
+
+ VehiclePropValue propValueMissingNumberAssociationTypes{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 10, NONE_USER, /*Missing number associations*/}},
+ };
+
+ actual = toUserIdentificationSetRequest(propValueMissingNumberAssociationTypes);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing number association types";
+}
+
+TEST(UserHalHelperTest, TestFailsToUserIdentificationSetRequestWithMissingUserInfo) {
+ VehiclePropValue propValue{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, /*Missing user info*/}},
+ };
+
+ auto actual = toUserIdentificationSetRequest(propValue);
+
+ EXPECT_FALSE(actual.ok()) << "No error returned on missing UserInfo";
+}
+
+TEST(UserHalHelperTest, TestSwitchUserRequestToVehiclePropValue) {
+ SwitchUserRequest request{
+ .requestId = 23,
+ .messageType = SwitchUserMessageType::VEHICLE_REQUEST,
+ .targetUser = {11, GUEST_USER},
+ };
+ VehiclePropValue expected{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23,
+ static_cast<int32_t>(SwitchUserMessageType::VEHICLE_REQUEST),
+ 11}},
+ };
+
+ VehiclePropValuePool pool;
+ auto result = toVehiclePropValue(pool, request);
+
+ ASSERT_TRUE(result.ok());
+ EXPECT_THAT(result.value()->timestamp, Gt(0));
+ // Don't rely on real timestamp in tests as the expected and actual objects won't have the same
+ // timestamps. Thus remove the timestamps before comparing them.
+ result.value()->timestamp = 0;
+ EXPECT_THAT(result.value(), Pointee(Eq(expected)));
+}
+
+TEST(UserHalHelperTest, TestFailsSwitchUserRequestToVehiclePropValueWithIncompatibleMessageType) {
+ SwitchUserRequest request{
+ .requestId = 23,
+ .messageType = SwitchUserMessageType::VEHICLE_RESPONSE,
+ .targetUser = {11, GUEST_USER},
+ };
+
+ VehiclePropValuePool pool;
+ auto result = toVehiclePropValue(pool, request);
+
+ EXPECT_FALSE(result.ok());
+}
+
+TEST(UserHalHelperTest, TestInitialUserInfoResponseToVehiclePropValue) {
+ InitialUserInfoResponse response{
+ .requestId = 23,
+ .action = InitialUserInfoResponseAction::CREATE,
+ .userToSwitchOrCreate = {11, GUEST_USER},
+ .userLocales = "en-US,pt-BR",
+ .userNameToCreate = "Owner",
+ };
+ VehiclePropValue expected{
+ .prop = INITIAL_USER_INFO,
+ .value = {.int32Values = {23,
+ static_cast<int32_t>(InitialUserInfoResponseAction::CREATE),
+ 11, GUEST_USER},
+ .stringValue = "en-US,pt-BR||Owner"},
+ };
+
+ VehiclePropValuePool pool;
+ auto actual = toVehiclePropValue(pool, response);
+
+ ASSERT_THAT(actual, NotNull());
+ EXPECT_THAT(actual->timestamp, Gt(0));
+ actual->timestamp = 0;
+ EXPECT_THAT(actual, Pointee(Eq(expected)));
+}
+
+TEST(UserHalHelperTest, TestSwitchUserResponseToVehiclePropValue) {
+ SwitchUserResponse response{
+ .requestId = 23,
+ .messageType = SwitchUserMessageType::VEHICLE_RESPONSE,
+ .status = SwitchUserStatus::FAILURE,
+ .errorMessage = "random error",
+ };
+ VehiclePropValue expected{
+ .prop = SWITCH_USER,
+ .value = {.int32Values = {23,
+ static_cast<int32_t>(SwitchUserMessageType::VEHICLE_RESPONSE),
+ static_cast<int32_t>(SwitchUserStatus::FAILURE)},
+ .stringValue = "random error"},
+ };
+
+ VehiclePropValuePool pool;
+ auto actual = toVehiclePropValue(pool, response);
+
+ ASSERT_THAT(actual, NotNull());
+ EXPECT_THAT(actual->timestamp, Gt(0));
+ actual->timestamp = 0;
+ EXPECT_THAT(actual, Pointee(Eq(expected)));
+}
+
+TEST(UserHalHelperTest, TestCreateUserResponseToVehiclePropValue) {
+ CreateUserResponse response{
+ .requestId = 23,
+ .status = CreateUserStatus::FAILURE,
+ .errorMessage = "random error",
+ };
+ VehiclePropValue expected{
+ .prop = CREATE_USER,
+ .value = {.int32Values = {23, static_cast<int32_t>(CreateUserStatus::FAILURE)},
+ .stringValue = "random error"},
+ };
+
+ VehiclePropValuePool pool;
+ auto actual = toVehiclePropValue(pool, response);
+
+ ASSERT_THAT(actual, NotNull());
+ EXPECT_THAT(actual->timestamp, Gt(0));
+ actual->timestamp = 0;
+ EXPECT_THAT(actual, Pointee(Eq(expected)));
+}
+
+TEST(UserHalHelperTest, TestUserIdentificationResponseToVehiclePropValue) {
+ UserIdentificationResponse response{
+ .requestId = 23,
+ .numberAssociation = 2,
+ .associations = {{UserIdentificationAssociationType::KEY_FOB,
+ UserIdentificationAssociationValue::ASSOCIATED_CURRENT_USER},
+ {UserIdentificationAssociationType::CUSTOM_1,
+ UserIdentificationAssociationValue::NOT_ASSOCIATED_ANY_USER}},
+ .errorMessage = "random error",
+ };
+ VehiclePropValue expected{
+ .prop = USER_IDENTIFICATION_ASSOCIATION,
+ .value = {.int32Values = {23, 2, USER_ID_ASSOC_KEY_FOB, USER_ID_ASSOC_CURRENT_USER,
+ USER_ID_ASSOC_CUSTOM_1, USER_ID_ASSOC_NO_USER},
+ .stringValue = "random error"},
+ };
+
+ VehiclePropValuePool pool;
+ auto actual = toVehiclePropValue(pool, response);
+
+ ASSERT_THAT(actual, NotNull());
+ EXPECT_THAT(actual->timestamp, Gt(0));
+ actual->timestamp = 0;
+ EXPECT_THAT(actual, Pointee(Eq(expected)));
+}
+
+} // namespace user_hal_helper
+} // namespace fake
+} // namespace vehicle
+} // namespace automotive
+} // namespace hardware
+} // namespace android