Skip to content

Commit

Permalink
Add basic authorization test case. (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanpf authored Nov 20, 2023
1 parent 6eaf5f7 commit 4a30aec
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 4 deletions.
7 changes: 3 additions & 4 deletions iot-gateway/modbus-nmi/modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
169 changes: 169 additions & 0 deletions tests/shpec/config/basic-auth_shpec/config.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
64 changes: 64 additions & 0 deletions tests/shpec/shell/basic-auth_shpec.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4a30aec

Please sign in to comment.