1 /* 2 * Copyright (c) 2003, 2007, 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 4844847 27 * @summary Test the Mac.update(ByteBuffer) method 28 * @author Andreas Sterbenz 29 */ 30 31 import java.util.*; 32 import java.nio.*; 33 34 import java.security.*; 35 36 import javax.crypto.*; 37 import javax.crypto.spec.*; 38 39 public class ByteBuffers { 40 41 public static void main(String[] args) throws Exception { 42 Provider p = Security.getProvider("SunJCE"); 43 Random random = new Random(); 44 int n = 10 * 1024; 45 byte[] t = new byte[n]; 46 random.nextBytes(t); 47 48 byte[] keyBytes = new byte[16]; 49 random.nextBytes(keyBytes); 50 SecretKey key = new SecretKeySpec(keyBytes, "HmacMD5"); 51 52 Mac mac = Mac.getInstance("HmacMD5"); 53 mac.init(key); 54 byte[] macValue = mac.doFinal(t); 55 56 // test 1: ByteBuffer with an accessible backing array 57 ByteBuffer b1 = ByteBuffer.allocate(n + 256); 58 b1.position(random.nextInt(256)); 59 b1.limit(b1.position() + n); 60 ByteBuffer b2 = b1.slice(); 61 b2.put(t); 62 b2.clear(); 63 verify(mac, macValue, b2, random); 64 65 // test 2: direct ByteBuffer 66 ByteBuffer b3 = ByteBuffer.allocateDirect(t.length); 67 b3.put(t); 68 b3.clear(); 69 verify(mac, macValue, b3, random); 70 71 // test 3: ByteBuffer without an accessible backing array 72 b2.clear(); 73 ByteBuffer b4 = b2.asReadOnlyBuffer(); 74 verify(mac, macValue, b4, random); 75 76 System.out.println("All tests passed"); 77 } 78 79 private static void verify(Mac mac, byte[] macValue, ByteBuffer b, Random random) throws Exception { 80 int lim = b.limit(); 81 b.limit(random.nextInt(lim)); 82 mac.update(b); 83 if (b.hasRemaining()) { 84 throw new Exception("Buffer not consumed"); 85 } 86 b.limit(lim); 87 mac.update(b); 88 if (b.hasRemaining()) { 89 throw new Exception("Buffer not consumed"); 90 } 91 byte[] newMacValue = mac.doFinal(); 92 if (Arrays.equals(macValue, newMacValue) == false) { 93 throw new Exception("Mac did not verify"); 94 } 95 } 96 }