# HG changeset patch # User akosarev # Date 1465314886 25200 # Node ID f2ddc41670a8d1fd30d398e53c0f3c242eb45290 # Parent da63f7ec513b82d9dfff964f66772f7c1f6647b5 8157603: TestCipher.java doesn't check one of the decrypted message as expected Reviewed-by: valeriep diff -r da63f7ec513b -r f2ddc41670a8 jdk/test/com/sun/crypto/provider/Cipher/TestCipher.java --- a/jdk/test/com/sun/crypto/provider/Cipher/TestCipher.java Tue Jun 07 11:43:46 2016 +0000 +++ b/jdk/test/com/sun/crypto/provider/Cipher/TestCipher.java Tue Jun 07 08:54:46 2016 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -150,11 +150,6 @@ } // Encryption - int PAD_LEN = 0; - if (pad.equalsIgnoreCase("PKCS5Padding")) { - // Need to consider pad bytes - PAD_LEN = 8; - } byte[] plainText = INPUT_TEXT.clone(); @@ -162,12 +157,13 @@ byte[] cipherText = ci.doFinal(INPUT_TEXT, ENC_OFFSET, TEXT_LEN); // Generate cipher and save to same buffer - int offset = ci.update( + int enc_bytes = ci.update( INPUT_TEXT, ENC_OFFSET, TEXT_LEN, INPUT_TEXT, STORAGE_OFFSET); - ci.doFinal(INPUT_TEXT, offset + STORAGE_OFFSET); + enc_bytes += ci.doFinal(INPUT_TEXT, enc_bytes + STORAGE_OFFSET); if (!equalsBlock( - INPUT_TEXT, STORAGE_OFFSET, cipherText, 0, cipherText.length)) { + INPUT_TEXT, STORAGE_OFFSET, enc_bytes, + cipherText, 0, cipherText.length)) { throw new RuntimeException( "Different ciphers generated with same buffer"); } @@ -183,8 +179,8 @@ byte[] recoveredText = ci.doFinal(cipherText, 0, cipherText.length); if (!equalsBlock( - plainText, ENC_OFFSET, recoveredText, 0, - recoveredText.length)) { + plainText, ENC_OFFSET, TEXT_LEN, + recoveredText, 0, recoveredText.length)) { throw new RuntimeException( "Recovered text not same as plain text"); } else { @@ -192,13 +188,13 @@ } // Recover text from cipher and save to same buffer - ci.update(INPUT_TEXT, STORAGE_OFFSET, TEXT_LEN + PAD_LEN, INPUT_TEXT, - ENC_OFFSET); - ci.doFinal(INPUT_TEXT, ENC_OFFSET); + int dec_bytes = ci.update( + INPUT_TEXT, STORAGE_OFFSET, enc_bytes, INPUT_TEXT, ENC_OFFSET); + dec_bytes += ci.doFinal(INPUT_TEXT, dec_bytes + ENC_OFFSET); if (!equalsBlock( - plainText, ENC_OFFSET, recoveredText, 0, - recoveredText.length)) { + plainText, ENC_OFFSET, TEXT_LEN, + INPUT_TEXT, ENC_OFFSET, dec_bytes)) { throw new RuntimeException( "Recovered text not same as plain text with same buffer"); } else { @@ -208,9 +204,12 @@ out.println("Test Passed."); } - private static boolean equalsBlock(byte[] b1, int off1, byte[] b2, int off2, - int len) { - for (int i = off1, j = off2, k = 0; k < len; i++, j++, k++) { + private static boolean equalsBlock(byte[] b1, int off1, int len1, + byte[] b2, int off2, int len2) { + if (len1 != len2) { + return false; + } + for (int i = off1, j = off2, k = 0; k < len1; i++, j++, k++) { if (b1[i] != b2[j]) { return false; }