Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/ByteBuffers.java
38855 views
/*1* Copyright (c) 2003, 2016, 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 485696626* @summary Test the Signature.update(ByteBuffer) method27* @author Andreas Sterbenz28* @library ..29* @key randomness30* @run main/othervm ByteBuffers31* @run main/othervm ByteBuffers sm32*/3334import java.nio.ByteBuffer;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.Provider;38import java.security.Signature;39import java.util.Random;4041public class ByteBuffers extends PKCS11Test {4243public static void main(String[] args) throws Exception {44main(new ByteBuffers(), args);45}4647@Override48public void main(Provider p) throws Exception {49Random random = new Random();50int n = 10 * 1024;51byte[] t = new byte[n];52random.nextBytes(t);5354KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);55kpg.initialize(512);56KeyPair kp = kpg.generateKeyPair();5758Signature sig = Signature.getInstance("MD5withRSA", p);59sig.initSign(kp.getPrivate());60sig.update(t);61byte[] signature = sig.sign();6263sig.initVerify(kp.getPublic());6465// test 1: ByteBuffer with an accessible backing array66ByteBuffer b1 = ByteBuffer.allocate(n + 256);67b1.position(random.nextInt(256));68b1.limit(b1.position() + n);69ByteBuffer b2 = b1.slice();70b2.put(t);71b2.clear();72verify(sig, signature, b2, random);7374// test 2: direct ByteBuffer75ByteBuffer b3 = ByteBuffer.allocateDirect(t.length);76b3.put(t);77b3.clear();78verify(sig, signature, b3, random);7980// test 3: ByteBuffer without an accessible backing array81b2.clear();82ByteBuffer b4 = b2.asReadOnlyBuffer();83verify(sig, signature, b4, random);8485System.out.println("All tests passed");86}8788private static void verify(Signature sig, byte[] signature, ByteBuffer b, Random random) throws Exception {89int lim = b.limit();90b.limit(random.nextInt(lim));91sig.update(b);92if (b.hasRemaining()) {93throw new Exception("Buffer not consumed");94}95b.limit(lim);96sig.update(b);97if (b.hasRemaining()) {98throw new Exception("Buffer not consumed");99}100if (sig.verify(signature) == false) {101throw new Exception("Signature did not verify");102}103}104}105106107