Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/PBEFunc/CipherNCFuncTest.java
41161 views
/*1* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 804860426* @library ../ /test/lib27* @build jdk.test.lib.RandomFactory28* @run main CipherNCFuncTest29* @summary This test verifies the assertion "There should be no transformation30* on the plaintext/ciphertext in encryption/decryption mechanism" for31* feature "NullCipher".32*/3334import javax.crypto.BadPaddingException;35import javax.crypto.Cipher;36import javax.crypto.IllegalBlockSizeException;37import javax.crypto.NullCipher;38import javax.crypto.ShortBufferException;39import jdk.test.lib.RandomFactory;4041public class CipherNCFuncTest {42public static void main(String[] args) throws ShortBufferException,43IllegalBlockSizeException, BadPaddingException {44byte[] plainText = new byte[801];45// Initialization46RandomFactory.getRandom().nextBytes(plainText);47Cipher ci = new NullCipher();48// Encryption49byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];50int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);51ci.doFinal(cipherText, offset);52// Decryption53byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];54int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);55// Comparison56if (len != plainText.length ||57!TestUtilities.equalsBlock(plainText, cipherText, len) ||58!TestUtilities.equalsBlock(plainText, recoveredText, len)) {59throw new RuntimeException(60"Test failed because plainText not equal to cipherText and revoveredText");61}62}63}646566