TestDigest.java revision 10854:558e97e47abe
1139731Simp/*
2553Srgrimes * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3553Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4553Srgrimes *
5553Srgrimes * This code is free software; you can redistribute it and/or modify it
6553Srgrimes * under the terms of the GNU General Public License version 2 only, as
7553Srgrimes * published by the Free Software Foundation.
8553Srgrimes *
9553Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
10553Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11553Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12553Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
13553Srgrimes * accompanied this code).
1413765Smpp *
15553Srgrimes * You should have received a copy of the GNU General Public License version
16553Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
17553Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18553Srgrimes *
19553Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20553Srgrimes * or visit www.oracle.com if you need additional information or have any
21553Srgrimes * questions.
22553Srgrimes */
23553Srgrimes
24553Srgrimes/*
25553Srgrimes * @test
26553Srgrimes * @bug     7088989
2750477Speter * @summary Ensure the various message digests works correctly
28553Srgrimes */
29553Srgrimes
30719Swollmanimport java.io.*;
3130805Sbdeimport java.security.*;
32719Swollmanimport java.security.spec.*;
33292668Sjhbimport java.util.*;
34292668Sjhbimport javax.crypto.*;
35553Srgrimesimport javax.crypto.spec.*;
3630805Sbde
37553Srgrimespublic class TestDigest extends UcryptoTest {
38185341Sjkim
39185341Sjkim    private static final String[] MD_ALGOS = {
40553Srgrimes        "MD5",
41553Srgrimes        "SHA",
4230805Sbde        "SHA-256",
43553Srgrimes        "SHA-384",
44185341Sjkim        "SHA-512"
45185341Sjkim    };
46185341Sjkim
4730805Sbde    public static void main(String[] args) throws Exception {
4830805Sbde        main(new TestDigest(), null);
49    }
50
51    public void doTest(Provider p) {
52        boolean testPassed = true;
53        byte[] msg = new byte[200];
54        (new SecureRandom()).nextBytes(msg);
55        String interopProvName = "SUN";
56
57        for (String a : MD_ALGOS) {
58            try {
59                MessageDigest md, md2;
60                try {
61                    md = MessageDigest.getInstance(a, p);
62                } catch (NoSuchAlgorithmException nsae) {
63                    System.out.println("Skipping Unsupported MD algo: " + a);
64                    continue;
65                }
66                md2 = MessageDigest.getInstance(a, interopProvName);
67                // Test Interoperability for update+digest calls
68                for (int i = 0; i < 3; i++) {
69                    md.update(msg);
70                    byte[] digest = md.digest();
71                    md2.update(msg);
72                    byte[] digest2 = md2.digest();
73                    if (!Arrays.equals(digest, digest2)) {
74                        System.out.println("DIFF1 FAILED for: " + a + " at iter " + i);
75                        testPassed = false;
76                    }
77                }
78
79                // Test Interoperability for digest calls
80                md = MessageDigest.getInstance(a, p);
81                md2 = MessageDigest.getInstance(a, interopProvName);
82
83                for (int i = 0; i < 3; i++) {
84                    byte[] digest = md.digest();
85                    byte[] digest2 = md2.digest();
86                    if (!Arrays.equals(digest, digest2)) {
87                        System.out.println("DIFF2 FAILED for: " + a + " at iter " + i);
88                        testPassed = false;
89                    }
90                }
91
92                // Test Cloning functionality
93                md = MessageDigest.getInstance(a, p);
94                md2 = (MessageDigest) md.clone(); // clone right after construction
95                byte[] digest = md.digest();
96                byte[] digest2 = md2.digest();
97                if (!Arrays.equals(digest, digest2)) {
98                    System.out.println("DIFF-3.1 FAILED for: " + a);
99                    testPassed = false;
100                }
101                md.update(msg);
102                md2 = (MessageDigest) md.clone(); // clone again after update call
103                digest = md.digest();
104                digest2 = md2.digest();
105                if (!Arrays.equals(digest, digest2)) {
106                    System.out.println("DIFF-3.2 FAILED for: " + a);
107                    testPassed = false;
108                }
109                md2 = (MessageDigest) md.clone(); // clone after digest
110                digest = md.digest();
111                digest2 = md2.digest();
112                if (!Arrays.equals(digest, digest2)) {
113                    System.out.println("DIFF-3.3 FAILED for: " + a);
114                    testPassed = false;
115                }
116            } catch(Exception ex) {
117                System.out.println("Unexpected Exception: " + a);
118                ex.printStackTrace();
119                testPassed = false;
120            }
121        }
122        if (!testPassed) {
123            throw new RuntimeException("One or more MD test failed!");
124        } else {
125            System.out.println("MD Tests Passed");
126        }
127    }
128}
129