Skip to content

Commit dbf4f06

Browse files
committed
8349151: Refactor test/java/security/cert/CertificateFactory/slowstream.sh to java test
Backport-of: fbc12be73217315d66792b6ad3faacab133bb9d5
1 parent 2a0ee4d commit dbf4f06

File tree

2 files changed

+64
-75
lines changed

2 files changed

+64
-75
lines changed
Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -21,31 +21,74 @@
2121
* questions.
2222
*/
2323

24-
import java.io.*;
25-
import java.security.cert.*;
24+
/*
25+
* @test
26+
* @bug 6813340
27+
* @summary X509Factory should not depend on is.available()==0
28+
*/
2629

27-
class SlowStreamReader {
30+
import java.io.File;
31+
import java.io.FileInputStream;
32+
import java.io.PipedInputStream;
33+
import java.io.PipedOutputStream;
34+
import java.security.cert.CertificateFactory;
35+
import java.util.concurrent.atomic.AtomicBoolean;
2836

37+
public class SlowStream {
2938
public static void main(String[] args) throws Exception {
30-
CertificateFactory factory = CertificateFactory.getInstance("X.509");
31-
if (factory.generateCertificates(System.in).size() != 5) {
32-
throw new Exception("Not all certs read");
33-
}
34-
}
35-
}
39+
final var outputStream = new PipedOutputStream();
40+
final var inputStream = new PipedInputStream(outputStream);
3641

37-
class SlowStreamWriter {
38-
public static void main(String[] args) throws Exception {
39-
for (int i=0; i<5; i++) {
40-
FileInputStream fin = new FileInputStream(new File(new File(
41-
System.getProperty("test.src", "."), "openssl"), "pem"));
42-
byte[] buffer = new byte[4096];
43-
while (true) {
44-
int len = fin.read(buffer);
45-
if (len < 0) break;
46-
System.out.write(buffer, 0, len);
42+
final var failed = new AtomicBoolean(false);
43+
44+
final var writer = new Thread(() -> {
45+
try {
46+
for (int i = 0; i < 5; i++) {
47+
try (final var fin = new FileInputStream(new File(
48+
new File(System.getProperty("test.src", "."),
49+
"openssl"),
50+
"pem"))) {
51+
52+
fin.transferTo(outputStream);
53+
54+
Thread.sleep(2000);
55+
}
56+
}
57+
outputStream.close();
58+
} catch (final Exception e) {
59+
System.out.println("Writer Thread Error: ");
60+
e.printStackTrace(System.out);
61+
failed.set(true);
62+
}
63+
});
64+
65+
final var reader = new Thread(() -> {
66+
try {
67+
final var factory = CertificateFactory.getInstance("X.509");
68+
final var numOfCerts = factory.generateCertificates(inputStream).size();
69+
if (numOfCerts != 5) {
70+
throw new Exception(
71+
String.format("Not all certs read. %d found 5 expected", numOfCerts)
72+
);
73+
}
74+
inputStream.close();
75+
} catch (final Exception e) {
76+
System.out.println("Reader Thread Error: ");
77+
e.printStackTrace(System.out);
78+
failed.set(true);
4779
}
48-
Thread.sleep(2000);
80+
});
81+
82+
writer.start();
83+
reader.start();
84+
85+
writer.join();
86+
reader.join();
87+
88+
if (failed.get()) {
89+
throw new RuntimeException(
90+
"The test failed, please check the reader and writer threads output"
91+
);
4992
}
5093
}
5194
}

test/jdk/java/security/cert/CertificateFactory/slowstream.sh

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)