@@ -2,6 +2,7 @@ package main
2
2
3
3
import (
4
4
"errors"
5
+ "reflect"
5
6
"testing"
6
7
)
7
8
@@ -25,50 +26,59 @@ func TestValidateStatusPort(t *testing.T) {
25
26
}
26
27
27
28
func TestParseNginxStatusAllowCIDRs (t * testing.T ) {
28
-
29
- var tests = []struct {
29
+ var badCIDRs = []struct {
30
30
input string
31
- expected []string
32
31
expectedError error
33
32
}{
34
- {"earth, ,,furball" ,
35
- []string {},
36
- errors .New ("invalid IP address: earth" )},
37
- {"127.0.0.1" ,
33
+ {
34
+ "earth, ,,furball" ,
35
+ errors .New ("invalid IP address: earth" ),
36
+ },
37
+ {
38
+ "127.0.0.1,10.0.1.0/24, ,,furball" ,
39
+ errors .New ("invalid CIDR address: an empty string is an invalid CIDR block or IP address" ),
40
+ },
41
+ {
42
+ "false" ,
43
+ errors .New ("invalid IP address: false" ),
44
+ },
45
+ }
46
+ for _ , badCIDR := range badCIDRs {
47
+ _ , err := parseNginxStatusAllowCIDRs (badCIDR .input )
48
+ if err == nil {
49
+ t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned no error when it should have returned error %q" , badCIDR .input , badCIDR .expectedError )
50
+ } else if err .Error () != badCIDR .expectedError .Error () {
51
+ t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned error %q when it should have returned error %q" , badCIDR .input , err , badCIDR .expectedError )
52
+ }
53
+ }
54
+
55
+ var goodCIDRs = []struct {
56
+ input string
57
+ expected []string
58
+ }{
59
+ {
60
+ "127.0.0.1" ,
38
61
[]string {"127.0.0.1" },
39
- nil },
40
- {"10.0.1.0/24" ,
62
+ },
63
+ {
64
+ "10.0.1.0/24" ,
41
65
[]string {"10.0.1.0/24" },
42
- nil },
43
- {"127.0.0.1,10.0.1.0/24,68.121.233.214 , 24.24.24.24/32" ,
44
- []string {"127.0.0.1" , "10.0.1.0/24" , "68.121.233.214" , "24.24.24.24/32" }, nil },
45
- {"127.0.0.1,10.0.1.0/24, ,,furball" ,
46
- []string {"127.0.0.1" , "10.0.1.0/24" },
47
- errors .New ("invalid CIDR address: an empty string is an invalid CIDR block or IP address" )},
48
- {"false" ,
49
- []string {},
50
- errors .New ("invalid IP address: false" )},
66
+ },
67
+ {
68
+ "127.0.0.1,10.0.1.0/24,68.121.233.214 , 24.24.24.24/32" ,
69
+ []string {"127.0.0.1" , "10.0.1.0/24" , "68.121.233.214" , "24.24.24.24/32" },
70
+ },
51
71
}
52
-
53
- for _ , test := range tests {
54
- splitArray , err := parseNginxStatusAllowCIDRs (test .input )
55
- if err != test .expectedError {
56
- if test .expectedError == nil {
57
- t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned an error %q when it should not have returned an error." , test .input , err .Error ())
58
- } else if err == nil {
59
- t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned no error when it should have returned error %q" , test .input , test .expectedError )
60
- } else if err .Error () != test .expectedError .Error () {
61
- t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned error %q when it should have returned error %q" , test .input , err .Error (), test .expectedError )
62
- }
72
+ for _ , goodCIDR := range goodCIDRs {
73
+ result , err := parseNginxStatusAllowCIDRs (goodCIDR .input )
74
+ if err != nil {
75
+ t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned an error when it should have returned no error: %q" , goodCIDR .input , err )
63
76
}
64
77
65
- for _ , expectedEntry := range test .expected {
66
- if ! contains (splitArray , expectedEntry ) {
67
- t .Errorf ("parseNginxStatusAllowCIDRs(%q) did not include %q but returned %q" , test .input , expectedEntry , splitArray )
68
- }
78
+ if ! reflect .DeepEqual (result , goodCIDR .expected ) {
79
+ t .Errorf ("parseNginxStatusAllowCIDRs(%q) returned %v expected %v: " , goodCIDR .input , result , goodCIDR .expected )
69
80
}
70
81
}
71
-
72
82
}
73
83
74
84
func TestValidateCIDRorIP (t * testing.T ) {
0 commit comments