test/jdk/sun/security/pkcs11/PKCS11Test.java
changeset 48665 257d7610663f
parent 48608 1dab70e20292
child 50731 ed322b4cfe49
equal deleted inserted replaced
48664:ed014587f0e2 48665:257d7610663f
    29 import java.io.File;
    29 import java.io.File;
    30 import java.io.FileInputStream;
    30 import java.io.FileInputStream;
    31 import java.io.IOException;
    31 import java.io.IOException;
    32 import java.io.InputStreamReader;
    32 import java.io.InputStreamReader;
    33 import java.io.StringReader;
    33 import java.io.StringReader;
       
    34 import java.nio.charset.StandardCharsets;
    34 import java.security.AlgorithmParameters;
    35 import java.security.AlgorithmParameters;
    35 import java.security.InvalidAlgorithmParameterException;
    36 import java.security.InvalidAlgorithmParameterException;
    36 import java.security.KeyPairGenerator;
    37 import java.security.KeyPairGenerator;
    37 import java.security.NoSuchProviderException;
    38 import java.security.NoSuchProviderException;
    38 import java.security.Provider;
    39 import java.security.Provider;
   385     /* Read the library to find out the verison */
   386     /* Read the library to find out the verison */
   386     static void getNSSInfo() {
   387     static void getNSSInfo() {
   387         getNSSInfo(nss_library);
   388         getNSSInfo(nss_library);
   388     }
   389     }
   389 
   390 
       
   391     // Try to parse the version for the specified library.
       
   392     // Assuming the library contains either of the following patterns:
       
   393     // $Header: NSS <version>
       
   394     // Version: NSS <version>
       
   395     // Here, <version> stands for NSS version.
   390     static double getNSSInfo(String library) {
   396     static double getNSSInfo(String library) {
   391         // look for two types of headers in NSS libraries
   397         // look for two types of headers in NSS libraries
   392         String nssHeader1 = "$Header: NSS";
   398         String nssHeader1 = "$Header: NSS";
   393         String nssHeader2 = "Version: NSS";
   399         String nssHeader2 = "Version: NSS";
   394         boolean found = false;
   400         boolean found = false;
   415                         // between the reads.
   421                         // between the reads.
   416                         System.arraycopy(data, 900, data, 0, 100);
   422                         System.arraycopy(data, 900, data, 0, 100);
   417                         read = 100 + is.read(data, 100, 900);
   423                         read = 100 + is.read(data, 100, 900);
   418                     }
   424                     }
   419 
   425 
   420                     s = new String(data, 0, read);
   426                     s = new String(data, 0, read, StandardCharsets.US_ASCII);
   421                     i = s.indexOf(nssHeader1);
   427                     i = s.indexOf(nssHeader1);
   422                     if (i > 0 || (i = s.indexOf(nssHeader2)) > 0) {
   428                     if (i > 0 || (i = s.indexOf(nssHeader2)) > 0) {
   423                         found = true;
   429                         found = true;
   424                         // If the nssHeader is before 920 we can break, otherwise
   430                         // If the nssHeader is before 920 we can break, otherwise
   425                         // we may not have the whole header so do another read.  If
   431                         // we may not have the whole header so do another read.  If
   441             return nss_version;
   447             return nss_version;
   442         }
   448         }
   443 
   449 
   444         // the index after whitespace after nssHeader
   450         // the index after whitespace after nssHeader
   445         int afterheader = s.indexOf("NSS", i) + 4;
   451         int afterheader = s.indexOf("NSS", i) + 4;
   446         int nextSpaceIndex = s.indexOf(' ', afterheader);
   452         String version = String.valueOf(s.charAt(afterheader));
   447 
   453         for (char c = s.charAt(++afterheader);
   448         // If the next space is not found,
   454                 c == '.' || (c >= '0' && c <= '9');
   449         // it has to print the content for further investigation.
   455                 c = s.charAt(++afterheader)) {
   450         if (nextSpaceIndex == -1) {
   456             version += c;
   451             System.out.println("===== Content start =====");
   457         }
   452             System.out.println(s);
       
   453             System.out.println("===== Content end =====");
       
   454         }
       
   455 
       
   456         String version = s.substring(afterheader, nextSpaceIndex);
       
   457 
   458 
   458         // If a "dot dot" release, strip the extra dots for double parsing
   459         // If a "dot dot" release, strip the extra dots for double parsing
   459         String[] dot = version.split("\\.");
   460         String[] dot = version.split("\\.");
   460         if (dot.length > 2) {
   461         if (dot.length > 2) {
   461             version = dot[0]+"."+dot[1];
   462             version = dot[0]+"."+dot[1];
   466 
   467 
   467         // Convert to double for easier version value checking
   468         // Convert to double for easier version value checking
   468         try {
   469         try {
   469             nss_version = Double.parseDouble(version);
   470             nss_version = Double.parseDouble(version);
   470         } catch (NumberFormatException e) {
   471         } catch (NumberFormatException e) {
       
   472             System.out.println("===== Content start =====");
       
   473             System.out.println(s);
       
   474             System.out.println("===== Content end =====");
   471             System.out.println("Failed to parse lib" + library +
   475             System.out.println("Failed to parse lib" + library +
   472                     " version. Set to 0.0");
   476                     " version. Set to 0.0");
   473             e.printStackTrace();
   477             e.printStackTrace();
   474         }
   478         }
   475 
   479