-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHostnamesTest.java
More file actions
149 lines (127 loc) · 5.05 KB
/
HostnamesTest.java
File metadata and controls
149 lines (127 loc) · 5.05 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
package storage;
import dev.aikido.agent_api.storage.Hostnames;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class HostnamesTest {
private Hostnames hostnames;
@BeforeEach
public void setUp() {
hostnames = new Hostnames(3);
}
@Test
public void testAddHostname() {
hostnames.add("example.com", 80);
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(1, entries.length);
assertEquals("example.com", entries[0].getHostname());
assertEquals(80, entries[0].getPort());
assertEquals(1, entries[0].getHits());
}
@Test
public void testAddMultiplePorts() {
hostnames.add("example.com", 80);
hostnames.add("example.com", 443);
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(2, entries.length);
assertEquals("example.com", entries[0].getHostname());
assertEquals(80, entries[0].getPort());
assertEquals(1, entries[0].getHits());
assertEquals("example.com", entries[1].getHostname());
assertEquals(443, entries[1].getPort());
assertEquals(1, entries[1].getHits());
}
@Test
public void testAddDuplicateHostname() {
hostnames.add("example.com", 80);
hostnames.add("example.com", 80); // Should not change the port
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(1, entries.length);
assertEquals(2, entries[0].getHits()); // Hits should increment
}
@Test
public void testMaxEntries() {
hostnames.add("example.com", 80);
hostnames.add("test.com", 443);
hostnames.add("localhost", 3000);
hostnames.add("newsite.com", 8080); // This should remove "example.com"
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(3, entries.length);
assertFalse(containsEntry(entries, "example.com", 80));
assertTrue(containsEntry(entries, "test.com", 443));
assertTrue(containsEntry(entries, "localhost", 3000));
assertTrue(containsEntry(entries, "newsite.com", 8080));
}
@Test
public void testAsArray() {
hostnames.add("example.com", 80);
hostnames.add("test.com", 443);
assertInstanceOf(Hostnames.HostnameEntry[].class, hostnames.asArray());
}
@Test
public void testClear() {
hostnames.add("example.com", 80);
hostnames.add("test.com", 443);
hostnames.clear();
assertEquals(0, hostnames.asArray().length);
}
@Test
public void testAddNonePort() {
hostnames.add("example.com", 0); // Using 0 to represent None
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(1, entries.length);
assertEquals("example.com", entries[0].getHostname());
assertEquals(0, entries[0].getPort());
assertEquals(1, entries[0].getHits());
}
@Test
public void testExceedMaxEntriesWithMultiplePorts() {
hostnames.add("example.com", 80);
hostnames.add("example.com", 443);
hostnames.add("test.com", 8080);
hostnames.add("newsite.com", 3000); // This should remove "example.com:80"
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(3, entries.length);
assertFalse(containsEntry(entries, "example.com", 80));
assertTrue(containsEntry(entries, "example.com", 443));
assertTrue(containsEntry(entries, "test.com", 8080));
assertTrue(containsEntry(entries, "newsite.com", 3000));
}
@Test
public void testAddNullHostnameIsIgnored() {
hostnames.add(null, 80);
assertEquals(0, hostnames.asArray().length);
}
@Test
public void testAddUppercaseHostnameIsLowercased() {
hostnames.add("EXAMPLE.COM", 80);
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(1, entries.length);
assertEquals("example.com", entries[0].getHostname());
}
@Test
public void testMixedCaseAndLowercaseSameEntryDeduplication() {
hostnames.add("Example.COM", 80);
hostnames.add("example.com", 80);
Hostnames.HostnameEntry[] entries = hostnames.asArray();
assertEquals(1, entries.length);
assertEquals("example.com", entries[0].getHostname());
assertEquals(2, entries[0].getHits());
}
private boolean containsEntry(Hostnames.HostnameEntry[] entries, String hostname, int port) {
for (Hostnames.HostnameEntry entry : entries) {
if (entry.getHostname().equals(hostname) && entry.getPort() == port) {
return true;
}
}
return false;
}
private int getHits(Hostnames.HostnameEntry[] entries, String hostname, int port) {
for (Hostnames.HostnameEntry entry : entries) {
if (entry.getHostname().equals(hostname) && entry.getPort() == port) {
return entry.getHits();
}
}
return 0; // Not found
}
}