Poly1305 { static void verifyMac(final byte[] key, byte[] data, byte[] mac) throws GeneralSecurityException { if (!Bytes.equal(computeMac(key, data), mac)) { throw new GeneralSecurityException("invalid MAC"); } } private  Poly1305();  static final int MAC_TAG_SIZE_IN_BYTES; static final int MAC_KEY_SIZE_IN_BYTES; }
@Test public void testPoly1305VerifyMacThrowsIllegalArgExpWhenKeyLenIsGreaterThan32() throws GeneralSecurityException { try { Poly1305.verifyMac(new byte[MAC_KEY_SIZE_IN_BYTES + 1], new byte[0], new byte[0]); fail("Expected IllegalArgumentException."); } catch (IllegalArgumentException e) { Truth.assertThat(e).hasMessageThat().containsMatch("The key length in bytes must be 32."); } }
@Test public void testPoly1305VerifyMacThrowsIllegalArgExpWhenKeyLenIsLessThan32() throws GeneralSecurityException { try { Poly1305.verifyMac(new byte[MAC_KEY_SIZE_IN_BYTES - 1], new byte[0], new byte[0]); fail("Expected IllegalArgumentException."); } catch (IllegalArgumentException e) { Truth.assertThat(e).hasMessageThat().containsMatch("The key length in bytes must be 32."); } }
@Test public void testFailedVerification() throws GeneralSecurityException { try { byte[] key = new byte[MAC_KEY_SIZE_IN_BYTES]; key[0] = 1; Poly1305.verifyMac(key, new byte[]{1}, new byte[MAC_TAG_SIZE_IN_BYTES]); fail("Expected GeneralSecurityException."); } catch (GeneralSecurityException e) { Truth.assertThat(e).hasMessageThat().containsMatch("invalid MAC"); } }