Skip to content

Commit

Permalink
Add tests for v3 get image only and get screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
thevindu-w committed Apr 23, 2024
1 parent b35c804 commit 3f81829
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 85 deletions.
6 changes: 6 additions & 0 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ export METHOD_SEND_TEXT=$(printf '\x02' | bin2hex)
export METHOD_GET_FILES=$(printf '\x03' | bin2hex)
export METHOD_SEND_FILES=$(printf '\x04' | bin2hex)
export METHOD_GET_IMAGE=$(printf '\x05' | bin2hex)
export METHOD_GET_IMAGE_ONLY=$(printf '\x06' | bin2hex)
export METHOD_GET_SCREENSHOT=$(printf '\x07' | bin2hex)

# Proto ack
export PROTO_SUPPORTED=$(printf '\x01' | bin2hex)
Expand All @@ -308,6 +310,10 @@ export METHOD_NO_DATA=$(printf '\x02' | bin2hex)
export METHOD_UNKNOWN_METHOD=$(printf '\x03' | bin2hex)
export METHOD_NOT_IMPLEMENTED=$(printf '\x04' | bin2hex)

export imgSample="89504e470d0a1a0a0000000d4948445200000005000000050802000000020db1b20\
00000264944415408d755cb2112002010804070fcff973168f0681bb042b99501f5ac8bbf9ad6c\
dfc0f828c0e0522b1809c0000000049454e44ae426082"

# Export variables and functions
export DETECTED_OS
export -f setColor showStatus client_tool copy_text get_copied_text copy_files copy_image clear_clipboard update_config
Expand Down
6 changes: 6 additions & 0 deletions tests/scripts/3.6_get_image_v3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

proto="$PROTO_V3"
method="$METHOD_GET_IMAGE_ONLY"

. scripts/common/get_image.sh
10 changes: 10 additions & 0 deletions tests/scripts/3.7_get_screenshot_v3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

proto="$PROTO_V3"
method="$METHOD_GET_SCREENSHOT"
disp_num=1
disp="$(printf '%016x' $disp_num)"

copy_image "$imgSample"

. scripts/common/get_screenshot.sh
50 changes: 50 additions & 0 deletions tests/scripts/common/get_image.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash

. init.sh

copy_image "$imgSample"

responseDump=$(echo -n "${proto}${method}" | hex2bin | client_tool)

protoAck="$PROTO_SUPPORTED"
methodAck="$METHOD_OK"
length=$(printf '%016x' "$((${#imgSample} / 2))")

expected="${protoAck}${methodAck}${length}${imgSample}"

if [ "$DETECTED_OS" = 'Linux' ]; then
if [ "$responseDump" != "$expected" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::20} ..."
echo 'Received:' "${responseDump::20} ..."
exit 1
fi
elif [ "$DETECTED_OS" = 'Windows' ]; then
if [ "${responseDump::36}" != "${expected::36}" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::20} ..."
echo 'Received:' "${responseDump::20} ..."
exit 1
fi
elif [ "$DETECTED_OS" = 'macOS' ]; then
if [ "${responseDump::17}" != "${expected::17}" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::17} ..."
echo 'Received:' "${responseDump::17} ..."
exit 1
fi
imgSize="$((0x${responseDump:4:16}))"
if [ "$imgSize" -gt '512' ]; then
showStatus info "Image is too large. size=${imgSize}."
exit 1
fi
if [ "${responseDump:20:16}" != "${expected:20:16}" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::17} ..."
echo 'Received:' "${responseDump::17} ..."
exit 1
fi
else
showStatus info "Unknown OS."
exit 1
fi
35 changes: 35 additions & 0 deletions tests/scripts/common/get_screenshot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

. init.sh

responseDump=$(echo -n "${proto}${method}${disp}" | hex2bin | client_tool)

protoAck="$PROTO_SUPPORTED"
methodAck="$METHOD_OK"

expected_proto_method_ack="${protoAck}${methodAck}"
len_expected_header="${#expected_proto_method_ack}"

if [ "${responseDump::len_expected_header}" != "$expected_proto_method_ack" ]; then
showStatus info 'Incorrect protocol:method ack.'
echo 'Expected:' "$expected_proto_method_ack"
echo 'Received:' "${responseDump::len_expected_header}"
exit 1
fi

length="$((16#${responseDump:len_expected_header:16}))"

if [ "$length" -le '512' ]; then
showStatus info 'Invalid image length.'
exit 1
fi

len_expected_header="$((len_expected_header + 16))"

expected_png_header="$(printf '\x89PNG\r\n\x1a\n' | bin2hex)"
png_header="${responseDump:len_expected_header:${#expected_png_header}}"

if [ "$png_header" != "$expected_png_header" ]; then
showStatus info 'Invalid image header.'
exit 1
fi
53 changes: 1 addition & 52 deletions tests/scripts/common/x.5.1_get_image.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,5 @@
#!/bin/bash

. init.sh

imgSample="89504e470d0a1a0a0000000d4948445200000005000000050802000000020db1b20\
00000264944415408d755cb2112002010804070fcff973168f0681bb042b99501f5ac8bbf9ad6c\
dfc0f828c0e0522b1809c0000000049454e44ae426082"

copy_image "$imgSample"

method="$METHOD_GET_IMAGE"

responseDump=$(echo -n "${proto}${method}" | hex2bin | client_tool)

protoAck="$PROTO_SUPPORTED"
methodAck="$METHOD_OK"
length=$(printf '%016x' "$((${#imgSample} / 2))")

expected="${protoAck}${methodAck}${length}${imgSample}"

if [ "$DETECTED_OS" = 'Linux' ]; then
if [ "$responseDump" != "$expected" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::20} ..."
echo 'Received:' "${responseDump::20} ..."
exit 1
fi
elif [ "$DETECTED_OS" = 'Windows' ]; then
if [ "${responseDump::36}" != "${expected::36}" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::20} ..."
echo 'Received:' "${responseDump::20} ..."
exit 1
fi
elif [ "$DETECTED_OS" = 'macOS' ]; then
if [ "${responseDump::17}" != "${expected::17}" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::17} ..."
echo 'Received:' "${responseDump::17} ..."
exit 1
fi
imgSize="$((0x${responseDump:4:16}))"
if [ "$imgSize" -gt '512' ]; then
showStatus info "Image is too large. size=${imgSize}."
exit 1
fi
if [ "${responseDump:20:16}" != "${expected:20:16}" ]; then
showStatus info 'Incorrect server response.'
echo 'Expected:' "${expected::17} ..."
echo 'Received:' "${responseDump::17} ..."
exit 1
fi
else
showStatus info "Unknown OS."
exit 1
fi
. scripts/common/get_image.sh
35 changes: 2 additions & 33 deletions tests/scripts/common/x.5.2_get_screenshot.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
#!/bin/bash

. init.sh

method="$METHOD_GET_IMAGE"
disp='' # not used in method 5

clear_clipboard

responseDump=$(echo -n "${proto}${method}" | hex2bin | client_tool)

protoAck="$PROTO_SUPPORTED"
methodAck="$METHOD_OK"

expected_proto_method_ack="${protoAck}${methodAck}"
len_expected_header="${#expected_proto_method_ack}"

if [ "${responseDump::len_expected_header}" != "$expected_proto_method_ack" ]; then
showStatus info 'Incorrect protocol:method ack.'
echo 'Expected:' "$expected_proto_method_ack"
echo 'Received:' "${responseDump::len_expected_header}"
exit 1
fi

length="$((16#${responseDump:len_expected_header:16}))"

if [ "$length" -le '8' ]; then
showStatus info 'Invalid image length.'
exit 1
fi

len_expected_header="$((len_expected_header + 16))"

expected_png_header="$(printf '\x89PNG\r\n\x1a\n' | bin2hex)"
png_header="${responseDump:len_expected_header:${#expected_png_header}}"

if [ "$png_header" != "$expected_png_header" ]; then
showStatus info 'Invalid image header.'
exit 1
fi
. scripts/common/get_screenshot.sh

0 comments on commit 3f81829

Please sign in to comment.