Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/util/DerValue/BadValue.java
38853 views
1
/*
2
* Copyright (c) 2009, 2019, 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 6864911
27
* @summary ASN.1/DER input stream parser needs more work
28
*/
29
30
import java.io.*;
31
import sun.security.util.*;
32
import sun.misc.IOUtils;
33
34
public class BadValue {
35
36
public static void main(String[] args) throws Exception {
37
38
// Test IOUtils.
39
40
// We have 4 bytes
41
InputStream in = new ByteArrayInputStream(new byte[10]);
42
byte[] bs = IOUtils.readExactlyNBytes(in, 4);
43
if (bs.length != 4 || in.available() != 6) {
44
throw new Exception("First read error");
45
}
46
// But only 6 left
47
bs = IOUtils.readNBytes(in, 10);
48
if (bs.length != 6 || in.available() != 0) {
49
throw new Exception("Second read error");
50
}
51
// MAX length results in exception
52
in = new ByteArrayInputStream(new byte[10]);
53
try {
54
bs = IOUtils.readExactlyNBytes(in, Integer.MAX_VALUE);
55
throw new Exception("No exception on MAX_VALUE length");
56
} catch (EOFException ex) {
57
// this is expected
58
}
59
// -1 length results in exception
60
in = new ByteArrayInputStream(new byte[10]);
61
try {
62
bs = IOUtils.readExactlyNBytes(in, -1);
63
throw new Exception("No exception on -1 length");
64
} catch (IOException ex) {
65
// this is expected
66
}
67
68
// 20>10, readAll means failure
69
in = new ByteArrayInputStream(new byte[10]);
70
try {
71
bs = IOUtils.readExactlyNBytes(in, 20);
72
throw new Exception("No exception on EOF");
73
} catch (EOFException e) {
74
// OK
75
}
76
int bignum = 10 * 1024 * 1024;
77
bs = IOUtils.readExactlyNBytes(new SuperSlowStream(bignum), bignum);
78
if (bs.length != bignum) {
79
throw new Exception("Read returned small array");
80
}
81
82
// Test DerValue
83
byte[] input = {0x04, (byte)0x84, 0x40, 0x00, 0x42, 0x46, 0x4b};
84
try {
85
new DerValue(new ByteArrayInputStream(input));
86
} catch (IOException ioe) {
87
// This is OK
88
}
89
}
90
}
91
92
/**
93
* An InputStream contains a given number of bytes, but only returns one byte
94
* per read.
95
*/
96
class SuperSlowStream extends InputStream {
97
private int p;
98
/**
99
* @param Initial capacity
100
*/
101
public SuperSlowStream(int capacity) {
102
p = capacity;
103
}
104
@Override
105
public int read() throws IOException {
106
if (p > 0) {
107
p--;
108
return 0;
109
} else {
110
return -1;
111
}
112
}
113
@Override
114
public int read(byte b[], int off, int len) throws IOException {
115
if (len == 0) return 0;
116
if (p > 0) {
117
p--;
118
b[off] = 0;
119
return 1;
120
} else {
121
return -1;
122
}
123
}
124
}
125
126