-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIPListTest.java
More file actions
171 lines (141 loc) · 5.9 KB
/
IPListTest.java
File metadata and controls
171 lines (141 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package helpers.net;
import dev.aikido.agent_api.helpers.net.IPList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class IPListTest {
private IPList blocklist;
@BeforeEach
public void setUp() {
blocklist = new IPList();
}
@Test
public void testBlocklistadd() {
assertFalse(blocklist.matches("192.168.1.1"));
blocklist.add("192.168.1.1");
assertTrue(blocklist.matches("192.168.1.1"));
assertEquals(1, blocklist.length());
// Test adding the same address again
blocklist.add("192.168.1.1");
assertTrue(blocklist.matches("192.168.1.1"));
assertEquals(1, blocklist.length());
}
@Test
public void testBlocklistAddSubnet() {
assertFalse(blocklist.matches("10.0.0.1"));
blocklist.add("10.0.0.0/8");
assertTrue(blocklist.matches("10.0.0.1"));
assertTrue(blocklist.matches("10.1.1.1"));
assertFalse(blocklist.matches("192.168.1.1"));
}
@Test
public void testBlocklistMultipleAddressesAndSubnets() {
blocklist.add("192.168.1.1");
blocklist.add("10.0.0.0/8");
assertTrue(blocklist.matches("192.168.1.1"));
assertTrue(blocklist.matches("10.0.0.1"));
assertTrue(blocklist.matches("10.1.1.1"));
assertFalse(blocklist.matches("172.16.0.1"));
assertEquals(2, blocklist.length());
}
@Test
public void testBlocklistInvalidIp() {
blocklist.add("192.168.1.1");
assertFalse(blocklist.matches("invalid_ip"));
}
@Test
public void testBlocklistSubnetEdgeCases() {
blocklist.add("192.168.1.0/24");
assertTrue(blocklist.matches("192.168.1.255")); // Last address in the subnet
assertTrue(blocklist.matches("192.168.1.0")); // First address in the subnet
assertFalse(blocklist.matches("192.168.2.1")); // Outside the subnet
assertTrue(blocklist.matches("192.168.1.128")); // Middle of the subnet
}
@Test
public void testBlocklistIpv6() {
blocklist.add("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
assertTrue(blocklist.matches("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
assertFalse(blocklist.matches("::1")); // Not in the blocklist
blocklist.add("2001:0db8::/32");
assertTrue(blocklist.matches("2001:0db8:abcd:0012:0000:0000:0000:0001"));
assertFalse(blocklist.matches("2001:0db9::"));
}
@Test
public void testBlocklistOverlappingSubnets() {
blocklist.add("192.168.1.0/24"); // Covers 192.168.1.0 to 192.168.1.255
blocklist.add("192.168.1.128/25"); // Covers 192.168.1.128 to 192.168.1.255
assertTrue(blocklist.matches("192.168.1.130")); // Inside both subnets
assertTrue(blocklist.matches("192.168.1.0")); // Inside first subnet
assertTrue(blocklist.matches("192.168.1.127")); // Inside first subnet, outside second
assertTrue(blocklist.matches("192.168.1.255")); // Last address in both subnets
assertFalse(blocklist.matches("192.168.2.1")); // Outside both subnets
}
@Test
public void testBlocklistMixedAddressTypes() {
blocklist.add("192.168.1.1");
blocklist.add("2001:0db8:85a3:0000:0000:8a2e:0370:7334");
assertTrue(blocklist.matches("192.168.1.1"));
assertTrue(blocklist.matches("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
assertFalse(blocklist.matches("192.168.1.2")); // Different IPv4
assertFalse(blocklist.matches("2001:0db8:85a3:0000:0000:8a2e:0370:7335")); // Different IPv6
}
@Test
public void testBlocklistSubnetWithSingleIp() {
blocklist.add("192.168.1.1/32"); // Single IP subnet
assertTrue(blocklist.matches("192.168.1.1"));
assertFalse(blocklist.matches("192.168.1.2")); // Outside the subnet
}
@Test
public void testBlocklistSubnetWithSubnet2() {
blocklist.add("192.168.2.1/24"); // Single IP subnet
blocklist.add(null); // null is ignored
assertTrue(blocklist.matches("192.168.2.1"));
assertTrue(blocklist.matches("192.168.2.2"));
}
@Test
public void testBlocklistMatchesIPv4MappedIPv6() {
blocklist.add("192.168.1.1");
assertTrue(blocklist.matches("::ffff:192.168.1.1"));
assertFalse(blocklist.matches("::ffff:192.168.1.2"));
blocklist.add("10.0.0.0/8");
assertTrue(blocklist.matches("::ffff:10.5.6.7"));
assertTrue(blocklist.matches("::ffff:10.0.0.1"));
assertFalse(blocklist.matches("::ffff:11.0.0.1"));
}
@Test
public void testBlocklistIPv6OnlyIgnoresIPv4MappedMismatch() {
blocklist.add("2001:db8::/32");
assertTrue(blocklist.matches("2001:db8::1"));
assertFalse(blocklist.matches("::ffff:192.168.1.1"));
assertFalse(blocklist.matches("192.168.1.1"));
}
@Test
public void testBlocklistStoredIPv4MappedMatchesIPv4Input() {
blocklist.add("::ffff:23.45.67.89");
assertTrue(blocklist.matches("::ffff:23.45.67.89"));
}
@Test
public void testBlocklistAddInvalidIpIgnored() {
blocklist.add("notanip");
assertEquals(0, blocklist.length());
assertFalse(blocklist.matches("192.168.1.1"));
}
@Test
public void testBlocklistLengthEmpty() {
assertEquals(0, blocklist.length());
}
@Test
public void testBlocklistStoredIPv4MappedMatchesPlainIPv4() {
blocklist.add("::ffff:23.45.67.89");
assertTrue(blocklist.matches("23.45.67.89"));
assertTrue(blocklist.matches("::ffff:23.45.67.89"));
assertFalse(blocklist.matches("23.45.67.90"));
}
@Test
public void testBlocklistStoredIPv4MappedCidrMatchesPlainIPv4() {
blocklist.add("::ffff:10.0.0.0/104");
assertTrue(blocklist.matches("10.1.2.3"));
assertTrue(blocklist.matches("::ffff:10.1.2.3"));
assertFalse(blocklist.matches("11.1.2.3"));
}
}