1
16
17 package iptables
18
19 import (
20 "reflect"
21 "testing"
22 )
23
24 func TestNoOpLocalDetector(t *testing.T) {
25 localDetector := NewNoOpLocalDetector()
26 if localDetector.IsImplemented() {
27 t.Error("NoOpLocalDetector returns true for IsImplemented")
28 }
29
30 ifLocal := localDetector.IfLocal()
31 if len(ifLocal) != 0 {
32 t.Errorf("NoOpLocalDetector returns %v for IsLocal (expected nil)", ifLocal)
33 }
34
35 ifNotLocal := localDetector.IfNotLocal()
36 if len(ifNotLocal) != 0 {
37 t.Errorf("NoOpLocalDetector returns %v for IsNotLocal (expected nil)", ifNotLocal)
38 }
39 }
40
41 func TestNewDetectLocalByCIDR(t *testing.T) {
42 cases := []struct {
43 cidr string
44 errExpected bool
45 }{
46 {
47 cidr: "10.0.0.0/14",
48 errExpected: false,
49 },
50 {
51 cidr: "2002:0:0:1234::/64",
52 errExpected: false,
53 },
54 {
55 cidr: "10.0.0.0",
56 errExpected: true,
57 },
58 {
59 cidr: "2002:0:0:1234::",
60 errExpected: true,
61 },
62 {
63 cidr: "",
64 errExpected: true,
65 },
66 }
67 for i, c := range cases {
68 r, err := NewDetectLocalByCIDR(c.cidr)
69 if c.errExpected {
70 if err == nil {
71 t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
72 }
73 continue
74 }
75 if err != nil {
76 t.Errorf("Case[%d] failed with error: %v", i, err)
77 }
78 }
79 }
80
81 func TestDetectLocalByCIDR(t *testing.T) {
82 cases := []struct {
83 cidr string
84 expectedIfLocalOutput []string
85 expectedIfNotLocalOutput []string
86 }{
87 {
88 cidr: "10.0.0.0/14",
89 expectedIfLocalOutput: []string{"-s", "10.0.0.0/14"},
90 expectedIfNotLocalOutput: []string{"!", "-s", "10.0.0.0/14"},
91 },
92 {
93 cidr: "2002:0:0:1234::/64",
94 expectedIfLocalOutput: []string{"-s", "2002:0:0:1234::/64"},
95 expectedIfNotLocalOutput: []string{"!", "-s", "2002:0:0:1234::/64"},
96 },
97 }
98 for _, c := range cases {
99 localDetector, err := NewDetectLocalByCIDR(c.cidr)
100 if err != nil {
101 t.Errorf("Error initializing localDetector: %v", err)
102 continue
103 }
104 if !localDetector.IsImplemented() {
105 t.Error("DetectLocalByCIDR returns false for IsImplemented")
106 }
107
108 ifLocal := localDetector.IfLocal()
109 ifNotLocal := localDetector.IfNotLocal()
110
111 if !reflect.DeepEqual(ifLocal, c.expectedIfLocalOutput) {
112 t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedIfLocalOutput, ifLocal)
113 }
114
115 if !reflect.DeepEqual(ifNotLocal, c.expectedIfNotLocalOutput) {
116 t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedIfNotLocalOutput, ifNotLocal)
117 }
118 }
119 }
120
121 func TestNewDetectLocalByBridgeInterface(t *testing.T) {
122 cases := []struct {
123 ifaceName string
124 errExpected bool
125 }{
126 {
127 ifaceName: "avz",
128 errExpected: false,
129 },
130 {
131 ifaceName: "",
132 errExpected: true,
133 },
134 }
135 for i, c := range cases {
136 r, err := NewDetectLocalByBridgeInterface(c.ifaceName)
137 if c.errExpected {
138 if err == nil {
139 t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
140 }
141 continue
142 }
143 if err != nil {
144 t.Errorf("Case[%d] failed with error: %v", i, err)
145 }
146 }
147 }
148
149 func TestNewDetectLocalByInterfaceNamePrefix(t *testing.T) {
150 cases := []struct {
151 ifacePrefix string
152 errExpected bool
153 }{
154 {
155 ifacePrefix: "veth",
156 errExpected: false,
157 },
158 {
159 ifacePrefix: "cbr0",
160 errExpected: false,
161 },
162 {
163 ifacePrefix: "",
164 errExpected: true,
165 },
166 }
167 for i, c := range cases {
168 r, err := NewDetectLocalByInterfaceNamePrefix(c.ifacePrefix)
169 if c.errExpected {
170 if err == nil {
171 t.Errorf("Case[%d] expected error, but succeeded with: %q", i, r)
172 }
173 continue
174 }
175 if err != nil {
176 t.Errorf("Case[%d] failed with error: %v", i, err)
177 }
178 }
179 }
180
181 func TestDetectLocalByBridgeInterface(t *testing.T) {
182 cases := []struct {
183 ifaceName string
184 expectedJumpIfOutput []string
185 expectedJumpIfNotOutput []string
186 }{
187 {
188 ifaceName: "eth0",
189 expectedJumpIfOutput: []string{"-i", "eth0"},
190 expectedJumpIfNotOutput: []string{"!", "-i", "eth0"},
191 },
192 }
193 for _, c := range cases {
194 localDetector, err := NewDetectLocalByBridgeInterface(c.ifaceName)
195 if err != nil {
196 t.Errorf("Error initializing localDetector: %v", err)
197 continue
198 }
199 if !localDetector.IsImplemented() {
200 t.Error("DetectLocalByBridgeInterface returns false for IsImplemented")
201 }
202
203 ifLocal := localDetector.IfLocal()
204 ifNotLocal := localDetector.IfNotLocal()
205
206 if !reflect.DeepEqual(ifLocal, c.expectedJumpIfOutput) {
207 t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, ifLocal)
208 }
209
210 if !reflect.DeepEqual(ifNotLocal, c.expectedJumpIfNotOutput) {
211 t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, ifNotLocal)
212 }
213 }
214 }
215
216 func TestDetectLocalByInterfaceNamePrefix(t *testing.T) {
217 cases := []struct {
218 ifacePrefix string
219 chain string
220 args []string
221 expectedJumpIfOutput []string
222 expectedJumpIfNotOutput []string
223 }{
224 {
225 ifacePrefix: "eth0",
226 expectedJumpIfOutput: []string{"-i", "eth0+"},
227 expectedJumpIfNotOutput: []string{"!", "-i", "eth0+"},
228 },
229 }
230 for _, c := range cases {
231 localDetector, err := NewDetectLocalByInterfaceNamePrefix(c.ifacePrefix)
232 if err != nil {
233 t.Errorf("Error initializing localDetector: %v", err)
234 continue
235 }
236 if !localDetector.IsImplemented() {
237 t.Error("DetectLocalByInterfaceNamePrefix returns false for IsImplemented")
238 }
239
240 ifLocal := localDetector.IfLocal()
241 ifNotLocal := localDetector.IfNotLocal()
242
243 if !reflect.DeepEqual(ifLocal, c.expectedJumpIfOutput) {
244 t.Errorf("IfLocal, expected: '%v', but got: '%v'", c.expectedJumpIfOutput, ifLocal)
245 }
246
247 if !reflect.DeepEqual(ifNotLocal, c.expectedJumpIfNotOutput) {
248 t.Errorf("IfNotLocal, expected: '%v', but got: '%v'", c.expectedJumpIfNotOutput, ifNotLocal)
249 }
250 }
251 }
252
View as plain text