Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/crypto/NullCipher/TestNPE.java
41149 views
1
/*
2
* Copyright (c) 2003, 2016, 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 4937853 8170876
27
* @summary Make sure normal calls of NullCipher does not throw NPE.
28
* @author Valerie Peng
29
* @key randomness
30
* @run main TestNPE
31
* @run main/othervm -Djava.security.debug=provider TestNPE
32
*/
33
import java.util.Arrays;
34
import java.security.AlgorithmParameters;
35
import java.security.Key;
36
import java.security.SecureRandom;
37
import java.security.cert.Certificate;
38
import java.security.spec.AlgorithmParameterSpec;
39
import javax.crypto.Cipher;
40
import javax.crypto.NullCipher;
41
import javax.crypto.spec.SecretKeySpec;
42
43
public class TestNPE {
44
private static byte[] BYTES = new byte[16];
45
static {
46
new SecureRandom().nextBytes(BYTES);
47
}
48
49
public static void main(String[] args) throws Exception {
50
NullCipher nc = new NullCipher();
51
// testing init(...)
52
nc.init(Cipher.ENCRYPT_MODE, (Certificate) null);
53
nc.init(Cipher.ENCRYPT_MODE, (Certificate) null, (SecureRandom) null);
54
nc.init(Cipher.ENCRYPT_MODE, (Key) null);
55
nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null);
56
nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null);
57
nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameterSpec) null,
58
(SecureRandom) null);
59
nc.init(Cipher.ENCRYPT_MODE, (Key) null, (AlgorithmParameters) null,
60
(SecureRandom) null);
61
nc.init(Cipher.ENCRYPT_MODE, (Key) null, (SecureRandom) null);
62
// testing getBlockSize()
63
if (nc.getBlockSize() != 1) {
64
throw new Exception("Error with getBlockSize()");
65
}
66
// testing update(...)
67
byte[] out = nc.update(BYTES);
68
if (!Arrays.equals(out, BYTES)) {
69
throw new Exception("Error with update(byte[])");
70
}
71
out = nc.update(BYTES, 0, BYTES.length);
72
if (!Arrays.equals(out, BYTES)) {
73
throw new Exception("Error with update(byte[], int, int)");
74
}
75
if (nc.update(BYTES, 0, BYTES.length, out) != BYTES.length) {
76
throw new Exception("Error with update(byte[], int, int, byte[])");
77
}
78
if (nc.update(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
79
throw new Exception(
80
"Error with update(byte[], int, int, byte[], int)");
81
}
82
// testing doFinal(...)
83
if (nc.doFinal() != null) {
84
throw new Exception("Error with doFinal()");
85
}
86
if (nc.doFinal(out, 0) != 0) {
87
throw new Exception("Error with doFinal(byte[], 0)");
88
}
89
out = nc.doFinal(BYTES);
90
if (!Arrays.equals(out, BYTES)) {
91
throw new Exception("Error with doFinal(byte[])");
92
}
93
out = nc.doFinal(BYTES, 0, BYTES.length);
94
if (!Arrays.equals(out, BYTES)) {
95
throw new Exception("Error with doFinal(byte[], int, int)");
96
}
97
if (nc.doFinal(BYTES, 0, BYTES.length, out) != BYTES.length) {
98
throw new Exception(
99
"Error with doFinal(byte[], int, int, byte[])");
100
}
101
if (nc.doFinal(BYTES, 0, BYTES.length, out, 0) != BYTES.length) {
102
throw new Exception(
103
"Error with doFinal(byte[], int, int, byte[], int)");
104
}
105
// testing getExemptionMechanism()
106
if (nc.getExemptionMechanism() != null) {
107
throw new Exception("Error with getExemptionMechanism()");
108
}
109
// testing getOutputSize(int)
110
if (nc.getOutputSize(5) != 5) {
111
throw new Exception("Error with getOutputSize()");
112
}
113
// testing getIV(), getParameters(), getAlgorithm(), etc.
114
if (nc.getIV() == null) { // should've been null;
115
// left it as is for backward-compatibility
116
throw new Exception("Error with getIV()");
117
}
118
if (nc.getParameters() != null) {
119
throw new Exception("Error with getParameters()");
120
}
121
if (nc.getAlgorithm() != null) {
122
throw new Exception("Error with getAlgorithm()");
123
}
124
if (nc.getProvider() != null) { // not associated with any provider
125
throw new Exception("Error with getProvider()");
126
}
127
System.out.println("Test Done");
128
}
129
}
130
131