1 // Copyright 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.chrome.browser; 6 7 import android.test.InstrumentationTestCase; 8 import android.test.suitebuilder.annotation.SmallTest; 9 10 import org.chromium.base.test.util.Feature; 11 12 import java.net.URI; 13 import java.net.URISyntaxException; 14 15 public class UrlUtilitiesTest extends InstrumentationTestCase { 16 @SmallTest 17 public void testIsAcceptedScheme() { 18 assertTrue(UrlUtilities.isAcceptedScheme("about:awesome")); 19 assertTrue(UrlUtilities.isAcceptedScheme("data:data")); 20 assertTrue(UrlUtilities.isAcceptedScheme( 21 "https://user:pass@:awesome.com:9000/bad-scheme:#fake:")); 22 assertTrue(UrlUtilities.isAcceptedScheme("http://awesome.example.com/")); 23 assertTrue(UrlUtilities.isAcceptedScheme("file://awesome.example.com/")); 24 assertTrue(UrlUtilities.isAcceptedScheme("inline:skates.co.uk")); 25 assertTrue(UrlUtilities.isAcceptedScheme("javascript:alert(1)")); 26 27 assertFalse(UrlUtilities.isAcceptedScheme("super:awesome")); 28 assertFalse(UrlUtilities.isAcceptedScheme( 29 "ftp://https:password@example.com/")); 30 assertFalse(UrlUtilities.isAcceptedScheme( 31 "ftp://https:password@example.com/?http:#http:")); 32 assertFalse(UrlUtilities.isAcceptedScheme( 33 "google-search://https:password (at) example.com/?http:#http:")); 34 assertFalse(UrlUtilities.isAcceptedScheme("chrome://http://version")); 35 assertFalse(UrlUtilities.isAcceptedScheme("")); 36 assertFalse(UrlUtilities.isAcceptedScheme(" http://awesome.example.com/")); 37 assertFalse(UrlUtilities.isAcceptedScheme("ht\ntp://awesome.example.com/")); 38 } 39 40 @SmallTest 41 public void testIsDownloadableScheme() { 42 assertTrue(UrlUtilities.isDownloadableScheme("data:data")); 43 assertTrue(UrlUtilities.isDownloadableScheme( 44 "https://user:pass@:awesome.com:9000/bad-scheme:#fake:")); 45 assertTrue(UrlUtilities.isDownloadableScheme("http://awesome.example.com/")); 46 assertTrue(UrlUtilities.isDownloadableScheme("filesystem://awesome.example.com/")); 47 48 assertFalse(UrlUtilities.isDownloadableScheme("inline:skates.co.uk")); 49 assertFalse(UrlUtilities.isDownloadableScheme("javascript:alert(1)")); 50 assertFalse(UrlUtilities.isDownloadableScheme("file://awesome.example.com/")); 51 assertFalse(UrlUtilities.isDownloadableScheme("about:awesome")); 52 assertFalse(UrlUtilities.isDownloadableScheme("super:awesome")); 53 assertFalse(UrlUtilities.isDownloadableScheme("ftp://https:password@example.com/")); 54 assertFalse(UrlUtilities.isDownloadableScheme( 55 "ftp://https:password@example.com/?http:#http:")); 56 assertFalse(UrlUtilities.isDownloadableScheme( 57 "google-search://https:password (at) example.com/?http:#http:")); 58 assertFalse(UrlUtilities.isDownloadableScheme("chrome://http://version")); 59 assertFalse(UrlUtilities.isDownloadableScheme("")); 60 assertFalse(UrlUtilities.isDownloadableScheme(" http://awesome.example.com/")); 61 assertFalse(UrlUtilities.isDownloadableScheme("ht\ntp://awesome.example.com/")); 62 } 63 64 @SmallTest 65 public void testFixUrl() throws URISyntaxException { 66 try { 67 URI uri; 68 uri = new URI(UrlUtilities.fixUrl("google.com")); 69 assertTrue("http".equals(uri.getScheme())); 70 uri = new URI(UrlUtilities.fixUrl("\n://user:pass (at) example.com:80/")); 71 assertTrue("http".equals(uri.getScheme())); 72 uri = new URI(UrlUtilities.fixUrl("inline:google.com")); 73 assertTrue("inline".equals(uri.getScheme())); 74 uri = new URI(UrlUtilities.fixUrl("chrome:user:pass@google:443/leg:foot")); 75 assertTrue("chrome".equals(uri.getScheme())); 76 uri = new URI(UrlUtilities.fixUrl("https://mail.google.com:/")); 77 assertTrue("https".equals(uri.getScheme())); 78 uri = new URI(UrlUtilities.fixUrl("://mail.google.com:/")); 79 assertTrue("http".equals(uri.getScheme())); 80 uri = new URI(UrlUtilities.fixUrl("//mail.google.com:/")); 81 assertTrue("http".equals(uri.getScheme())); 82 } catch (URISyntaxException e) { 83 assertFalse(true); 84 } 85 } 86 87 @SmallTest 88 @Feature({"Webapps"}) 89 public void testGetOriginForDisplay() { 90 URI uri; 91 92 uri = URI.create("http://chopped.com/is/awesome"); 93 assertEquals("http://chopped.com", UrlUtilities.getOriginForDisplay(uri, true)); 94 assertEquals("chopped.com", UrlUtilities.getOriginForDisplay(uri, false)); 95 96 uri = URI.create("http://lopped.com"); 97 assertEquals("http://lopped.com", UrlUtilities.getOriginForDisplay(uri, true)); 98 assertEquals("lopped.com", UrlUtilities.getOriginForDisplay(uri, false)); 99 100 uri = URI.create("http://dropped.com?things"); 101 assertEquals("http://dropped.com", UrlUtilities.getOriginForDisplay(uri, true)); 102 assertEquals("dropped.com", UrlUtilities.getOriginForDisplay(uri, false)); 103 104 uri = URI.create("http://dfalcant@stopped.com:1234"); 105 assertEquals("http://stopped.com:1234", UrlUtilities.getOriginForDisplay(uri, true)); 106 assertEquals("stopped.com:1234", UrlUtilities.getOriginForDisplay(uri, false)); 107 108 uri = URI.create("http://dfalcant:secret@stopped.com:9999"); 109 assertEquals("http://stopped.com:9999", UrlUtilities.getOriginForDisplay(uri, true)); 110 assertEquals("stopped.com:9999", UrlUtilities.getOriginForDisplay(uri, false)); 111 112 uri = URI.create("chrome://settings:443"); 113 assertEquals("chrome://settings:443", UrlUtilities.getOriginForDisplay(uri, true)); 114 assertEquals("settings:443", UrlUtilities.getOriginForDisplay(uri, false)); 115 116 uri = URI.create("about:blank"); 117 assertEquals("about:blank", UrlUtilities.getOriginForDisplay(uri, true)); 118 assertEquals("about:blank", UrlUtilities.getOriginForDisplay(uri, false)); 119 } 120 121 } 122