From 4a30aecdb66f35e6ec769503d3d3aa6d64bfdda9 Mon Sep 17 00:00:00 2001 From: pfans Date: Mon, 20 Nov 2023 14:40:57 +0800 Subject: [PATCH] Add basic authorization test case. (#96) --- iot-gateway/modbus-nmi/modbus.c | 7 +- .../shpec/config/basic-auth_shpec/config.json | 169 ++++++++++++++++++ tests/shpec/shell/basic-auth_shpec.sh | 64 +++++++ 3 files changed, 236 insertions(+), 4 deletions(-) create mode 100644 tests/shpec/config/basic-auth_shpec/config.json create mode 100644 tests/shpec/shell/basic-auth_shpec.sh diff --git a/iot-gateway/modbus-nmi/modbus.c b/iot-gateway/modbus-nmi/modbus.c index c28b84b..57a5a47 100644 --- a/iot-gateway/modbus-nmi/modbus.c +++ b/iot-gateway/modbus-nmi/modbus.c @@ -197,10 +197,9 @@ static int lock_file(int lock_flag) { static int fd = -1; if (fd == -1) { - fd = open("/tmp/pipy-modbus.lockfile", O_WRONLY | O_CREAT); - } - if (fd == -1) { - return -1; + if ((fd = open("/tmp/pipy-modbus.lockfile", O_WRONLY | O_CREAT)) == -1) { + return -1; + } } if (lock_flag) { return flock(fd, LOCK_EX); diff --git a/tests/shpec/config/basic-auth_shpec/config.json b/tests/shpec/config/basic-auth_shpec/config.json new file mode 100644 index 0000000..107d5b2 --- /dev/null +++ b/tests/shpec/config/basic-auth_shpec/config.json @@ -0,0 +1,169 @@ +{ + "Configs": { + "EnableDebug": true + }, + "Consumers": [ + { + "userid": "test", + "password": "123456", + "Headers-Authorization": { + "authorization": "Basic dGVzdDoxMjM0NTY=" + } + } + ], + "Listeners": [ + { + "Protocol": "HTTP", + "Port": 8080 + }, + { + "Protocol": "HTTP", + "Port": 8081 + }, + { + "Protocol": "HTTP", + "Port": 8082 + } + ], + "RouteRules": { + "8080": { + "*": { + "RouteType": "HTTP", + "Matches": [ + { + "Path": { + "Type": "Prefix", + "Path": "/" + }, + "EnableHeadersAuthorization": true, + "HeadersAuthorizationType": "Basic", + "BackendService": { + "backendService1": { + "Weight": 100 + } + } + } + ] + } + }, + "8081": { + "*": { + "Matches": [ + { + "ServerRoot": "www1", + "Index": [ + "index.html", + "index.htm" + ], + "TryFiles": [ + "$uri", + "$uri/default/", + "=404" + ] + } + ] + } + }, + "8082": { + "*": { + "Matches": [ + { + "ServerRoot": "www2", + "Index": [ + "default.html", + "index.html" + ] + } + ] + } + } + }, + "Services": { + "backendService1": { + "StickyCookieName": "_srv_id", + "StickyCookieExpires": 3600, + "HealthCheck": { + "Interval": 10, + "MaxFails": 3, + "FailTimeout": 30, + "Path": "/", + "Matches": [ + { + "StatusCodes": [ + 200 + ] + } + ] + }, + "Endpoints": { + "127.0.0.1:8081": { + "Weight": 50 + }, + "127.0.0.1:8082": { + "Weight": 50 + } + } + } + }, + "Chains": { + "HTTPRoute": [ + "common/access-control.js", + "common/ratelimit.js", + "common/consumer.js", + "http/codec.js", + "http/auth.js", + "http/route.js", + "http/service.js", + "http/metrics.js", + "http/tracing.js", + "http/logging.js", + "http/circuit-breaker.js", + "http/throttle-domain.js", + "http/throttle-route.js", + "filter/request-redirect.js", + "filter/header-modifier.js", + "filter/url-rewrite.js", + "http/forward.js", + "http/default.js" + ], + "HTTPSRoute": [ + "common/access-control.js", + "common/ratelimit.js", + "common/tls-termination.js", + "common/consumer.js", + "http/codec.js", + "http/auth.js", + "http/route.js", + "http/service.js", + "http/metrics.js", + "http/tracing.js", + "http/logging.js", + "http/circuit-breaker.js", + "http/throttle-domain.js", + "http/throttle-route.js", + "filter/request-redirect.js", + "filter/header-modifier.js", + "filter/url-rewrite.js", + "http/forward.js", + "http/default.js" + ], + "TLSPassthrough": [ + "common/access-control.js", + "common/ratelimit.js", + "tls/passthrough.js", + "common/consumer.js" + ], + "TLSTerminate": [ + "common/access-control.js", + "common/ratelimit.js", + "common/tls-termination.js", + "common/consumer.js", + "tls/forward.js" + ], + "TCPRoute": [ + "common/access-control.js", + "common/ratelimit.js", + "tcp/forward.js" + ] + } +} diff --git a/tests/shpec/shell/basic-auth_shpec.sh b/tests/shpec/shell/basic-auth_shpec.sh new file mode 100644 index 0000000..bbbdd88 --- /dev/null +++ b/tests/shpec/shell/basic-auth_shpec.sh @@ -0,0 +1,64 @@ + +### header ### +export main=`ps aux | grep pipy | grep " --admin-port=12300" | grep -v grep | awk '{print $2}'` +if ! [ "x$main" = "x" ]; then + kill -9 "$main" > /dev/null +fi +# kill -SIGINT $pipypid +export pipy=`ps aux | grep pipy | grep " --admin-port=12301" | grep -v grep | awk '{print $2}'` +if ! [ "x$pipy" = "x" ]; then + kill -9 "$pipy" > /dev/null +fi + +export name=`echo "$BASH_SOURCE" | awk -F "[./]" '{print $4}'` + +if test -f "config/$name/main.js"; then + (pipy "config/$name/main.js" --admin-port=12300 >/dev/null 2>&1 &) +fi + +cp -f "config/$name/config.json" pjs/ +(pipy pjs/main.js --admin-port=12301 >tmp/pipy.log 2>&1 &) +for i in {1..10} +do + cat tmp/pipy.log 2>&1 | grep "Thread 0 started" >/dev/null + if ! [ $? -eq 0 ]; then + sleep 0.5 + else + break + fi +done + +### shell ### + +describe "SHPEC basic authorization Test" + + start="$(date +%s%3N)" + curl -s -i http://127.0.0.1:8080/ > tmp/1.log + curl -si http://127.0.0.1:8080/ -H "authorization: Basic dGVzdDoxMjM0NTY=" > tmp/2.log + output_1="$(cat tmp/1.log)" + output_2="$(cat tmp/2.log)" + end="$(date +%s%3N)" + runtime=$((end-start)) + + it "1: 401 Unauthorized" + assert grep "$output_1" "401 Unauthorized" + end + + it "2: 200 OK" + assert grep "$output_2" "200 OK" + end + +end + +### footer ### + +export main=`ps aux | grep pipy | grep " --admin-port=12300" | grep -v grep | awk '{print $2}'` +if ! [ "x$main" = "x" ]; then + kill -9 "$main" > /dev/null +fi +# kill -SIGINT $pipypid +export pipy=`ps aux | grep pipy | grep " --admin-port=12301" | grep -v grep | awk '{print $2}'` +if ! [ "x$pipy" = "x" ]; then + kill -9 "$pipy" > /dev/null +fi +