1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.tools.tcpproxy;
23
24 import java.util.Random;
25
26 import junit.framework.TestCase;
27
28
29
30
31
32
33
34 public class TestConnectionDetails extends TestCase {
35
36 public void testConstructor() throws Exception {
37 try {
38 new ConnectionDetails(new EndPoint("one", 4789),
39 new EndPoint("one", 4789), true);
40 fail("Expected IllegalArgumentException");
41 }
42 catch (IllegalArgumentException e) {
43 }
44 }
45
46 public void testAccessors() throws Exception {
47
48 final Random random = new Random();
49
50 for (int i=0; i<10; ++i) {
51 final byte[] localBytes = new byte[random.nextInt(30)];
52 random.nextBytes(localBytes);
53 final EndPoint localEndPoint =
54 new EndPoint(new String(localBytes), random.nextInt(65536));
55
56 final byte[] remoteBytes = new byte[random.nextInt(30)];
57 random.nextBytes(remoteBytes);
58 final EndPoint remoteEndPoint =
59 new EndPoint(new String(remoteBytes), random.nextInt(65536));
60
61 final boolean isSecure = random.nextBoolean();
62
63 final ConnectionDetails connectionDetails =
64 new ConnectionDetails(localEndPoint, remoteEndPoint, isSecure);
65
66 assertEquals(localEndPoint, connectionDetails.getLocalEndPoint());
67 assertEquals(remoteEndPoint, connectionDetails.getRemoteEndPoint());
68 assertTrue(!(isSecure ^ connectionDetails.isSecure()));
69 }
70 }
71
72 public void testToString() throws Exception {
73
74 final ConnectionDetails connectionDetails =
75 new ConnectionDetails(new EndPoint("one", 55),
76 new EndPoint("two", 121), true);
77
78 assertEquals("one:55->two:121", connectionDetails.toString());
79 }
80
81 public void testEquality() throws Exception {
82 final ConnectionDetails[] cd = {
83 new ConnectionDetails(new EndPoint("A", 55),
84 new EndPoint("B", 80), false),
85 new ConnectionDetails(new EndPoint("a", 55),
86 new EndPoint("B", 80), false),
87 new ConnectionDetails(new EndPoint("c", 55),
88 new EndPoint("B", 80), false),
89 new ConnectionDetails(new EndPoint("a", 55),
90 new EndPoint("B", 80), true),
91 new ConnectionDetails(new EndPoint("a", 56),
92 new EndPoint("B", 80), false),
93 };
94
95 assertEquals(cd[0], cd[0]);
96 assertFalse(cd[0].equals(cd[1]));
97 assertFalse(cd[1].equals(cd[0]));
98 assertFalse(cd[0].equals(cd[2]));
99 assertFalse(cd[1].equals(cd[3]));
100 assertFalse(cd[1].equals(cd[4]));
101
102 assertFalse(cd[0].equals(this));
103
104 assertFalse(cd[0].equals(cd[0].getOtherEnd()));
105 assertFalse(cd[0].equals(
106 new ConnectionDetails(cd[0].getLocalEndPoint(),
107 cd[0].getRemoteEndPoint(),
108 cd[0].isSecure())));
109 assertEquals(cd[0], cd[0].getOtherEnd().getOtherEnd());
110 }
111
112 public void testGetOtherEnd() throws Exception {
113 final ConnectionDetails connectionDetails =
114 new ConnectionDetails(new EndPoint("blah", 123),
115 new EndPoint("blurgh", 9999), true);
116
117 final ConnectionDetails otherEnd = connectionDetails.getOtherEnd();
118
119 assertEquals(connectionDetails.getLocalEndPoint(),
120 otherEnd.getRemoteEndPoint());
121
122 assertEquals(connectionDetails.getRemoteEndPoint(),
123 otherEnd.getLocalEndPoint());
124
125 assertEquals(connectionDetails.isSecure(), otherEnd.isSecure());
126
127 assertEquals(connectionDetails, otherEnd.getOtherEnd());
128 }
129
130 public void testGetConnectionIdentity() throws Exception {
131 final ConnectionDetails connectionDetails1 =
132 new ConnectionDetails(new EndPoint("foo", 123),
133 new EndPoint("bar", 9999), true);
134
135 final ConnectionDetails connectionDetails2 =
136 new ConnectionDetails(new EndPoint("foo", 123),
137 new EndPoint("beer", 9999), true);
138
139 assertTrue(!(connectionDetails1.getConnectionIdentity().equals(
140 connectionDetails2.getConnectionIdentity())));
141
142 assertEquals(connectionDetails1.getConnectionIdentity(),
143 connectionDetails1.getConnectionIdentity());
144
145 assertEquals(connectionDetails1.getConnectionIdentity(),
146 connectionDetails1.getOtherEnd().getConnectionIdentity());
147
148 assertEquals(connectionDetails2.getConnectionIdentity(),
149 connectionDetails2.getOtherEnd().getConnectionIdentity());
150 }
151 }