From bf78c99bc20f4f1b00006b3a4b79f5178a349f4e Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Fri, 27 Aug 2021 20:24:37 +0900 Subject: [PATCH] Add new parcel functions Adds: - rpc_port_parcel_get_raw() - rpc_port_parcel_create_from_raw() Change-Id: I1568112a40405e5fbaef3c47a46fc2a23bf68fa6 Signed-off-by: Hwankyu Jhun --- include/rpc-port-parcel.h | 30 +++++++++++ src/rpc-port-parcel.cc | 35 +++++++++++++ test/unit_tests/rpc_port_parcel_test.cc | 67 +++++++++++++++++++++++++ 3 files changed, 132 insertions(+) diff --git a/include/rpc-port-parcel.h b/include/rpc-port-parcel.h index 7f1193d..73fdbfe 100644 --- a/include/rpc-port-parcel.h +++ b/include/rpc-port-parcel.h @@ -511,6 +511,36 @@ int rpc_port_parcel_header_get_seq_num(rpc_port_parcel_header_h header, int *seq */ int rpc_port_parcel_header_get_timestamp(rpc_port_parcel_header_h header, struct timespec *timestamp); +/** + * @brief Gets the raw data of the rpc port parcel handle. + * @since_tizen 6.5 + * @remarks You MUST NOT release @a raw using free(). It's managed by platform. + * @param[in] h The rpc port parcel handle + * @param[out] raw The raw data + * @param[out] size The size of the raw data + * @return @c 0 on success, + * otherwise a negative error value + * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter + */ +int rpc_port_parcel_get_raw(rpc_port_parcel_h h, void **raw, unsigned int *size); + +/** + * @brief Creates the rpc port parcel handle with given the raw data. + * @since_tizen 6.5 + * @remarks You must release @a h using rpc_port_parcel_destroy(). + * @param[out] h The rpc port parcel handle + * @param[in] raw The raw data + * @param[in] size The size of the raw data + * @return @c 0 on success, + * otherwise a negative error value + * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #RPC_PORT_ERROR_OUT_OF_MEMORY Out of memory + * @see rpc_port_parcel_destroy() + */ +int rpc_port_parcel_create_from_raw(rpc_port_parcel_h *h, const void *raw, unsigned int size); + /** * @} */ diff --git a/src/rpc-port-parcel.cc b/src/rpc-port-parcel.cc index 4c33ab5..35361db 100644 --- a/src/rpc-port-parcel.cc +++ b/src/rpc-port-parcel.cc @@ -512,3 +512,38 @@ RPC_API int rpc_port_parcel_header_get_timestamp( *timestamp = parcel_header->GetTimeStamp(); return RPC_PORT_ERROR_NONE; } + +RPC_API int rpc_port_parcel_get_raw(rpc_port_parcel_h h, void** raw, + unsigned int* size) { + if (h == nullptr || raw == nullptr || size == nullptr) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + auto* parcel = static_cast(h); + void* raw_data = nullptr; + uint32_t raw_size = 0; + parcel_get_raw(parcel->GetHandle(), &raw_data, &raw_size); + + *raw = raw_data; + *size = static_cast(raw_size); + return RPC_PORT_ERROR_NONE; +} + +RPC_API int rpc_port_parcel_create_from_raw(rpc_port_parcel_h* h, + const void* raw, unsigned int size) { + if (h == nullptr || raw == nullptr || size == 0) + return RPC_PORT_ERROR_INVALID_PARAMETER; + + rpc_port_parcel_h parcel; + int ret = rpc_port_parcel_create(&parcel); + if (ret != RPC_PORT_ERROR_NONE) + return ret; + + ret = rpc_port_parcel_from_array(parcel, raw, size); + if (ret != RPC_PORT_ERROR_NONE) { + rpc_port_parcel_destroy(parcel); + return ret; + } + + *h = parcel; + return RPC_PORT_ERROR_NONE; +} diff --git a/test/unit_tests/rpc_port_parcel_test.cc b/test/unit_tests/rpc_port_parcel_test.cc index ffe8975..63038ca 100644 --- a/test/unit_tests/rpc_port_parcel_test.cc +++ b/test/unit_tests/rpc_port_parcel_test.cc @@ -465,3 +465,70 @@ TEST_F(ParcelTest, rpc_port_parcel_header_get_timestamp_N) { int ret = rpc_port_parcel_header_get_timestamp(nullptr, nullptr); ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER); } + +/* + * @testcase rpc_port_parcel_get_raw_P + * @description Gets the raw data from the rpc port parcel handle + * @apicovered rpc_port_parcel_get_raw + */ +TEST_F(ParcelTest, rpc_port_parcel_get_raw_P) { + int ret = rpc_port_parcel_write_string(handle_, "test"); + ASSERT_EQ(ret, RPC_PORT_ERROR_NONE); + + void* raw = nullptr; + unsigned int raw_size = 0; + ret = rpc_port_parcel_get_raw(handle_, &raw, &raw_size); + ASSERT_EQ(ret, RPC_PORT_ERROR_NONE); + ASSERT_NE(raw, nullptr); + ASSERT_NE(raw_size, 0); +} + +/* + * @testcase rpc_port_parcel_get_raw_N + * @description Gets the raw data from the rpc port parcel handle + * @apicovered rpc_port_parcel_get_raw + */ +TEST_F(ParcelTest, rpc_port_parcel_get_raw_N) { + int ret = rpc_port_parcel_get_raw(nullptr, nullptr, nullptr); + ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER); +} + +/* + * @testcase rpc_port_parcel_create_from_raw_P + * @description Creates the rpc parcel handle with given the raw data. + * @apicovered rpc_port_parcel_create_from_raw + */ +TEST_F(ParcelTest, rpc_port_parcel_create_from_raw_P) { + int ret = rpc_port_parcel_write_string(handle_, "test"); + ASSERT_EQ(ret, RPC_PORT_ERROR_NONE); + + void* raw = nullptr; + unsigned int raw_size = 0; + ret = rpc_port_parcel_get_raw(handle_, &raw, &raw_size); + ASSERT_EQ(ret, RPC_PORT_ERROR_NONE); + + rpc_port_parcel_h parcel = nullptr; + ret = rpc_port_parcel_create_from_raw(&parcel, raw, raw_size); + ASSERT_EQ(ret, RPC_PORT_ERROR_NONE); + + std::unique_ptr::type, + decltype(rpc_port_parcel_destroy)*> parcel_auto( + parcel, rpc_port_parcel_destroy); + + char* str = nullptr; + ret = rpc_port_parcel_read_string(parcel, &str); + std::unique_ptr str_auto(str, std::free); + ASSERT_EQ(ret, RPC_PORT_ERROR_NONE); + ASSERT_NE(str, nullptr); + ASSERT_EQ(std::string(str), "test"); +} + +/* + * @testcase rpc_port_parcel_create_from_raw_B + * @description Creates the rpc parcel handle with given the raw data. + * @apicovered rpc_port_parcel_create_from_raw + */ +TEST_F(ParcelTest, rpc_port_parcel_create_from_raw_N) { + int ret = rpc_port_parcel_create_from_raw(nullptr, nullptr, 0); + ASSERT_EQ(ret, RPC_PORT_ERROR_INVALID_PARAMETER); +} -- 2.34.1