jdk/test/com/oracle/security/ucrypto/Test8004873.java
changeset 27182 4525d13b8af1
equal deleted inserted replaced
27181:29f9c4f56e80 27182:4525d13b8af1
       
     1 /*
       
     2  * Copyright (c) 2014, 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     8004873
       
    27  * @summary Need to include data buffered by Padding impl when calculating
       
    28  * output buffer sizes.
       
    29  */
       
    30 
       
    31 import java.io.*;
       
    32 import java.security.*;
       
    33 import java.security.spec.*;
       
    34 import java.util.*;
       
    35 import javax.crypto.*;
       
    36 import javax.crypto.spec.*;
       
    37 
       
    38 public class Test8004873 extends UcryptoTest {
       
    39 
       
    40     private static final String[] PADDEDCIPHER_ALGOS = {
       
    41         "AES/ECB/PKCS5Padding",
       
    42         "AES/CBC/PKCS5Padding",
       
    43         "AES/CFB128/PKCS5Padding"
       
    44     };
       
    45 
       
    46     private static final SecretKey AES_KEY;
       
    47 
       
    48     static {
       
    49         byte[] keyValue = {
       
    50             62, 124, -2, -15, 86, -25, 18, -112, 110, 31, 96, 59,
       
    51             89, 70, 60, 103};
       
    52         AES_KEY = new SecretKeySpec(keyValue, "AES");
       
    53     }
       
    54 
       
    55     public static void main(String[] args) throws Exception {
       
    56         main(new Test8004873(), null);
       
    57     }
       
    58 
       
    59     public void doTest(Provider prov) throws Exception {
       
    60         boolean result = true;
       
    61         for (String algo : PADDEDCIPHER_ALGOS) {
       
    62             if (!testOOS(algo, prov)) {
       
    63                 result = false;
       
    64                 System.out.println(algo + " Test Failed!");
       
    65             }
       
    66         }
       
    67         if (!result) {
       
    68             throw new Exception("One or more test failed!");
       
    69         }
       
    70     }
       
    71 
       
    72     private boolean testOOS(String algo, Provider prov)
       
    73         throws Exception {
       
    74 
       
    75         String password = "abcd1234";
       
    76         Cipher c;
       
    77         try {
       
    78             c = Cipher.getInstance(algo, prov);
       
    79         } catch(NoSuchAlgorithmException nsae) {
       
    80             System.out.println("Skipping Unsupported algo: " + algo);
       
    81             return true;
       
    82         }
       
    83         c.init(Cipher.ENCRYPT_MODE, AES_KEY);
       
    84         AlgorithmParameters params = c.getParameters();
       
    85         ByteArrayOutputStream baos = new ByteArrayOutputStream();
       
    86         CipherOutputStream cos = new CipherOutputStream(baos, c);
       
    87         ObjectOutputStream oos = new ObjectOutputStream(cos);
       
    88         oos.writeObject(password);
       
    89         oos.flush();
       
    90         oos.close();
       
    91         byte[] encrypted = baos.toByteArray();
       
    92 
       
    93         c.init(Cipher.DECRYPT_MODE, AES_KEY, params);
       
    94 
       
    95         ByteArrayInputStream bais = new ByteArrayInputStream(encrypted);
       
    96         CipherInputStream cis = new CipherInputStream(bais, c);
       
    97         ObjectInputStream ois = new ObjectInputStream(cis);
       
    98 
       
    99         String recovered = (String) ois.readObject();
       
   100         return recovered.equals(password);
       
   101     }
       
   102 }