OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
8 import android.test.InstrumentationTestCase; | |
9 | 8 |
| 9 import org.junit.Assert; |
| 10 import org.junit.Test; |
| 11 import org.junit.runner.RunWith; |
| 12 |
| 13 import org.chromium.base.test.BaseJUnit4ClassRunner; |
10 import org.chromium.base.test.util.UrlUtils; | 14 import org.chromium.base.test.util.UrlUtils; |
11 | 15 |
12 import java.net.URI; | 16 import java.net.URI; |
13 import java.net.URLDecoder; | 17 import java.net.URLDecoder; |
14 | 18 |
15 public class EncodeHtmlDataUriTest extends InstrumentationTestCase { | 19 @RunWith(BaseJUnit4ClassRunner.class) |
| 20 public class EncodeHtmlDataUriTest { |
16 private static final String DATA_URI_PREFIX = "data:text/html;utf-8,"; | 21 private static final String DATA_URI_PREFIX = "data:text/html;utf-8,"; |
17 | 22 |
18 private String getData(String dataUri) { | 23 private String getData(String dataUri) { |
19 assertNotNull("Data URI is null", dataUri); | 24 Assert.assertNotNull("Data URI is null", dataUri); |
20 assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(DATA_URI
_PREFIX)); | 25 Assert.assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(D
ATA_URI_PREFIX)); |
21 return dataUri.substring(DATA_URI_PREFIX.length()); | 26 return dataUri.substring(DATA_URI_PREFIX.length()); |
22 } | 27 } |
23 | 28 |
24 private String decode(String dataUri) throws java.io.UnsupportedEncodingExce
ption { | 29 private String decode(String dataUri) throws java.io.UnsupportedEncodingExce
ption { |
25 String data = getData(dataUri); | 30 String data = getData(dataUri); |
26 return URLDecoder.decode(data, "UTF-8"); | 31 return URLDecoder.decode(data, "UTF-8"); |
27 } | 32 } |
28 | 33 |
| 34 @Test |
29 @SmallTest | 35 @SmallTest |
30 public void testDelimitersEncoding() throws java.io.UnsupportedEncodingExcep
tion { | 36 public void testDelimitersEncoding() throws java.io.UnsupportedEncodingExcep
tion { |
31 String testString = "><#%\"'"; | 37 String testString = "><#%\"'"; |
32 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | 38 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); |
33 String decodedUri = decode(encodedUri); | 39 String decodedUri = decode(encodedUri); |
34 assertEquals("Delimiters are not properly encoded", decodedUri, testStri
ng); | 40 Assert.assertEquals("Delimiters are not properly encoded", decodedUri, t
estString); |
35 } | 41 } |
36 | 42 |
| 43 @Test |
37 @SmallTest | 44 @SmallTest |
38 public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodin
gException { | 45 public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodin
gException { |
39 String testString = "{}|\\^[]`"; | 46 String testString = "{}|\\^[]`"; |
40 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | 47 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); |
41 String decodedUri = decode(encodedUri); | 48 String decodedUri = decode(encodedUri); |
42 assertEquals("Unwise characters are not properly encoded", decodedUri, t
estString); | 49 Assert.assertEquals("Unwise characters are not properly encoded", decode
dUri, testString); |
43 } | 50 } |
44 | 51 |
| 52 @Test |
45 @SmallTest | 53 @SmallTest |
46 public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingExcep
tion { | 54 public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingExcep
tion { |
47 String testString = " \n\t"; | 55 String testString = " \n\t"; |
48 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | 56 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); |
49 String decodedUri = decode(encodedUri); | 57 String decodedUri = decode(encodedUri); |
50 assertEquals("Whitespace characters are not properly encoded", decodedUr
i, testString); | 58 Assert.assertEquals( |
| 59 "Whitespace characters are not properly encoded", decodedUri, te
stString); |
51 } | 60 } |
52 | 61 |
| 62 @Test |
53 @SmallTest | 63 @SmallTest |
54 public void testReturnsValidUri() | 64 public void testReturnsValidUri() |
55 throws java.net.URISyntaxException, java.io.UnsupportedEncodingExcep
tion { | 65 throws java.net.URISyntaxException, java.io.UnsupportedEncodingExcep
tion { |
56 String testString = "<html><body onload=\"alert('Hello \\\"world\\\"');\
"></body></html>"; | 66 String testString = "<html><body onload=\"alert('Hello \\\"world\\\"');\
"></body></html>"; |
57 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | 67 String encodedUri = UrlUtils.encodeHtmlDataUri(testString); |
58 String decodedUri = decode(encodedUri); | 68 String decodedUri = decode(encodedUri); |
59 // Verify that the encoded URI is valid. | 69 // Verify that the encoded URI is valid. |
60 new URI(encodedUri); | 70 new URI(encodedUri); |
61 // Verify that something sensible was encoded. | 71 // Verify that something sensible was encoded. |
62 assertEquals("Simple HTML is not properly encoded", decodedUri, testStri
ng); | 72 Assert.assertEquals("Simple HTML is not properly encoded", decodedUri, t
estString); |
63 } | 73 } |
64 } | 74 } |
OLD | NEW |