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     7088989
  27  * @summary Ensure the various message digests works correctly
  28  * @key randomness
  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 TestDigest extends UcryptoTest {
  39 
  40     private static final String[] MD_ALGOS = {
  41         "MD5",
  42         "SHA",
  43         "SHA-256",
  44         "SHA-384",
  45         "SHA-512"
  46     };
  47 
  48     public static void main(String[] args) throws Exception {
  49         main(new TestDigest(), null);
  50     }
  51 
  52     public void doTest(Provider p) {
  53         boolean testPassed = true;
  54         byte[] msg = new byte[200];
  55         (new SecureRandom()).nextBytes(msg);
  56         String interopProvName = "SUN";
  57 
  58         for (String a : MD_ALGOS) {
  59             try {
  60                 MessageDigest md, md2;
  61                 try {
  62                     md = MessageDigest.getInstance(a, p);
  63                 } catch (NoSuchAlgorithmException nsae) {
  64                     System.out.println("Skipping Unsupported MD algo: " + a);
  65                     continue;
  66                 }
  67                 md2 = MessageDigest.getInstance(a, interopProvName);
  68                 // Test Interoperability for update+digest calls
  69                 for (int i = 0; i < 3; i++) {
  70                     md.update(msg);
  71                     byte[] digest = md.digest();
  72                     md2.update(msg);
  73                     byte[] digest2 = md2.digest();
  74                     if (!Arrays.equals(digest, digest2)) {
  75                         System.out.println("DIFF1 FAILED for: " + a + " at iter " + i);
  76                         testPassed = false;
  77                     }
  78                 }
  79 
  80                 // Test Interoperability for digest calls
  81                 md = MessageDigest.getInstance(a, p);
  82                 md2 = MessageDigest.getInstance(a, interopProvName);
  83 
  84                 for (int i = 0; i < 3; i++) {
  85                     byte[] digest = md.digest();
  86                     byte[] digest2 = md2.digest();
  87                     if (!Arrays.equals(digest, digest2)) {
  88                         System.out.println("DIFF2 FAILED for: " + a + " at iter " + i);
  89                         testPassed = false;
  90                     }
  91                 }
  92 
  93                 // Test Cloning functionality
  94                 md = MessageDigest.getInstance(a, p);
  95                 md2 = (MessageDigest) md.clone(); // clone right after construction
  96                 byte[] digest = md.digest();
  97                 byte[] digest2 = md2.digest();
  98                 if (!Arrays.equals(digest, digest2)) {
  99                     System.out.println("DIFF-3.1 FAILED for: " + a);
 100                     testPassed = false;
 101                 }
 102                 md.update(msg);
 103                 md2 = (MessageDigest) md.clone(); // clone again after update call
 104                 digest = md.digest();
 105                 digest2 = md2.digest();
 106                 if (!Arrays.equals(digest, digest2)) {
 107                     System.out.println("DIFF-3.2 FAILED for: " + a);
 108                     testPassed = false;
 109                 }
 110                 md2 = (MessageDigest) md.clone(); // clone after digest
 111                 digest = md.digest();
 112                 digest2 = md2.digest();
 113                 if (!Arrays.equals(digest, digest2)) {
 114                     System.out.println("DIFF-3.3 FAILED for: " + a);
 115                     testPassed = false;
 116                 }
 117             } catch(Exception ex) {
 118                 System.out.println("Unexpected Exception: " + a);
 119                 ex.printStackTrace();
 120                 testPassed = false;
 121             }
 122         }
 123         if (!testPassed) {
 124             throw new RuntimeException("One or more MD test failed!");
 125         } else {
 126             System.out.println("MD Tests Passed");
 127         }
 128     }
 129 }
--- EOF ---