• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Google Inc.
3  * Licensed to The Android Open Source Project.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.mail.ui;
19 
20 import android.test.AndroidTestCase;
21 import android.test.suitebuilder.annotation.SmallTest;
22 
23 import com.android.mail.ui.HtmlConversationTemplates;
24 
25 import junit.framework.ComparisonFailure;
26 
27 public class ImgSrcReplacementTest extends AndroidTestCase {
28 
replace(final String input, final String expectedOutput)29     private static void replace(final String input, final String expectedOutput) {
30         assertEquals(expectedOutput, HtmlConversationTemplates.replaceAbsoluteImgUrls(input));
31     }
32 
33     @SmallTest
testSimple()34     public void testSimple() {
35         replace(
36             "<img src=\"http://google.com/favicon.ico\">",
37             "<img src='data:' blocked-src=\"http://google.com/favicon.ico\">"
38         );
39     }
40 
41     @SmallTest
testSimpleSingleQuotes()42     public void testSimpleSingleQuotes() {
43         replace(
44             "<img src='http://google.com/favicon.ico'>",
45             "<img src='data:' blocked-src='http://google.com/favicon.ico'>"
46         );
47     }
48 
49     @SmallTest
testSimpleNoQuotes()50     public void testSimpleNoQuotes() {
51         replace(
52             "<img src=http://google.com/favicon.ico>",
53             "<img src='data:' blocked-src=http://google.com/favicon.ico>"
54         );
55     }
56 
57     @SmallTest
testWithElementId()58     public void testWithElementId() {
59         replace(
60             "<img id=\"foo\" src=\"http://google.com/favicon.ico\">",
61             "<img id=\"foo\" src='data:' blocked-src=\"http://google.com/favicon.ico\">"
62         );
63     }
64 
65     @SmallTest
testWithElementIdAndAltAttr()66     public void testWithElementIdAndAltAttr() {
67         replace(
68             "<img id=\"foo\" src=\"http://google.com/favicon.ico\" alt='foo'>",
69             "<img id=\"foo\" src='data:' blocked-src=\"http://google.com/favicon.ico\" alt='foo'>"
70         );
71     }
72 
73     @SmallTest
testHttps()74     public void testHttps() {
75         replace(
76             "<img src=\"https://google.com/favicon.ico\">",
77             "<img src='data:' blocked-src=\"https://google.com/favicon.ico\">"
78         );
79     }
80 
81     @SmallTest
testRelativeUrl()82     public void testRelativeUrl() {
83         // should not modify
84         replace(
85             "<img src=\"?foo1234\">",
86             "<img src=\"?foo1234\">"
87         );
88     }
89 
90     @SmallTest
testNoSrcAttr()91     public void testNoSrcAttr() {
92         // should not modify
93         replace(
94             "<img id='foo' width='500' alt='foo'>",
95             "<img id='foo' width='500' alt='foo'>"
96         );
97     }
98 
99     @SmallTest
testSrcElsewhere()100     public void testSrcElsewhere() {
101         replace(
102             "<img id='foo' width='500' src='http://google.com' alt='foo'> src=httplawl",
103             "<img id='foo' width='500' src='data:' blocked-src='http://google.com' alt='foo'> src=httplawl"
104         );
105     }
106 
107     @SmallTest
testWithInnerWhitespace()108     public void testWithInnerWhitespace() {
109         replace(
110             "<  img    src   =   \"http://google.com/favicon.ico\"    >",
111             "<  img    src='data:' blocked-src   =   \"http://google.com/favicon.ico\"    >"
112         );
113     }
114 
115     @SmallTest
testValueWithTheWordSrc()116     public void testValueWithTheWordSrc() {
117         replace(
118             "<img src=\"http://google.com/foo?src=http%3A%2F%2Fgoogle.com\">",
119             "<img src='data:' blocked-src=\"http://google.com/foo?src=http%3A%2F%2Fgoogle.com\">"
120         );
121     }
122 
123     @SmallTest
testWithOtherAttrsAndValueWithTheWordSrc()124     public void testWithOtherAttrsAndValueWithTheWordSrc() {
125         replace(
126             "<img id='src' src=\"http://google.com/foo?src=http%3A%2F%2Fgoogle.com\">",
127             "<img id='src' src='data:' blocked-src=\"http://google.com/foo?src=http%3A%2F%2Fgoogle.com\">"
128         );
129     }
130 
testValueWithTheWordSrcAndASpace()131     public void testValueWithTheWordSrcAndASpace() {
132         // Doesn't work, but this is not likely to be common.
133         // For a regex to handle this properly, it would have to avoid matching on attribute values,
134         // maybe by counting quotes.
135         try {
136             replace(
137                     "<img src=\"http://google.com/foo? src=http%3A%2F%2Fgoogle.com\">",
138                     "<img src='data:' blocked-src=\"http://google.com/foo? src=http%3A%2F%2Fgoogle.com\">"
139                 );
140         } catch (ComparisonFailure fail) {
141             System.out.println("passing known broken case");
142         }
143     }
144 
145 }
146