1 /*
   2  * Copyright (c) 2014, 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 /**
  25  * @test
  26  * @bug 8042990
  27  * @summary Testing some auxilary functions from the
  28  *          sun.security.pkcs11.wrapper package
  29  */
  30 
  31 import java.util.Arrays;
  32 import java.util.Random;
  33 import sun.security.pkcs11.wrapper.Functions;
  34 
  35 public class TestFun {
  36     private static final Random random = new Random();
  37 
  38     public static void main(String[] args) throws Throwable {
  39         for (int i = 0; i != 128; ++i) {
  40             long longVal = random.nextLong();
  41             String s1 = Functions.toFullHexString(longVal);
  42             String s2 = String.format("%016X", longVal);
  43             if (! s1.equals(s2))
  44                 throw new RuntimeException("Hex representations of " +
  45                         longVal + " aren't equal: '" + s1 + "' != '" +
  46                         s2 + "'");
  47 
  48             int intVal = random.nextInt();
  49             String s3 = Functions.toFullHexString(intVal);
  50             String s4 = String.format("%08X", intVal);
  51             if (! s3.equals(s4))
  52                 throw new RuntimeException("Hex representations of " +
  53                         intVal + " aren't equal: '" + s3 + "' != '" +
  54                         s4 + "'");
  55 
  56             byte[] bytes = new byte[random.nextInt(32)];
  57             String s5 = Functions.toHexString(bytes);
  58             String s6 = "";
  59             for (byte b : bytes)
  60                 s6 += String.format("%02x", (int)b & 0xff);
  61             if (! s5.equals(s6))
  62                 throw new RuntimeException("Hex representations of " +
  63                         Arrays.asList(bytes) + " aren't equal: '" + s5 +
  64                         "' != '" + s6 + "'");
  65         }
  66     }
  67 }