1 /*
   2  * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8048604
  27  * @library ../ /test/lib
  28  * @summary This test verifies the assertion "There should be no transformation
  29  *          on the plaintext/ciphertext in encryption/decryption mechanism" for
  30  *          feature "NullCipher".
  31  */
  32 
  33 import javax.crypto.BadPaddingException;
  34 import javax.crypto.Cipher;
  35 import javax.crypto.IllegalBlockSizeException;
  36 import javax.crypto.NullCipher;
  37 import javax.crypto.ShortBufferException;
  38 import jdk.test.lib.RandomFactory;
  39 
  40 public class CipherNCFuncTest {
  41     public static void main(String[] args) throws ShortBufferException,
  42             IllegalBlockSizeException, BadPaddingException {
  43         byte[] plainText = new byte[801];
  44         // Initialization
  45         RandomFactory.getRandom().nextBytes(plainText);
  46         Cipher ci = new NullCipher();
  47         // Encryption
  48         byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
  49         int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
  50         ci.doFinal(cipherText, offset);
  51         // Decryption
  52         byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
  53         int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
  54         // Comparison
  55         if (len != plainText.length ||
  56                 !TestUtilities.equalsBlock(plainText, cipherText, len) ||
  57                 !TestUtilities.equalsBlock(plainText, recoveredText, len)) {
  58             throw new RuntimeException(
  59                 "Test failed because plainText not equal to cipherText and revoveredText");
  60         }
  61     }
  62 }