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 org.junit.Assert;
22 import org.junit.Test;
25 * Tests {@link URIParsedResult}.
29 public final class URIParsedResultTestCase extends Assert {
32 public void testBookmarkDocomo() {
33 doTest("MEBKM:URL:google.com;;", "http://google.com", null);
34 doTest("MEBKM:URL:http://google.com;;", "http://google.com", null);
35 doTest("MEBKM:URL:google.com;TITLE:Google;", "http://google.com", "Google");
39 public void testURI() {
40 doTest("google.com", "http://google.com", null);
41 doTest("http://google.com", "http://google.com", null);
42 doTest("https://google.com", "https://google.com", null);
43 doTest("google.com:443", "http://google.com:443", null);
44 doTest("https://www.google.com/calendar/hosted/google.com/embed?mode=AGENDA&force_login=true&src=google.com_726f6f6d5f6265707075@resource.calendar.google.com",
45 "https://www.google.com/calendar/hosted/google.com/embed?mode=AGENDA&force_login=true&src=google.com_726f6f6d5f6265707075@resource.calendar.google.com",
50 public void testURLTO() {
51 doTest("urlto::bar.com", "http://bar.com", null);
52 doTest("urlto::http://bar.com", "http://bar.com", null);
53 doTest("urlto:foo:bar.com", "http://bar.com", "foo");
57 public void testGarbage() {
58 String text = "Da65cV1g^>%^f0bAbPn1CJB6lV7ZY8hs0Sm:DXU0cd]GyEeWBz8]bUHLB";
59 Result fakeResult = new Result(text, null, null, BarcodeFormat.QR_CODE);
60 ParsedResult result = ResultParser.parseResult(fakeResult);
61 assertSame(ParsedResultType.TEXT, result.getType());
62 assertEquals(text, result.getDisplayResult());
66 public void testIsPossiblyMalicious() {
67 doTestIsPossiblyMalicious("http://google.com", false);
68 doTestIsPossiblyMalicious("http://google.com@evil.com", true);
69 doTestIsPossiblyMalicious("http://google.com:@evil.com", true);
70 doTestIsPossiblyMalicious("google.com:@evil.com", true);
71 doTestIsPossiblyMalicious("https://google.com:443", false);
72 doTestIsPossiblyMalicious("http://google.com/foo@bar", false);
75 private static void doTest(String contents, String uri, String title) {
76 Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
77 ParsedResult result = ResultParser.parseResult(fakeResult);
78 assertSame(ParsedResultType.URI, result.getType());
79 URIParsedResult uriResult = (URIParsedResult) result;
80 assertEquals(uri, uriResult.getURI());
81 assertEquals(title, uriResult.getTitle());
84 private static void doTestIsPossiblyMalicious(String uri, boolean expected) {
85 URIParsedResult result = new URIParsedResult(uri, null);
86 assertEquals(expected, result.isPossiblyMaliciousURI());