mirror of
https://github.com/zxing/zxing.git
synced 2024-11-10 04:54:04 -08:00
Tighten up detection of URLs on some garbage strings
git-svn-id: https://zxing.googlecode.com/svn/trunk@1167 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
00c49493c0
commit
a38b0123f6
|
@ -46,13 +46,42 @@ final class URIResultParser extends ResultParser {
|
|||
* need to know when a string is obviously not a URI.
|
||||
*/
|
||||
static boolean isBasicallyValidURI(String uri) {
|
||||
|
||||
if (uri == null || uri.indexOf(' ') >= 0 || uri.indexOf('\n') >= 0) {
|
||||
return false;
|
||||
}
|
||||
int period = uri.indexOf('.');
|
||||
// Look for period in a domain but followed by at least a two-char TLD
|
||||
return period < uri.length() - 2 && (period >= 0 || uri.indexOf(':') >= 0);
|
||||
// Forget strings that don't have a valid-looking protocol
|
||||
int period = uri.indexOf('.');
|
||||
if (period >= uri.length() - 2) {
|
||||
return false;
|
||||
}
|
||||
int colon = uri.indexOf(':');
|
||||
if (period < 0 && colon < 0) {
|
||||
return false;
|
||||
}
|
||||
if (colon >= 0) {
|
||||
if (period < 0 || period > colon) {
|
||||
// colon ends the protocol
|
||||
for (int i = 0; i < colon; i++) {
|
||||
char c = uri.charAt(i);
|
||||
if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// colon starts the port; crudely look for at least two numbers
|
||||
if (colon >= uri.length() - 2) {
|
||||
return false;
|
||||
}
|
||||
for (int i = colon + 1; i < colon + 3; i++) {
|
||||
char c = uri.charAt(i);
|
||||
if (c < '0' || c > '9') {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -73,7 +73,7 @@ public final class AddressBookParsedResultTestCase extends TestCase {
|
|||
String note) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.ADDRESSBOOK, result.getType());
|
||||
assertSame(ParsedResultType.ADDRESSBOOK, result.getType());
|
||||
AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
|
||||
assertEquals(title, addressResult.getTitle());
|
||||
assertTrue(Arrays.equals(names, addressResult.getNames()));
|
||||
|
|
|
@ -43,7 +43,7 @@ public final class CalendarParsedResultTestCase extends TestCase {
|
|||
String attendee) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.CALENDAR, result.getType());
|
||||
assertSame(ParsedResultType.CALENDAR, result.getType());
|
||||
CalendarParsedResult calResult = (CalendarParsedResult) result;
|
||||
assertEquals(title, calResult.getTitle());
|
||||
assertEquals(summary, calResult.getSummary());
|
||||
|
|
|
@ -42,7 +42,7 @@ public final class EmailAddressParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents, String email, String subject, String body) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.EMAIL_ADDRESS, result.getType());
|
||||
assertSame(ParsedResultType.EMAIL_ADDRESS, result.getType());
|
||||
EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
|
||||
assertEquals(email, emailResult.getEmailAddress());
|
||||
assertEquals("mailto:" + email, emailResult.getMailtoURI());
|
||||
|
|
|
@ -35,7 +35,7 @@ public final class GeoParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents, double latitude, double longitude, double altitude) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.GEO, result.getType());
|
||||
assertSame(ParsedResultType.GEO, result.getType());
|
||||
GeoParsedResult geoResult = (GeoParsedResult) result;
|
||||
assertEquals(latitude, geoResult.getLatitude());
|
||||
assertEquals(longitude, geoResult.getLongitude());
|
||||
|
|
|
@ -34,7 +34,7 @@ public final class ISBNParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.EAN_13);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.ISBN, result.getType());
|
||||
assertSame(ParsedResultType.ISBN, result.getType());
|
||||
ISBNParsedResult isbnResult = (ISBNParsedResult) result;
|
||||
assertEquals(contents, isbnResult.getISBN());
|
||||
}
|
||||
|
|
|
@ -295,20 +295,10 @@ public final class ParsedReaderResultTestCase extends TestCase {
|
|||
Result fakeResult = new Result(contents, null, null, format);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertNotNull(result);
|
||||
assertEquals(type, result.getType());
|
||||
assertSame(type, result.getType());
|
||||
|
||||
String displayResult = result.getDisplayResult();
|
||||
assertTrue("Expected:\n" + goldenResult + "\nbut got:\n" + displayResult,
|
||||
goldenResult.equals(displayResult));
|
||||
assertEquals(goldenResult, displayResult);
|
||||
}
|
||||
|
||||
/*
|
||||
private static void doTestResult(byte[] rawBytes, ParsedResultType type) {
|
||||
Result fakeResult = new Result(null, rawBytes, null, null);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertNotNull(result);
|
||||
assertEquals(type, result.getType());
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
|
@ -37,7 +37,7 @@ public final class ProductParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents, String normalized, BarcodeFormat format) {
|
||||
Result fakeResult = new Result(contents, null, null, format);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.PRODUCT, result.getType());
|
||||
assertSame(ParsedResultType.PRODUCT, result.getType());
|
||||
ProductParsedResult productResult = (ProductParsedResult) result;
|
||||
assertEquals(contents, productResult.getProductID());
|
||||
assertEquals(normalized, productResult.getNormalizedProductID());
|
||||
|
|
|
@ -42,7 +42,7 @@ public final class SMSMMSParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents, String number, String subject, String body, String via) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.SMS, result.getType());
|
||||
assertSame(ParsedResultType.SMS, result.getType());
|
||||
SMSParsedResult smsResult = (SMSParsedResult) result;
|
||||
assertEquals(number, smsResult.getNumber());
|
||||
assertEquals(subject, smsResult.getSubject());
|
||||
|
|
|
@ -35,7 +35,7 @@ public final class TelParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents, String number, String title) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.TEL, result.getType());
|
||||
assertSame(ParsedResultType.TEL, result.getType());
|
||||
TelParsedResult telResult = (TelParsedResult) result;
|
||||
assertEquals(number, telResult.getNumber());
|
||||
assertEquals(title, telResult.getTitle());
|
||||
|
|
|
@ -49,6 +49,14 @@ public final class URIParsedResultTestCase extends TestCase {
|
|||
doTest("urlto:foo:bar.com", "http://bar.com", "foo");
|
||||
}
|
||||
|
||||
public void testGarbage() {
|
||||
String text = "Da65cV1g^>%^f0bAbPn1CJB6lV7ZY8hs0Sm:DXU0cd]GyEeWBz8]bUHLB";
|
||||
Result fakeResult = new Result(text, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertSame(ParsedResultType.TEXT, result.getType());
|
||||
assertEquals(text, result.getDisplayResult());
|
||||
}
|
||||
|
||||
public void testIsPossiblyMalicious() {
|
||||
doTestIsPossiblyMalicious("http://google.com", false);
|
||||
doTestIsPossiblyMalicious("http://google.com@evil.com", true);
|
||||
|
@ -61,7 +69,7 @@ public final class URIParsedResultTestCase extends TestCase {
|
|||
private static void doTest(String contents, String uri, String title) {
|
||||
Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
|
||||
ParsedResult result = ResultParser.parseResult(fakeResult);
|
||||
assertEquals(ParsedResultType.URI, result.getType());
|
||||
assertSame(ParsedResultType.URI, result.getType());
|
||||
URIParsedResult uriResult = (URIParsedResult) result;
|
||||
assertEquals(uri, uriResult.getURI());
|
||||
assertEquals(title, uriResult.getTitle());
|
||||
|
|
Loading…
Reference in a new issue