Oops realized DM decoder also benefits from that last possible fix
[zxing.git] / core / test / src / com / google / zxing / client / result / URIParsedResultTestCase.java
1 /*
2  * Copyright 2007 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.result;
18
19 import junit.framework.TestCase;
20 import com.google.zxing.Result;
21 import com.google.zxing.BarcodeFormat;
22
23 /**
24  * Tests {@link URIParsedResult}.
25  *
26  * @author Sean Owen
27  */
28 public final class URIParsedResultTestCase extends TestCase {
29
30   public void testBookmarkDocomo() {
31     doTest("MEBKM:URL:google.com;;", "http://google.com", null);
32     doTest("MEBKM:URL:http://google.com;;", "http://google.com", null);    
33     doTest("MEBKM:URL:google.com;TITLE:Google;", "http://google.com", "Google");
34   }
35
36   public void testURI() {
37     doTest("google.com", "http://google.com", null);
38     doTest("http://google.com", "http://google.com", null);
39     doTest("https://google.com", "https://google.com", null);
40     doTest("google.com:443", "http://google.com:443", null);
41     doTest("https://www.google.com/calendar/hosted/google.com/embed?mode=AGENDA&force_login=true&src=google.com_726f6f6d5f6265707075@resource.calendar.google.com",
42            "https://www.google.com/calendar/hosted/google.com/embed?mode=AGENDA&force_login=true&src=google.com_726f6f6d5f6265707075@resource.calendar.google.com",
43            null);
44   }
45
46   public void testURLTO() {
47     doTest("urlto::bar.com", "http://bar.com", null);
48     doTest("urlto::http://bar.com", "http://bar.com", null);    
49     doTest("urlto:foo:bar.com", "http://bar.com", "foo");
50   }
51
52   public void testIsPossiblyMalicious() {
53     doTestIsPossiblyMalicious("http://google.com", false);
54     doTestIsPossiblyMalicious("http://google.com@evil.com", true);
55     doTestIsPossiblyMalicious("http://google.com:@evil.com", true);
56     doTestIsPossiblyMalicious("google.com:@evil.com", true);
57     doTestIsPossiblyMalicious("https://google.com:443", false);
58     doTestIsPossiblyMalicious("http://google.com/foo@bar", false);    
59   }
60
61   private static void doTest(String contents, String uri, String title) {
62     Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
63     ParsedResult result = ResultParser.parseResult(fakeResult);
64     assertEquals(ParsedResultType.URI, result.getType());
65     URIParsedResult uriResult = (URIParsedResult) result;
66     assertEquals(uri, uriResult.getURI());
67     assertEquals(title, uriResult.getTitle());
68   }
69
70   private static void doTestIsPossiblyMalicious(String uri, boolean expected) {
71     URIParsedResult result = new URIParsedResult(uri, null);
72     assertEquals(expected, result.isPossiblyMaliciousURI());
73   }
74
75 }