OLD | NEW |
| (Empty) |
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 | |
14 public class UrlUtilitiesTest extends InstrumentationTestCase { | |
15 @SmallTest | |
16 public void testIsAcceptedScheme() { | |
17 assertTrue(UrlUtilities.isAcceptedScheme("about:awesome")); | |
18 assertTrue(UrlUtilities.isAcceptedScheme("data:data")); | |
19 assertTrue(UrlUtilities.isAcceptedScheme( | |
20 "https://user:pass@:awesome.com:9000/bad-scheme:#fake:")); | |
21 assertTrue(UrlUtilities.isAcceptedScheme("http://awesome.example.com/"))
; | |
22 assertTrue(UrlUtilities.isAcceptedScheme("file://awesome.example.com/"))
; | |
23 assertTrue(UrlUtilities.isAcceptedScheme("inline:skates.co.uk")); | |
24 assertTrue(UrlUtilities.isAcceptedScheme("javascript:alert(1)")); | |
25 | |
26 assertFalse(UrlUtilities.isAcceptedScheme("super:awesome")); | |
27 assertFalse(UrlUtilities.isAcceptedScheme( | |
28 "ftp://https:password@example.com/")); | |
29 assertFalse(UrlUtilities.isAcceptedScheme( | |
30 "ftp://https:password@example.com/?http:#http:")); | |
31 assertFalse(UrlUtilities.isAcceptedScheme( | |
32 "google-search://https:password@example.com/?http:#http:")); | |
33 assertFalse(UrlUtilities.isAcceptedScheme("chrome://http://version")); | |
34 assertFalse(UrlUtilities.isAcceptedScheme("")); | |
35 assertFalse(UrlUtilities.isAcceptedScheme(" http://awesome.example.com/
")); | |
36 assertFalse(UrlUtilities.isAcceptedScheme("ht\ntp://awesome.example.com/
")); | |
37 } | |
38 | |
39 @SmallTest | |
40 public void testIsDownloadableScheme() { | |
41 assertTrue(UrlUtilities.isDownloadableScheme("data:data")); | |
42 assertTrue(UrlUtilities.isDownloadableScheme( | |
43 "https://user:pass@:awesome.com:9000/bad-scheme:#fake:")); | |
44 assertTrue(UrlUtilities.isDownloadableScheme("http://awesome.example.com
/")); | |
45 assertTrue(UrlUtilities.isDownloadableScheme("filesystem://awesome.examp
le.com/")); | |
46 | |
47 assertFalse(UrlUtilities.isDownloadableScheme("inline:skates.co.uk")); | |
48 assertFalse(UrlUtilities.isDownloadableScheme("javascript:alert(1)")); | |
49 assertFalse(UrlUtilities.isDownloadableScheme("file://awesome.example.co
m/")); | |
50 assertFalse(UrlUtilities.isDownloadableScheme("about:awesome")); | |
51 assertFalse(UrlUtilities.isDownloadableScheme("super:awesome")); | |
52 assertFalse(UrlUtilities.isDownloadableScheme("ftp://https:password@exam
ple.com/")); | |
53 assertFalse(UrlUtilities.isDownloadableScheme( | |
54 "ftp://https:password@example.com/?http:#http:")); | |
55 assertFalse(UrlUtilities.isDownloadableScheme( | |
56 "google-search://https:password@example.com/?http:#http:")); | |
57 assertFalse(UrlUtilities.isDownloadableScheme("chrome://http://version")
); | |
58 assertFalse(UrlUtilities.isDownloadableScheme("")); | |
59 assertFalse(UrlUtilities.isDownloadableScheme(" http://awesome.example.
com/")); | |
60 assertFalse(UrlUtilities.isDownloadableScheme("ht\ntp://awesome.example.
com/")); | |
61 } | |
62 | |
63 @SmallTest | |
64 public void testIsValidForIntentFallbackUrl() { | |
65 assertTrue(UrlUtilities.isValidForIntentFallbackNavigation( | |
66 "https://user:pass@:awesome.com:9000/bad-scheme:#fake:")); | |
67 assertTrue(UrlUtilities.isValidForIntentFallbackNavigation("http://aweso
me.example.com/")); | |
68 assertFalse(UrlUtilities.isValidForIntentFallbackNavigation("inline:skat
es.co.uk")); | |
69 assertFalse(UrlUtilities.isValidForIntentFallbackNavigation("javascript:
alert(1)")); | |
70 assertFalse(UrlUtilities.isValidForIntentFallbackNavigation("")); | |
71 } | |
72 | |
73 @SmallTest | |
74 @Feature({"Webapps"}) | |
75 public void testGetOriginForDisplay() { | |
76 URI uri; | |
77 | |
78 uri = URI.create("http://chopped.com/is/awesome"); | |
79 assertEquals("http://chopped.com", UrlUtilities.getOriginForDisplay(uri,
true)); | |
80 assertEquals("chopped.com", UrlUtilities.getOriginForDisplay(uri, false)
); | |
81 | |
82 uri = URI.create("http://lopped.com"); | |
83 assertEquals("http://lopped.com", UrlUtilities.getOriginForDisplay(uri,
true)); | |
84 assertEquals("lopped.com", UrlUtilities.getOriginForDisplay(uri, false))
; | |
85 | |
86 uri = URI.create("http://dropped.com?things"); | |
87 assertEquals("http://dropped.com", UrlUtilities.getOriginForDisplay(uri,
true)); | |
88 assertEquals("dropped.com", UrlUtilities.getOriginForDisplay(uri, false)
); | |
89 | |
90 uri = URI.create("http://dfalcant@stopped.com:1234"); | |
91 assertEquals("http://stopped.com:1234", UrlUtilities.getOriginForDisplay
(uri, true)); | |
92 assertEquals("stopped.com:1234", UrlUtilities.getOriginForDisplay(uri, f
alse)); | |
93 | |
94 uri = URI.create("http://dfalcant:secret@stopped.com:9999"); | |
95 assertEquals("http://stopped.com:9999", UrlUtilities.getOriginForDisplay
(uri, true)); | |
96 assertEquals("stopped.com:9999", UrlUtilities.getOriginForDisplay(uri, f
alse)); | |
97 | |
98 uri = URI.create("chrome://settings:443"); | |
99 assertEquals("chrome://settings:443", UrlUtilities.getOriginForDisplay(u
ri, true)); | |
100 assertEquals("settings:443", UrlUtilities.getOriginForDisplay(uri, false
)); | |
101 | |
102 uri = URI.create("about:blank"); | |
103 assertEquals("about:blank", UrlUtilities.getOriginForDisplay(uri, true))
; | |
104 assertEquals("about:blank", UrlUtilities.getOriginForDisplay(uri, false)
); | |
105 } | |
106 | |
107 @SmallTest | |
108 public void testValidateIntentUrl() { | |
109 // Valid action, hostname, and (empty) path. | |
110 assertTrue(UrlUtilities.validateIntentUrl( | |
111 "intent://10010#Intent;scheme=tel;action=com.google.android.apps
." | |
112 + "authenticator.AUTHENTICATE;end")); | |
113 // Valid package, scheme, hostname, and path. | |
114 assertTrue(UrlUtilities.validateIntentUrl( | |
115 "intent://scan/#Intent;package=com.google.zxing.client.android;" | |
116 + "scheme=zxing;end;")); | |
117 // Valid package, scheme, component, hostname, and path. | |
118 assertTrue(UrlUtilities.validateIntentUrl( | |
119 "intent://wump-hey.example.com/#Intent;package=com.example.wump;
" | |
120 + "scheme=yow;component=com.example.PUMPKIN;end;")); | |
121 // Valid package, scheme, action, hostname, and path. | |
122 assertTrue(UrlUtilities.validateIntentUrl( | |
123 "intent://wump-hey.example.com/#Intent;package=com.example.wump;
" | |
124 + "scheme=eeek;action=frighten_children;end;")); | |
125 // Valid package, component, String extra, hostname, and path. | |
126 assertTrue(UrlUtilities.validateIntentUrl( | |
127 "intent://testing/#Intent;package=cybergoat.noodle.crumpet;" | |
128 + "component=wump.noodle/Crumpet;S.goat=leg;end")); | |
129 | |
130 // Valid package, component, int extra (with URL-encoded key), String | |
131 // extra, hostname, and path. | |
132 assertTrue(UrlUtilities.validateIntentUrl( | |
133 "intent://testing/#Intent;package=cybergoat.noodle.crumpet;" | |
134 + "component=wump.noodle/Crumpet;i.pumpkinCount%3D=42;" | |
135 + "S.goat=leg;end")); | |
136 | |
137 // Android's Intent.toUri does not generate URLs like this, but | |
138 // Google Authenticator does, and we must handle them. | |
139 assertTrue(UrlUtilities.validateIntentUrl( | |
140 "intent:#Intent;action=com.google.android.apps.chrome." | |
141 + "TEST_AUTHENTICATOR;category=android.intent.category." | |
142 + "BROWSABLE;S.inputData=cancelled;end")); | |
143 | |
144 // Junk after end. | |
145 assertFalse(UrlUtilities.validateIntentUrl( | |
146 "intent://10010#Intent;scheme=tel;action=com.google.android.apps
." | |
147 + "authenticator.AUTHENTICATE;end','*');" | |
148 + "alert(document.cookie);//")); | |
149 // component appears twice. | |
150 assertFalse(UrlUtilities.validateIntentUrl( | |
151 "intent://wump-hey.example.com/#Intent;package=com.example.wump;
" | |
152 + "scheme=yow;component=com.example.PUMPKIN;" | |
153 + "component=com.example.AVOCADO;end;")); | |
154 // scheme contains illegal character. | |
155 assertFalse(UrlUtilities.validateIntentUrl( | |
156 "intent://wump-hey.example.com/#Intent;package=com.example.wump;
" | |
157 + "scheme=hello+goodbye;component=com.example.PUMPKIN;end;")); | |
158 // category contains illegal character. | |
159 assertFalse(UrlUtilities.validateIntentUrl( | |
160 "intent://wump-hey.example.com/#Intent;package=com.example.wump;
" | |
161 + "category=42%_by_volume;end")); | |
162 // Incorrectly URL-encoded. | |
163 assertFalse(UrlUtilities.validateIntentUrl( | |
164 "intent://testing/#Intent;package=cybergoat.noodle.crumpet;" | |
165 + "component=wump.noodle/Crumpet;i.pumpkinCount%%3D=42;" | |
166 + "S.goat=⋚end")); | |
167 } | |
168 | |
169 } | |
OLD | NEW |