forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encodeHtmlDataUri to avoid malformed URIs in tests
BUG= Review URL: https://chromiumcodereview.appspot.com/12079009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@179229 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
aruslan@chromium.org
committed
Jan 29, 2013
1 parent
c9c2060
commit 71a4586
Showing
2 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
content/public/android/javatests/src/org/chromium/content/browser/EncodeHtmlDataUriTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright (c) 2012 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package org.chromium.content.browser; | ||
|
||
import android.test.InstrumentationTestCase; | ||
import android.test.suitebuilder.annotation.SmallTest; | ||
|
||
import org.chromium.base.test.util.UrlUtils; | ||
|
||
import java.net.URI; | ||
import java.net.URLDecoder; | ||
|
||
public class EncodeHtmlDataUriTest extends InstrumentationTestCase { | ||
private static final String DATA_URI_PREFIX = "data:text/html;utf-8,"; | ||
|
||
private String getData(String dataUri) { | ||
assertNotNull("Data URI is null", dataUri); | ||
assertTrue("Incorrect HTML Data URI prefix", dataUri.startsWith(DATA_URI_PREFIX)); | ||
return dataUri.substring(DATA_URI_PREFIX.length()); | ||
} | ||
|
||
private String decode(String dataUri) throws java.io.UnsupportedEncodingException { | ||
String data = getData(dataUri); | ||
return URLDecoder.decode(data, "UTF-8"); | ||
} | ||
|
||
@SmallTest | ||
public void testDelimitersEncoding() throws java.io.UnsupportedEncodingException { | ||
String testString = "><#%\"'"; | ||
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | ||
String decodedUri = decode(encodedUri); | ||
assertEquals("Delimiters are not properly encoded", decodedUri, testString); | ||
} | ||
|
||
@SmallTest | ||
public void testUnwiseCharactersEncoding() throws java.io.UnsupportedEncodingException { | ||
String testString = "{}|\\^[]`"; | ||
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | ||
String decodedUri = decode(encodedUri); | ||
assertEquals("Unwise characters are not properly encoded", decodedUri, testString); | ||
} | ||
|
||
@SmallTest | ||
public void testWhitespaceEncoding() throws java.io.UnsupportedEncodingException { | ||
String testString = " \n\t"; | ||
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | ||
String decodedUri = decode(encodedUri); | ||
assertEquals("Whitespace characters are not properly encoded", decodedUri, testString); | ||
} | ||
|
||
@SmallTest | ||
public void testReturnsValidUri() | ||
throws java.net.URISyntaxException, java.io.UnsupportedEncodingException { | ||
String testString = "<html><body onload=\"alert('Hello \\\"world\\\"');\"></body></html>"; | ||
String encodedUri = UrlUtils.encodeHtmlDataUri(testString); | ||
String decodedUri = decode(encodedUri); | ||
// Verify that the encoded URI is valid. | ||
new URI(encodedUri); | ||
// Verify that something sensible was encoded. | ||
assertEquals("Simple HTML is not properly encoded", decodedUri, testString); | ||
} | ||
} |