|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/golang-lru/v2/expirable" |
| 5 | + "github.com/jasonlovesdoggo/caddy-defender/ranges/data" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "go.uber.org/zap" |
| 12 | +) |
| 13 | + |
| 14 | +// Test data |
| 15 | +var ( |
| 16 | + validCIDRs = []string{ |
| 17 | + "192.168.1.0/24", |
| 18 | + "10.0.0.0/8", |
| 19 | + "2001:db8::/32", |
| 20 | + } |
| 21 | + invalidCIDRs = []string{ |
| 22 | + "invalid-cidr", |
| 23 | + "192.168.1.0/33", // Invalid subnet mask |
| 24 | + } |
| 25 | + predefinedCIDRs = map[string][]string{ |
| 26 | + "openai": { |
| 27 | + "203.0.113.0/24", |
| 28 | + "2001:db8:1::/48", |
| 29 | + }, |
| 30 | + } |
| 31 | +) |
| 32 | + |
| 33 | +// Mock logger for testing |
| 34 | +var testLogger = zap.NewNop() |
| 35 | + |
| 36 | +// TestRawIPInRanges tests the rawIPInRanges function. |
| 37 | +func TestRawIPInRanges(t *testing.T) { |
| 38 | + tests := []struct { |
| 39 | + name string |
| 40 | + ip string |
| 41 | + cidrRanges []string |
| 42 | + expected bool |
| 43 | + }{ |
| 44 | + { |
| 45 | + name: "IPv4 in range", |
| 46 | + ip: "192.168.1.100", |
| 47 | + cidrRanges: validCIDRs, |
| 48 | + expected: true, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "IPv4 not in range", |
| 52 | + ip: "192.168.2.100", |
| 53 | + cidrRanges: validCIDRs, |
| 54 | + expected: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "IPv6 in range", |
| 58 | + ip: "2001:db8::1", |
| 59 | + cidrRanges: validCIDRs, |
| 60 | + expected: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "IPv6 not in range", |
| 64 | + ip: "2001:db8:1::1", |
| 65 | + cidrRanges: []string{"2001:db8::/48"}, // Narrower range |
| 66 | + expected: false, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Invalid CIDR", |
| 70 | + ip: "192.168.1.100", |
| 71 | + cidrRanges: invalidCIDRs, |
| 72 | + expected: false, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Predefined CIDR (IPv4)", |
| 76 | + ip: "203.0.113.10", |
| 77 | + cidrRanges: []string{"openai"}, |
| 78 | + expected: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "Predefined CIDR (IPv6)", |
| 82 | + ip: "2001:db8:1::10", |
| 83 | + cidrRanges: []string{"openai"}, |
| 84 | + expected: true, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + for _, tt := range tests { |
| 89 | + t.Run(tt.name, func(t *testing.T) { |
| 90 | + clientIP := net.ParseIP(tt.ip) |
| 91 | + assert.NotNil(t, clientIP, "Failed to parse IP") |
| 92 | + |
| 93 | + // Mock predefined CIDRs |
| 94 | + data.IPRanges = predefinedCIDRs |
| 95 | + |
| 96 | + result := rawIPInRanges(clientIP, tt.cidrRanges, testLogger) |
| 97 | + assert.Equal(t, tt.expected, result, "Unexpected result for IP %s", tt.ip) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// TestIPInRanges tests the IPInRanges function, including caching behavior. |
| 103 | +func TestIPInRanges(t *testing.T) { |
| 104 | + tests := []struct { |
| 105 | + name string |
| 106 | + ip string |
| 107 | + cidrRanges []string |
| 108 | + expected bool |
| 109 | + }{ |
| 110 | + { |
| 111 | + name: "IPv4 in range (cached)", |
| 112 | + ip: "192.168.1.100", |
| 113 | + cidrRanges: validCIDRs, |
| 114 | + expected: true, |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "IPv4 not in range (cached)", |
| 118 | + ip: "192.168.2.100", |
| 119 | + cidrRanges: validCIDRs, |
| 120 | + expected: false, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, tt := range tests { |
| 125 | + t.Run(tt.name, func(t *testing.T) { |
| 126 | + clientIP := net.ParseIP(tt.ip) |
| 127 | + assert.NotNil(t, clientIP, "Failed to parse IP") |
| 128 | + |
| 129 | + // Mock predefined CIDRs |
| 130 | + data.IPRanges = predefinedCIDRs |
| 131 | + |
| 132 | + // First call (not cached) |
| 133 | + result := IPInRanges(clientIP, tt.cidrRanges, testLogger) |
| 134 | + assert.Equal(t, tt.expected, result, "Unexpected result for IP %s (first call)", tt.ip) |
| 135 | + |
| 136 | + // Second call (cached) |
| 137 | + result = IPInRanges(clientIP, tt.cidrRanges, testLogger) |
| 138 | + assert.Equal(t, tt.expected, result, "Unexpected result for IP %s (second call)", tt.ip) |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// TestIPInRangesCacheExpiration tests the cache expiration behavior. |
| 144 | +func TestIPInRangesCacheExpiration(t *testing.T) { |
| 145 | + // Set a short cache expiration time for testing |
| 146 | + cache = expirable.NewLRU[string, bool](MaxKeys, nil, time.Millisecond*10) |
| 147 | + |
| 148 | + clientIP := net.ParseIP("192.168.1.100") |
| 149 | + assert.NotNil(t, clientIP, "Failed to parse IP") |
| 150 | + |
| 151 | + // Mock predefined CIDRs |
| 152 | + data.IPRanges = predefinedCIDRs |
| 153 | + |
| 154 | + // First call (not cached) |
| 155 | + result := IPInRanges(clientIP, validCIDRs, testLogger) |
| 156 | + assert.True(t, result, "Expected IP to be in range (first call)") |
| 157 | + |
| 158 | + // Wait for cache to expire |
| 159 | + time.Sleep(time.Millisecond * 20) |
| 160 | + |
| 161 | + // Second call (cache expired) |
| 162 | + result = IPInRanges(clientIP, validCIDRs, testLogger) |
| 163 | + assert.True(t, result, "Expected IP to be in range (second call, cache expired)") |
| 164 | +} |
0 commit comments