055f86c3511fd710eaabac624ac5c97e4d7d60fb
[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 com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import junit.framework.TestCase;
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 testGarbage() {
53     String text = "Da65cV1g^>%^f0bAbPn1CJB6lV7ZY8hs0Sm:DXU0cd]GyEeWBz8]bUHLB";
54     Result fakeResult = new Result(text, null, null, BarcodeFormat.QR_CODE);
55     ParsedResult result = ResultParser.parseResult(fakeResult);
56     assertSame(ParsedResultType.TEXT, result.getType());
57     assertEquals(text, result.getDisplayResult());
58   }
59
60   public void testIsPossiblyMalicious() {
61     doTestIsPossiblyMalicious("http://google.com", false);
62     doTestIsPossiblyMalicious("http://google.com@evil.com", true);
63     doTestIsPossiblyMalicious("http://google.com:@evil.com", true);
64     doTestIsPossiblyMalicious("google.com:@evil.com", true);
65     doTestIsPossiblyMalicious("https://google.com:443", false);
66     doTestIsPossiblyMalicious("http://google.com/foo@bar", false);    
67   }
68
69   private static void doTest(String contents, String uri, String title) {
70     Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
71     ParsedResult result = ResultParser.parseResult(fakeResult);
72     assertSame(ParsedResultType.URI, result.getType());
73     URIParsedResult uriResult = (URIParsedResult) result;
74     assertEquals(uri, uriResult.getURI());
75     assertEquals(title, uriResult.getTitle());
76   }
77
78   private static void doTestIsPossiblyMalicious(String uri, boolean expected) {
79     URIParsedResult result = new URIParsedResult(uri, null);
80     assertEquals(expected, result.isPossiblyMaliciousURI());
81   }
82
83 }