|
| 1 | +// Copyright 2023 The casbin Authors. All Rights Reserved. |
| 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 util |
| 16 | + |
| 17 | +import "testing" |
| 18 | + |
| 19 | +func TestIsIntranetIp(t *testing.T) { |
| 20 | + tests := []struct { |
| 21 | + name string |
| 22 | + ip string |
| 23 | + expected bool |
| 24 | + }{ |
| 25 | + // Private IPv4 addresses |
| 26 | + {"Private 10.x.x.x", "10.0.0.1", true}, |
| 27 | + {"Private 192.168.x.x", "192.168.1.1", true}, |
| 28 | + {"Private 172.16.x.x", "172.16.0.1", true}, |
| 29 | + {"Private 172.31.x.x", "172.31.255.255", true}, |
| 30 | + |
| 31 | + // Loopback |
| 32 | + {"Loopback IPv4", "127.0.0.1", true}, |
| 33 | + {"Loopback IPv6", "::1", true}, |
| 34 | + |
| 35 | + // Link-local |
| 36 | + {"Link-local IPv4", "169.254.1.1", true}, |
| 37 | + {"Link-local IPv6", "fe80::1", true}, |
| 38 | + |
| 39 | + // Public IPs |
| 40 | + {"Public Google DNS", "8.8.8.8", false}, |
| 41 | + {"Public Cloudflare DNS", "1.1.1.1", false}, |
| 42 | + {"Public IPv4", "123.45.67.89", false}, |
| 43 | + |
| 44 | + // With port |
| 45 | + {"Private with port", "192.168.1.1:8080", true}, |
| 46 | + {"Public with port", "8.8.8.8:53", false}, |
| 47 | + |
| 48 | + // Invalid IPs |
| 49 | + {"Invalid IP", "invalid", false}, |
| 50 | + {"Empty string", "", false}, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + result := IsIntranetIp(tt.ip) |
| 56 | + if result != tt.expected { |
| 57 | + t.Errorf("IsIntranetIp(%s) = %v, expected %v", tt.ip, result, tt.expected) |
| 58 | + } |
| 59 | + }) |
| 60 | + } |
| 61 | +} |
0 commit comments