FirmwareImage implements Chunk.Chunkable { public byte[] block(int index) { if (index >= blockCount()) { throw new ArrayIndexOutOfBoundsException("Invalid block index " + index); } byte[] theBlock = new byte[FW_BLOCK_SIZE + 2]; byte[] rawIndex = intToTwoBytes(index, Constants.CC2540_BYTE_ORDER); System.arraycopy(rawIndex, 0, theBlock, 0, 2); int blockStart = index * FW_BLOCK_SIZE; int length = FW_BLOCK_SIZE; while (blockStart + length > data().length) { length--; } System.arraycopy(data(), blockStart, theBlock, 2, length); return theBlock; } FirmwareImage(byte[] rawData, String filename); byte[] data(); String name(); int sizeBytes(); long version(); @Override byte[] getChunkableData(); int crc(); int crcShadow(); int intVersion(); int length(); byte[] uniqueID(); byte[] reserved(); byte[] metadata(); int blockCount(); byte[] block(int index);  }
@Test public void testFirmwareBlocksInvalid() throws ImageParsingException { FirmwareImage image = new FirmwareImage(rawImageData_invalid, ""); assertThat(image.block(0)).isEqualTo(intArrayToByteArray(new int[]{ 0x00, 0x00, 0x2B, 0x65, 0xFF, 0xFF, 0x64, 0x00, 0x00, 0x7C, 0x41, 0x41, 0x41, 0x41, 0xFF, 0xFF, 0xFF, 0xFF })); assertThat(image.block(1)).isEqualTo(intArrayToByteArray(new int[]{ 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF })); assertThat(image.block(2)).isEqualTo(intArrayToByteArray(new int[]{ 0x02, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })); try { image.block(3); fail("Shouldn't have worked"); } catch (Exception e) { assertThat(e).isNotNull(); } }
@Test public void testFirmwareBlocksValid() throws ImageParsingException { FirmwareImage image1 = new FirmwareImage(rawImageData_valid, ""); assertThat(image1.block(0)).isEqualTo(intArrayToByteArray(new int[]{ 0x00, 0x00, 0x2B, 0x65, 0xFF, 0xFF, 0x64, 0x00, 0x00, 0x7C, 0x41, 0x41, 0x41, 0x41, 0xFF, 0xFF, 0xFF, 0xFF })); try { image1.block(1); fail("Shouldn't have worked"); } catch (ArrayIndexOutOfBoundsException e) { assertThat(e).isNotNull(); } }