public void testParseData() throws IOException, StorageFileException { long indexFree = 25L; long indexConversation = 13L; int version = 17; byte[] dataPlain = new byte[Storage.CHUNK_SIZE - 4 - Encryption.ENCRYPTION_OVERHEAD]; System.arraycopy( LowLevel.getBytesUnsignedInt(indexFree), 0, dataPlain, Storage.CHUNK_SIZE - 4 - Encryption.ENCRYPTION_OVERHEAD - 8, 4); System.arraycopy( LowLevel.getBytesUnsignedInt(indexConversation), 0, dataPlain, Storage.CHUNK_SIZE - 4 - Encryption.ENCRYPTION_OVERHEAD - 4, 4); byte[] dataEncrypted = Encryption.encryptSymmetric(dataPlain, Encryption.retreiveEncryptionKey()); byte[] dataAll = new byte[Storage.CHUNK_SIZE]; System.arraycopy(dataEncrypted, 0, dataAll, 4, Storage.CHUNK_SIZE - 4); // wrong header (SMS) dataAll[0] = 0x53; // S dataAll[1] = 0x53; // S dataAll[2] = 0x53; // S try { // inject it in the file Storage.getDatabase().setEntry(0, dataAll); Header.forceClearCache(); Header.getHeader(); assertTrue(false); } catch (StorageFileException ex) { } // fixed, version set dataAll[1] = 0x4D; // M dataAll[3] = (byte) version; Storage.getDatabase().setEntry(0, dataAll); Header.forceClearCache(); Header header = null; try { header = Header.getHeader(); assertEquals(indexFree, header.getIndexEmpty()); assertEquals(indexConversation, header.getIndexConversations()); assertEquals(version, header.getVersion()); } catch (StorageFileException ex) { assertTrue(false); } }
public void testCreateData() throws StorageFileException, IOException, WrongKeyException { long indexFree = 25L; long indexConversation = 13L; int version = 32; Header header = Header.createHeader(); header.setIndexEmpty(indexFree); header.setIndexConversations(indexConversation); header.setVersion(version); header.saveToFile(); byte[] dataAll = Storage.getDatabase().getEntry(0); // chunk length assertEquals(dataAll.length, Storage.CHUNK_SIZE); // plain header assertEquals(dataAll[0], (byte) 0x53); // S assertEquals(dataAll[1], (byte) 0x4D); // M assertEquals(dataAll[2], (byte) 0x53); // S assertEquals(dataAll[3], (byte) version); // Version // decrypt the encoded part ByteBuffer buf = ByteBuffer.allocate(Storage.CHUNK_SIZE - 4); buf.put(dataAll, 4, Storage.CHUNK_SIZE - 4); byte[] dataPlain = Encryption.decryptSymmetric(buf.array(), Encryption.retreiveEncryptionKey()); // check the indices assertEquals( LowLevel.getUnsignedInt( dataPlain, Storage.CHUNK_SIZE - 4 - Encryption.ENCRYPTION_OVERHEAD - 8), indexFree); assertEquals( LowLevel.getUnsignedInt( dataPlain, Storage.CHUNK_SIZE - 4 - Encryption.ENCRYPTION_OVERHEAD - 4), indexConversation); }