J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @test |
| 26 | * @bug 4844847 |
| 27 | * @summary Test the MessageDigest.update(ByteBuffer) method |
| 28 | * @author Andreas Sterbenz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.nio.*; |
| 33 | |
| 34 | import java.security.*; |
| 35 | |
| 36 | public class ByteBuffers { |
| 37 | |
| 38 | public static void main(String[] args) throws Exception { |
| 39 | Provider p = Security.getProvider("SUN"); |
| 40 | Random random = new Random(); |
| 41 | int n = 10 * 1024; |
| 42 | byte[] t = new byte[n]; |
| 43 | random.nextBytes(t); |
| 44 | |
| 45 | MessageDigest md = MessageDigest.getInstance("MD5", p); |
| 46 | byte[] d1 = md.digest(t); |
| 47 | |
| 48 | // test 1: ByteBuffer with an accessible backing array |
| 49 | ByteBuffer b1 = ByteBuffer.allocate(n + 256); |
| 50 | b1.position(random.nextInt(256)); |
| 51 | b1.limit(b1.position() + n); |
| 52 | ByteBuffer b2 = b1.slice(); |
| 53 | b2.put(t); |
| 54 | b2.clear(); |
| 55 | byte[] d2 = digest(md, b2, random); |
| 56 | if (Arrays.equals(d1, d2) == false) { |
| 57 | throw new Exception("Test 1 failed"); |
| 58 | } |
| 59 | |
| 60 | // test 2: direct ByteBuffer |
| 61 | ByteBuffer b3 = ByteBuffer.allocateDirect(t.length); |
| 62 | b3.put(t); |
| 63 | b3.clear(); |
| 64 | byte[] d3 = digest(md, b3, random); |
| 65 | if (Arrays.equals(d1, d3) == false) { |
| 66 | throw new Exception("Test 2 failed"); |
| 67 | } |
| 68 | |
| 69 | // test 3: ByteBuffer without an accessible backing array |
| 70 | b2.clear(); |
| 71 | ByteBuffer b4 = b2.asReadOnlyBuffer(); |
| 72 | byte[] d4 = digest(md, b4, random); |
| 73 | if (Arrays.equals(d1, d4) == false) { |
| 74 | throw new Exception("Test 3 failed"); |
| 75 | } |
| 76 | System.out.println("All tests passed"); |
| 77 | } |
| 78 | |
| 79 | private static byte[] digest(MessageDigest md, ByteBuffer b, Random random) throws Exception { |
| 80 | int lim = b.limit(); |
| 81 | b.limit(random.nextInt(lim)); |
| 82 | md.update(b); |
| 83 | if (b.hasRemaining()) { |
| 84 | throw new Exception("Buffer not consumed"); |
| 85 | } |
| 86 | b.limit(lim); |
| 87 | md.update(b); |
| 88 | if (b.hasRemaining()) { |
| 89 | throw new Exception("Buffer not consumed"); |
| 90 | } |
| 91 | return md.digest(); |
| 92 | } |
| 93 | } |