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  */
  29 
  30 import java.io.*;
  31 import java.security.*;
  32 import java.security.spec.*;
  33 import java.util.*;
  34 import javax.crypto.*;
  35 import javax.crypto.spec.*;
  36 
  37 public class TestDigest extends UcryptoTest {
  38 
  39     private static final String[] MD_ALGOS = {
  40         "MD5",
  41         "SHA",
  42         "SHA-256",
  43         "SHA-384",
  44         "SHA-512"
  45     };
  46 
  47     public static void main(String[] args) throws Exception {
  48         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 }
--- EOF ---