Path: blob/master/test/jdk/javax/crypto/Cipher/ByteBuffers.java
41149 views
/*1* Copyright (c) 2003, 2007, 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 484484726* @summary Test the Cipher.update/doFinal(ByteBuffer, ByteBuffer) methods27* @author Andreas Sterbenz28* @key randomness29*/3031import java.util.*;32import java.nio.*;3334import java.security.*;3536import javax.crypto.*;37import javax.crypto.spec.*;3839public class ByteBuffers {4041public static void main(String[] args) throws Exception {42Provider p = Security.getProvider("SunJCE");43Random random = new Random();44int n = 10 * 1024;45byte[] t = new byte[n];46random.nextBytes(t);4748byte[] keyBytes = new byte[8];49random.nextBytes(keyBytes);50SecretKey key = new SecretKeySpec(keyBytes, "DES");5152Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");53cipher.init(Cipher.ENCRYPT_MODE, key);5455byte[] outBytes = cipher.doFinal(t);5657// create ByteBuffers for input (i1, i2, i3) and fill them58ByteBuffer i0 = ByteBuffer.allocate(n + 256);59i0.position(random.nextInt(256));60i0.limit(i0.position() + n);61ByteBuffer i1 = i0.slice();62i1.put(t);6364ByteBuffer i2 = ByteBuffer.allocateDirect(t.length);65i2.put(t);6667i1.clear();68ByteBuffer i3 = i1.asReadOnlyBuffer();6970ByteBuffer o0 = ByteBuffer.allocate(n + 512);71o0.position(random.nextInt(256));72o0.limit(o0.position() + n + 256);73ByteBuffer o1 = o0.slice();7475ByteBuffer o2 = ByteBuffer.allocateDirect(t.length + 256);7677crypt(cipher, i1, o1, outBytes, random);78crypt(cipher, i2, o1, outBytes, random);79crypt(cipher, i3, o1, outBytes, random);80crypt(cipher, i1, o2, outBytes, random);81crypt(cipher, i2, o2, outBytes, random);82crypt(cipher, i3, o2, outBytes, random);8384System.out.println("All tests passed");85}8687private static void crypt(Cipher cipher, ByteBuffer in, ByteBuffer out, byte[] outBytes, Random random) throws Exception {88in.clear();89out.clear();90out.put(new byte[out.remaining()]);91out.clear();92int lim = in.limit();93in.limit(random.nextInt(lim));94cipher.update(in, out);95if (in.hasRemaining()) {96throw new Exception("Buffer not consumed");97}98in.limit(lim);99cipher.doFinal(in, out);100if (in.hasRemaining()) {101throw new Exception("Buffer not consumed");102}103out.flip();104byte[] b = new byte[out.remaining()];105out.get(b);106if (Arrays.equals(outBytes, b) == false) {107throw new Exception("Encryption output mismatch");108}109}110}111112113