From b418616ea468da83886c19c26528961ddc99a773 Mon Sep 17 00:00:00 2001 From: Nirav Atre Date: Fri, 8 Jun 2018 16:51:29 -0700 Subject: [PATCH] Test HAL for HIDL safe_union construct This change implements a basic HAL and server-side functionality to test the HIDL implementation of safe unions. Bug: 79878527 Test: Ran make, new tests in hidl_test/ pass successfully (included in a separate CL). Change-Id: Ia420137bc1dc0a188e04176081c7f5418e74449c --- tests/safeunion/1.0/.hidl_for_test | 0 tests/safeunion/1.0/Android.bp | 21 +++ tests/safeunion/1.0/ISafeUnion.hal | 38 ++++ tests/safeunion/1.0/default/Android.bp | 21 +++ tests/safeunion/1.0/default/SafeUnion.cpp | 204 ++++++++++++++++++++++ tests/safeunion/1.0/default/SafeUnion.h | 68 ++++++++ tests/safeunion/1.0/types.hal | 67 +++++++ 7 files changed, 419 insertions(+) create mode 100644 tests/safeunion/1.0/.hidl_for_test create mode 100644 tests/safeunion/1.0/Android.bp create mode 100644 tests/safeunion/1.0/ISafeUnion.hal create mode 100644 tests/safeunion/1.0/default/Android.bp create mode 100644 tests/safeunion/1.0/default/SafeUnion.cpp create mode 100644 tests/safeunion/1.0/default/SafeUnion.h create mode 100644 tests/safeunion/1.0/types.hal diff --git a/tests/safeunion/1.0/.hidl_for_test b/tests/safeunion/1.0/.hidl_for_test new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/safeunion/1.0/Android.bp b/tests/safeunion/1.0/Android.bp new file mode 100644 index 0000000000..5082f47212 --- /dev/null +++ b/tests/safeunion/1.0/Android.bp @@ -0,0 +1,21 @@ +// This file is autogenerated by hidl-gen -Landroidbp. + +hidl_interface { + name: "android.hardware.tests.safeunion@1.0", + root: "android.hardware", + srcs: [ + "types.hal", + "ISafeUnion.hal", + ], + interfaces: [ + "android.hidl.base@1.0", + ], + types: [ + "EmptySafeUnion", + "SmallSafeUnion", + "LargeSafeUnion", + "MiscTypesSafeUnion", + ], + gen_java: false, +} + diff --git a/tests/safeunion/1.0/ISafeUnion.hal b/tests/safeunion/1.0/ISafeUnion.hal new file mode 100644 index 0000000000..91d8b8cd69 --- /dev/null +++ b/tests/safeunion/1.0/ISafeUnion.hal @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2018 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.tests.safeunion@1.0; + +interface ISafeUnion { + newLargeSafeUnion() generates (LargeSafeUnion myUnion); + setA(LargeSafeUnion myUnion, int8_t a) generates (LargeSafeUnion myUnion); + setB(LargeSafeUnion myUnion, uint16_t b) generates (LargeSafeUnion myUnion); + setC(LargeSafeUnion myUnion, int32_t c) generates (LargeSafeUnion myUnion); + setD(LargeSafeUnion myUnion, uint64_t d) generates (LargeSafeUnion myUnion); + setE(LargeSafeUnion myUnion, int8_t[13] e) generates (LargeSafeUnion myUnion); + setF(LargeSafeUnion myUnion, int64_t[5] f) generates (LargeSafeUnion myUnion); + setG(LargeSafeUnion myUnion, string g) generates (LargeSafeUnion myUnion); + setH(LargeSafeUnion myUnion, vec h) generates (LargeSafeUnion myUnion); + setI(LargeSafeUnion myUnion, vec i) generates (LargeSafeUnion myUnion); + setJ(LargeSafeUnion myUnion, J j) generates (LargeSafeUnion myUnion); + setK(LargeSafeUnion myUnion, K k) generates (LargeSafeUnion myUnion); + setL(LargeSafeUnion myUnion, SmallSafeUnion l) generates (LargeSafeUnion myUnion); + + newMiscTypesSafeUnion() generates (MiscTypesSafeUnion myUnion); + setMiscA(MiscTypesSafeUnion myUnion, memory a) generates (MiscTypesSafeUnion myUnion); + setMiscB(MiscTypesSafeUnion myUnion, handle b) generates (MiscTypesSafeUnion myUnion); + setMiscC(MiscTypesSafeUnion myUnion, bitfield c) generates (MiscTypesSafeUnion myUnion); +}; diff --git a/tests/safeunion/1.0/default/Android.bp b/tests/safeunion/1.0/default/Android.bp new file mode 100644 index 0000000000..fc2443e7df --- /dev/null +++ b/tests/safeunion/1.0/default/Android.bp @@ -0,0 +1,21 @@ +cc_library { + name: "android.hardware.tests.safeunion@1.0-impl", + defaults: ["hidl_defaults"], + relative_install_path: "hw", + srcs: [ + "SafeUnion.cpp", + ], + shared_libs: [ + "libbase", + "libcutils", + "libhidlbase", + "libhidltransport", + "libhwbinder", + "liblog", + "libutils", + ], + + // These are static libs only for testing purposes and portability. Shared + // libs should be used on device. + static_libs: ["android.hardware.tests.safeunion@1.0"], +} diff --git a/tests/safeunion/1.0/default/SafeUnion.cpp b/tests/safeunion/1.0/default/SafeUnion.cpp new file mode 100644 index 0000000000..d968987f15 --- /dev/null +++ b/tests/safeunion/1.0/default/SafeUnion.cpp @@ -0,0 +1,204 @@ +/* + * Copyright (C) 2018 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 "SafeUnion.h" +#include + +namespace android { +namespace hardware { +namespace tests { +namespace safeunion { +namespace V1_0 { +namespace implementation { + +// Methods from ::android::hardware::tests::safeunion::V1_0::ISafeUnion follow. +Return SafeUnion::newLargeSafeUnion(newLargeSafeUnion_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) newLargeSafeUnion()"; + + LargeSafeUnion ret; + _hidl_cb(ret); + return Void(); +} + +Return SafeUnion::setA(const LargeSafeUnion& myUnion, int8_t a, setA_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setA(myUnion, " << a << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.a(a); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setB(const LargeSafeUnion& myUnion, uint16_t b, setB_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setB(myUnion, " << b << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.b(b); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setC(const LargeSafeUnion& myUnion, int32_t c, setC_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setC(myUnion, " << c << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.c(c); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setD(const LargeSafeUnion& myUnion, uint64_t d, setD_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setD(myUnion, " << d << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.d(d); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setE(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_array& e, setE_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setE(myUnion, " << toString(e) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.e(e); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setF(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_array& f, setF_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setF(myUnion, " << toString(f) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.f(f); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setG(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_string& g, setG_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setG(myUnion, " << toString(g) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.g(g); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setH(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_vec& h, setH_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setH(myUnion, " << toString(h) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.h(h); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setI(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const hidl_vec& i, setI_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setI(myUnion, " << toString(i) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.i(i); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setJ(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const J& j, setJ_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setJ(myUnion, " << toString(j) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.j(j); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setK(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const LargeSafeUnion::K& k, setK_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setK(myUnion, " << toString(k) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.k(k); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setL(const ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion& myUnion, const ::android::hardware::tests::safeunion::V1_0::SmallSafeUnion& l, setL_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setL(myUnion, " << toString(l) << ")"; + + LargeSafeUnion myNewUnion = myUnion; + myNewUnion.l(l); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::newMiscTypesSafeUnion(newMiscTypesSafeUnion_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) newMiscTypesSafeUnion()"; + + MiscTypesSafeUnion ret; + _hidl_cb(ret); + return Void(); +} + +Return SafeUnion::setMiscA(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_memory& a, setMiscA_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setMiscA(myUnion, " << toString(a) << ")"; + + MiscTypesSafeUnion myNewUnion = myUnion; + myNewUnion.a(a); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setMiscB(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_handle& b, setMiscB_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setMiscB(myUnion, " << toString(b) << ")"; + + MiscTypesSafeUnion myNewUnion = myUnion; + myNewUnion.b(b); + + _hidl_cb(myNewUnion); + return Void(); +} + +Return SafeUnion::setMiscC(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, hidl_bitfield c, setMiscC_cb _hidl_cb) { + LOG(INFO) << "SERVER(SafeUnion) setMiscC(myUnion, " << c << ")"; + + MiscTypesSafeUnion myNewUnion = myUnion; + myNewUnion.c(c); + + _hidl_cb(myNewUnion); + return Void(); +} + + +ISafeUnion* HIDL_FETCH_ISafeUnion(const char* /* name */) { + return new SafeUnion(); +} + +} // namespace implementation +} // namespace V1_0 +} // namespace safeunion +} // namespace tests +} // namespace hardware +} // namespace android diff --git a/tests/safeunion/1.0/default/SafeUnion.h b/tests/safeunion/1.0/default/SafeUnion.h new file mode 100644 index 0000000000..6b9997a466 --- /dev/null +++ b/tests/safeunion/1.0/default/SafeUnion.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 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_TESTS_SAFEUNION_V1_0_SAFEUNION_H +#define ANDROID_HARDWARE_TESTS_SAFEUNION_V1_0_SAFEUNION_H + +#include +#include +#include + +namespace android { +namespace hardware { +namespace tests { +namespace safeunion { +namespace V1_0 { +namespace implementation { + +using ::android::hardware::Return; +using ::android::hardware::Void; +using ::android::hardware::tests::safeunion::V1_0::SmallSafeUnion; +using ::android::hardware::tests::safeunion::V1_0::LargeSafeUnion; +using ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion; + +struct SafeUnion : public ISafeUnion { + // Methods from ::android::hardware::tests::safeunion::V1_0::ISafeUnion follow. + Return newLargeSafeUnion(newLargeSafeUnion_cb _hidl_cb) override; + Return setA(const LargeSafeUnion& myUnion, int8_t a, setA_cb _hidl_cb) override; + Return setB(const LargeSafeUnion& myUnion, uint16_t b, setB_cb _hidl_cb) override; + Return setC(const LargeSafeUnion& myUnion, int32_t c, setC_cb _hidl_cb) override; + Return setD(const LargeSafeUnion& myUnion, uint64_t d, setD_cb _hidl_cb) override; + Return setE(const LargeSafeUnion& myUnion, const hidl_array& e, setE_cb _hidl_cb) override; + Return setF(const LargeSafeUnion& myUnion, const hidl_array& f, setF_cb _hidl_cb) override; + Return setG(const LargeSafeUnion& myUnion, const hidl_string& g, setG_cb _hidl_cb) override; + Return setH(const LargeSafeUnion& myUnion, const hidl_vec& h, setH_cb _hidl_cb) override; + Return setI(const LargeSafeUnion& myUnion, const hidl_vec& i, setI_cb _hidl_cb) override; + Return setJ(const LargeSafeUnion& myUnion, const J& j, setJ_cb _hidl_cb) override; + Return setK(const LargeSafeUnion& myUnion, const LargeSafeUnion::K& k, setK_cb _hidl_cb) override; + Return setL(const LargeSafeUnion& myUnion, const SmallSafeUnion& l, setL_cb _hidl_cb) override; + + Return newMiscTypesSafeUnion(newMiscTypesSafeUnion_cb _hidl_cb) override; + Return setMiscA(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_memory& a, setMiscA_cb _hidl_cb) override; + Return setMiscB(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, const hidl_handle& b, setMiscB_cb _hidl_cb) override; + Return setMiscC(const ::android::hardware::tests::safeunion::V1_0::MiscTypesSafeUnion& myUnion, hidl_bitfield c, setMiscC_cb _hidl_cb) override; +}; + +extern "C" ISafeUnion* HIDL_FETCH_ISafeUnion(const char* name); + +} // namespace implementation +} // namespace V1_0 +} // namespace safeunion +} // namespace tests +} // namespace hardware +} // namespace android + +#endif // ANDROID_HARDWARE_TESTS_SAFEUNION_V1_0_SAFEUNION_H diff --git a/tests/safeunion/1.0/types.hal b/tests/safeunion/1.0/types.hal new file mode 100644 index 0000000000..a70079d744 --- /dev/null +++ b/tests/safeunion/1.0/types.hal @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2018 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.tests.safeunion@1.0; + +enum BitField : uint8_t { + V0 = 1 << 0, + V1 = 1 << 1, + V2 = 1 << 2, + V3 = 1 << 3, +}; + +struct J { + vec j1; + uint8_t[65] j2; + string j3; +}; + +safe_union EmptySafeUnion { +}; + +safe_union SmallSafeUnion { + uint8_t a; +}; + +safe_union LargeSafeUnion { + int8_t a; + uint16_t b; + int32_t c; + uint64_t d; + + int8_t[13] e; + int64_t[5] f; + + string g; + vec h; + vec i; + + J j; + struct K { + uint8_t k1; + uint64_t k2; + } k; + + SmallSafeUnion l; +}; + +// TODO(b/110269925): Test more HIDL types. Missing: +// death_recipient, fmq_{sync,unsync}, pointer, ref. +safe_union MiscTypesSafeUnion { + memory a; + handle b; + bitfield c; +};