Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/CICO/CICOChainingTest.java
41155 views
1
/*
2
* Copyright (c) 2007, 2015, 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
import java.io.ByteArrayInputStream;
25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.OutputStream;
28
import java.io.PipedInputStream;
29
import java.io.PipedOutputStream;
30
import java.util.Arrays;
31
import javax.crypto.CipherInputStream;
32
import javax.crypto.CipherOutputStream;
33
34
/*
35
* @test
36
* @bug 8048604
37
* @summary This test verifies the assertion "The chaining feature of
38
* Filter streams should be supported." for feature "CipherInputStream &
39
* CipherOutputStream"
40
* @run main CICOChainingTest
41
*/
42
public class CICOChainingTest {
43
/**
44
* Plain text length.
45
*/
46
private static final int PLAIN_TEXT_LENGTH = 200;
47
48
public static void main(String argv[]) throws Exception {
49
CICOChainingTest test = new CICOChainingTest();
50
test.chainTest(true);
51
test.chainTest(false);
52
}
53
54
/**
55
* Chain CipherInputStream/CipherOutputStream with other stream, encrypt
56
* the text and decrypt it, recovered text is supposed to be same as
57
* original text.
58
* @param useInt true if read byte by byte false if read with buffer.
59
* @throws IOException any I/O operation failed.
60
*/
61
public void chainTest(boolean useInt) throws IOException {
62
byte[] plainText = TestUtilities.generateBytes(PLAIN_TEXT_LENGTH);
63
byte[] recoveredText = new byte[plainText.length];
64
// Do initialization
65
try (MyNullCipherInputStream ciInput1 = new MyNullCipherInputStream(
66
new ByteArrayInputStream(plainText));
67
PipedOutputStream piOut = new PipedOutputStream();
68
MyNullCipherInputStream ciInput2 = new MyNullCipherInputStream(
69
new PipedInputStream(piOut));
70
MyNullCipherOutputStream ciOut = new MyNullCipherOutputStream(
71
piOut);) {
72
if (useInt) {
73
int buffer = ciInput1.read();
74
while (buffer != -1) {
75
piOut.write(buffer);
76
buffer = ciInput1.read();
77
}
78
} else {
79
byte[] buffer = new byte[20];
80
int len = ciInput1.read(buffer);
81
while (len != -1) {
82
ciOut.write(buffer, 0, len);
83
len = ciInput1.read(buffer);
84
}
85
}
86
ciOut.flush();
87
piOut.flush();
88
// Get the output
89
ciInput2.read(recoveredText);
90
if (ciInput2.available() > 0) {
91
throw new RuntimeException("Expected no data from ciInput2, but"
92
+ " ciInput2.available() = " + ciInput2.available());
93
}
94
}
95
// Verify output is same to input.
96
if (!Arrays.equals(plainText, recoveredText)) {
97
throw new RuntimeException("plainText:" + new String(plainText)
98
+ " recoveredText:" + new String(recoveredText)
99
+ " Test failed due to result compare fail");
100
}
101
}
102
}
103
104
class MyNullCipherInputStream extends CipherInputStream {
105
106
public MyNullCipherInputStream(InputStream is) {
107
super(is);
108
}
109
}
110
111
class MyNullCipherOutputStream extends CipherOutputStream {
112
113
public MyNullCipherOutputStream(OutputStream os) {
114
super(os);
115
}
116
}
117
118