-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathEgressInterfaceFinderTest.java
More file actions
135 lines (120 loc) · 4.96 KB
/
EgressInterfaceFinderTest.java
File metadata and controls
135 lines (120 loc) · 4.96 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
package com.fasterxml.uuid;
import com.fasterxml.uuid.EgressInterfaceFinder.EgressResolutionException;
import com.fasterxml.uuid.EgressInterfaceFinder.Finder;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import static com.fasterxml.uuid.EgressInterfaceFinder.DEFAULT_TIMEOUT_MILLIS;
import static org.junit.jupiter.api.Assertions.*;
public class EgressInterfaceFinderTest {
private final EgressInterfaceFinder finder = new EgressInterfaceFinder();
@Test
public void testUnspecifiedIPv4LocalAddress() throws UnknownHostException {
EgressResolutionException ex = null;
try {
finder.fromLocalAddress(InetAddress.getByName("0.0.0.0"));
} catch (EgressResolutionException e) {
ex = e;
}
assertNotNull(ex, "EgressResolutionException was not thrown");
String message = ex.getMessage();
assertTrue(message.startsWith("local address"), String.format(
"message [%s] does not begin with \"local address\"",
message));
assertEquals(1, ex.getMessages().size());
}
@Test
public void testUnspecifiedIPv6LocalAddress() throws Exception {
EgressResolutionException ex = null;
try {
finder.fromLocalAddress(InetAddress.getByName("::"));
} catch (EgressResolutionException e) {
ex = e;
}
assertNotNull(ex, "EgressResolutionException was not thrown");
String message = ex.getMessage();
assertTrue(message.startsWith("local address"), String.format(
"message [%s] does not begin with \"local address\"",
message));
assertEquals(1, ex.getMessages().size());
}
@Test
public void testFromLocalAddress() throws Exception {
NetworkInterface anInterface =
NetworkInterface.getNetworkInterfaces().nextElement();
InetAddress anAddress = anInterface.getInetAddresses().nextElement();
assertEquals(anInterface, finder.fromLocalAddress(anAddress));
}
@Test
public void testFromIncorrectLocalAddress() throws Exception {
EgressResolutionException ex = null;
try {
String name = EgressInterfaceFinder.randomRootServerName();
finder.fromLocalAddress(InetAddress.getByName(name));
} catch (EgressResolutionException e) {
ex = e;
}
assertNotNull(ex, "EgressResolutionException was not thrown");
String message = ex.getMessage();
assertTrue(message.startsWith("no interface found"), String.format(
"message [%s] does not begin with \"no interface found\"",
message));
assertEquals(1, ex.getMessages().size());
}
@Test
public void testFromRemoteDatagramSocketConnection() throws Exception {
if (!System.getProperty("os.name").startsWith("Mac")) {
String name = EgressInterfaceFinder.randomRootServerName();
InetSocketAddress address = new InetSocketAddress(name, 53);
finder.fromRemoteDatagramSocketConnection(address);
}
}
@Test
public void testFromRemoteSocketConnection() throws Exception {
String name = EgressInterfaceFinder.randomRootServerName();
InetSocketAddress address = new InetSocketAddress(name, 53);
finder.fromRemoteSocketConnection(DEFAULT_TIMEOUT_MILLIS, address);
}
@Test
public void testFromRemoteConnection() throws Exception {
String name = EgressInterfaceFinder.randomRootServerName();
InetSocketAddress address = new InetSocketAddress(name, 53);
finder.fromRemoteConnection(DEFAULT_TIMEOUT_MILLIS, address);
}
@Test
public void testFromRootNameServerConnection() throws Exception {
finder.fromRootNameserverConnection(DEFAULT_TIMEOUT_MILLIS);
}
@Test
public void testAggregateExceptions() {
EgressResolutionException ex = null;
final int[] counter = {0};
Finder aFinder = new Finder() {
@Override
public NetworkInterface egressInterface()
throws EgressResolutionException {
throw new EgressResolutionException(
String.format("exception %d", ++counter[0]),
new Exception("test exception"));
}
};
try {
finder.fromAggregate(new Finder[] { aFinder, aFinder, aFinder});
} catch (EgressResolutionException e) {
ex = e;
}
assertNotNull(ex, "EgressResolutionException was not thrown");
assertEquals(9, ex.getMessages().size());
}
@Test
public void testDefaultMechanisms() throws Exception {
try {
finder.egressInterface();
} catch (EgressResolutionException e) {
e.report();
throw e;
}
}
}