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.net.ServerSocket;
25 import java.net.Socket;
26 import java.util.Random;
27
28 import junit.framework.TestCase;
29
30
31
32
33
34
35
36 public class TestEndPoint extends TestCase {
37
38 public void testAccessors() throws Exception {
39
40 final Random random = new Random();
41
42 for (int i=0; i<10; ++i) {
43 final byte[] bytes = new byte[random.nextInt(30)];
44 random.nextBytes(bytes);
45 final String hostname = new String(bytes);
46 final int port = random.nextInt(65536);
47
48 final EndPoint endPoint = new EndPoint(hostname, port);
49 assertEquals(hostname.toLowerCase(), endPoint.getHost());
50 assertEquals(port, endPoint.getPort());
51 }
52 }
53
54 public void testEquality() throws Exception {
55 final EndPoint[] endPoint = {
56 new EndPoint("Abcdef", 55),
57 new EndPoint("aBCDef", 55),
58 new EndPoint("c", 55),
59 new EndPoint("a", 5512),
60 new EndPoint("a", 56),
61 };
62
63 assertEquals(endPoint[0], endPoint[0]);
64 assertEquals(endPoint[0], endPoint[1]);
65 assertEquals(endPoint[1], endPoint[0]);
66 assertFalse(endPoint[0].equals(endPoint[2]));
67 assertFalse(endPoint[1].equals(endPoint[3]));
68 assertFalse(endPoint[1].equals(endPoint[4]));
69
70 assertFalse(endPoint[0].equals(this));
71 }
72
73 public void testComparable() throws Exception {
74 final EndPoint[] ordered = {
75 new EndPoint("abc", 21331),
76 new EndPoint("abc", 21431),
77 new EndPoint("x", 131),
78 new EndPoint("X", 132),
79 new EndPoint("X", 91321),
80 };
81
82 for (int i=0; i<ordered.length; ++i) {
83 for (int j=0; j<i; ++j) {
84 assertTrue("EndPoint " + i + " is greater than EndPoint " + j,
85 ordered[i].compareTo(ordered[j]) > 0);
86 }
87
88 assertEquals(0, ordered[i].compareTo(ordered[i]));
89
90 for (int j=i+1; j<ordered.length; ++j) {
91 assertTrue("EndPoint " + i + " is less than EndPoint " + j,
92 ordered[i].compareTo(ordered[j]) < 0);
93 }
94 }
95
96
97 assertEquals(0, new EndPoint("blah", 999).compareTo(
98 new EndPoint("blah", 999)));
99 }
100
101 public void testServerEndPoint() throws Exception {
102 final ServerSocket serverSocket = new ServerSocket(0);
103 final EndPoint endPoint = EndPoint.serverEndPoint(serverSocket);
104 assertNotNull(endPoint);
105 serverSocket.close();
106 }
107
108 public void testClientEndPoint() throws Exception {
109 final ServerSocket serverSocket = new ServerSocket(0);
110 final Socket socket = new Socket(serverSocket.getInetAddress(),
111 serverSocket.getLocalPort());
112 final EndPoint endPoint = EndPoint.clientEndPoint(socket);
113 assertNotNull(endPoint);
114 socket.close();
115 serverSocket.close();
116 }
117 }