• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.common.net;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.testing.EqualsTester;
21 import com.google.common.testing.SerializableTester;
22 import junit.framework.TestCase;
23 import org.checkerframework.checker.nullness.qual.Nullable;
24 
25 /**
26  * Tests for {@link HostAndPort}
27  *
28  * @author Paul Marks
29  */
30 @GwtCompatible
31 public class HostAndPortTest extends TestCase {
32 
testFromStringWellFormed()33   public void testFromStringWellFormed() {
34     // Well-formed inputs.
35     checkFromStringCase("google.com", 80, "google.com", 80, false);
36     checkFromStringCase("google.com", 80, "google.com", 80, false);
37     checkFromStringCase("192.0.2.1", 82, "192.0.2.1", 82, false);
38     checkFromStringCase("[2001::1]", 84, "2001::1", 84, false);
39     checkFromStringCase("2001::3", 86, "2001::3", 86, false);
40     checkFromStringCase("host:", 80, "host", 80, false);
41   }
42 
testFromStringBadDefaultPort()43   public void testFromStringBadDefaultPort() {
44     // Well-formed strings with bad default ports.
45     checkFromStringCase("gmail.com:81", -1, "gmail.com", 81, true);
46     checkFromStringCase("192.0.2.2:83", -1, "192.0.2.2", 83, true);
47     checkFromStringCase("[2001::2]:85", -1, "2001::2", 85, true);
48     checkFromStringCase("goo.gl:65535", 65536, "goo.gl", 65535, true);
49     // No port, bad default.
50     checkFromStringCase("google.com", -1, "google.com", -1, false);
51     checkFromStringCase("192.0.2.1", 65536, "192.0.2.1", -1, false);
52     checkFromStringCase("[2001::1]", -1, "2001::1", -1, false);
53     checkFromStringCase("2001::3", 65536, "2001::3", -1, false);
54   }
55 
testFromStringUnusedDefaultPort()56   public void testFromStringUnusedDefaultPort() {
57     // Default port, but unused.
58     checkFromStringCase("gmail.com:81", 77, "gmail.com", 81, true);
59     checkFromStringCase("192.0.2.2:83", 77, "192.0.2.2", 83, true);
60     checkFromStringCase("[2001::2]:85", 77, "2001::2", 85, true);
61   }
62 
testFromStringNonAsciiDigits()63   public void testFromStringNonAsciiDigits() {
64     // Same as testFromStringUnusedDefaultPort but with Gujarati digits for port numbers.
65     checkFromStringCase("gmail.com:૮1", 77, null, -1, false);
66     checkFromStringCase("192.0.2.2:૮૩", 77, null, -1, false);
67     checkFromStringCase("[2001::2]:૮૫", 77, null, -1, false);
68   }
69 
testFromStringBadPort()70   public void testFromStringBadPort() {
71     // Out-of-range ports.
72     checkFromStringCase("google.com:65536", 1, null, 99, false);
73     checkFromStringCase("google.com:9999999999", 1, null, 99, false);
74     // Invalid port parts.
75     checkFromStringCase("google.com:port", 1, null, 99, false);
76     checkFromStringCase("google.com:-25", 1, null, 99, false);
77     checkFromStringCase("google.com:+25", 1, null, 99, false);
78     checkFromStringCase("google.com:25  ", 1, null, 99, false);
79     checkFromStringCase("google.com:25\t", 1, null, 99, false);
80     checkFromStringCase("google.com:0x25 ", 1, null, 99, false);
81   }
82 
testFromStringUnparseableNonsense()83   public void testFromStringUnparseableNonsense() {
84     // Some nonsense that causes parse failures.
85     checkFromStringCase("[goo.gl]", 1, null, 99, false);
86     checkFromStringCase("[goo.gl]:80", 1, null, 99, false);
87     checkFromStringCase("[", 1, null, 99, false);
88     checkFromStringCase("[]:", 1, null, 99, false);
89     checkFromStringCase("[]:80", 1, null, 99, false);
90     checkFromStringCase("[]bad", 1, null, 99, false);
91   }
92 
testFromStringParseableNonsense()93   public void testFromStringParseableNonsense() {
94     // Examples of nonsense that gets through.
95     checkFromStringCase("[[:]]", 86, "[:]", 86, false);
96     checkFromStringCase("x:y:z", 87, "x:y:z", 87, false);
97     checkFromStringCase("", 88, "", 88, false);
98     checkFromStringCase(":", 99, "", 99, false);
99     checkFromStringCase(":123", -1, "", 123, true);
100     checkFromStringCase("\nOMG\t", 89, "\nOMG\t", 89, false);
101   }
102 
testFromStringParseableIncompleteAddresses()103   public void testFromStringParseableIncompleteAddresses() {
104     checkFromStringCase("1.2.3", 87, "1.2.3", 87, false);
105     checkFromStringCase("1.2.3:99", 87, "1.2.3", 99, true);
106     checkFromStringCase("2001:4860:4864:5", 87, "2001:4860:4864:5", 87, false);
107     checkFromStringCase("[2001:4860:4864:5]:99", 87, "2001:4860:4864:5", 99, true);
108   }
109 
checkFromStringCase( String hpString, int defaultPort, @Nullable String expectHost, int expectPort, boolean expectHasExplicitPort)110   private static void checkFromStringCase(
111       String hpString,
112       int defaultPort,
113       @Nullable String expectHost,
114       int expectPort,
115       boolean expectHasExplicitPort) {
116     HostAndPort hp;
117     try {
118       hp = HostAndPort.fromString(hpString);
119     } catch (IllegalArgumentException e) {
120       // Make sure we expected this.
121       assertNull(expectHost);
122       return;
123     }
124     assertNotNull(expectHost);
125 
126     // Apply withDefaultPort(), yielding hp2.
127     final boolean badDefaultPort = (defaultPort < 0 || defaultPort > 65535);
128     HostAndPort hp2 = null;
129     try {
130       hp2 = hp.withDefaultPort(defaultPort);
131       assertFalse(badDefaultPort);
132     } catch (IllegalArgumentException e) {
133       assertTrue(badDefaultPort);
134     }
135 
136     // Check the pre-withDefaultPort() instance.
137     if (expectHasExplicitPort) {
138       assertTrue(hp.hasPort());
139       assertEquals(expectPort, hp.getPort());
140     } else {
141       assertFalse(hp.hasPort());
142       try {
143         hp.getPort();
144         fail("Expected IllegalStateException");
145       } catch (IllegalStateException expected) {
146       }
147     }
148     assertEquals(expectHost, hp.getHost());
149 
150     // Check the post-withDefaultPort() instance (if any).
151     if (!badDefaultPort) {
152       try {
153         int port = hp2.getPort();
154         assertTrue(expectPort != -1);
155         assertEquals(expectPort, port);
156       } catch (IllegalStateException e) {
157         // Make sure we expected this to fail.
158         assertEquals(-1, expectPort);
159       }
160       assertEquals(expectHost, hp2.getHost());
161     }
162   }
163 
testFromParts()164   public void testFromParts() {
165     HostAndPort hp = HostAndPort.fromParts("gmail.com", 81);
166     assertEquals("gmail.com", hp.getHost());
167     assertTrue(hp.hasPort());
168     assertEquals(81, hp.getPort());
169 
170     try {
171       HostAndPort.fromParts("gmail.com:80", 81);
172       fail("Expected IllegalArgumentException");
173     } catch (IllegalArgumentException expected) {
174     }
175 
176     try {
177       HostAndPort.fromParts("gmail.com", -1);
178       fail("Expected IllegalArgumentException");
179     } catch (IllegalArgumentException expected) {
180     }
181   }
182 
testFromHost()183   public void testFromHost() {
184     HostAndPort hp = HostAndPort.fromHost("gmail.com");
185     assertEquals("gmail.com", hp.getHost());
186     assertFalse(hp.hasPort());
187 
188     hp = HostAndPort.fromHost("[::1]");
189     assertEquals("::1", hp.getHost());
190     assertFalse(hp.hasPort());
191 
192     try {
193       HostAndPort.fromHost("gmail.com:80");
194       fail("Expected IllegalArgumentException");
195     } catch (IllegalArgumentException expected) {
196     }
197 
198     try {
199       HostAndPort.fromHost("[gmail.com]");
200       fail("Expected IllegalArgumentException");
201     } catch (IllegalArgumentException expected) {
202     }
203   }
204 
testGetPortOrDefault()205   public void testGetPortOrDefault() {
206     assertEquals(80, HostAndPort.fromString("host:80").getPortOrDefault(123));
207     assertEquals(123, HostAndPort.fromString("host").getPortOrDefault(123));
208   }
209 
testHashCodeAndEquals()210   public void testHashCodeAndEquals() {
211     HostAndPort hpNoPort1 = HostAndPort.fromString("foo::123");
212     HostAndPort hpNoPort2 = HostAndPort.fromString("foo::123");
213     HostAndPort hpNoPort3 = HostAndPort.fromString("[foo::123]");
214     HostAndPort hpNoPort4 = HostAndPort.fromHost("[foo::123]");
215     HostAndPort hpNoPort5 = HostAndPort.fromHost("foo::123");
216 
217     HostAndPort hpWithPort1 = HostAndPort.fromParts("[foo::123]", 80);
218     HostAndPort hpWithPort2 = HostAndPort.fromParts("foo::123", 80);
219     HostAndPort hpWithPort3 = HostAndPort.fromString("[foo::123]:80");
220 
221     new EqualsTester()
222         .addEqualityGroup(hpNoPort1, hpNoPort2, hpNoPort3, hpNoPort4, hpNoPort5)
223         .addEqualityGroup(hpWithPort1, hpWithPort2, hpWithPort3)
224         .testEquals();
225   }
226 
testRequireBracketsForIPv6()227   public void testRequireBracketsForIPv6() {
228     // Bracketed IPv6 works fine.
229     assertEquals("::1", HostAndPort.fromString("[::1]").requireBracketsForIPv6().getHost());
230     assertEquals("::1", HostAndPort.fromString("[::1]:80").requireBracketsForIPv6().getHost());
231     // Non-bracketed non-IPv6 works fine.
232     assertEquals("x", HostAndPort.fromString("x").requireBracketsForIPv6().getHost());
233     assertEquals("x", HostAndPort.fromString("x:80").requireBracketsForIPv6().getHost());
234 
235     // Non-bracketed IPv6 fails.
236     try {
237       HostAndPort.fromString("::1").requireBracketsForIPv6();
238       fail("Expected IllegalArgumentException");
239     } catch (IllegalArgumentException expected) {
240     }
241   }
242 
testToString()243   public void testToString() {
244     // With ports.
245     assertEquals("foo:101", "" + HostAndPort.fromString("foo:101"));
246     assertEquals(":102", HostAndPort.fromString(":102").toString());
247     assertEquals("[1::2]:103", HostAndPort.fromParts("1::2", 103).toString());
248     assertEquals("[::1]:104", HostAndPort.fromString("[::1]:104").toString());
249 
250     // Without ports.
251     assertEquals("foo", "" + HostAndPort.fromString("foo"));
252     assertEquals("", HostAndPort.fromString("").toString());
253     assertEquals("[1::2]", HostAndPort.fromString("1::2").toString());
254     assertEquals("[::1]", HostAndPort.fromString("[::1]").toString());
255 
256     // Garbage in, garbage out.
257     assertEquals("[::]]:107", HostAndPort.fromParts("::]", 107).toString());
258     assertEquals("[[:]]:108", HostAndPort.fromString("[[:]]:108").toString());
259   }
260 
testSerialization()261   public void testSerialization() {
262     SerializableTester.reserializeAndAssert(HostAndPort.fromParts("host", 80));
263     SerializableTester.reserializeAndAssert(HostAndPort.fromString("host"));
264     SerializableTester.reserializeAndAssert(HostAndPort.fromString("host:80"));
265     SerializableTester.reserializeAndAssert(HostAndPort.fromString("[::1]:104"));
266     SerializableTester.reserializeAndAssert(HostAndPort.fromParts("1::2", 103));
267   }
268 }
269