Skip to content

8349151: Refactor test/java/security/cert/CertificateFactory/slowstream.sh to java test #3763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 64 additions & 21 deletions test/jdk/java/security/cert/CertificateFactory/SlowStream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,31 +21,74 @@
* questions.
*/

import java.io.*;
import java.security.cert.*;
/*
* @test
* @bug 6813340
* @summary X509Factory should not depend on is.available()==0
*/

class SlowStreamReader {
import java.io.File;
import java.io.FileInputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.security.cert.CertificateFactory;
import java.util.concurrent.atomic.AtomicBoolean;

public class SlowStream {
public static void main(String[] args) throws Exception {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
if (factory.generateCertificates(System.in).size() != 5) {
throw new Exception("Not all certs read");
}
}
}
final var outputStream = new PipedOutputStream();
final var inputStream = new PipedInputStream(outputStream);

class SlowStreamWriter {
public static void main(String[] args) throws Exception {
for (int i=0; i<5; i++) {
FileInputStream fin = new FileInputStream(new File(new File(
System.getProperty("test.src", "."), "openssl"), "pem"));
byte[] buffer = new byte[4096];
while (true) {
int len = fin.read(buffer);
if (len < 0) break;
System.out.write(buffer, 0, len);
final var failed = new AtomicBoolean(false);

final var writer = new Thread(() -> {
try {
for (int i = 0; i < 5; i++) {
try (final var fin = new FileInputStream(new File(
new File(System.getProperty("test.src", "."),
"openssl"),
"pem"))) {

fin.transferTo(outputStream);

Thread.sleep(2000);
}
}
outputStream.close();
} catch (final Exception e) {
System.out.println("Writer Thread Error: ");
e.printStackTrace(System.out);
failed.set(true);
}
});

final var reader = new Thread(() -> {
try {
final var factory = CertificateFactory.getInstance("X.509");
final var numOfCerts = factory.generateCertificates(inputStream).size();
if (numOfCerts != 5) {
throw new Exception(
String.format("Not all certs read. %d found 5 expected", numOfCerts)
);
}
inputStream.close();
} catch (final Exception e) {
System.out.println("Reader Thread Error: ");
e.printStackTrace(System.out);
failed.set(true);
}
Thread.sleep(2000);
});

writer.start();
reader.start();

writer.join();
reader.join();

if (failed.get()) {
throw new RuntimeException(
"The test failed, please check the reader and writer threads output"
);
}
}
}
54 changes: 0 additions & 54 deletions test/jdk/java/security/cert/CertificateFactory/slowstream.sh

This file was deleted.