Path: blob/master/test/jdk/sun/security/pkcs11/MessageDigest/ByteBuffers.java
41153 views
/*1* Copyright (c) 2003, 2020, 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 8080462 824233226* @summary Test the MessageDigest.update(ByteBuffer) method27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm ByteBuffers32*/3334import java.nio.ByteBuffer;35import java.security.*;36import java.util.Arrays;37import java.util.Random;38import java.util.List;3940public class ByteBuffers extends PKCS11Test {4142private static Random random = new Random();4344public static void main(String[] args) throws Exception {45main(new ByteBuffers(), args);46}4748@Override49public void main(Provider p) throws Exception {50List<String> ALGS = getSupportedAlgorithms("MessageDigest",51"SHA", p);5253int n = 10 * 1024;54byte[] t = new byte[n];55random.nextBytes(t);5657for (String alg : ALGS) {58runTest(p, alg, t);59}60}6162private void runTest(Provider p, String alg, byte[] data) throws Exception {63System.out.println("Test against " + p.getName() + " and " + alg);64MessageDigest md = MessageDigest.getInstance(alg, p);6566byte[] d1 = md.digest(data);6768int n = data.length;6970// test 1: ByteBuffer with an accessible backing array71ByteBuffer b1 = ByteBuffer.allocate(n + 256);72b1.position(random.nextInt(256));73b1.limit(b1.position() + n);74ByteBuffer b2 = b1.slice();75b2.put(data);76b2.clear();77byte[] d2 = digest(md, b2);78if (Arrays.equals(d1, d2) == false) {79throw new Exception("Test 1 failed");80}8182// test 2: direct ByteBuffer83ByteBuffer b3 = ByteBuffer.allocateDirect(n);84b3.put(data);85b3.clear();86byte[] d3 = digest(md, b3);87if (Arrays.equals(d1, d2) == false) {88throw new Exception("Test 2 failed");89}9091// test 3: ByteBuffer without an accessible backing array92b2.clear();93ByteBuffer b4 = b2.asReadOnlyBuffer();94byte[] d4 = digest(md, b4);95if (Arrays.equals(d1, d2) == false) {96throw new Exception("Test 3 failed");97}98System.out.println("All tests passed");99}100101private static byte[] digest(MessageDigest md, ByteBuffer b)102throws Exception {103int lim = b.limit();104b.limit(random.nextInt(lim));105md.update(b);106if (b.hasRemaining()) {107throw new Exception("Buffer not consumed");108}109b.limit(lim);110md.update(b);111if (b.hasRemaining()) {112throw new Exception("Buffer not consumed");113}114return md.digest();115}116}117118119