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