From bd1fc91136595a6ea032c684c9b036e876771804 Mon Sep 17 00:00:00 2001 From: Zdenek Zambersky Date: Wed, 31 Jan 2024 13:51:26 +0100 Subject: [PATCH] Check that tstclnt supports required option --- jtreg-wrappers/SysDepsProps.java | 109 +++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 21 deletions(-) diff --git a/jtreg-wrappers/SysDepsProps.java b/jtreg-wrappers/SysDepsProps.java index f322e39..de864ab 100644 --- a/jtreg-wrappers/SysDepsProps.java +++ b/jtreg-wrappers/SysDepsProps.java @@ -1,7 +1,11 @@ import java.io.InputStream; import java.io.IOException; +import java.io.BufferedReader; +import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.FileSystems; +import java.util.List; +import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import java.util.concurrent.Callable; @@ -13,30 +17,58 @@ Thread streamDiscarder(final InputStream is) { @Override public void run() { try { + while (is.read() >= 0) { /* discard */ }; + } catch (Exception e) { + e.printStackTrace(); + } finally { try { - while (is.read() >= 0) { /* discard */ }; - } finally { is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + }; + } + + Thread streamReader(final InputStream is, final List linesBuf) { + if (linesBuf == null) { + return streamDiscarder(is); + } + return new Thread() { + @Override + public void run() { + try (InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr)){ + String line; + while ((line = br.readLine()) != null) { + linesBuf.add(line); } } catch (Exception e) { e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } }; } - boolean checkOnPath(String cmd) { - ProcessBuilder pb = new ProcessBuilder(cmd, "--help"); + boolean runCmd(String[] command, List stdoutBuf, List stderrBuf) { + ProcessBuilder pb = new ProcessBuilder(command); Process p = null; - Thread outDiscarder = null; - Thread errDiscarder = null; + Thread outReader = null; + Thread errReader= null; int retVal = 0; try { p = pb.start(); // throws exception if program does not exist - outDiscarder = streamDiscarder(p.getInputStream()); - outDiscarder.start(); - errDiscarder = streamDiscarder(p.getErrorStream()); - errDiscarder.start(); + outReader = streamReader(p.getInputStream(), stdoutBuf); + outReader.start(); + errReader = streamReader(p.getErrorStream(), stderrBuf); + errReader.start(); p.getOutputStream().close(); p.waitFor(); return true; @@ -46,31 +78,66 @@ boolean checkOnPath(String cmd) { } } finally { try { - if (outDiscarder != null) { - outDiscarder.join(); + if (outReader != null) { + outReader.join(); } - if (errDiscarder != null) { - errDiscarder.join(); + if (errReader != null) { + errReader.join(); } } catch (InterruptedException ex) {} } return false; } + boolean checkOnPath(String cmd) { + return runCmd(new String[]{cmd, "--help"}, null, null); + } + + boolean containsString(List list, String str) { + for (String s : list) { + if (s.contains(str)) { + return true; + } + } + return false; + } + + boolean checkSupportsOption(String cmd, String option) { + List stdoutBuf = new ArrayList(); + List stderrBuf = new ArrayList(); + if (!runCmd(new String[]{cmd, "--help"}, stdoutBuf, stderrBuf)) { + return false; + } + return containsString(stdoutBuf, option) || containsString(stderrBuf, option); + } + boolean fileExists(String path) { return Files.exists(FileSystems.getDefault().getPath(path)); } - boolean checkGnutlsCli() { - return checkOnPath("gnutls-cli"); + String getTstclntCmd() { + String cmd = "/usr/lib64/nss/unsupported-tools/tstclnt"; + if (fileExists(cmd)) { + return cmd; + } + cmd = "/usr/lib/nss/unsupported-tools/tstclnt"; + if (fileExists(cmd)) { + return cmd; + } + cmd = "tstclnt"; + if (checkOnPath(cmd)) { + return cmd; + } + return null; } boolean checkTstclnt() { - if (fileExists("/usr/lib64/nss/unsupported-tools/tstclnt") - || fileExists("/usr/lib/nss/unsupported-tools/tstclnt")) { - return true; - } - return checkOnPath("tstclnt"); + String cmd = getTstclntCmd(); + return cmd == null ? false : checkSupportsOption(cmd, "-Q"); + } + + boolean checkGnutlsCli() { + return checkOnPath("gnutls-cli"); } @Override