Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ public void testFromStringUnparseableNonsense() {
checkFromStringCase("[]:", 1, null, 99, false);
checkFromStringCase("[]:80", 1, null, 99, false);
checkFromStringCase("[]bad", 1, null, 99, false);
checkFromStringCase("[[:]]", 1, null, 99, false);
checkFromStringCase("[[:]]:108", 1, null, 99, false);
checkFromStringCase("[2001:db8::a]:25]:443", 1, null, 99, false);
checkFromStringCase("[::1]:22]", 1, null, 99, false);
}

public void testFromStringParseableNonsense() {
// Examples of nonsense that gets through.
checkFromStringCase("[[:]]", 86, "[:]", 86, false);
checkFromStringCase("[[:]", 86, "[:", 86, false);
checkFromStringCase("x:y:z", 87, "x:y:z", 87, false);
checkFromStringCase("", 88, "", 88, false);
checkFromStringCase(":", 99, "", 99, false);
Expand Down Expand Up @@ -175,6 +179,8 @@ public void testFromHost() {
assertThrows(IllegalArgumentException.class, () -> HostAndPort.fromHost("gmail.com:80"));

assertThrows(IllegalArgumentException.class, () -> HostAndPort.fromHost("[gmail.com]"));

assertThrows(IllegalArgumentException.class, () -> HostAndPort.fromHost("[::1]:22]"));
}

public void testGetPortOrDefault() {
Expand Down Expand Up @@ -229,7 +235,7 @@ public void testToString() {

// Garbage in, garbage out.
assertThat(HostAndPort.fromParts("::]", 107).toString()).isEqualTo("[::]]:107");
assertThat(HostAndPort.fromString("[[:]]:108").toString()).isEqualTo("[[:]]:108");
assertThat(HostAndPort.fromString("[[:]:108").toString()).isEqualTo("[[:]:108");
}

public void testSerialization() {
Expand Down
2 changes: 1 addition & 1 deletion android/guava/src/com/google/common/net/HostAndPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static String[] getHostAndPortFromBracketedHost(String hostPortString) {
"Bracketed host-port string must start with a bracket: %s",
hostPortString);
int colonIndex = hostPortString.indexOf(':');
int closeBracketIndex = hostPortString.lastIndexOf(']');
int closeBracketIndex = hostPortString.indexOf(']');
checkArgument(
colonIndex > -1 && closeBracketIndex > colonIndex,
"Invalid bracketed host/port: %s",
Expand Down
10 changes: 8 additions & 2 deletions guava-tests/test/com/google/common/net/HostAndPortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,15 @@ public void testFromStringUnparseableNonsense() {
checkFromStringCase("[]:", 1, null, 99, false);
checkFromStringCase("[]:80", 1, null, 99, false);
checkFromStringCase("[]bad", 1, null, 99, false);
checkFromStringCase("[[:]]", 1, null, 99, false);
checkFromStringCase("[[:]]:108", 1, null, 99, false);
checkFromStringCase("[2001:db8::a]:25]:443", 1, null, 99, false);
checkFromStringCase("[::1]:22]", 1, null, 99, false);
}

public void testFromStringParseableNonsense() {
// Examples of nonsense that gets through.
checkFromStringCase("[[:]]", 86, "[:]", 86, false);
checkFromStringCase("[[:]", 86, "[:", 86, false);
checkFromStringCase("x:y:z", 87, "x:y:z", 87, false);
checkFromStringCase("", 88, "", 88, false);
checkFromStringCase(":", 99, "", 99, false);
Expand Down Expand Up @@ -182,6 +186,8 @@ public void testFromHost() {
assertThrows(IllegalArgumentException.class, () -> HostAndPort.fromHost("gmail.com:80"));

assertThrows(IllegalArgumentException.class, () -> HostAndPort.fromHost("[gmail.com]"));

assertThrows(IllegalArgumentException.class, () -> HostAndPort.fromHost("[::1]:22]"));
}

public void testGetPortOrDefault() {
Expand Down Expand Up @@ -236,7 +242,7 @@ public void testToString() {

// Garbage in, garbage out.
assertThat(HostAndPort.fromParts("::]", 107).toString()).isEqualTo("[::]]:107");
assertThat(HostAndPort.fromString("[[:]]:108").toString()).isEqualTo("[[:]]:108");
assertThat(HostAndPort.fromString("[[:]:108").toString()).isEqualTo("[[:]:108");
}

public void testSerialization() {
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/net/HostAndPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private static String[] getHostAndPortFromBracketedHost(String hostPortString) {
"Bracketed host-port string must start with a bracket: %s",
hostPortString);
int colonIndex = hostPortString.indexOf(':');
int closeBracketIndex = hostPortString.lastIndexOf(']');
int closeBracketIndex = hostPortString.indexOf(']');
checkArgument(
colonIndex > -1 && closeBracketIndex > colonIndex,
"Invalid bracketed host/port: %s",
Expand Down
Loading