HostAndPortTest.java revision 3c77433663281544363151bf284b0240dfd22a42
1/*
2 * Copyright (C) 2011 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.net;
18
19import com.google.common.testing.SerializableTester;
20
21import junit.framework.TestCase;
22
23/**
24 * Tests for {@link HostAndPort}
25 *
26 * @author Paul Marks
27 */
28public class HostAndPortTest extends TestCase {
29
30  public void testFromStringWellFormed() {
31    // Well-formed inputs.
32    checkFromStringCase("google.com",           80, "google.com", 80, false);
33    checkFromStringCase("google.com",           80, "google.com", 80, false);
34    checkFromStringCase("192.0.2.1",            82, "192.0.2.1",  82, false);
35    checkFromStringCase("[2001::1]",            84, "2001::1",    84, false);
36    checkFromStringCase("2001::3",              86, "2001::3",    86, false);
37    checkFromStringCase("host:",                80, "host",       80, false);
38  }
39
40  public void testFromStringBadDefaultPort() {
41    // Well-formed strings with bad default ports.
42    checkFromStringCase("gmail.com:81",         -1, "gmail.com",  81, true);
43    checkFromStringCase("192.0.2.2:83",         -1, "192.0.2.2",  83, true);
44    checkFromStringCase("[2001::2]:85",         -1, "2001::2",    85, true);
45    checkFromStringCase("goo.gl:65535",      65536, "goo.gl",  65535, true);
46    // No port, bad default.
47    checkFromStringCase("google.com",           -1, "google.com", -1, false);
48    checkFromStringCase("192.0.2.1",         65536, "192.0.2.1",  -1, false);
49    checkFromStringCase("[2001::1]",            -1, "2001::1",    -1, false);
50    checkFromStringCase("2001::3",           65536, "2001::3",    -1, false);
51  }
52
53  public void testFromStringUnusedDefaultPort() {
54    // Default port, but unused.
55    checkFromStringCase("gmail.com:81",         77, "gmail.com",  81, true);
56    checkFromStringCase("192.0.2.2:83",         77, "192.0.2.2",  83, true);
57    checkFromStringCase("[2001::2]:85",         77, "2001::2",    85, true);
58  }
59
60  public void testFromStringBadPort() {
61    // Out-of-range ports.
62    checkFromStringCase("google.com:65536",      1, null,         99, false);
63    checkFromStringCase("google.com:9999999999", 1, null,         99, false);
64    // Invalid port parts.
65    checkFromStringCase("google.com:port",       1, null,         99, false);
66    checkFromStringCase("google.com:-25",        1, null,         99, false);
67    checkFromStringCase("google.com:+25",        1, null,         99, false);
68    checkFromStringCase("google.com:25  ",       1, null,         99, false);
69    checkFromStringCase("google.com:25\t",       1, null,         99, false);
70    checkFromStringCase("google.com:0x25 ",      1, null,         99, false);
71  }
72
73  public void testFromStringUnparseableNonsense() {
74    // Some nonsense that causes parse failures.
75    checkFromStringCase("[goo.gl]",              1, null,         99, false);
76    checkFromStringCase("[goo.gl]:80",           1, null,         99, false);
77    checkFromStringCase("[",                     1, null,         99, false);
78    checkFromStringCase("[]:",                   1, null,         99, false);
79    checkFromStringCase("[]:80",                 1, null,         99, false);
80    checkFromStringCase("[]bad",                 1, null,         99, false);
81  }
82
83  public void testFromStringParseableNonsense() {
84    // Examples of nonsense that gets through.
85    checkFromStringCase("[[:]]",                86, "[:]",        86, false);
86    checkFromStringCase("x:y:z",                87, "x:y:z",      87, false);
87    checkFromStringCase("",                     88, "",           88, false);
88    checkFromStringCase(":",                    99, "",           99, false);
89    checkFromStringCase(":123",                 -1, "",          123, true);
90    checkFromStringCase("\nOMG\t",              89, "\nOMG\t",    89, false);
91  }
92
93  private static void checkFromStringCase(
94      String hpString,
95      int defaultPort,
96      String expectHost,
97      int expectPort,
98      boolean expectHasExplicitPort) {
99    HostAndPort hp;
100    try {
101      hp = HostAndPort.fromString(hpString);
102    } catch (IllegalArgumentException e) {
103      // Make sure we expected this.
104      assertNull(expectHost);
105      return;
106    }
107    assertNotNull(expectHost);
108
109    // Apply withDefaultPort(), yielding hp2.
110    final boolean badDefaultPort = (defaultPort < 0 || defaultPort > 65535);
111    HostAndPort hp2 = null;
112    try {
113      hp2 = hp.withDefaultPort(defaultPort);
114      assertFalse(badDefaultPort);
115    } catch (IllegalArgumentException e) {
116      assertTrue(badDefaultPort);
117    }
118
119    // Check the pre-withDefaultPort() instance.
120    if (expectHasExplicitPort) {
121      assertTrue(hp.hasPort());
122      assertEquals(expectPort, hp.getPort());
123    } else {
124      assertFalse(hp.hasPort());
125      try {
126        hp.getPort();
127        fail("Expected IllegalStateException");
128      } catch (IllegalStateException expected) {
129      }
130    }
131    assertEquals(expectHost, hp.getHostText());
132
133    // Check the post-withDefaultPort() instance (if any).
134    if (!badDefaultPort) {
135      try {
136        int port = hp2.getPort();
137        assertTrue(expectPort != -1);
138        assertEquals(expectPort, port);
139      } catch (IllegalStateException e) {
140        // Make sure we expected this to fail.
141        assertEquals(-1, expectPort);
142      }
143      assertEquals(expectHost, hp2.getHostText());
144    }
145  }
146
147  public void testFromParts() {
148    HostAndPort hp = HostAndPort.fromParts("gmail.com", 81);
149    assertEquals("gmail.com", hp.getHostText());
150    assertTrue(hp.hasPort());
151    assertEquals(81, hp.getPort());
152
153    try {
154      HostAndPort.fromParts("gmail.com:80", 81);
155      fail("Expected IllegalArgumentException");
156    } catch (IllegalArgumentException expected) {
157    }
158
159    try {
160      HostAndPort.fromParts("gmail.com", -1);
161      fail("Expected IllegalArgumentException");
162    } catch (IllegalArgumentException expected) {
163    }
164  }
165
166  public void testGetPortOrDefault() {
167    assertEquals(80, HostAndPort.fromString("host:80").getPortOrDefault(123));
168    assertEquals(123, HostAndPort.fromString("host").getPortOrDefault(123));
169  }
170
171  public void testHashCodeAndEquals() {
172    HostAndPort hp1 = HostAndPort.fromString("foo::123");
173    HostAndPort hp2 = HostAndPort.fromString("foo::123");
174    HostAndPort hp3 = HostAndPort.fromString("[foo::123]");
175    HostAndPort hp4 = HostAndPort.fromParts("[foo::123]", 80);
176    HostAndPort hp5 = HostAndPort.fromString("[foo::123]:80");
177    assertEquals(hp1.hashCode(), hp1.hashCode());
178    assertEquals(hp1.hashCode(), hp2.hashCode());
179    assertFalse(hp1.hashCode() == hp3.hashCode());
180    assertFalse(hp3.hashCode() == hp4.hashCode());
181    assertEquals(hp4.hashCode(), hp5.hashCode());
182
183    assertTrue(hp1.equals(hp1));
184    assertTrue(hp1.equals(hp2));
185    assertFalse(hp1.equals(hp3));
186    assertFalse(hp3.equals(hp4));
187    assertTrue(hp4.equals(hp5));
188    assertFalse(hp1.equals(null));
189  }
190
191  public void testRequireBracketsForIPv6() {
192    // Bracketed IPv6 works fine.
193    assertEquals("::1", HostAndPort.fromString("[::1]").requireBracketsForIPv6().getHostText());
194    assertEquals("::1", HostAndPort.fromString("[::1]:80").requireBracketsForIPv6().getHostText());
195    // Non-bracketed non-IPv6 works fine.
196    assertEquals("x", HostAndPort.fromString("x").requireBracketsForIPv6().getHostText());
197    assertEquals("x", HostAndPort.fromString("x:80").requireBracketsForIPv6().getHostText());
198
199    // Non-bracketed IPv6 fails.
200    try {
201      HostAndPort.fromString("::1").requireBracketsForIPv6();
202      fail("Expected IllegalArgumentException");
203    } catch (IllegalArgumentException expected) {
204    }
205  }
206
207  public void testToString() {
208    // With ports.
209    assertEquals("foo:101", "" + HostAndPort.fromString("foo:101"));
210    assertEquals(":102", HostAndPort.fromString(":102").toString());
211    assertEquals("[1::2]:103", HostAndPort.fromParts("1::2", 103).toString());
212    assertEquals("[::1]:104", HostAndPort.fromString("[::1]:104").toString());
213
214    // Without ports.
215    assertEquals("foo", "" + HostAndPort.fromString("foo"));
216    assertEquals("", HostAndPort.fromString("").toString());
217    assertEquals("[1::2]", HostAndPort.fromString("1::2").toString());
218    assertEquals("[::1]", HostAndPort.fromString("[::1]").toString());
219
220    // Garbage in, garbage out.
221    assertEquals("[::]]:107", HostAndPort.fromParts("::]", 107).toString());
222    assertEquals("[[:]]:108", HostAndPort.fromString("[[:]]:108").toString());
223  }
224
225  public void testSerialization() {
226    SerializableTester.reserializeAndAssert(HostAndPort.fromParts("host", 80));
227    SerializableTester.reserializeAndAssert(HostAndPort.fromString("host"));
228    SerializableTester.reserializeAndAssert(HostAndPort.fromString("host:80"));
229    SerializableTester.reserializeAndAssert(HostAndPort.fromString("[::1]:104"));
230    SerializableTester.reserializeAndAssert(HostAndPort.fromParts("1::2", 103));
231  }
232}
233