• 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 
checkFromStringCase( String hpString, int defaultPort, @Nullable String expectHost, int expectPort, boolean expectHasExplicitPort)103   private static void checkFromStringCase(
104       String hpString,
105       int defaultPort,
106       @Nullable String expectHost,
107       int expectPort,
108       boolean expectHasExplicitPort) {
109     HostAndPort hp;
110     try {
111       hp = HostAndPort.fromString(hpString);
112     } catch (IllegalArgumentException e) {
113       // Make sure we expected this.
114       assertNull(expectHost);
115       return;
116     }
117     assertNotNull(expectHost);
118 
119     // Apply withDefaultPort(), yielding hp2.
120     final boolean badDefaultPort = (defaultPort < 0 || defaultPort > 65535);
121     HostAndPort hp2 = null;
122     try {
123       hp2 = hp.withDefaultPort(defaultPort);
124       assertFalse(badDefaultPort);
125     } catch (IllegalArgumentException e) {
126       assertTrue(badDefaultPort);
127     }
128 
129     // Check the pre-withDefaultPort() instance.
130     if (expectHasExplicitPort) {
131       assertTrue(hp.hasPort());
132       assertEquals(expectPort, hp.getPort());
133     } else {
134       assertFalse(hp.hasPort());
135       try {
136         hp.getPort();
137         fail("Expected IllegalStateException");
138       } catch (IllegalStateException expected) {
139       }
140     }
141     assertEquals(expectHost, hp.getHost());
142 
143     // Check the post-withDefaultPort() instance (if any).
144     if (!badDefaultPort) {
145       try {
146         int port = hp2.getPort();
147         assertTrue(expectPort != -1);
148         assertEquals(expectPort, port);
149       } catch (IllegalStateException e) {
150         // Make sure we expected this to fail.
151         assertEquals(-1, expectPort);
152       }
153       assertEquals(expectHost, hp2.getHost());
154     }
155   }
156 
testFromParts()157   public void testFromParts() {
158     HostAndPort hp = HostAndPort.fromParts("gmail.com", 81);
159     assertEquals("gmail.com", hp.getHost());
160     assertTrue(hp.hasPort());
161     assertEquals(81, hp.getPort());
162 
163     try {
164       HostAndPort.fromParts("gmail.com:80", 81);
165       fail("Expected IllegalArgumentException");
166     } catch (IllegalArgumentException expected) {
167     }
168 
169     try {
170       HostAndPort.fromParts("gmail.com", -1);
171       fail("Expected IllegalArgumentException");
172     } catch (IllegalArgumentException expected) {
173     }
174   }
175 
testFromHost()176   public void testFromHost() {
177     HostAndPort hp = HostAndPort.fromHost("gmail.com");
178     assertEquals("gmail.com", hp.getHost());
179     assertFalse(hp.hasPort());
180 
181     hp = HostAndPort.fromHost("[::1]");
182     assertEquals("::1", hp.getHost());
183     assertFalse(hp.hasPort());
184 
185     try {
186       HostAndPort.fromHost("gmail.com:80");
187       fail("Expected IllegalArgumentException");
188     } catch (IllegalArgumentException expected) {
189     }
190 
191     try {
192       HostAndPort.fromHost("[gmail.com]");
193       fail("Expected IllegalArgumentException");
194     } catch (IllegalArgumentException expected) {
195     }
196   }
197 
testGetPortOrDefault()198   public void testGetPortOrDefault() {
199     assertEquals(80, HostAndPort.fromString("host:80").getPortOrDefault(123));
200     assertEquals(123, HostAndPort.fromString("host").getPortOrDefault(123));
201   }
202 
testHashCodeAndEquals()203   public void testHashCodeAndEquals() {
204     HostAndPort hpNoPort1 = HostAndPort.fromString("foo::123");
205     HostAndPort hpNoPort2 = HostAndPort.fromString("foo::123");
206     HostAndPort hpNoPort3 = HostAndPort.fromString("[foo::123]");
207     HostAndPort hpNoPort4 = HostAndPort.fromHost("[foo::123]");
208     HostAndPort hpNoPort5 = HostAndPort.fromHost("foo::123");
209 
210     HostAndPort hpWithPort1 = HostAndPort.fromParts("[foo::123]", 80);
211     HostAndPort hpWithPort2 = HostAndPort.fromParts("foo::123", 80);
212     HostAndPort hpWithPort3 = HostAndPort.fromString("[foo::123]:80");
213 
214     new EqualsTester()
215         .addEqualityGroup(hpNoPort1, hpNoPort2, hpNoPort3, hpNoPort4, hpNoPort5)
216         .addEqualityGroup(hpWithPort1, hpWithPort2, hpWithPort3)
217         .testEquals();
218   }
219 
testRequireBracketsForIPv6()220   public void testRequireBracketsForIPv6() {
221     // Bracketed IPv6 works fine.
222     assertEquals("::1", HostAndPort.fromString("[::1]").requireBracketsForIPv6().getHost());
223     assertEquals("::1", HostAndPort.fromString("[::1]:80").requireBracketsForIPv6().getHost());
224     // Non-bracketed non-IPv6 works fine.
225     assertEquals("x", HostAndPort.fromString("x").requireBracketsForIPv6().getHost());
226     assertEquals("x", HostAndPort.fromString("x:80").requireBracketsForIPv6().getHost());
227 
228     // Non-bracketed IPv6 fails.
229     try {
230       HostAndPort.fromString("::1").requireBracketsForIPv6();
231       fail("Expected IllegalArgumentException");
232     } catch (IllegalArgumentException expected) {
233     }
234   }
235 
testToString()236   public void testToString() {
237     // With ports.
238     assertEquals("foo:101", "" + HostAndPort.fromString("foo:101"));
239     assertEquals(":102", HostAndPort.fromString(":102").toString());
240     assertEquals("[1::2]:103", HostAndPort.fromParts("1::2", 103).toString());
241     assertEquals("[::1]:104", HostAndPort.fromString("[::1]:104").toString());
242 
243     // Without ports.
244     assertEquals("foo", "" + HostAndPort.fromString("foo"));
245     assertEquals("", HostAndPort.fromString("").toString());
246     assertEquals("[1::2]", HostAndPort.fromString("1::2").toString());
247     assertEquals("[::1]", HostAndPort.fromString("[::1]").toString());
248 
249     // Garbage in, garbage out.
250     assertEquals("[::]]:107", HostAndPort.fromParts("::]", 107).toString());
251     assertEquals("[[:]]:108", HostAndPort.fromString("[[:]]:108").toString());
252   }
253 
testSerialization()254   public void testSerialization() {
255     SerializableTester.reserializeAndAssert(HostAndPort.fromParts("host", 80));
256     SerializableTester.reserializeAndAssert(HostAndPort.fromString("host"));
257     SerializableTester.reserializeAndAssert(HostAndPort.fromString("host:80"));
258     SerializableTester.reserializeAndAssert(HostAndPort.fromString("[::1]:104"));
259     SerializableTester.reserializeAndAssert(HostAndPort.fromParts("1::2", 103));
260   }
261 }
262