]> git.corax.cc Git - toolbox/commitdiff
include/uipc: Make uipc module extend ipc interface
authorMatthias Kruk <m@m10k.eu>
Mon, 9 Jan 2023 08:22:50 +0000 (17:22 +0900)
committerMatthias Kruk <m@m10k.eu>
Mon, 9 Jan 2023 08:22:50 +0000 (17:22 +0900)
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
test/uipc_spec.sh

index 6ee8f8d14dc1a0a92b355de3cae5edf58dfd2247..4b998abe0e6688206875afe5981d78096e3ec304 100644 (file)
@@ -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 <<EOF | log_highlight "uipc message"
 Message version: $version [supported: $version_ok]
 
-$(_uipc_decode <<< "$msg" | jq .)
+$(ipc_decode <<< "$msg" | jq .)
 EOF
 
        return 0
@@ -119,7 +100,7 @@ _uipc_msg_new() {
        local message
        local encoded_message
 
-       if ! encoded_data=$(_uipc_encode <<< "$data"); then
+       if ! encoded_data=$(ipc_encode <<< "$data"); then
                log_error "Could not encode data"
 
        elif ! timestamp=$(date +"%s"); then
@@ -134,7 +115,7 @@ _uipc_msg_new() {
                                     "data"        "$encoded_data"); then
                log_error "Could not make message"
 
-       elif ! encoded_message=$(_uipc_encode "$message"); then
+       elif ! encoded_message=$(ipc_encode "$message"); then
                log_error "Could not encode message"
 
        else
@@ -220,7 +201,7 @@ uipc_msg_get_data() {
                return 1
        fi
 
-       if ! data_raw=$(_uipc_decode <<< "$data"); then
+       if ! data_raw=$(ipc_decode <<< "$data"); then
                return 1
        fi
 
index 94209ea546fda96833e9b4691250c818947955a5..d11c4944f2d9934c11c0a20dbdf6821e2c23ba5b 100644 (file)
@@ -28,12 +28,12 @@ cleanup() {
 }
 
 Describe "Encoding"
-  It "_uipc_encode() outputs base64"
+  It "ipc_encode() outputs base64"
     _test_encoding() {
         local data
 
        data=$(dd if=/dev/urandom bs=1024 count=1024 2>/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