EmptyFinalBuffer.java revision 15716:7cd8128e16be
1/*
2 * Copyright (c) 2016, 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 6946830
27 * @summary Test the Cipher.doFinal() with 0-length buffer
28 * @key randomness
29 */
30
31import java.util.*;
32import java.nio.*;
33
34import java.security.*;
35
36import javax.crypto.*;
37import javax.crypto.spec.*;
38
39public class EmptyFinalBuffer {
40
41    private static final String[] ALGOS = {
42        "AES/ECB/PKCS5Padding", "AES/CBC/PKCS5Padding"
43    };
44
45    public static void main(String[] args) throws Exception {
46
47        Provider[] provs = Security.getProviders();
48
49        SecretKey key = new SecretKeySpec(new byte[16], "AES");
50
51        boolean testFailed = false;
52        for (Provider p : provs) {
53            System.out.println("Testing: " + p.getName());
54            for (String algo : ALGOS) {
55                System.out.print("Algo: " + algo);
56                Cipher c;
57                try {
58                    c = Cipher.getInstance(algo, p);
59                } catch (NoSuchAlgorithmException nsae) {
60                    // skip
61                    System.out.println("=> No Support");
62                    continue;
63                }
64                c.init(Cipher.ENCRYPT_MODE, key);
65                AlgorithmParameters params = c.getParameters();
66                c.init(Cipher.DECRYPT_MODE, key, params);
67                try {
68                    byte[] out = c.doFinal(new byte[0]);
69                    System.out.println("=> Accepted w/ " +
70                        (out == null? "null" : (out.length + "-byte")) +
71                        " output");
72                } catch (Exception e) {
73                    testFailed = true;
74                    System.out.println("=> Rejected w/ Exception");
75                    e.printStackTrace();
76                }
77            }
78        }
79        if (testFailed) {
80            throw new Exception("One or more tests failed");
81        } else {
82            System.out.println("All tests passed");
83        }
84    }
85}
86