Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2012 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.content.browser;
      6 
      7 import android.test.InstrumentationTestCase;
      8 import android.test.suitebuilder.annotation.SmallTest;
      9 
     10 import org.chromium.base.test.util.UrlUtils;
     11 
     12 import java.net.URI;
     13 import java.net.URLDecoder;
     14 
     15 public class EncodeHtmlDataUriTest extends InstrumentationTestCase {
     16     private static final String DATA_URI_PREFIX = "data:text/html;utf-8,";
     17 
     18     private String getData(String dataUri) {
     19         assertNotNull("Data URI is null", dataUri);
     20         assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(DATA_URI_PREFIX));
     21         return dataUri.substring(DATA_URI_PREFIX.length());
     22     }
     23 
     24     private String decode(String dataUri) throws java.io.UnsupportedEncodingException {
     25         String data = getData(dataUri);
     26         return URLDecoder.decode(data, "UTF-8");
     27     }
     28 
     29     @SmallTest
     30     public void testDelimitersEncoding() throws java.io.UnsupportedEncodingException {
     31         String testString = "><#%\"'";
     32         String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
     33         String decodedUri = decode(encodedUri);
     34         assertEquals("Delimiters are not properly encoded", decodedUri, testString);
     35     }
     36 
     37     @SmallTest
     38     public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodingException {
     39         String testString = "{}|\\^[]`";
     40         String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
     41         String decodedUri = decode(encodedUri);
     42         assertEquals("Unwise characters are not properly encoded", decodedUri, testString);
     43     }
     44 
     45     @SmallTest
     46     public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingException {
     47         String testString = " \n\t";
     48         String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
     49         String decodedUri = decode(encodedUri);
     50         assertEquals("Whitespace characters are not properly encoded", decodedUri, testString);
     51     }
     52 
     53     @SmallTest
     54     public void testReturnsValidUri()
     55             throws java.net.URISyntaxException, java.io.UnsupportedEncodingException {
     56         String testString = "<html><body onload=\"alert('Hello \\\"world\\\"');\"></body></html>";
     57         String encodedUri = UrlUtils.encodeHtmlDataUri(testString);
     58         String decodedUri = decode(encodedUri);
     59         // Verify that the encoded URI is valid.
     60         new URI(encodedUri);
     61         // Verify that something sensible was encoded.
     62         assertEquals("Simple HTML is not properly encoded", decodedUri, testString);
     63     }
     64 }
     65