From b0cf1a89168620f7490a375056059c6f84b05081 Mon Sep 17 00:00:00 2001 From: sakal Date: Wed, 25 May 2016 02:26:22 -0700 Subject: [PATCH] Improve IP_PATTERN in DirectRTCClient in Android AppRTC Demo Earlier, strings like "aaaa" and "1111" would be interpreted as IP addresses, which is not optimal. This CL improves the IP_PATTERN and adds a test for it. Review-Url: https://codereview.webrtc.org/2009493002 Cr-Commit-Position: refs/heads/master@{#12887} --- .../org/appspot/apprtc/DirectRTCClient.java | 4 +- .../appspot/apprtc/DirectRTCClientTest.java | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/webrtc/examples/androidapp/src/org/appspot/apprtc/DirectRTCClient.java b/webrtc/examples/androidapp/src/org/appspot/apprtc/DirectRTCClient.java index 4b6c87719e..b85f6da204 100644 --- a/webrtc/examples/androidapp/src/org/appspot/apprtc/DirectRTCClient.java +++ b/webrtc/examples/androidapp/src/org/appspot/apprtc/DirectRTCClient.java @@ -42,10 +42,10 @@ public class DirectRTCClient implements AppRTCClient, TCPChannelClient.TCPChanne // IPv6 + "\\[((([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?::" + "(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?)\\]|" - + "\\[(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})\\]|" + + "\\[(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4})\\]|" // IPv6 without [] + "((([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?::(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})?)|" - + "(([0-9a-fA-F]{1,4}:)*[0-9a-fA-F]{1,4})|" + + "(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4})|" // Literals + "localhost" + ")" diff --git a/webrtc/examples/androidjunit/src/org/appspot/apprtc/DirectRTCClientTest.java b/webrtc/examples/androidjunit/src/org/appspot/apprtc/DirectRTCClientTest.java index 26ad536adf..9a18107a6b 100644 --- a/webrtc/examples/androidjunit/src/org/appspot/apprtc/DirectRTCClientTest.java +++ b/webrtc/examples/androidjunit/src/org/appspot/apprtc/DirectRTCClientTest.java @@ -18,6 +18,10 @@ import org.robolectric.annotation.Config; import org.webrtc.IceCandidate; import org.webrtc.SessionDescription; +import java.util.regex.Matcher; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.isNotNull; @@ -57,6 +61,50 @@ public class DirectRTCClientTest { server = new DirectRTCClient(serverEvents); } + @Test + public void testValidIpPattern() { + // Strings that should match the pattern. + final String[] ipAddresses = new String[] { + "0.0.0.0", + "127.0.0.1", + "192.168.0.1", + "0.0.0.0:8888", + "127.0.0.1:8888", + "192.168.0.1:8888", + "::", + "::1", + "2001:0db8:85a3:0000:0000:8a2e:0370:7946", + "[::]", + "[::1]", + "[2001:0db8:85a3:0000:0000:8a2e:0370:7946]", + "[::]:8888", + "[::1]:8888", + "[2001:0db8:85a3:0000:0000:8a2e:0370:7946]:8888" + }; + + for (String ip : ipAddresses) { + assertTrue(ip + " didn't match IP_PATTERN even though it should.", + DirectRTCClient.IP_PATTERN.matcher(ip).matches()); + } + } + + @Test + public void testInvalidIpPattern() { + // Strings that shouldn't match the pattern. + final String[] invalidIpAddresses = new String[] { + "Hello, World!", + "aaaa", + "1111", + "[hello world]", + "hello:world" + }; + + for (String invalidIp : invalidIpAddresses) { + assertFalse(invalidIp + " matched IP_PATTERN even though it shouldn't.", + DirectRTCClient.IP_PATTERN.matcher(invalidIp).matches()); + } + } + @Test public void testDirectRTCClient() { server.connectToRoom(new AppRTCClient.RoomConnectionParameters(ROOM_URL, "0.0.0.0", LOOPBACK));