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 package org.graalvm.compiler.hotspot.test;
  24 
  25 import java.security.MessageDigest;
  26 import java.security.NoSuchAlgorithmException;
  27 import java.security.NoSuchProviderException;
  28 
  29 import org.junit.Test;
  30 import org.junit.internal.AssumptionViolatedException;
  31 
  32 import org.graalvm.compiler.api.test.Graal;
  33 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  34 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  35 import org.graalvm.compiler.hotspot.replacements.SHA2Substitutions;
  36 import org.graalvm.compiler.hotspot.replacements.SHA5Substitutions;
  37 import org.graalvm.compiler.hotspot.replacements.SHASubstitutions;
  38 import org.graalvm.compiler.runtime.RuntimeProvider;
  39 
  40 import jdk.vm.ci.code.InstalledCode;
  41 import jdk.vm.ci.meta.ResolvedJavaMethod;
  42 
  43 /**
  44  * Exercise the execution of the SHA digest substitutions.
  45  */
  46 public class TestSHASubstitutions extends HotSpotGraalCompilerTest {
  47 
  48     public byte[] testDigest(String name, byte[] data) throws NoSuchAlgorithmException {
  49         MessageDigest digest;
  50         try {
  51             digest = MessageDigest.getInstance(name, "SUN");
  52             digest.update(data);
  53             return digest.digest();
  54         } catch (NoSuchProviderException e) {
  55             return null;
  56         }
  57     }
  58 
  59     byte[] getData() {
  60         byte[] data = new byte[1024 * 16];
  61         for (int i = 0; i < data.length; i++) {
  62             data[i] = (byte) i;
  63         }
  64         return data;
  65     }
  66 
  67     GraalHotSpotVMConfig getConfig() {
  68         HotSpotGraalRuntimeProvider rt = (HotSpotGraalRuntimeProvider) Graal.getRequiredCapability(RuntimeProvider.class);
  69         return rt.getVMConfig();
  70     }
  71 
  72     @Test
  73     public void testSha1() {
  74         if (getConfig().useSHA1Intrinsics()) {
  75             testWithInstalledIntrinsic("sun.security.provider.SHA", SHASubstitutions.implCompressName, "testDigest", "SHA-1", getData());
  76         }
  77     }
  78 
  79     void testWithInstalledIntrinsic(String className, String methodName, String testSnippetName, Object... args) {
  80         Class<?> c;
  81         try {
  82             c = Class.forName(className);
  83         } catch (ClassNotFoundException e) {
  84             // It's ok to not find the class - a different security provider
  85             // may have been installed
  86             return;
  87         }
  88         InstalledCode code = null;
  89         try {
  90             ResolvedJavaMethod method = getResolvedJavaMethod(testSnippetName);
  91             Object receiver = method.isStatic() ? null : this;
  92             Result expect = executeExpected(method, receiver, args);
  93             code = compileAndInstallSubstitution(c, methodName);
  94             assertTrue("Failed to install " + methodName, code != null);
  95             testAgainstExpected(method, expect, receiver, args);
  96         } catch (AssumptionViolatedException e) {
  97             // Suppress so that subsequent calls to this method within the
  98             // same Junit @Test annotated method can proceed.
  99         }
 100         if (code != null) {
 101             code.invalidate();
 102         }
 103     }
 104 
 105     @Test
 106     public void testSha256() {
 107         if (getConfig().useSHA256Intrinsics()) {
 108             testWithInstalledIntrinsic("sun.security.provider.SHA2", SHA2Substitutions.implCompressName, "testDigest", "SHA-256", getData());
 109         }
 110     }
 111 
 112     @Test
 113     public void testSha512() {
 114         if (getConfig().useSHA512Intrinsics()) {
 115             testWithInstalledIntrinsic("sun.security.provider.SHA5", SHA5Substitutions.implCompressName, "testDigest", "SHA-512", getData());
 116         }
 117     }
 118 
 119 }