blob: c7c33be54de5516c3fbc2970de81c269ca483ee1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 Signature.update(ByteBuffer) method
28 * @author Andreas Sterbenz
29 */
30
31import java.util.*;
32import java.nio.*;
33
34import java.security.*;
35
36public 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 KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA", p);
46 kpg.initialize(512);
47 KeyPair kp = kpg.generateKeyPair();
48
49 Signature sig = Signature.getInstance("DSA", p);
50 sig.initSign(kp.getPrivate());
51 sig.update(t);
52 byte[] signature = sig.sign();
53
54 sig.initVerify(kp.getPublic());
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(sig, signature, b2, random);
64
65 // test 2: direct ByteBuffer
66 ByteBuffer b3 = ByteBuffer.allocateDirect(t.length);
67 b3.put(t);
68 b3.clear();
69 verify(sig, signature, b3, random);
70
71 // test 3: ByteBuffer without an accessible backing array
72 b2.clear();
73 ByteBuffer b4 = b2.asReadOnlyBuffer();
74 verify(sig, signature, b4, random);
75
76 System.out.println("All tests passed");
77 }
78
79 private static void verify(Signature sig, byte[] signature, ByteBuffer b, Random random) throws Exception {
80 int lim = b.limit();
81 b.limit(random.nextInt(lim));
82 sig.update(b);
83 if (b.hasRemaining()) {
84 throw new Exception("Buffer not consumed");
85 }
86 b.limit(lim);
87 sig.update(b);
88 if (b.hasRemaining()) {
89 throw new Exception("Buffer not consumed");
90 }
91 if (sig.verify(signature) == false) {
92 throw new Exception("Signature did not verify");
93 }
94 }
95}