// lets test some error cases
// first, passing null and 0
try
{
EthernetAddress ethernet_address = new EthernetAddress(0L);
ethernet_address.toByteArray((byte[])null, 0);
// if we reached here we failed because we didn't get an exception
fail("Expected exception not caught");
}
catch (NullPointerException ex)
{
// this is the success case so do nothing
}
catch (Exception ex)
{
fail("Caught unexpected exception: " + ex);
}
// now an array that is too small
try
{
EthernetAddress ethernet_address = new EthernetAddress(0L);
byte[] ethernet_address_byte_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH - 1];
ethernet_address.toByteArray(ethernet_address_byte_array, 0);
// if we reached here we failed because we didn't get an exception
fail("Expected exception not caught");
} catch (IllegalArgumentException ex) {
// this is the success case so do nothing
} catch (Exception ex) {
fail("Caught unexpected exception: " + ex);
}
// now an index that is negative
try {
EthernetAddress ethernet_address = new EthernetAddress(0L);
byte[] ethernet_address_byte_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH];
ethernet_address.toByteArray(ethernet_address_byte_array, -1);
// if we reached here we failed because we didn't get an exception
fail("Expected exception not caught");
} catch (IllegalArgumentException ex) {
// this is the success case so do nothing
} catch (Exception ex) {
fail("Caught unexpected exception: " + ex);
}
// now an index that is too big
try
{
EthernetAddress ethernet_address = new EthernetAddress(0L);
byte[] ethernet_address_byte_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH];
ethernet_address.toByteArray(
ethernet_address_byte_array, ETHERNET_ADDRESS_ARRAY_LENGTH);
// if we reached here we failed because we didn't get an exception
fail("Expected exception not caught");
} catch (IllegalArgumentException ex) {
// this is the success case so do nothing
} catch (Exception ex) {
fail("Caught unexpected exception: " + ex);
}
// now an index that is in the array,
// but without enough bytes to read ETHERNET_ADDRESS_ARRAY_LENGTH
try {
EthernetAddress ethernet_address = new EthernetAddress(0L);
byte[] ethernet_address_byte_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH];
ethernet_address.toByteArray(ethernet_address_byte_array, 1);
// if we reached here we failed because we didn't get an exception
fail("Expected exception not caught");
} catch (IllegalArgumentException ex) {
// this is the success case so do nothing
} catch (Exception ex) {
fail("Caught unexpected exception: " + ex);
}
// we'll test making a couple EthernetAddresss and then check
// that toByteArray
// returns the same value in byte form as used to create it
// here we'll test the null EthernetAddress at offset 0
EthernetAddress ethernet_address = new EthernetAddress(0L);
byte[] test_array = new byte[ETHERNET_ADDRESS_ARRAY_LENGTH];
Arrays.fill(test_array, (byte)'x');
ethernet_address.toByteArray(test_array, 0);
assertEthernetAddressArraysAreEqual(
NULL_ETHERNET_ADDRESS_BYTE_ARRAY, 0, test_array, 0);
// now test a non-null EthernetAddress
ethernet_address =
new EthernetAddress(MIXED_CASE_VALID_ETHERNET_ADDRESS_STRING);
Arrays.fill(test_array, (byte)'x');
ethernet_address.toByteArray(test_array);
assertEthernetAddressArraysAreEqual(
VALID_ETHERNET_ADDRESS_BYTE_ARRAY, 0, test_array, 0);
// now test a null EthernetAddress case with extra data in the array
ethernet_address = new EthernetAddress(0L);
test_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH + EXTRA_DATA_LENGTH];
Arrays.fill(test_array, (byte)'x');
ethernet_address.toByteArray(test_array, 0);
assertEthernetAddressArraysAreEqual(
NULL_ETHERNET_ADDRESS_BYTE_ARRAY, 0, test_array, 0);
for (int i = 0; i < EXTRA_DATA_LENGTH; i++) {
assertEquals("Expected array fill value changed",
(byte)'x',
test_array[i + ETHERNET_ADDRESS_ARRAY_LENGTH]);
}
// now test a null EthernetAddress case with extra data in the array
ethernet_address = new EthernetAddress(0L);
test_array = new byte[ETHERNET_ADDRESS_ARRAY_LENGTH + EXTRA_DATA_LENGTH];
Arrays.fill(test_array, (byte)'x');
ethernet_address.toByteArray(test_array, EXTRA_DATA_LENGTH/2);
assertEthernetAddressArraysAreEqual(
NULL_ETHERNET_ADDRESS_BYTE_ARRAY, 0,
test_array, EXTRA_DATA_LENGTH/2);
for (int i = 0; i < EXTRA_DATA_LENGTH/2; i++)
{
assertEquals("Expected array fill value changed",
(byte)'x',
test_array[i]);
assertEquals("Expected array fill value changed",
(byte)'x',
test_array[i + ETHERNET_ADDRESS_ARRAY_LENGTH +
EXTRA_DATA_LENGTH/2]);
}
// now test a good EthernetAddress case with extra data in the array
ethernet_address =
new EthernetAddress(MIXED_CASE_VALID_ETHERNET_ADDRESS_STRING);
test_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH + EXTRA_DATA_LENGTH];
Arrays.fill(test_array, (byte)'x');
ethernet_address.toByteArray(test_array, 0);
assertEthernetAddressArraysAreEqual(
VALID_ETHERNET_ADDRESS_BYTE_ARRAY, 0, test_array, 0);
for (int i = 0; i < EXTRA_DATA_LENGTH; i++)
{
assertEquals("Expected array fill value changed",
(byte)'x',
test_array[i + ETHERNET_ADDRESS_ARRAY_LENGTH]);
}
// now test a good EthernetAddress case with extra data in the array
ethernet_address =
new EthernetAddress(MIXED_CASE_VALID_ETHERNET_ADDRESS_STRING);
test_array =
new byte[ETHERNET_ADDRESS_ARRAY_LENGTH + EXTRA_DATA_LENGTH];
Arrays.fill(test_array, (byte)'x');
ethernet_address.toByteArray(test_array, EXTRA_DATA_LENGTH/2);
assertEthernetAddressArraysAreEqual(
VALID_ETHERNET_ADDRESS_BYTE_ARRAY, 0,
test_array, EXTRA_DATA_LENGTH/2);
for (int i = 0; i < EXTRA_DATA_LENGTH/2; i++)
{