Fix error on musl libc

This commit is contained in:
Zhong Lufan 2023-01-29 22:35:48 +08:00
parent abc588ed40
commit 72b452db96
No known key found for this signature in database
GPG Key ID: BE2B3A1E76AD7D04
3 changed files with 75 additions and 12 deletions

View File

@ -10,7 +10,54 @@ on:
- test/test.bats.sh - test/test.bats.sh
jobs: jobs:
test: test-on-alpine:
runs-on: ubuntu-latest
container:
image: alpine:latest
steps:
- name: Check out code
uses: actions/checkout@v3
with:
submodules: true
- name: Install dependencies
run: apk add bash curl openssl
- name: Run test
env:
AliAccessKeyId: ${{ secrets.ALIACCESSKEYID }}
AliAccessKeySecret: ${{ secrets.ALIACCESSKEYSECRET }}
run: ./test/bats/bin/bats test/test.bats.sh
test-on-openwrt:
runs-on: ubuntu-latest
container:
image: openwrtorg/rootfs:x86-64-openwrt-22.03
steps:
- name: Fix var directory
run: mkdir -p /var/lock
- name: Install dependencies
run: |
opkg update
opkg install git git-http
opkg install bash curl openssl-util
# Bats dependencies
opkg install coreutils-nl
- name: Check out code
if: ${{ github.event.act }}
uses: actions/checkout@v3
with:
submodules: true
- name: Check out code (with git)
if: ${{ !github.event.act }}
run: git clone --depth 1 --no-tags --recurse-submodules https://github.com/${{ github.repository }}.git /tmp/${{ github.sha }}
- name: Run test
env:
AliAccessKeyId: ${{ secrets.ALIACCESSKEYID }}
AliAccessKeySecret: ${{ secrets.ALIACCESSKEYSECRET }}
run: |
[[ -d /tmp/${{ github.sha }} ]] && cd /tmp/${{ github.sha }}
./test/bats/bin/bats test/test.bats.sh
test-on-ubuntu:
runs-on: ubuntu-18.04 # Test on old version bash runs-on: ubuntu-18.04 # Test on old version bash
steps: steps:
- name: Check out code - name: Check out code

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
.env.dev .env.dev
.env.local .env.local
.env.test .env.test
.secrets

View File

@ -8,6 +8,8 @@ for _aliapi_command in openssl curl; do
done done
unset _aliapi_command unset _aliapi_command
_ALIYUN_SDK_RUN_ON_MUSL_LIBC=$(ldd "$SHELL" | grep -q /lib/ld-musl && echo 1 || echo 0)
ALIYUN_SDK_LAST_HTTP_CODE=0 ALIYUN_SDK_LAST_HTTP_CODE=0
# aliapi_rpc <http_method> <host> <api_version> <api_action> [<--key> <value>...] # aliapi_rpc <http_method> <host> <api_version> <api_action> [<--key> <value>...]
@ -73,12 +75,17 @@ _aliapi_check_vars() {
} }
_aliapi_signature_rpc() { _aliapi_signature_rpc() {
if [[ ${LC_ALL:-X} != C ]]; then
LC_ALL=C _aliapi_signature_rpc "$@"
return $?
fi
local -u _http_method=$1 local -u _http_method=$1
local _str=$2 _query_str _sign_str local _str=$2 _query_str _sign_str
local _newline=' local _newline='
' '
_str=$(LC_ALL=C sort <<< "${_str//&/$_newline}") _str=$(sort <<< "${_str//"&"/"$_newline"}")
_query_str=${_str//$_newline/&} _query_str=${_str//"$_newline"/"&"}
_sign_str="$_http_method&$(_aliapi_urlencode "/")&$(_aliapi_urlencode "$_query_str")" _sign_str="$_http_method&$(_aliapi_urlencode "/")&$(_aliapi_urlencode "$_query_str")"
printf "%s" "$_sign_str" | openssl dgst -sha1 -hmac "$_AliAccessKeySecret&" -binary | openssl base64 -e printf "%s" "$_sign_str" | openssl dgst -sha1 -hmac "$_AliAccessKeySecret&" -binary | openssl base64 -e
} }
@ -103,13 +110,21 @@ _aliapi_urlencode() {
LC_ALL=C _aliapi_urlencode "$@" LC_ALL=C _aliapi_urlencode "$@"
return $? return $?
fi fi
local char string=$1 local char hex string=$1
while [[ -n $string ]]; do while [[ -n $string ]]; do
char=${string:0:1} char=${string:0:1}
string=${string:1} string=${string:1}
case $char in case $char in
[-._~0-9A-Za-z]) printf %c "$char";; [-._~0-9A-Za-z]) printf %c "$char";;
*) printf %%%02X "'$char";; *)
if [[ _ALIYUN_SDK_RUN_ON_MUSL_LIBC -eq 1 ]]; then
# Hack musl libc for not ASCII chars (incomplete test)
hex=$(printf %02X "'$char")
printf %%%s "${hex:${#hex}-2}"
else
printf %%%02X "'$char"
fi
;;
esac esac
done done
echo echo