Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.

Commit 5e2ccb5

Browse files
authored
Merge pull request #16 from BetterCorp/testings
Testings
2 parents 2576a98 + 7c9568b commit 5e2ccb5

8 files changed

+132
-22
lines changed

.golangci.yml

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ linters-settings:
2929
linters:
3030
enable-all: true
3131
disable:
32+
- deadcode # deprecated
33+
- varcheck # deprecated
34+
- structcheck # deprecated
35+
- nosnakecase # deprecated
3236
- interfacer # deprecated
3337
- maligned # deprecated
3438
- scopelint # deprecated

cloudflarewarp.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type Config struct {
2727

2828
// TrustResult for Trust IP test result.
2929
type TrustResult struct {
30+
isFatal bool
3031
isError bool
3132
trusted bool
3233
directIP string
@@ -86,8 +87,16 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
8687

8788
func (r *RealIPOverWriter) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
8889
trustResult := r.trust(req.RemoteAddr)
89-
if trustResult.directIP == "" || trustResult.isError {
90-
http.Error(rw, "Unknown source", 500)
90+
if trustResult.isFatal {
91+
http.Error(rw, "Unknown source", http.StatusInternalServerError)
92+
return
93+
}
94+
if trustResult.isError {
95+
http.Error(rw, "Unknown source", http.StatusBadRequest)
96+
return
97+
}
98+
if trustResult.directIP == "" {
99+
http.Error(rw, "Unknown source", http.StatusUnprocessableEntity)
91100
return
92101
}
93102
if trustResult.trusted {
@@ -118,6 +127,7 @@ func (r *RealIPOverWriter) trust(s string) *TrustResult {
118127
temp, _, err := net.SplitHostPort(s)
119128
if err != nil {
120129
return &TrustResult{
130+
isFatal: true,
121131
isError: true,
122132
trusted: false,
123133
directIP: "",
@@ -126,6 +136,7 @@ func (r *RealIPOverWriter) trust(s string) *TrustResult {
126136
ip := net.ParseIP(temp)
127137
if ip == nil {
128138
return &TrustResult{
139+
isFatal: false,
129140
isError: true,
130141
trusted: false,
131142
directIP: "",
@@ -134,13 +145,15 @@ func (r *RealIPOverWriter) trust(s string) *TrustResult {
134145
for _, network := range r.TrustIP {
135146
if network.Contains(ip) {
136147
return &TrustResult{
148+
isFatal: false,
137149
isError: false,
138150
trusted: true,
139151
directIP: ip.String(),
140152
}
141153
}
142154
}
143155
return &TrustResult{
156+
isFatal: false,
144157
isError: false,
145158
trusted: false,
146159
directIP: ip.String(),

cloudflarewarp_test.go

+44-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/http"
66
"net/http/httptest"
7+
"strconv"
78
"testing"
89

910
plugin "github.com/BetterCorp/cloudflarewarp"
@@ -20,7 +21,8 @@ func TestNew(t *testing.T) {
2021
t.Fatal(err)
2122
}
2223
testCases := []struct {
23-
expect500 bool
24+
ipv6 bool
25+
expect400 bool
2426
trusted bool
2527
remote string
2628
desc string
@@ -56,14 +58,43 @@ func TestNew(t *testing.T) {
5658
expectedScheme: "",
5759
trusted: false,
5860
},
61+
{
62+
remote: "10.0.1.20",
63+
desc: "not trust ip4/6",
64+
cfConnectingIP: "1001:3984:3989::1",
65+
cfVisitor: "",
66+
expected: "",
67+
expectedScheme: "",
68+
trusted: false,
69+
},
70+
{
71+
remote: "1001:3984:3989::1",
72+
ipv6: true,
73+
desc: "not trust ip6/6",
74+
cfConnectingIP: "1001:3984:3989::1",
75+
cfVisitor: "",
76+
expected: "",
77+
expectedScheme: "",
78+
trusted: false,
79+
},
80+
{
81+
remote: "1001:3984:3989::1",
82+
ipv6: true,
83+
desc: "not trust ip6/4",
84+
cfConnectingIP: "10.0.1.20",
85+
cfVisitor: "",
86+
expected: "",
87+
expectedScheme: "",
88+
trusted: false,
89+
},
5990
{
6091
remote: "10.0.2",
6192
desc: "wrong IP format",
6293
cfConnectingIP: "10.0.0.1",
6394
cfVisitor: "",
6495
expected: "",
6596
expectedScheme: "",
66-
expect500: true,
97+
expect400: true,
6798
trusted: false,
6899
},
69100
{
@@ -73,7 +104,7 @@ func TestNew(t *testing.T) {
73104
cfVisitor: "",
74105
expected: "",
75106
expectedScheme: "",
76-
expect500: true,
107+
expect400: true,
77108
trusted: false,
78109
},
79110
{
@@ -104,18 +135,24 @@ func TestNew(t *testing.T) {
104135
if err != nil {
105136
t.Fatal(err)
106137
}
107-
req.RemoteAddr = test.remote + ":36001"
138+
if test.ipv6 == true {
139+
req.RemoteAddr = "[" + test.remote + "]:36001"
140+
} else {
141+
req.RemoteAddr = test.remote + ":36001"
142+
}
108143
req.Header.Set("X-Real-Ip", test.remote)
109144
req.Header.Set("Cf-Connecting-IP", test.cfConnectingIP)
110145
req.Header.Set("Cf-Visitor", test.cfVisitor)
111146

112147
handler.ServeHTTP(recorder, req)
113148

114-
if recorder.Result().StatusCode == 500 {
115-
if test.expect500 == true {
149+
if recorder.Result().StatusCode == http.StatusBadRequest {
150+
if test.expect400 == true {
116151
return
117152
}
118-
t.Errorf("invalid response: 500")
153+
}
154+
if recorder.Result().StatusCode != http.StatusOK {
155+
t.Errorf("invalid response: " + strconv.Itoa(recorder.Result().StatusCode))
119156
return
120157
}
121158

test/config/invalid.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
[http]
3+
[http.middlewares]
4+
[http.middlewares.cloudflarewarp]
5+
[http.middlewares.cloudflarewarp.plugin]
6+
[http.middlewares.cloudflarewarp.plugin.cloudflarewarp]
7+
trustip=[]
8+

test/config/invalid.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
http:
2+
middlewares:
3+
cloudflarewarp:
4+
plugin:
5+
cloudflarewarp:
6+
disableDefault: false

test/test-prod.sh

+25-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
TEST_IP="187.2.2.3"
44

55
rm -rf ./logs-success
6+
rm -rf ./logs-success-toml
7+
rm -rf ./logs-success-yml
68
rm -rf ./logs-fail
9+
rm -rf ./logs-fail-toml
10+
rm -rf ./logs-fail-yml
11+
rm -rf ./logs-invalid
12+
rm -rf ./logs-invalid-toml
13+
rm -rf ./logs-invalid-yml
714

815
if [ "${1}" = "stack" ]; then
916
docker swarm init
@@ -27,7 +34,7 @@ if [ ! "${1}" = "stack" ]; then
2734
cp docker-compose-prod.yml docker-compose.yml
2835
fi
2936

30-
rm -rf ./logs-success-toml
37+
sleep 1s
3138

3239
bash test-base.sh success toml "${1}" $TEST_IP
3340

@@ -38,8 +45,6 @@ mv ./tempconfig ./logs-success-toml/config
3845

3946
sleep 1s
4047

41-
rm -rf ./logs-fail-toml
42-
4348
bash test-base.sh fail toml "${1}" $TEST_IP
4449

4550
sleep 1s
@@ -49,7 +54,14 @@ mv ./tempconfig ./logs-fail-toml/config
4954

5055
sleep 1s
5156

52-
rm -rf ./logs-success-yml
57+
bash test-base.sh invalid toml "${1}" "1522.20.2"
58+
59+
sleep 1s
60+
61+
mv ./logs ./logs-invalid-toml
62+
mv ./tempconfig ./logs-invalid-toml/config
63+
64+
sleep 1s
5365

5466
bash ./test-verify.sh toml $TEST_IP
5567

@@ -64,8 +76,6 @@ mv ./tempconfig ./logs-success-yml/config
6476

6577
sleep 1s
6678

67-
rm -rf ./logs-fail-yml
68-
6979
bash test-base.sh fail yml "${1}" $TEST_IP
7080

7181
sleep 1s
@@ -75,6 +85,15 @@ mv ./tempconfig ./logs-fail-yml/config
7585

7686
sleep 1s
7787

88+
bash test-base.sh invalid yml "${1}" "1522.20.2"
89+
90+
sleep 1s
91+
92+
mv ./logs ./logs-invalid-yml
93+
mv ./tempconfig ./logs-invalid-yml/config
94+
95+
sleep 1s
96+
7897
bash ./test-verify.sh yml $TEST_IP
7998

8099
if [ ! "${1}" = "stack" ]; then

test/test-verify.sh

+6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
SUCCESS_CONFIG_FILE="./logs-success-${1}/output.log"
22
FAIL_CONFIG_FILE="./logs-fail-${1}/output.log"
3+
INVALID_CONFIG_FILE="./logs-invalid-${1}/output.log"
34

45
echo "RUNNING TESTS FOR ${1}"
56
echo " - Succ $SUCCESS_CONFIG_FILE"
67
echo " - Fail $FAIL_CONFIG_FILE"
8+
echo " - Inva $INVALID_CONFIG_FILE"
79

810
sleep 1s
911

@@ -28,6 +30,10 @@ if ! grep -q "X-Is-Trusted: no" $FAIL_CONFIG_FILE; then
2830
echo "'X-Is-Trusted: no' header was not added to the invalid request ($FAIL_CONFIG_FILE)"
2931
exit 5
3032
fi
33+
if ! grep -q "X-Is-Trusted: no" $INVALID_CONFIG_FILE; then
34+
echo "'X-Is-Trusted: no' header was not added to the invalid request ($INVALID_CONFIG_FILE)"
35+
exit 5
36+
fi
3137
#if ! grep -q "X-Forwarded-For: 10.0.0.2" $FAIL_CONFIG_FILE; then
3238
# echo "Forwarded header was not defined as the original IP"
3339
# exit 5

test/test.sh

+24-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
TEST_IP="187.2.2.1"
44

55
rm -rf ./logs-success
6+
rm -rf ./logs-success-toml
7+
rm -rf ./logs-success-yml
68
rm -rf ./logs-fail
9+
rm -rf ./logs-fail-toml
10+
rm -rf ./logs-fail-yml
11+
rm -rf ./logs-invalid
12+
rm -rf ./logs-invalid-toml
13+
rm -rf ./logs-invalid-yml
714

815
if [ "${1}" = "stack" ]; then
916
docker swarm init
@@ -15,8 +22,6 @@ docker pull traefik:2.8
1522

1623
sleep 1s
1724

18-
rm -rf ./logs-success-toml
19-
2025
bash test-base.sh success toml "${1}" $TEST_IP
2126

2227
sleep 1s
@@ -26,8 +31,6 @@ mv ./tempconfig ./logs-success-toml/config
2631

2732
sleep 1s
2833

29-
rm -rf ./logs-fail-toml
30-
3134
bash test-base.sh fail toml "${1}" $TEST_IP
3235

3336
sleep 1s
@@ -37,7 +40,14 @@ mv ./tempconfig ./logs-fail-toml/config
3740

3841
sleep 1s
3942

40-
rm -rf ./logs-success-yml
43+
bash test-base.sh invalid toml "${1}" "1522.20.2"
44+
45+
sleep 1s
46+
47+
mv ./logs ./logs-invalid-toml
48+
mv ./tempconfig ./logs-invalid-toml/config
49+
50+
sleep 1s
4151

4252
bash ./test-verify.sh toml $TEST_IP
4353

@@ -52,8 +62,6 @@ mv ./tempconfig ./logs-success-yml/config
5262

5363
sleep 1s
5464

55-
rm -rf ./logs-fail-yml
56-
5765
bash test-base.sh fail yml "${1}" $TEST_IP
5866

5967
sleep 1s
@@ -63,4 +71,13 @@ mv ./tempconfig ./logs-fail-yml/config
6371

6472
sleep 1s
6573

74+
bash test-base.sh invalid yml "${1}" "1522.20.2"
75+
76+
sleep 1s
77+
78+
mv ./logs ./logs-invalid-yml
79+
mv ./tempconfig ./logs-invalid-yml/config
80+
81+
sleep 1s
82+
6683
bash ./test-verify.sh yml $TEST_IP

0 commit comments

Comments
 (0)