private static byte[] cipherData(BufferedBlockCipher cipher, byte[] data) { int minSize = cipher.getOutputSize(data.length); byte[] outBuf = new byte[minSize]; int len1 = cipher.processBytes(data, 0, data.length, outBuf, 0); int len2 = -1; try { len2 = cipher.doFinal(outBuf, len1); } catch (InvalidCipherTextException icte) { icte.printStackTrace(); } int actualLength = len1 + len2; byte[] result = new byte[actualLength]; System.arraycopy(outBuf, 0, result, 0, result.length); return result; }
public static byte[] encryptCCM(final byte[] data, final byte[] key, final byte[] nonce) { final byte[] ccm = new byte[25 + 8]; final ByteBuffer buffer = ByteBuffer.allocate(ccm.length + 8); CCMBlockCipher ccmBlockCipher = new CCMBlockCipher(new AESEngine()); AEADParameters aeadParameters = new AEADParameters(new KeyParameter(key), 64, nonce); ccmBlockCipher.init(true, aeadParameters); ccmBlockCipher.processBytes(data, 0, data.length, ccm, data.length); try { ccmBlockCipher.doFinal(ccm, 0); } catch (InvalidCipherTextException e) { e.printStackTrace(); } return ccm; }
@Test public void publishXpub() throws Exception { Contacts contacts = init(); LinkedList<String> responseList = new LinkedList<>(); responseList.add(fail);//magic - string doesn't matter 404 will be caught responseList.add(success);//put metadata mockInterceptor.setResponseStringList(responseList); LinkedList<Integer> responseCodeList = new LinkedList<>(); responseCodeList.add(404);//fetch magic - 404 = new magic hash responseCodeList.add(200); mockInterceptor.setResponseCodeList(responseCodeList); try { contacts.publishXpub(); Assert.assertTrue(true); } catch (MetadataException e) { e.printStackTrace(); Assert.fail(); } catch (IOException e) { e.printStackTrace(); Assert.fail(); } catch (InvalidCipherTextException e) { e.printStackTrace(); Assert.fail(); } }
@Test public void fetch_IOException() throws Exception { Contacts contacts = init(); try { mockInterceptor.setIOException(true); contacts.fetch(); } catch (MetadataException e) { e.printStackTrace(); } catch (IOException e) { Assert.assertTrue(true); return; } catch (InvalidCipherTextException e) { e.printStackTrace(); } finally { mockInterceptor.setIOException(false); } Assert.assertTrue("IOException not caught", false); }
@Test public void publishXpub_IOException() throws Exception { Contacts contacts = init(); try { mockInterceptor.setIOException(true); contacts.publishXpub(); } catch (MetadataException e) { e.printStackTrace(); } catch (IOException e) { Assert.assertTrue(true); return; } catch (InvalidCipherTextException e) { e.printStackTrace(); } finally { mockInterceptor.setIOException(false); } Assert.fail(); }
@Test public void fetchXpub_IOException() throws Exception { Contacts contacts = init(); mockInterceptor.setResponseString("{\"payload\":\"eyJ4cHViIjoieHB1YjY4aGpMM01rdmZ6S1pSdXZmQUFBZWFiYUFuWmpnWXFVM0ZTbTFMRUNxRGNhVHI1N013YzREY2lHcTJKQnJyVG9zOHNuUHg3OG1MdEt4dGFQSzJMcWJoVTVnMW9QRk1hR29uRTI3a0g4S0dBIn0=\",\"version\":1,\"type_id\":4,\"signature\":\"IE2zczTK0sRPLRu/vfbM3v6S7gAIh2o+UQxkn1P4uUT+KqCU+P8kVEt7SLjixIQqSb4UzlKNKirXUBiNWGU4Ygg=\",\"prev_magic_hash\":\"8f122f88cad5faedcc3433dbf0618cea17a5682da2c3dfdf36d03d63f88a90c6\",\"created_at\":1482242589000,\"updated_at\":1482244810000,\"address\":\"16uJDcPbvegnJUhgXr5TW9nd9wbJYNWBAd\"}"); try { mockInterceptor.setIOException(true); contacts.fetchXpub("16uJDcPbvegnJUhgXr5TW9nd9wbJYNWBAd"); } catch (MetadataException e) { e.printStackTrace(); } catch (IOException e) { Assert.assertTrue(true); return; } catch (InvalidCipherTextException e) { e.printStackTrace(); } finally { mockInterceptor.setIOException(false); } Assert.assertTrue(false); }
@Test public void save_IOException() throws Exception { Contacts contacts = init(); Contact contact = new Contact(); contact.setName("John"); mockInterceptor.setResponseString(success); contacts.addContact(contact); mockInterceptor.setIOException(true); try { contacts.save(); } catch (IOException e) { Assert.assertTrue(true); return; } catch (MetadataException e) { e.printStackTrace(); } catch (InvalidCipherTextException e) { e.printStackTrace(); } finally { mockInterceptor.setIOException(false); } Assert.assertTrue(false); }
@Test public void wipe_IOException() throws Exception { Contacts contacts = init(); Contact contact = new Contact(); contact.setName("John"); mockInterceptor.setResponseString(success); contacts.addContact(contact); mockInterceptor.setIOException(true); try { contacts.wipe(); } catch (IOException e) { Assert.assertTrue(true); return; } catch (MetadataException e) { e.printStackTrace(); } catch (InvalidCipherTextException e) { e.printStackTrace(); } finally { mockInterceptor.setIOException(false); } Assert.assertTrue(false); }