@Test public void testVerifyCode() throws Exception { for (String algorithm : algs) { for (String key : keys) { for (int i = 0; i < testStrings.length; ++i) { InputStream in = IOUtils.toInputStream(testStrings[i]); InputStream keyStream = IOUtils.toInputStream(key); InputStream code = new ByteArrayInputStream( Hex.decodeHex(codes.get(algorithm).get(key)[i].toCharArray())); assert (MessageAuthenticator.verifyCode(algorithm, in, keyStream, code)); in = IOUtils.toInputStream(testStrings[i]); keyStream = IOUtils.toInputStream(key); code = new ByteArrayInputStream( Hex.decodeHex( codes.get(algorithm).get(key)[(i + 1) % testStrings.length].toCharArray())); assertFalse(MessageAuthenticator.verifyCode(algorithm, in, keyStream, code)); } } } }
@Test public void testGenerateCode() throws Exception { for (String algorithm : algs) { for (String key : keys) { for (int i = 0; i < testStrings.length; ++i) { InputStream in = IOUtils.toInputStream(testStrings[i]); InputStream keyStream = IOUtils.toInputStream(key); PipedInputStream result = new PipedInputStream(); OutputStream out = new PipedOutputStream(result); MessageAuthenticator.generateCode(algorithm, in, keyStream, out); out.close(); String actual = new String(Hex.encodeHex(IOUtils.toByteArray(result))); assertEquals(codes.get(algorithm).get(key)[i].toLowerCase(), actual.toLowerCase()); } } } }