jdk/test/sun/security/pkcs11/MessageDigest/DigestKAT.java
changeset 35379 1e8e336ef66b
parent 12685 8a448b5b9006
child 40975 680639c9b307
equal deleted inserted replaced
35378:7e19fa0e4e5b 35379:1e8e336ef66b
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    25  * @test
    25  * @test
    26  * @bug 4856966
    26  * @bug 4856966
    27  * @summary Basic known-answer-test for all our MessageDigest algorithms
    27  * @summary Basic known-answer-test for all our MessageDigest algorithms
    28  * @author Andreas Sterbenz
    28  * @author Andreas Sterbenz
    29  * @library ..
    29  * @library ..
       
    30  * @run main/othervm DigestKAT
       
    31  * @run main/othervm DigestKAT sm
    30  */
    32  */
    31 
    33 
    32 import java.io.*;
    34 import java.io.ByteArrayOutputStream;
    33 import java.util.*;
    35 import java.io.IOException;
    34 
    36 import java.io.StringReader;
    35 import java.security.*;
    37 import java.security.MessageDigest;
       
    38 import java.security.Provider;
       
    39 import java.util.Arrays;
    36 
    40 
    37 public class DigestKAT extends PKCS11Test {
    41 public class DigestKAT extends PKCS11Test {
    38 
    42 
    39     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
    43     private final static char[] hexDigits = "0123456789abcdef".toCharArray();
    40 
    44 
    41     public static String toString(byte[] b) {
    45     public static String toString(byte[] b) {
    42         StringBuffer sb = new StringBuffer(b.length * 3);
    46         StringBuilder sb = new StringBuilder(b.length * 3);
    43         for (int i = 0; i < b.length; i++) {
    47         for (int i = 0; i < b.length; i++) {
    44             int k = b[i] & 0xff;
    48             int k = b[i] & 0xff;
    45             if (i != 0) {
    49             if (i != 0) {
    46                 sb.append(':');
    50                 sb.append(':');
    47             }
    51             }
   104         DigestTest(String alg, byte[] data, byte[] digest) {
   108         DigestTest(String alg, byte[] data, byte[] digest) {
   105             this.alg = alg;
   109             this.alg = alg;
   106             this.data = data;
   110             this.data = data;
   107             this.digest = digest;
   111             this.digest = digest;
   108         }
   112         }
       
   113         @Override
   109         void run(Provider p) throws Exception {
   114         void run(Provider p) throws Exception {
   110             if (p.getService("MessageDigest", alg) == null) {
   115             if (p.getService("MessageDigest", alg) == null) {
   111                 System.out.println("Skipped " + alg);
   116                 System.out.println("Skipped " + alg);
   112                 return;
   117                 return;
   113             }
   118             }
   121                 }
   126                 }
   122                 System.out.println("dig:  " + DigestKAT.toString(digest));
   127                 System.out.println("dig:  " + DigestKAT.toString(digest));
   123                 System.out.println("out:  " + DigestKAT.toString(myDigest));
   128                 System.out.println("out:  " + DigestKAT.toString(myDigest));
   124                 throw new Exception("Digest test for " + alg + " failed");
   129                 throw new Exception("Digest test for " + alg + " failed");
   125             }
   130             }
   126 //          System.out.println("Passed " + alg);
       
   127         }
   131         }
   128     }
   132     }
   129 
   133 
   130     private static byte[] s(String s) {
   134     private static byte[] s(String s) {
   131         try {
   135         try {
   219         System.out.println("All tests passed");
   223         System.out.println("All tests passed");
   220         long stop = System.currentTimeMillis();
   224         long stop = System.currentTimeMillis();
   221         System.out.println("Done (" + (stop - start) + " ms).");
   225         System.out.println("Done (" + (stop - start) + " ms).");
   222     }
   226     }
   223 
   227 
       
   228     @Override
   224     public void main(Provider p) throws Exception{
   229     public void main(Provider p) throws Exception{
   225         runTests(tests, p);
   230         runTests(tests, p);
   226     }
   231     }
   227 
   232 
   228     public static void main(String[] args) throws Exception {
   233     public static void main(String[] args) throws Exception {
   229         main(new DigestKAT());
   234         main(new DigestKAT(), args);
   230     }
   235     }
   231 
   236 
   232 }
   237 }