Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/MessageDigest/ByteBuffers.java
38855 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4856966 808046226* @summary Test the MessageDigest.update(ByteBuffer) method27* @author Andreas Sterbenz28* @library ..29* @key randomness30* @run main/othervm ByteBuffers31*/3233import java.nio.ByteBuffer;34import java.security.*;35import java.util.Arrays;36import java.util.Random;3738public class ByteBuffers extends PKCS11Test {3940static final String[] ALGS = {41"SHA-224", "SHA-256", "SHA-384", "SHA-512", "SHA-512/224", "SHA-512/256"42};4344private static Random random = new Random();4546public static void main(String[] args) throws Exception {47main(new ByteBuffers(), args);48}4950@Override51public void main(Provider p) throws Exception {52int n = 10 * 1024;53byte[] t = new byte[n];54random.nextBytes(t);5556for (String alg : ALGS) {57runTest(p, alg, t);58}59}6061private void runTest(Provider p, String alg, byte[] data) throws Exception {62System.out.println("Test against " + p.getName() + " and " + alg);63MessageDigest md;64try {65md = MessageDigest.getInstance(alg, p);66} catch (NoSuchAlgorithmException e) {67System.out.println("Skip " + alg + " due to no support");68return;69}7071byte[] d1 = md.digest(data);7273int n = data.length;7475// test 1: ByteBuffer with an accessible backing array76ByteBuffer b1 = ByteBuffer.allocate(n + 256);77b1.position(random.nextInt(256));78b1.limit(b1.position() + n);79ByteBuffer b2 = b1.slice();80b2.put(data);81b2.clear();82byte[] d2 = digest(md, b2);83if (Arrays.equals(d1, d2) == false) {84throw new Exception("Test 1 failed");85}8687// test 2: direct ByteBuffer88ByteBuffer b3 = ByteBuffer.allocateDirect(n);89b3.put(data);90b3.clear();91byte[] d3 = digest(md, b3);92if (Arrays.equals(d1, d2) == false) {93throw new Exception("Test 2 failed");94}9596// test 3: ByteBuffer without an accessible backing array97b2.clear();98ByteBuffer b4 = b2.asReadOnlyBuffer();99byte[] d4 = digest(md, b4);100if (Arrays.equals(d1, d2) == false) {101throw new Exception("Test 3 failed");102}103System.out.println("All tests passed");104}105106private static byte[] digest(MessageDigest md, ByteBuffer b)107throws Exception {108int lim = b.limit();109b.limit(random.nextInt(lim));110md.update(b);111if (b.hasRemaining()) {112throw new Exception("Buffer not consumed");113}114b.limit(lim);115md.update(b);116if (b.hasRemaining()) {117throw new Exception("Buffer not consumed");118}119return md.digest();120}121}122123124