test/jdk/sun/security/ssl/StatusStapling/java.base/sun/security/ssl/TestUtils.java
changeset 50768 68fa3d4026ea
parent 50767 356eaea05bf0
child 50769 1bf8f9840705
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
     1 /*
       
     2  * Copyright (c) 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 sun.security.ssl;
       
    25 
       
    26 import java.nio.ByteBuffer;
       
    27 import java.util.Arrays;
       
    28 import java.util.Map;
       
    29 
       
    30 public class TestUtils {
       
    31 
       
    32     // private constructor to prevent instantiation for this utility class
       
    33     private TestUtils() {
       
    34         throw new AssertionError();
       
    35     }
       
    36 
       
    37     public static void runTests(Map<String, TestCase> testList) {
       
    38         int testNo = 0;
       
    39         int numberFailed = 0;
       
    40         Map.Entry<Boolean, String> result;
       
    41 
       
    42         System.out.println("============ Tests ============");
       
    43         for (String testName : testList.keySet()) {
       
    44             System.out.println("Test " + ++testNo + ": " + testName);
       
    45             result = testList.get(testName).runTest();
       
    46             System.out.print("Result: " + (result.getKey() ? "PASS" : "FAIL"));
       
    47             System.out.println(" " +
       
    48                     (result.getValue() != null ? result.getValue() : ""));
       
    49             System.out.println("-------------------------------------------");
       
    50             if (!result.getKey()) {
       
    51                 numberFailed++;
       
    52             }
       
    53         }
       
    54 
       
    55         System.out.println("End Results: " + (testList.size() - numberFailed) +
       
    56                 " Passed" + ", " + numberFailed + " Failed.");
       
    57         if (numberFailed > 0) {
       
    58             throw new RuntimeException(
       
    59                     "One or more tests failed, see test output for details");
       
    60         }
       
    61     }
       
    62 
       
    63     public static void dumpBytes(byte[] data) {
       
    64         dumpBytes(ByteBuffer.wrap(data));
       
    65     }
       
    66 
       
    67     public static void dumpBytes(ByteBuffer data) {
       
    68         int i = 0;
       
    69 
       
    70         data.mark();
       
    71         while (data.hasRemaining()) {
       
    72             if (i % 16 == 0 && i != 0) {
       
    73                 System.out.print("\n");
       
    74             }
       
    75             System.out.print(String.format("%02X ", data.get()));
       
    76             i++;
       
    77         }
       
    78         System.out.print("\n");
       
    79         data.reset();
       
    80     }
       
    81 
       
    82     public static void valueCheck(byte[] array1, byte[] array2) {
       
    83         if (!Arrays.equals(array1, array2)) {
       
    84             throw new RuntimeException("Array mismatch");
       
    85         }
       
    86     }
       
    87 
       
    88     // Compares a range of bytes at specific offsets in each array
       
    89     public static void valueCheck(byte[] array1, byte[] array2, int skip1,
       
    90             int skip2, int length) {
       
    91         ByteBuffer buf1 = ByteBuffer.wrap(array1);
       
    92         ByteBuffer buf2 = ByteBuffer.wrap(array2);
       
    93 
       
    94         // Skip past however many bytes are requested in both buffers
       
    95         buf1.position(skip1);
       
    96         buf2.position(skip2);
       
    97 
       
    98         // Reset the limits on each buffer to account for the length
       
    99         buf1.limit(buf1.position() + length);
       
   100         buf2.limit(buf2.position() + length);
       
   101 
       
   102         if (!buf1.equals(buf2)) {
       
   103             throw new RuntimeException("Array range mismatch");
       
   104         }
       
   105     }
       
   106 
       
   107     // Concatenate 1 or more arrays
       
   108     public static byte[] gatherBuffers(byte[]... arrays) {
       
   109         int totalLength = 0;
       
   110         for (byte[] ar : arrays) {
       
   111             totalLength += ar != null ? ar.length : 0;
       
   112         }
       
   113 
       
   114         byte[] resultBuf = new byte[totalLength];
       
   115         int offset = 0;
       
   116         for (byte[] ar : arrays) {
       
   117             if (ar != null) {
       
   118                 System.arraycopy(ar, 0, resultBuf, offset, ar.length);
       
   119                 offset += ar.length;
       
   120             }
       
   121         }
       
   122         return resultBuf;
       
   123     }
       
   124 }