Commit f6d17105 authored by Leon Han's avatar Leon Han Committed by Commit Bot

[webnfc] Fix improper use of (expected = XXXException) in NFCTest.java

@Test(expected = InvalidNdefMessageException.class) indicates the test
will and must throw an InvalidNdefMessageException, once the first
exception gets thrown, the following code won't run and the test just
goes to the end.

So if a test would possibly throw more than one exception, it cannot use
this mechanism. Instead we use a pattern like
  XXX xxx = null;
  try {
    xxx = getXXX();
  } catch (XXXException e) {
  }
  assertNull(xxx);
to confirm something went wrong inside getXXX();

Bug: 520391
Change-Id: Ibb681cbef1e0717ed695d7d7f2b9931739e6714c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2071715
Auto-Submit: Leon Han <leon.han@intel.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Leon Han <leon.han@intel.com>
Cr-Commit-Position: refs/heads/master@{#744166}
parent c8a31b70
...@@ -781,58 +781,75 @@ public class NFCTest { ...@@ -781,58 +781,75 @@ public class NFCTest {
/** /**
* Test external record conversion with invalid custom type. * Test external record conversion with invalid custom type.
*/ */
@Test(expected = InvalidNdefMessageException.class) @Test
@Feature({"NFCTest"}) @Feature({"NFCTest"})
public void testInvalidExternalRecordType() throws InvalidNdefMessageException { public void testInvalidExternalRecordType() {
NdefRecord extMojoNdefRecord = new NdefRecord(); NdefRecord extMojoNdefRecord = new NdefRecord();
extMojoNdefRecord.category = NdefRecordTypeCategory.EXTERNAL; extMojoNdefRecord.category = NdefRecordTypeCategory.EXTERNAL;
extMojoNdefRecord.id = DUMMY_RECORD_ID;
extMojoNdefRecord.data = ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT); extMojoNdefRecord.data = ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT);
{ {
// Must have a ':'. // Must have a ':'.
extMojoNdefRecord.recordType = "abc.com"; extMojoNdefRecord.recordType = "abc.com";
NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord); NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord);
android.nfc.NdefMessage extNdefMessage = android.nfc.NdefMessage extNdefMessage = null;
NdefMessageUtils.toNdefMessage(extMojoNdefMessage); try {
extNdefMessage = NdefMessageUtils.toNdefMessage(extMojoNdefMessage);
} catch (InvalidNdefMessageException e) {
}
assertNull(extNdefMessage); assertNull(extNdefMessage);
} }
{ {
// '-' is allowed in the domain part. // '~' is allowed in the domain part.
extMojoNdefRecord.recordType = "abc-123.com:xyz"; extMojoNdefRecord.recordType = "abc~123.com:xyz";
NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord); NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord);
android.nfc.NdefMessage extNdefMessage = android.nfc.NdefMessage extNdefMessage = null;
NdefMessageUtils.toNdefMessage(extMojoNdefMessage); try {
extNdefMessage = NdefMessageUtils.toNdefMessage(extMojoNdefMessage);
} catch (InvalidNdefMessageException e) {
}
assertNotNull(extNdefMessage); assertNotNull(extNdefMessage);
assertEquals(1, extNdefMessage.getRecords().length); assertEquals(1, extNdefMessage.getRecords().length);
assertEquals(android.nfc.NdefRecord.TNF_EXTERNAL_TYPE, assertEquals(android.nfc.NdefRecord.TNF_EXTERNAL_TYPE,
extNdefMessage.getRecords()[0].getTnf()); extNdefMessage.getRecords()[0].getTnf());
assertEquals("abc-1.com:xyz", new String(extNdefMessage.getRecords()[0].getType())); assertEquals("abc~123.com:xyz", new String(extNdefMessage.getRecords()[0].getType()));
assertEquals(DUMMY_RECORD_ID, new String(extNdefMessage.getRecords()[0].getId())); assertEquals(DUMMY_RECORD_ID, new String(extNdefMessage.getRecords()[0].getId()));
assertEquals(TEST_TEXT, new String(extNdefMessage.getRecords()[0].getPayload())); assertEquals(TEST_TEXT, new String(extNdefMessage.getRecords()[0].getPayload()));
} }
{ {
// '-' is not allowed in the type part. // '~' is not allowed in the type part.
extMojoNdefRecord.recordType = "abc.com:xyz-123"; extMojoNdefRecord.recordType = "abc.com:xyz~123";
NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord); NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord);
android.nfc.NdefMessage extNdefMessage = android.nfc.NdefMessage extNdefMessage = null;
NdefMessageUtils.toNdefMessage(extMojoNdefMessage); try {
extNdefMessage = NdefMessageUtils.toNdefMessage(extMojoNdefMessage);
} catch (InvalidNdefMessageException e) {
}
assertNull(extNdefMessage); assertNull(extNdefMessage);
} }
{ {
// As the 2 cases above have proved that '-' is allowed in the domain part but not // As the 2 cases above have proved that '~' is allowed in the domain part but not
// allowed in the type part, from this case we can say that the first occurrence of // allowed in the type part, from this case we can say that the first occurrence of
// ':' is used to separate the domain part and the type part, i.e. "xyz-123:uvw" is // ':' is used to separate the domain part and the type part, i.e. "xyz~123:uvw" is
// separated as the type part and is treated as invalid due to the existence of '-'. // separated as the type part and is treated as invalid due to the existence of '~'.
extMojoNdefRecord.recordType = "abc.com:xyz-123:uvw"; extMojoNdefRecord.recordType = "abc.com:xyz~123:uvw";
NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord); NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord);
android.nfc.NdefMessage extNdefMessage = android.nfc.NdefMessage extNdefMessage = null;
NdefMessageUtils.toNdefMessage(extMojoNdefMessage); try {
extNdefMessage = NdefMessageUtils.toNdefMessage(extMojoNdefMessage);
} catch (InvalidNdefMessageException e) {
}
assertNull(extNdefMessage); assertNull(extNdefMessage);
} }
{ {
// |recordType| is a string mixed with ASCII/non-ASCII, FAIL. // |recordType| is a string mixed with ASCII/non-ASCII, FAIL.
extMojoNdefRecord.recordType = "example.com:hellö"; extMojoNdefRecord.recordType = "example.com:hellö";
android.nfc.NdefMessage extNdefMessage_nonASCII = android.nfc.NdefMessage extNdefMessage_nonASCII = null;
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(extMojoNdefRecord)); try {
extNdefMessage_nonASCII =
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(extMojoNdefRecord));
} catch (InvalidNdefMessageException e) {
}
assertNull(extNdefMessage_nonASCII); assertNull(extNdefMessage_nonASCII);
char[] chars = new char[251]; char[] chars = new char[251];
...@@ -841,22 +858,33 @@ public class NFCTest { ...@@ -841,22 +858,33 @@ public class NFCTest {
// |recordType|'s length is 255, OK. // |recordType|'s length is 255, OK.
extMojoNdefRecord.recordType = domain + ":xyz"; extMojoNdefRecord.recordType = domain + ":xyz";
android.nfc.NdefMessage extNdefMessage_255 = android.nfc.NdefMessage extNdefMessage_255 = null;
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(extMojoNdefRecord)); try {
extNdefMessage_255 =
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(extMojoNdefRecord));
} catch (InvalidNdefMessageException e) {
}
assertNotNull(extNdefMessage_255); assertNotNull(extNdefMessage_255);
// Exceeding the maximum length 255, FAIL. // Exceeding the maximum length 255, FAIL.
extMojoNdefRecord.recordType = domain + ":xyze"; extMojoNdefRecord.recordType = domain + ":xyze";
android.nfc.NdefMessage extNdefMessage_256 = android.nfc.NdefMessage extNdefMessage_256 = null;
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(extMojoNdefRecord)); try {
extNdefMessage_256 =
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(extMojoNdefRecord));
} catch (InvalidNdefMessageException e) {
}
assertNull(extNdefMessage_256); assertNull(extNdefMessage_256);
} }
{ {
// '/' is not allowed in the type part. // '/' is not allowed in the type part.
extMojoNdefRecord.recordType = "abc.com:xyz/"; extMojoNdefRecord.recordType = "abc.com:xyz/";
NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord); NdefMessage extMojoNdefMessage = createMojoNdefMessage(extMojoNdefRecord);
android.nfc.NdefMessage extNdefMessage = android.nfc.NdefMessage extNdefMessage = null;
NdefMessageUtils.toNdefMessage(extMojoNdefMessage); try {
extNdefMessage = NdefMessageUtils.toNdefMessage(extMojoNdefMessage);
} catch (InvalidNdefMessageException e) {
}
assertNull(extNdefMessage); assertNull(extNdefMessage);
} }
} }
...@@ -864,9 +892,9 @@ public class NFCTest { ...@@ -864,9 +892,9 @@ public class NFCTest {
/** /**
* Test local type record conversion with invalid local type. * Test local type record conversion with invalid local type.
*/ */
@Test(expected = InvalidNdefMessageException.class) @Test
@Feature({"NFCTest"}) @Feature({"NFCTest"})
public void testInvalidLocalRecordType() throws InvalidNdefMessageException { public void testInvalidLocalRecordType() {
NdefRecord localMojoNdefRecord = new NdefRecord(); NdefRecord localMojoNdefRecord = new NdefRecord();
localMojoNdefRecord.category = NdefRecordTypeCategory.LOCAL; localMojoNdefRecord.category = NdefRecordTypeCategory.LOCAL;
localMojoNdefRecord.data = ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT); localMojoNdefRecord.data = ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT);
...@@ -875,15 +903,22 @@ public class NFCTest { ...@@ -875,15 +903,22 @@ public class NFCTest {
localMojoNdefRecord.recordType = "dummyLocalTypeNotStartingwith:"; localMojoNdefRecord.recordType = "dummyLocalTypeNotStartingwith:";
localMojoNdefRecord.data = ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT); localMojoNdefRecord.data = ApiCompatibilityUtils.getBytesUtf8(TEST_TEXT);
NdefMessage localMojoNdefMessage = createMojoNdefMessage(localMojoNdefRecord); NdefMessage localMojoNdefMessage = createMojoNdefMessage(localMojoNdefRecord);
android.nfc.NdefMessage localNdefMessage = android.nfc.NdefMessage localNdefMessage = null;
NdefMessageUtils.toNdefMessage(localMojoNdefMessage); try {
localNdefMessage = NdefMessageUtils.toNdefMessage(localMojoNdefMessage);
} catch (InvalidNdefMessageException e) {
}
assertNull(localNdefMessage); assertNull(localNdefMessage);
} }
{ {
// |recordType| is a string mixed with ASCII/non-ASCII, FAIL. // |recordType| is a string mixed with ASCII/non-ASCII, FAIL.
localMojoNdefRecord.recordType = ":hellö"; localMojoNdefRecord.recordType = ":hellö";
android.nfc.NdefMessage localNdefMessage_nonASCII = android.nfc.NdefMessage localNdefMessage_nonASCII = null;
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(localMojoNdefRecord)); try {
localNdefMessage_nonASCII =
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(localMojoNdefRecord));
} catch (InvalidNdefMessageException e) {
}
assertNull(localNdefMessage_nonASCII); assertNull(localNdefMessage_nonASCII);
char[] chars = new char[255]; char[] chars = new char[255];
...@@ -892,14 +927,22 @@ public class NFCTest { ...@@ -892,14 +927,22 @@ public class NFCTest {
// The length of the real local type is 255, OK. // The length of the real local type is 255, OK.
localMojoNdefRecord.recordType = ":" + chars_255; localMojoNdefRecord.recordType = ":" + chars_255;
android.nfc.NdefMessage localNdefMessage_255 = android.nfc.NdefMessage localNdefMessage_255 = null;
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(localMojoNdefRecord)); try {
localNdefMessage_255 =
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(localMojoNdefRecord));
} catch (InvalidNdefMessageException e) {
}
assertNotNull(localNdefMessage_255); assertNotNull(localNdefMessage_255);
// Exceeding the maximum length 255, FAIL. // Exceeding the maximum length 255, FAIL.
localMojoNdefRecord.recordType = ":a" + chars_255; localMojoNdefRecord.recordType = ":a" + chars_255;
android.nfc.NdefMessage localNdefMessage_256 = android.nfc.NdefMessage localNdefMessage_256 = null;
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(localMojoNdefRecord)); try {
localNdefMessage_256 =
NdefMessageUtils.toNdefMessage(createMojoNdefMessage(localMojoNdefRecord));
} catch (InvalidNdefMessageException e) {
}
assertNull(localNdefMessage_256); assertNull(localNdefMessage_256);
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment