From 02ad43688a9306bc083a9e4f357e7ed06764b9db Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Mon, 9 Jan 2023 17:22:50 +0900 Subject: [PATCH] include/uipc: Make uipc module extend ipc interface To avoid code duplication, the uipc module should use the encode and decode functions of the ipc module instead of declaring its own ones. This commit modifies the uipc module so that it extends the interface of the ipc module, using the functions provided by the latter. --- include/uipc.sh | 33 +++++++-------------------------- test/uipc_spec.sh | 22 +++++++++++----------- 2 files changed, 18 insertions(+), 37 deletions(-) diff --git a/include/uipc.sh b/include/uipc.sh index 6ee8f8d..4b998ab 100644 --- a/include/uipc.sh +++ b/include/uipc.sh @@ -26,37 +26,18 @@ __init() { declare -gxir __uipc_version=1 - return 0 -} - -_uipc_encode() { - local decoded="$1" - - if (( $# > 0 )); then - base64 -w 0 <<< "$decoded" - else - base64 -w 0 < /dev/stdin - fi -} - -_uipc_decode() { - local encoded="$1" + implements "ipc" - if (( $# > 0 )); then - base64 -d <<< "$encoded" - else - base64 -d < /dev/stdin - fi + return 0 } - _uipc_msg_get() { local msg="$1" local field="$2" local value - if ! value=$(_uipc_decode "$msg" | jq -e -r ".$field" 2>/dev/null); then + if ! value=$(ipc_decode "$msg" | jq -e -r ".$field" 2>/dev/null); then return 1 fi @@ -98,7 +79,7 @@ uipc_msg_dump() { cat </dev/null | - _uipc_encode) + ipc_encode) if ! is_base64 "$data"; then return 1 @@ -46,7 +46,7 @@ Describe "Encoding" The status should equal 0 End - It "_uipc_encode() output has correct length" + It "ipc_encode() output has correct length" _test_encoding_length() { local data local block_size @@ -62,7 +62,7 @@ Describe "Encoding" input_bits=$((input_bytes * 8)) actual_length=$(dd if=/dev/urandom bs="$block_size" count="$block_num" 2>/dev/null | - _uipc_encode | wc -c) + ipc_encode | wc -c) if (( input_bits % 24 > 0 )); then # data is padded @@ -81,12 +81,12 @@ Describe "Encoding" The status should equal 0 End - It "_uipc_encode() output does not contain newlines" + It "ipc_encode() output does not contain newlines" _test_encoding_newlines() { local lines lines=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | - _uipc_encode | wc -l) + ipc_encode | wc -l) if (( lines != 0 )); then return 1 @@ -99,15 +99,15 @@ Describe "Encoding" The status should equal 0 End - It "_uipc_decode() reverses _ipc_encode()" + It "ipc_decode() reverses _ipc_encode()" _test_encode_decode() { local data_before local data_encoded local data_after data_before=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | base64 -w 0) - data_encoded=$(_uipc_encode <<< "$data_before") - data_after=$(_uipc_decode <<< "$data_encoded") + data_encoded=$(ipc_encode <<< "$data_before") + data_after=$(ipc_decode <<< "$data_encoded") if [[ "$data_before" != "$data_after" ]]; then return 1 @@ -152,7 +152,7 @@ Describe "Message" return 1 fi - if ! _uipc_decode <<< "$msg" | jq -r -e . ; then + if ! ipc_decode <<< "$msg" | jq -r -e . ; then return 1 fi @@ -173,7 +173,7 @@ Describe "Message" return 1 fi - if ! spec/validate.py spec/ipc_message.schema.json <(_uipc_decode "$msg"); then + if ! spec/validate.py spec/ipc_message.schema.json <(ipc_decode "$msg"); then return 1 fi -- 2.47.3