From: Matthias Kruk Date: Mon, 9 Jan 2023 08:20:13 +0000 (+0900) Subject: include/ipc: Make encode and decode methods part of the public API X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=b381443dbb5daca5a65ffc14d669b0cf72080545;p=toolbox include/ipc: Make encode and decode methods part of the public API The ipc and uipc modules use identical methods to encode and decode data. Instead, one module should allow the other to use its methods. This commit modifies the IPC module so that it declares the two methods as part of its interface that can be used by other modules. --- diff --git a/include/ipc.sh b/include/ipc.sh index 2978e46..b930956 100644 --- a/include/ipc.sh +++ b/include/ipc.sh @@ -26,10 +26,12 @@ __init() { declare -gxir __ipc_version=1 + interface "encode" \ + "decode" return 0 } -_ipc_encode() { +ipc_encode() { local decoded="$1" if (( $# > 0 )); then @@ -39,7 +41,7 @@ _ipc_encode() { fi } -_ipc_decode() { +ipc_decode() { local encoded="$1" if (( $# > 0 )); then @@ -55,7 +57,7 @@ _ipc_sign() { local signature if ! signature=$(gpg --output - --detach-sig <(echo "$data") | - _ipc_encode); then + ipc_encode); then return 1 fi @@ -72,7 +74,7 @@ _ipc_verify() { err=0 - if ! result=$(gpg --verify <(_ipc_decode <<< "$signature") <(echo "$data") 2>&1); then + if ! result=$(gpg --verify <(ipc_decode <<< "$signature") <(echo "$data") 2>&1); then err=1 fi @@ -87,7 +89,7 @@ _ipc_get() { local value - if ! value=$(_ipc_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 @@ -260,7 +262,7 @@ Signature valid: $signature_ok Signer : $signer_name <$signer_email> Key fingerprint: $signer_key -$(_ipc_decode <<< "$msg" | jq .) +$(ipc_decode <<< "$msg" | jq .) EOF return 0 @@ -284,7 +286,7 @@ _ipc_msg_new() { local envelope local encoded_envelope - if ! encoded_data=$(_ipc_encode <<< "$data"); then + if ! encoded_data=$(ipc_encode <<< "$data"); then log_error "Could not encode data" elif ! timestamp=$(date +"%s"); then @@ -299,7 +301,7 @@ _ipc_msg_new() { "data" "$encoded_data"); then log_error "Could not make message" - elif ! encoded_message=$(_ipc_encode "$message"); then + elif ! encoded_message=$(ipc_encode "$message"); then log_error "Could not encode message" elif ! signature=$(_ipc_sign "$encoded_message"); then @@ -309,7 +311,7 @@ _ipc_msg_new() { "signature" "$signature"); then log_error "Could not make envelope" - elif ! encoded_envelope=$(_ipc_encode "$envelope"); then + elif ! encoded_envelope=$(ipc_encode "$envelope"); then log_error "Could not encode envelope" else @@ -395,7 +397,7 @@ ipc_msg_get_data() { return 1 fi - if ! data_raw=$(_ipc_decode <<< "$data"); then + if ! data_raw=$(ipc_decode <<< "$data"); then return 1 fi diff --git a/test/ipc_spec.sh b/test/ipc_spec.sh index 4657b7f..2ed1911 100644 --- a/test/ipc_spec.sh +++ b/test/ipc_spec.sh @@ -67,12 +67,12 @@ cleanup() { } Describe "Encoding" - It "_ipc_encode() outputs base64" + It "ipc_encode() outputs base64" _test_encoding() { local data data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | - _ipc_encode) + ipc_encode) if ! [[ "$data" =~ ^[a-zA-Z0-9+/]+[=]*$ ]]; then return 1 @@ -85,7 +85,7 @@ Describe "Encoding" The status should equal 0 End - It "_ipc_encode() output has correct length" + It "ipc_encode() output has correct length" _test_encoding_length() { local data local block_size @@ -101,7 +101,7 @@ Describe "Encoding" input_bits=$((input_bytes * 8)) actual_length=$(dd if=/dev/urandom bs="$block_size" count="$block_num" 2>/dev/null | - _ipc_encode | wc -c) + ipc_encode | wc -c) if (( input_bits % 24 > 0 )); then # data is padded @@ -120,12 +120,12 @@ Describe "Encoding" The status should equal 0 End - It "_ipc_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 | - _ipc_encode | wc -l) + ipc_encode | wc -l) if (( lines != 0 )); then return 1 @@ -139,15 +139,15 @@ Describe "Encoding" End - It "_ipc_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=$(_ipc_encode <<< "$data_before") - data_after=$(_ipc_decode <<< "$data_encoded") + data_encoded=$(ipc_encode <<< "$data_before") + data_after=$(ipc_decode <<< "$data_encoded") if [[ "$data_before" != "$data_after" ]]; then return 1 @@ -169,7 +169,7 @@ Describe "Authentication" _test_ipc_sign_length() { local data - data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | _ipc_encode) + data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | ipc_encode) if ! signature=$(_ipc_sign <<< "$data"); then return 1 @@ -191,7 +191,7 @@ Describe "Authentication" local data local signature - data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | _ipc_encode) + data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | ipc_encode) if ! signature=$(_ipc_sign "$data"); then return 1 @@ -214,7 +214,7 @@ Describe "Authentication" local data local signature - data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | _ipc_encode) + data=$(dd if=/dev/urandom bs=1024 count=1024 2>/dev/null | ipc_encode) if ! signature=$(_ipc_sign "$data"); then return 1 @@ -264,7 +264,7 @@ Describe "Message" return 1 fi - if ! _ipc_decode <<< "$msg" | jq -r -e . ; then + if ! ipc_decode <<< "$msg" | jq -r -e . ; then return 1 fi @@ -285,7 +285,7 @@ Describe "Message" return 1 fi - if ! spec/validate.py spec/ipc_envelope.schema.json <(_ipc_decode "$msg"); then + if ! spec/validate.py spec/ipc_envelope.schema.json <(ipc_decode "$msg"); then return 1 fi @@ -305,7 +305,7 @@ Describe "Message" fi if ! spec/validate.py spec/ipc_message.schema.json \ - <(_ipc_get "$msg" "message" | _ipc_decode); then + <(_ipc_get "$msg" "message" | ipc_decode); then return 1 fi