1 /* 2 * Copyright (c) 2003, 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 24 /** 25 * @test 26 * @bug 4856966 27 * @summary Test the Signature.update(ByteBuffer) method 28 * @author Andreas Sterbenz 29 * @library .. 30 */ 31 32 import java.util.*; 33 import java.nio.*; 34 35 import java.security.*; 36 37 public class ByteBuffers extends PKCS11Test { 38 39 public static void main(String[] args) throws Exception { 40 main(new ByteBuffers()); 41 } 42 43 public void main(Provider p) throws Exception { 44 Random random = new Random(); 45 int n = 10 * 1024; 46 byte[] t = new byte[n]; 47 random.nextBytes(t); 48 49 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p); 50 kpg.initialize(512); 51 KeyPair kp = kpg.generateKeyPair(); 52 53 Signature sig = Signature.getInstance("MD5withRSA", p); 54 sig.initSign(kp.getPrivate()); 55 sig.update(t); 56 byte[] signature = sig.sign(); 57 58 sig.initVerify(kp.getPublic()); 59 60 // test 1: ByteBuffer with an accessible backing array 61 ByteBuffer b1 = ByteBuffer.allocate(n + 256); 62 b1.position(random.nextInt(256)); 63 b1.limit(b1.position() + n); 64 ByteBuffer b2 = b1.slice(); 65 b2.put(t); 66 b2.clear(); 67 verify(sig, signature, b2, random); 68 69 // test 2: direct ByteBuffer 70 ByteBuffer b3 = ByteBuffer.allocateDirect(t.length); 71 b3.put(t); 72 b3.clear(); 73 verify(sig, signature, b3, random); 74 75 // test 3: ByteBuffer without an accessible backing array 76 b2.clear(); 77 ByteBuffer b4 = b2.asReadOnlyBuffer(); 78 verify(sig, signature, b4, random); 79 80 System.out.println("All tests passed"); 81 } 82 83 private static void verify(Signature sig, byte[] signature, ByteBuffer b, Random random) throws Exception { 84 int lim = b.limit(); 85 b.limit(random.nextInt(lim)); 86 sig.update(b); 87 if (b.hasRemaining()) { 88 throw new Exception("Buffer not consumed"); 89 } 90 b.limit(lim); 91 sig.update(b); 92 if (b.hasRemaining()) { 93 throw new Exception("Buffer not consumed"); 94 } 95 if (sig.verify(signature) == false) { 96 throw new Exception("Signature did not verify"); 97 } 98 } 99 }