Revision control
Copy as Markdown
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
package org.mozilla.focus.utils;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Pattern;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
@RunWith(RobolectricTestRunner.class)
public class TestFormattedDomain {
private final String BUGZILLA_URL = "https://bugzilla.mozilla.org/enter_bug.cgi?format=guided#h=dupes%7CData%20%26%20BI%20Services%20Team%7C";
private static final Pattern EMPTY_PATH = Pattern.compile("/*");
/**
* Returns true if {@link URI#getPath()} is not empty, false otherwise where empty means the given path contains
* characters other than "/".
*
*/
public static boolean isPathEmpty(@NonNull final URI uri) {
final String path = uri.getPath();
return TextUtils.isEmpty(path) || EMPTY_PATH.matcher(path).matches();
}
@Test
public void testIsPathEmptyWithURINoPath() throws Exception {
Assert.assertTrue(isPathEmpty(uri));
}
@Test
public void testIsPathEmptyWithURISlashPath() throws Exception {
Assert.assertTrue(isPathEmpty(uri));
}
@Test
public void testIsPathEmptyWithURIDoubleSlashPath() throws Exception {
Assert.assertTrue(isPathEmpty(uri));
}
@Test
public void testIsPathEmptyWithURIEncodedSpaceSlashPath() throws Exception {
Assert.assertFalse(isPathEmpty(uri));
}
@Test
public void testIsPathEmptyWithURIPath() throws Exception {
Assert.assertFalse(isPathEmpty(uri));
}
// --- format, include PublicSuffix --- //
@Test
public void testGetFormattedDomainWithSuffix0Parts() {
final boolean includePublicSuffix = true;
final int subdomainCount = 0;
assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google.com");
assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "example.com");
assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "foo.com");
assertGetFormattedDomain("https://user:pass@m.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "foo.com");
}
@Test
public void testGetFormattedDomainWithSuffix1Parts() {
final boolean includePublicSuffix = true;
final int subdomainCount = 1;
assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google.com");
assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "www.example.com");
assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "blog.foo.com");
assertGetFormattedDomain("https://user:pass@m.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "m.foo.com");
}
@Test
public void testGetFormattedDomainWithSuffix2Parts() {
final boolean includePublicSuffix = true;
final int subdomainCount = 2;
assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google.com");
assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "www.example.com");
assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "m.blog.foo.com");
assertGetFormattedDomain("https://user:pass@m.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "m.foo.com");
}
// --- format, exclude PublicSuffix --- //
@Test
public void testGetFormattedDomainNoSuffix0Parts() {
final boolean includePublicSuffix = false;
final int subdomainCount = 0;
assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google");
assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "example");
assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "foo");
assertGetFormattedDomain("https://user:pass@m.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "foo");
}
@Test
public void testGetFormattedDomainNoSuffix1Parts() {
final boolean includePublicSuffix = false;
final int subdomainCount = 1;
assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google");
assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "www.example");
assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "blog.foo");
assertGetFormattedDomain("https://user:pass@m.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "m.foo");
}
@Test
public void testGetFormattedDomainNoSuffix2Parts() {
final boolean includePublicSuffix = false;
final int subdomainCount = 2;
assertGetFormattedDomain("https://google.com/search", includePublicSuffix, subdomainCount, "google");
assertGetFormattedDomain("https://www.example.com/index.html", includePublicSuffix, subdomainCount, "www.example");
assertGetFormattedDomain("https://m.blog.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "m.blog.foo");
assertGetFormattedDomain("https://user:pass@m.foo.com/bar/baz?noo=abc#123", includePublicSuffix, subdomainCount, "m.foo");
}
// --- format, saving time by not splitting up these tests on public suffix param. --- //
@Test
public void testGetFormattedDomainTwoLevelPublicSuffix() throws Exception {
}
@Test
public void testGetFormattedDomainNormalTwoLevelPublicSuffixWithSubdomain() throws Exception {
assertGetFormattedDomain(BUGZILLA_URL, false, 0, "mozilla");
assertGetFormattedDomain(BUGZILLA_URL, true, 0, "mozilla.org");
}
@Test
public void testGetFormattedDomainWilcardDomain() throws Exception {
// TLD entry: *.kawasaki.jp
}
@Test
public void testGetFormattedDomainWilcardDomainWithAdditionalSubdomain() throws Exception {
// TLD entry: *.kawasaki.jp
}
@Test
public void testGetFormattedDomainExceptionDomain() throws Exception {
// TLD entry: !city.kawasaki.jp
}
@Test
public void testGetFormattedDomainExceptionDomainWithAdditionalSubdomain() throws Exception {
// TLD entry: !city.kawasaki.jp
}
@Test
public void testGetFormattedDomainExceptionDomainBugzillaURL() throws Exception {
// TLD entry: !city.kawasaki.jp
}
@Test
public void testGetFormattedDomainURIHasNoHost() throws Exception {
}
@Test
public void testGetFormattedDomainIPv4() throws Exception {
}
@Test
public void testGetFormattedDomainIPv6() throws Exception {
assertGetFormattedDomain("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]", false, 0, "[3ffe:1900:4545:3:200:f8ff:fe21:67cf]");
assertGetFormattedDomain("http://[3ffe:1900:4545:3:200:f8ff:fe21:67cf]", true, 0, "[3ffe:1900:4545:3:200:f8ff:fe21:67cf]");
}
@Test(expected = NullPointerException.class)
public void testGetFormattedDomainNullContextThrows() throws Exception {
}
@Test(expected = NullPointerException.class)
public void testGetFormattedDomainNullURIThrows() throws Exception {
FormattedDomain.format(RuntimeEnvironment.application, null, false, 0);
}
private void assertGetFormattedDomain(final String uriString, final boolean includePublicSuffix,
final int subdomainCount, final String expected) {
final URI uri;
try {
uri = new URI(uriString);
} catch (final URISyntaxException e) {
throw new IllegalArgumentException("Invalid URI passed into test: " + uriString);
}
Assert.assertEquals("for input:" + uriString + "||",
expected,
FormattedDomain.format(RuntimeEnvironment.application, uri, includePublicSuffix, subdomainCount));
}
@Test
public void testIsIPv4RealAddress() {
assertTrue(FormattedDomain.isIPv4("192.168.1.1"));
assertTrue(FormattedDomain.isIPv4("8.8.8.8"));
assertTrue(FormattedDomain.isIPv4("63.245.215.20"));
}
@Test
public void testIsIPv4WithProtocol() {
}
@Test
public void testIsIPv4WithPort() {
assertFalse(FormattedDomain.isIPv4("8.8.8.8:400"));
assertFalse(FormattedDomain.isIPv4("8.8.8.8:1337"));
}
@Test
public void testIsIPv4WithPath() {
assertFalse(FormattedDomain.isIPv4("8.8.8.8/index.html"));
assertFalse(FormattedDomain.isIPv4("8.8.8.8/"));
}
@Test
public void testIsIPv4WithIPv6() {
assertFalse(FormattedDomain.isIPv4("2001:db8::1 "));
assertFalse(FormattedDomain.isIPv4("2001:db8:0:1:1:1:1:1"));
assertFalse(FormattedDomain.isIPv4("[2001:db8:a0b:12f0::1]"));
}
}