2 * Copyright 2007 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.result;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import junit.framework.TestCase;
24 * Tests {@link URIParsedResult}.
28 public final class URIParsedResultTestCase extends TestCase {
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");
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",
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");
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());
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);
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());
78 private static void doTestIsPossiblyMalicious(String uri, boolean expected) {
79 URIParsedResult result = new URIParsedResult(uri, null);
80 assertEquals(expected, result.isPossiblyMaliciousURI());