go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/vpp/nat/models_test.go (about) 1 // Copyright (c) 2018 Cisco and/or its affiliates. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package vpp_nat 16 17 import ( 18 "testing" 19 20 "go.ligato.io/vpp-agent/v3/pkg/models" 21 ) 22 23 /*func TestDNAT44Key(t *testing.T) { 24 tests := []struct { 25 name string 26 label string 27 expectedKey string 28 }{ 29 { 30 name: "valid DNAT44 label", 31 label: "dnat1", 32 expectedKey: "vpp/config/v2/nat44/dnat/dnat1", 33 }, 34 { 35 name: "invalid DNAT44 label", 36 label: "", 37 expectedKey: "vpp/config/v2/nat44/dnat/<invalid>", 38 }, 39 } 40 for _, test := range tests { 41 t.Run(test.name, func(t *testing.T) { 42 key := DNAT44Key(test.label) 43 if key != test.expectedKey { 44 t.Errorf("failed for: label=%s\n"+ 45 "expected key:\n\t%q\ngot key:\n\t%q", 46 test.label, test.expectedKey, key) 47 } 48 }) 49 } 50 }*/ 51 52 func TestInterfaceNAT44Key(t *testing.T) { 53 tests := []struct { 54 name string 55 iface string 56 isInside bool 57 expectedKey string 58 }{ 59 { 60 name: "interface-with-IN-feature", 61 iface: "tap0", 62 isInside: true, 63 expectedKey: "vpp/nat44/interface/tap0/feature/in", 64 }, 65 { 66 name: "interface-with-OUT-feature", 67 iface: "tap1", 68 isInside: false, 69 expectedKey: "vpp/nat44/interface/tap1/feature/out", 70 }, 71 { 72 name: "gbe-interface-OUT", 73 iface: "GigabitEthernet0/8/0", 74 isInside: false, 75 expectedKey: "vpp/nat44/interface/GigabitEthernet0/8/0/feature/out", 76 }, 77 { 78 name: "gbe-interface-IN", 79 iface: "GigabitEthernet0/8/0", 80 isInside: true, 81 expectedKey: "vpp/nat44/interface/GigabitEthernet0/8/0/feature/in", 82 }, 83 { 84 name: "invalid-interface-with-IN-feature", 85 iface: "", 86 isInside: true, 87 expectedKey: "vpp/nat44/interface/<invalid>/feature/in", 88 }, 89 { 90 name: "invalid-interface-with-OUT-feature", 91 iface: "", 92 isInside: false, 93 expectedKey: "vpp/nat44/interface/<invalid>/feature/out", 94 }, 95 } 96 for _, test := range tests { 97 t.Run(test.name, func(t *testing.T) { 98 key := DerivedInterfaceNAT44Key(test.iface, test.isInside) 99 if key != test.expectedKey { 100 t.Errorf("failed for: iface=%s isInside=%t\n"+ 101 "expected key:\n\t%q\ngot key:\n\t%q", 102 test.iface, test.isInside, test.expectedKey, key) 103 } 104 }) 105 } 106 } 107 108 func TestParseInterfaceNAT44Key(t *testing.T) { 109 tests := []struct { 110 name string 111 key string 112 expectedIface string 113 expectedIsInside bool 114 expectedIsInterfaceNAT44Key bool 115 }{ 116 { 117 name: "interface-with-IN-feature", 118 key: "vpp/nat44/interface/tap0/feature/in", 119 expectedIface: "tap0", 120 expectedIsInside: true, 121 expectedIsInterfaceNAT44Key: true, 122 }, 123 { 124 name: "interface-with-OUT-feature", 125 key: "vpp/nat44/interface/tap1/feature/out", 126 expectedIface: "tap1", 127 expectedIsInside: false, 128 expectedIsInterfaceNAT44Key: true, 129 }, 130 { 131 name: "gbe-interface-OUT", 132 key: "vpp/nat44/interface/GigabitEthernet0/8/0/feature/out", 133 expectedIface: "GigabitEthernet0/8/0", 134 expectedIsInside: false, 135 expectedIsInterfaceNAT44Key: true, 136 }, 137 { 138 name: "gbe-interface-IN", 139 key: "vpp/nat44/interface/GigabitEthernet0/8/0/feature/in", 140 expectedIface: "GigabitEthernet0/8/0", 141 expectedIsInside: true, 142 expectedIsInterfaceNAT44Key: true, 143 }, 144 { 145 name: "invalid-interface", 146 key: "vpp/nat44/interface/<invalid>/feature/in", 147 expectedIface: "<invalid>", 148 expectedIsInside: true, 149 expectedIsInterfaceNAT44Key: true, 150 }, 151 { 152 name: "not interface key 1", 153 key: "vpp/nat44/address/192.168.1.1/twice-nat/on", 154 expectedIface: "", 155 expectedIsInside: false, 156 expectedIsInterfaceNAT44Key: false, 157 }, 158 { 159 name: "not interface key 2", 160 key: "vpp/config/v2/nat44/dnat/dnat1", 161 expectedIface: "", 162 expectedIsInside: false, 163 expectedIsInterfaceNAT44Key: false, 164 }, 165 } 166 for _, test := range tests { 167 t.Run(test.name, func(t *testing.T) { 168 iface, isInside, isInterfaceNAT44Key := ParseDerivedInterfaceNAT44Key(test.key) 169 if isInterfaceNAT44Key != test.expectedIsInterfaceNAT44Key { 170 t.Errorf("expected isInterfaceNAT44Key: %v\tgot: %v", test.expectedIsInterfaceNAT44Key, isInterfaceNAT44Key) 171 } 172 if iface != test.expectedIface { 173 t.Errorf("expected iface: %s\tgot: %s", test.expectedIface, iface) 174 } 175 if isInside != test.expectedIsInside { 176 t.Errorf("expected isInside: %t\tgot: %t", test.expectedIsInside, isInside) 177 } 178 }) 179 } 180 } 181 182 func TestAddressNAT44Key(t *testing.T) { 183 tests := []struct { 184 name string 185 address string 186 twiceNat bool 187 expectedKey string 188 }{ 189 { 190 name: "twice NAT is disabled", 191 address: "192.168.1.1", 192 twiceNat: false, 193 expectedKey: "vpp/nat44/address/192.168.1.1/twice-nat/off", 194 }, 195 { 196 name: "twice NAT is enabled", 197 address: "192.168.1.1", 198 twiceNat: true, 199 expectedKey: "vpp/nat44/address/192.168.1.1/twice-nat/on", 200 }, 201 { 202 name: "invalid address", 203 address: "invalid", 204 twiceNat: true, 205 expectedKey: "vpp/nat44/address/invalid/twice-nat/on", 206 }, 207 { 208 name: "empty address", 209 address: "", 210 twiceNat: true, 211 expectedKey: "vpp/nat44/address//twice-nat/on", 212 }, 213 } 214 for _, test := range tests { 215 t.Run(test.name, func(t *testing.T) { 216 key := DerivedAddressNAT44Key(test.address, test.twiceNat) 217 if key != test.expectedKey { 218 t.Errorf("failed for: address=%s twiceNat=%t\n"+ 219 "expected key:\n\t%q\ngot key:\n\t%q", 220 test.address, test.twiceNat, test.expectedKey, key) 221 } 222 }) 223 } 224 } 225 226 func TestParseAddressNAT44Key(t *testing.T) { 227 tests := []struct { 228 name string 229 key string 230 expectedAddress string 231 expectedTwiceNat bool 232 expectedIsAddressNAT44Key bool 233 }{ 234 { 235 name: "twice NAT is disabled", 236 key: "vpp/nat44/address/192.168.1.1/twice-nat/off", 237 expectedAddress: "192.168.1.1", 238 expectedTwiceNat: false, 239 expectedIsAddressNAT44Key: true, 240 }, 241 { 242 name: "twice NAT is enabled", 243 key: "vpp/nat44/address/192.168.1.1/twice-nat/on", 244 expectedAddress: "192.168.1.1", 245 expectedTwiceNat: true, 246 expectedIsAddressNAT44Key: true, 247 }, 248 { 249 name: "invalid address (not validated)", 250 key: "vpp/nat44/address/invalid/twice-nat/on", 251 expectedAddress: "invalid", 252 expectedTwiceNat: true, 253 expectedIsAddressNAT44Key: true, 254 }, 255 { 256 name: "empty address (not validated)", 257 key: "vpp/nat44/address//twice-nat/on", 258 expectedAddress: "", 259 expectedTwiceNat: true, 260 expectedIsAddressNAT44Key: true, 261 }, 262 { 263 name: "not address key", 264 key: "vpp/nat44/interface/tap0/feature/in", 265 expectedIsAddressNAT44Key: false, 266 }, 267 { 268 name: "not address key (missing twice-nat flag)", 269 key: "vpp/nat44/address/192.168.1.1", 270 expectedIsAddressNAT44Key: false, 271 }, 272 } 273 for _, test := range tests { 274 t.Run(test.name, func(t *testing.T) { 275 address, twiceNat, isAddressNAT44Key := ParseDerivedAddressNAT44Key(test.key) 276 if isAddressNAT44Key != test.expectedIsAddressNAT44Key { 277 t.Errorf("expected isAddressNAT44Key: %v\tgot: %v", test.expectedIsAddressNAT44Key, isAddressNAT44Key) 278 } 279 if address != test.expectedAddress { 280 t.Errorf("expected address: %s\tgot: %s", test.expectedAddress, address) 281 } 282 if twiceNat != test.expectedTwiceNat { 283 t.Errorf("expected twiceNat: %t\tgot: %t", test.expectedTwiceNat, twiceNat) 284 } 285 }) 286 } 287 } 288 289 func TestNat44AddressPoolKey(t *testing.T) { 290 tests := []*struct { 291 name string 292 pool Nat44AddressPool 293 expectedKey string 294 }{ 295 { 296 "named-pool", 297 Nat44AddressPool{ 298 Name: "myPool", 299 VrfId: 0, 300 FirstIp: "1.2.3.4", 301 LastIp: "1.2.3.5", 302 TwiceNat: false, 303 }, 304 "config/vpp/nat/v2/nat44-pool/myPool", 305 }, 306 { 307 "DEPRECATED key: unnamed pool with 1 IP address", 308 Nat44AddressPool{ 309 Name: "", 310 VrfId: 0, 311 FirstIp: "1.2.3.4", 312 LastIp: "", 313 TwiceNat: false, 314 }, 315 "config/vpp/nat/v2/nat44-pool/vrf/0/address/1.2.3.4", 316 }, 317 { 318 "DEPRECATED key: unnamed pool with multiple IP addresses", 319 Nat44AddressPool{ 320 Name: "", 321 VrfId: 0, 322 FirstIp: "1.2.3.4", 323 LastIp: "1.2.3.10", 324 TwiceNat: false, 325 }, 326 "config/vpp/nat/v2/nat44-pool/vrf/0/address/1.2.3.4-1.2.3.10", 327 }, 328 { 329 "DEPRECATED key: unnamed pool with 1 IP address (first and last IP address is the same)", 330 Nat44AddressPool{ 331 Name: "", 332 VrfId: 0, 333 FirstIp: "1.2.3.4", 334 LastIp: "1.2.3.4", 335 TwiceNat: false, 336 }, 337 "config/vpp/nat/v2/nat44-pool/vrf/0/address/1.2.3.4", 338 }, 339 } 340 for _, test := range tests { 341 t.Run(test.name, func(t *testing.T) { 342 key := models.Key(&test.pool) 343 if key != test.expectedKey { 344 t.Errorf("failed key for NAT44 address pool: %+v\n"+ 345 "expected key:\n\t%q\ngot key:\n\t%q", 346 &test.pool, test.expectedKey, key) 347 } 348 }) 349 } 350 }