1 /* 2 * Copyright (c) 2013, 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 package jdk.test.lib; 25 26 import java.io.File; 27 import static jdk.test.lib.Asserts.assertTrue; 28 import java.io.IOException; 29 import java.lang.reflect.Field; 30 import java.net.InetAddress; 31 import java.net.MalformedURLException; 32 import java.net.ServerSocket; 33 import java.net.URL; 34 import java.net.URLClassLoader; 35 import java.net.UnknownHostException; 36 import java.nio.file.Files; 37 import java.nio.file.Path; 38 import java.nio.file.Paths; 39 import java.util.ArrayList; 40 import java.util.Arrays; 41 import java.util.Collection; 42 import java.util.Collections; 43 import java.util.Iterator; 44 import java.util.List; 45 import java.util.Random; 46 import java.util.function.BooleanSupplier; 47 import java.util.concurrent.TimeUnit; 48 import java.util.function.Consumer; 49 import java.util.regex.Matcher; 50 import java.util.regex.Pattern; 51 import sun.misc.Unsafe; 52 53 /** 54 * Common library for various test helper functions. 55 */ 56 public final class Utils { 57 58 /** 59 * Returns the value of 'test.class.path' system property. 60 */ 61 public static final String TEST_CLASS_PATH = System.getProperty("test.class.path", "."); 62 63 /** 64 * Returns the sequence used by operating system to separate lines. 65 */ 66 public static final String NEW_LINE = System.getProperty("line.separator"); 67 68 /** 69 * Returns the value of 'test.vm.opts' system property. 70 */ 71 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim(); 72 73 /** 74 * Returns the value of 'test.java.opts' system property. 75 */ 76 public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim(); 77 78 /** 79 * Returns the value of 'test.src' system property. 80 */ 81 public static final String TEST_SRC = System.getProperty("test.src", "").trim(); 82 83 private static Unsafe unsafe = null; 84 85 /** 86 * Defines property name for seed value. 87 */ 88 public static final String SEED_PROPERTY_NAME = "jdk.test.lib.random.seed"; 89 90 /* (non-javadoc) 91 * Random generator with (or without) predefined seed. Depends on 92 * "jdk.test.lib.random.seed" property value. 93 */ 94 private static volatile Random RANDOM_GENERATOR; 95 96 /** 97 * Contains the seed value used for {@link java.util.Random} creation. 98 */ 99 public static final long SEED = Long.getLong(SEED_PROPERTY_NAME, new Random().nextLong()); 100 /** 101 * Returns the value of 'test.timeout.factor' system property 102 * converted to {@code double}. 103 */ 104 public static final double TIMEOUT_FACTOR; 105 static { 106 String toFactor = System.getProperty("test.timeout.factor", "1.0"); 107 TIMEOUT_FACTOR = Double.parseDouble(toFactor); 108 } 109 110 /** 111 * Returns the value of JTREG default test timeout in milliseconds 112 * converted to {@code long}. 113 */ 114 public static final long DEFAULT_TEST_TIMEOUT = TimeUnit.SECONDS.toMillis(120); 115 116 private Utils() { 117 // Private constructor to prevent class instantiation 118 } 119 120 /** 121 * Returns the list of VM options. 122 * 123 * @return List of VM options 124 */ 125 public static List<String> getVmOptions() { 126 return Arrays.asList(safeSplitString(VM_OPTIONS)); 127 } 128 129 /** 130 * Returns the list of VM options with -J prefix. 131 * 132 * @return The list of VM options with -J prefix 133 */ 134 public static List<String> getForwardVmOptions() { 135 String[] opts = safeSplitString(VM_OPTIONS); 136 for (int i = 0; i < opts.length; i++) { 137 opts[i] = "-J" + opts[i]; 138 } 139 return Arrays.asList(opts); 140 } 141 142 /** 143 * Returns the default JTReg arguments for a jvm running a test. 144 * This is the combination of JTReg arguments test.vm.opts and test.java.opts. 145 * @return An array of options, or an empty array if no options. 146 */ 147 public static String[] getTestJavaOpts() { 148 List<String> opts = new ArrayList<String>(); 149 Collections.addAll(opts, safeSplitString(VM_OPTIONS)); 150 Collections.addAll(opts, safeSplitString(JAVA_OPTIONS)); 151 return opts.toArray(new String[0]); 152 } 153 154 /** 155 * Returns the default JTReg arguments for a jvm running a test without 156 * options that matches regular expressions in {@code filters}. 157 * This is the combination of JTReg arguments test.vm.opts and test.java.opts. 158 * @param filters Regular expressions used to filter out options. 159 * @return An array of options, or an empty array if no options. 160 */ 161 public static String[] getFilteredTestJavaOpts(String... filters) { 162 String options[] = getTestJavaOpts(); 163 164 if (filters.length == 0) { 165 return options; 166 } 167 168 List<String> filteredOptions = new ArrayList<String>(options.length); 169 Pattern patterns[] = new Pattern[filters.length]; 170 for (int i = 0; i < filters.length; i++) { 171 patterns[i] = Pattern.compile(filters[i]); 172 } 173 174 for (String option : options) { 175 boolean matched = false; 176 for (int i = 0; i < patterns.length && !matched; i++) { 177 Matcher matcher = patterns[i].matcher(option); 178 matched = matcher.find(); 179 } 180 if (!matched) { 181 filteredOptions.add(option); 182 } 183 } 184 185 return filteredOptions.toArray(new String[filteredOptions.size()]); 186 } 187 188 /** 189 * Combines given arguments with default JTReg arguments for a jvm running a test. 190 * This is the combination of JTReg arguments test.vm.opts and test.java.opts 191 * @return The combination of JTReg test java options and user args. 192 */ 193 public static String[] addTestJavaOpts(String... userArgs) { 194 List<String> opts = new ArrayList<String>(); 195 Collections.addAll(opts, getTestJavaOpts()); 196 Collections.addAll(opts, userArgs); 197 return opts.toArray(new String[0]); 198 } 199 200 /** 201 * Splits a string by white space. 202 * Works like String.split(), but returns an empty array 203 * if the string is null or empty. 204 */ 205 private static String[] safeSplitString(String s) { 206 if (s == null || s.trim().isEmpty()) { 207 return new String[] {}; 208 } 209 return s.trim().split("\\s+"); 210 } 211 212 /** 213 * @return The full command line for the ProcessBuilder. 214 */ 215 public static String getCommandLine(ProcessBuilder pb) { 216 StringBuilder cmd = new StringBuilder(); 217 for (String s : pb.command()) { 218 cmd.append(s).append(" "); 219 } 220 return cmd.toString(); 221 } 222 223 /** 224 * Returns the free port on the local host. 225 * The function will spin until a valid port number is found. 226 * 227 * @return The port number 228 * @throws InterruptedException if any thread has interrupted the current thread 229 * @throws IOException if an I/O error occurs when opening the socket 230 */ 231 public static int getFreePort() throws InterruptedException, IOException { 232 int port = -1; 233 234 while (port <= 0) { 235 Thread.sleep(100); 236 237 ServerSocket serverSocket = null; 238 try { 239 serverSocket = new ServerSocket(0); 240 port = serverSocket.getLocalPort(); 241 } finally { 242 serverSocket.close(); 243 } 244 } 245 246 return port; 247 } 248 249 /** 250 * Returns the name of the local host. 251 * 252 * @return The host name 253 * @throws UnknownHostException if IP address of a host could not be determined 254 */ 255 public static String getHostname() throws UnknownHostException { 256 InetAddress inetAddress = InetAddress.getLocalHost(); 257 String hostName = inetAddress.getHostName(); 258 259 assertTrue((hostName != null && !hostName.isEmpty()), 260 "Cannot get hostname"); 261 262 return hostName; 263 } 264 265 /** 266 * Uses "jcmd -l" to search for a jvm pid. This function will wait 267 * forever (until jtreg timeout) for the pid to be found. 268 * @param key Regular expression to search for 269 * @return The found pid. 270 */ 271 public static int waitForJvmPid(String key) throws Throwable { 272 final long iterationSleepMillis = 250; 273 System.out.println("waitForJvmPid: Waiting for key '" + key + "'"); 274 System.out.flush(); 275 while (true) { 276 int pid = tryFindJvmPid(key); 277 if (pid >= 0) { 278 return pid; 279 } 280 Thread.sleep(iterationSleepMillis); 281 } 282 } 283 284 /** 285 * Searches for a jvm pid in the output from "jcmd -l". 286 * 287 * Example output from jcmd is: 288 * 12498 sun.tools.jcmd.JCmd -l 289 * 12254 /tmp/jdk8/tl/jdk/JTwork/classes/com/sun/tools/attach/Application.jar 290 * 291 * @param key A regular expression to search for. 292 * @return The found pid, or -1 if not found. 293 * @throws Exception If multiple matching jvms are found. 294 */ 295 public static int tryFindJvmPid(String key) throws Throwable { 296 OutputAnalyzer output = null; 297 try { 298 JDKToolLauncher jcmdLauncher = JDKToolLauncher.create("jcmd"); 299 jcmdLauncher.addToolArg("-l"); 300 output = ProcessTools.executeProcess(jcmdLauncher.getCommand()); 301 output.shouldHaveExitValue(0); 302 303 // Search for a line starting with numbers (pid), follwed by the key. 304 Pattern pattern = Pattern.compile("([0-9]+)\\s.*(" + key + ").*\\r?\\n"); 305 Matcher matcher = pattern.matcher(output.getStdout()); 306 307 int pid = -1; 308 if (matcher.find()) { 309 pid = Integer.parseInt(matcher.group(1)); 310 System.out.println("findJvmPid.pid: " + pid); 311 if (matcher.find()) { 312 throw new Exception("Found multiple JVM pids for key: " + key); 313 } 314 } 315 return pid; 316 } catch (Throwable t) { 317 System.out.println(String.format("Utils.findJvmPid(%s) failed: %s", key, t)); 318 throw t; 319 } 320 } 321 322 /** 323 * Return the contents of the named file as a single String, 324 * or null if not found. 325 * @param filename name of the file to read 326 * @return String contents of file, or null if file not found. 327 * @throws IOException 328 * if an I/O error occurs reading from the file or a malformed or 329 * unmappable byte sequence is read 330 */ 331 public static String fileAsString(String filename) throws IOException { 332 Path filePath = Paths.get(filename); 333 if (!Files.exists(filePath)) return null; 334 return new String(Files.readAllBytes(filePath)); 335 } 336 337 /** 338 * @return Unsafe instance. 339 */ 340 public static synchronized Unsafe getUnsafe() { 341 if (unsafe == null) { 342 try { 343 Field f = Unsafe.class.getDeclaredField("theUnsafe"); 344 f.setAccessible(true); 345 unsafe = (Unsafe) f.get(null); 346 } catch (NoSuchFieldException | IllegalAccessException e) { 347 throw new RuntimeException("Unable to get Unsafe instance.", e); 348 } 349 } 350 return unsafe; 351 } 352 private static final char[] hexArray = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; 353 354 /** 355 * Returns hex view of byte array 356 * 357 * @param bytes byte array to process 358 * @return Space separated hexadecimal string representation of bytes 359 */ 360 361 public static String toHexString(byte[] bytes) { 362 char[] hexView = new char[bytes.length * 3]; 363 int i = 0; 364 for (byte b : bytes) { 365 hexView[i++] = hexArray[(b >> 4) & 0x0F]; 366 hexView[i++] = hexArray[b & 0x0F]; 367 hexView[i++] = ' '; 368 } 369 return new String(hexView); 370 } 371 372 /** 373 * Returns {@link java.util.Random} generator initialized with particular seed. 374 * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME} 375 * In case no seed is provided, the method uses a random number. 376 * The used seed printed to stdout. 377 * @return {@link java.util.Random} generator with particular seed. 378 */ 379 public static Random getRandomInstance() { 380 if (RANDOM_GENERATOR == null) { 381 synchronized (Utils.class) { 382 if (RANDOM_GENERATOR == null) { 383 RANDOM_GENERATOR = new Random(SEED); 384 System.out.printf("For random generator using seed: %d%n", SEED); 385 System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED); 386 } 387 } 388 } 389 return RANDOM_GENERATOR; 390 } 391 392 /** 393 * Returns random element of non empty collection 394 * 395 * @param <T> a type of collection element 396 * @param collection collection of elements 397 * @return random element of collection 398 * @throws IllegalArgumentException if collection is empty 399 */ 400 public static <T> T getRandomElement(Collection<T> collection) 401 throws IllegalArgumentException { 402 if (collection.isEmpty()) { 403 throw new IllegalArgumentException("Empty collection"); 404 } 405 Random random = getRandomInstance(); 406 int elementIndex = 1 + random.nextInt(collection.size() - 1); 407 Iterator<T> iterator = collection.iterator(); 408 while (--elementIndex != 0) { 409 iterator.next(); 410 } 411 return iterator.next(); 412 } 413 414 /** 415 * Wait for condition to be true 416 * 417 * @param condition, a condition to wait for 418 */ 419 public static final void waitForCondition(BooleanSupplier condition) { 420 waitForCondition(condition, -1L, 100L); 421 } 422 423 /** 424 * Wait until timeout for condition to be true 425 * 426 * @param condition, a condition to wait for 427 * @param timeout a time in milliseconds to wait for condition to be true 428 * specifying -1 will wait forever 429 * @return condition value, to determine if wait was successful 430 */ 431 public static final boolean waitForCondition(BooleanSupplier condition, 432 long timeout) { 433 return waitForCondition(condition, timeout, 100L); 434 } 435 436 /** 437 * Wait until timeout for condition to be true for specified time 438 * 439 * @param condition, a condition to wait for 440 * @param timeout a time in milliseconds to wait for condition to be true, 441 * specifying -1 will wait forever 442 * @param sleepTime a time to sleep value in milliseconds 443 * @return condition value, to determine if wait was successful 444 */ 445 public static final boolean waitForCondition(BooleanSupplier condition, 446 long timeout, long sleepTime) { 447 long startTime = System.currentTimeMillis(); 448 while (!(condition.getAsBoolean() || (timeout != -1L 449 && ((System.currentTimeMillis() - startTime) > timeout)))) { 450 try { 451 Thread.sleep(sleepTime); 452 } catch (InterruptedException e) { 453 Thread.currentThread().interrupt(); 454 throw new Error(e); 455 } 456 } 457 return condition.getAsBoolean(); 458 } 459 460 /** 461 * Adjusts the provided timeout value for the TIMEOUT_FACTOR 462 * @param tOut the timeout value to be adjusted 463 * @return The timeout value adjusted for the value of "test.timeout.factor" 464 * system property 465 */ 466 public static long adjustTimeout(long tOut) { 467 return Math.round(tOut * Utils.TIMEOUT_FACTOR); 468 } 469 470 /** 471 * Ensures a requested class is loaded 472 * @param aClass class to load 473 */ 474 public static void ensureClassIsLoaded(Class<?> aClass) { 475 if (aClass == null) { 476 throw new Error("Requested null class"); 477 } 478 try { 479 Class.forName(aClass.getName(), /* initialize = */ true, 480 ClassLoader.getSystemClassLoader()); 481 } catch (ClassNotFoundException e) { 482 throw new Error("Class not found", e); 483 } 484 } 485 /** 486 * @param parent a class loader to be the parent for the returned one 487 * @return an UrlClassLoader with urls made of the 'test.class.path' jtreg 488 * property and with the given parent 489 */ 490 public static URLClassLoader getTestClassPathURLClassLoader(ClassLoader parent) { 491 URL[] urls = Arrays.stream(TEST_CLASS_PATH.split(File.pathSeparator)) 492 .map(Paths::get) 493 .map(Path::toUri) 494 .map(x -> { 495 try { 496 return x.toURL(); 497 } catch (MalformedURLException ex) { 498 throw new Error("Test issue. JTREG property" 499 + " 'test.class.path'" 500 + " is not defined correctly", ex); 501 } 502 }).toArray(URL[]::new); 503 return new URLClassLoader(urls, parent); 504 } 505 506 /** 507 * Runs runnable and checks that it throws expected exception. If exceptionException is null it means 508 * that we expect no exception to be thrown. 509 * @param runnable what we run 510 * @param expectedException expected exception 511 */ 512 public static void runAndCheckException(Runnable runnable, Class<? extends Throwable> expectedException) { 513 runAndCheckException(runnable, t -> { 514 if (t == null) { 515 if (expectedException != null) { 516 throw new AssertionError("Didn't get expected exception " + expectedException.getSimpleName()); 517 } 518 } else { 519 String message = "Got unexpected exception " + t.getClass().getSimpleName(); 520 if (expectedException == null) { 521 throw new AssertionError(message, t); 522 } else if (!expectedException.isAssignableFrom(t.getClass())) { 523 message += " instead of " + expectedException.getSimpleName(); 524 throw new AssertionError(message, t); 525 } 526 } 527 }); 528 } 529 530 /** 531 * Runs runnable and makes some checks to ensure that it throws expected exception. 532 * @param runnable what we run 533 * @param checkException a consumer which checks that we got expected exception and raises a new exception otherwise 534 */ 535 public static void runAndCheckException(Runnable runnable, Consumer<Throwable> checkException) { 536 try { 537 runnable.run(); 538 checkException.accept(null); 539 } catch (Throwable t) { 540 checkException.accept(t); 541 } 542 } 543 544 /** 545 * Converts to VM type signature 546 * 547 * @param type Java type to convert 548 * @return string representation of VM type 549 */ 550 public static String toJVMTypeSignature(Class<?> type) { 551 if (type.isPrimitive()) { 552 if (type == boolean.class) { 553 return "Z"; 554 } else if (type == byte.class) { 555 return "B"; 556 } else if (type == char.class) { 557 return "C"; 558 } else if (type == double.class) { 559 return "D"; 560 } else if (type == float.class) { 561 return "F"; 562 } else if (type == int.class) { 563 return "I"; 564 } else if (type == long.class) { 565 return "J"; 566 } else if (type == short.class) { 567 return "S"; 568 } else if (type == void.class) { 569 return "V"; 570 } else { 571 throw new Error("Unsupported type: " + type); 572 } 573 } 574 String result = type.getName().replaceAll("\\.", "/"); 575 if (!type.isArray()) { 576 return "L" + result + ";"; 577 } 578 return result; 579 } 580 } 581