jdk/test/sun/security/pkcs11/PKCS11Test.java
author vinnie
Mon, 05 Mar 2012 14:13:29 +0000
changeset 12042 88bd85bd793d
parent 12039 fe21bbf8f775
child 12294 f313586fc3cc
permissions -rw-r--r--
7151109: backout CR 6988842 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
12042
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
// common infrastructure for SunPKCS11 tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public abstract class PKCS11Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    // directory of the test source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    static final String BASE = System.getProperty("test.src", ".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static final char SEP = File.separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private final static String REL_CLOSED = "../../../../closed/sun/security/pkcs11".replace('/', SEP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // directory corresponding to BASE in the /closed hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final String CLOSED_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        // hack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        String absBase = new File(BASE).getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int k = absBase.indexOf(SEP + "test" + SEP + "sun" + SEP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if (k < 0) k = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        String p1 = absBase.substring(0, k + 6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        String p2 = absBase.substring(k + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        CLOSED_BASE = p1 + "closed" + p2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static String NSPR_PREFIX = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    static Provider getSunPKCS11(String config) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Class clazz = Class.forName("sun.security.pkcs11.SunPKCS11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Constructor cons = clazz.getConstructor(new Class[] {String.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Object obj = cons.newInstance(new Object[] {config});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return (Provider)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public abstract void main(Provider p) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private void premain(Provider p) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        long start = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        System.out.println("Running test with provider " + p.getName() + "...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        main(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        long stop = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        System.out.println("Completed test with provider " + p.getName() + " (" + (stop - start) + " ms).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static void main(PKCS11Test test) throws Exception {
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    75
        Provider[] oldProviders = Security.getProviders();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    76
        try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    77
            System.out.println("Beginning test run " + test.getClass().getName() + "...");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    78
            testDefault(test);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    79
            testNSS(test);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    80
            testDeimos(test);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    81
        } finally {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    82
            Provider[] newProviders = Security.getProviders();
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    83
            // Do not restore providers if nothing changed. This is especailly
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    84
            // useful for ./Provider/Login.sh, where a SecurityManager exists.
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    85
            if (oldProviders.length == newProviders.length) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    86
                boolean found = false;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    87
                for (int i = 0; i<oldProviders.length; i++) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    88
                    if (oldProviders[i] != newProviders[i]) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    89
                        found = true;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    90
                        break;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    91
                    }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    92
                }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    93
                if (!found) return;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    94
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    95
            for (Provider p: newProviders) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    96
                Security.removeProvider(p.getName());
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    97
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    98
            for (Provider p: oldProviders) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    99
                Security.addProvider(p);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   100
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   101
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void testDeimos(PKCS11Test test) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (new File("/opt/SUNWconn/lib/libpkcs11.so").isFile() == false ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            "true".equals(System.getProperty("NO_DEIMOS"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String base = getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        String p11config = base + SEP + "nss" + SEP + "p11-deimos.txt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Provider p = getSunPKCS11(p11config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        test.premain(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static void testDefault(PKCS11Test test) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // run test for default configured PKCS11 providers (if any)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if ("true".equals(System.getProperty("NO_DEFAULT"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        Provider[] providers = Security.getProviders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        for (int i = 0; i < providers.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            Provider p = providers[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (p.getName().startsWith("SunPKCS11-")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                test.premain(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static String PKCS11_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    private final static String PKCS11_REL_PATH = "sun/security/pkcs11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public static String getBase() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (PKCS11_BASE != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return PKCS11_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        File cwd = new File(System.getProperty("test.src", ".")).getCanonicalFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            File file = new File(cwd, "TEST.ROOT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (file.isFile()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            cwd = cwd.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (cwd == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                throw new Exception("Test root directory not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        PKCS11_BASE = new File(cwd, PKCS11_REL_PATH.replace('/', SEP)).getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return PKCS11_BASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public static String getNSSLibDir() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Properties props = System.getProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        String osName = props.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (osName.startsWith("Win")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            osName = "Windows";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            NSPR_PREFIX = "lib";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        String osid = osName + "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                + props.getProperty("os.arch") + "-" + props.getProperty("sun.arch.data.model");
12042
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   163
        String ostype = osMap.get(osid);
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   164
        if (ostype == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println("Unsupported OS, skipping: " + osid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return null;
12042
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   167
//          throw new Exception("Unsupported OS " + osid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
12042
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   169
        if (ostype.length() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.out.println("NSS not supported on this platform, skipping test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
12042
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   173
        String base = getBase();
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   174
        String libdir = base + SEP + "nss" + SEP + "lib" + SEP + ostype + SEP;
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   175
        System.setProperty("pkcs11test.nss.libdir", libdir);
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   176
        return libdir;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   179
    protected static void safeReload(String lib) throws Exception {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   180
        try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   181
            System.load(lib);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   182
        } catch (UnsatisfiedLinkError e) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   183
            if (e.getMessage().contains("already loaded")) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   184
                return;
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   185
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   186
        }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   187
    }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   188
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static boolean loadNSPR(String libdir) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        // load NSS softoken dependencies in advance to avoid resolver issues
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   191
        safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "nspr4"));
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   192
        safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plc4"));
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   193
        safeReload(libdir + System.mapLibraryName(NSPR_PREFIX + "plds4"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public static void testNSS(PKCS11Test test) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        String libdir = getNSSLibDir();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (libdir == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        String base = getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (loadNSPR(libdir) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        String libfile = libdir + System.mapLibraryName("softokn3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        String customDBdir = System.getProperty("CUSTOM_DB_DIR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        String dbdir = (customDBdir != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                customDBdir :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                base + SEP + "nss" + SEP + "db";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        // NSS always wants forward slashes for the config path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        dbdir = dbdir.replace('\\', '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        String customConfig = System.getProperty("CUSTOM_P11_CONFIG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        String customConfigName = System.getProperty("CUSTOM_P11_CONFIG_NAME", "p11-nss.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        String p11config = (customConfig != null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                customConfig :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                                base + SEP + "nss" + SEP + customConfigName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        System.setProperty("pkcs11test.nss.lib", libfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        System.setProperty("pkcs11test.nss.db", dbdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        Provider p = getSunPKCS11(p11config);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        test.premain(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private static final Map<String,String> osMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        osMap = new HashMap<String,String>();
12042
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   234
        osMap.put("SunOS-sparc-32", "solaris-sparc");
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   235
        osMap.put("SunOS-sparcv9-64", "solaris-sparcv9");
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   236
        osMap.put("SunOS-x86-32", "solaris-i586");
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   237
        osMap.put("SunOS-amd64-64", "solaris-amd64");
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   238
        osMap.put("Linux-i386-32", "linux-i586");
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   239
        osMap.put("Linux-amd64-64", "linux-amd64");
88bd85bd793d 7151109: backout CR 6988842
vinnie
parents: 12039
diff changeset
   240
        osMap.put("Windows-x86-32", "windows-i586");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private final static char[] hexDigits = "0123456789abcdef".toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public static String toString(byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            return "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        StringBuffer sb = new StringBuffer(b.length * 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        for (int i = 0; i < b.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            int k = b[i] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            sb.append(hexDigits[k >>> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            sb.append(hexDigits[k & 0xf]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    public static byte[] parse(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (s.equals("(null)")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            int n = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            StringReader r = new StringReader(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                int b1 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (b1 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                int b2 = nextNibble(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (b2 < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    throw new RuntimeException("Invalid string " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                int b = (b1 << 4) | b2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            throw new RuntimeException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private static int nextNibble(StringReader r) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            int ch = r.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (ch == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else if ((ch >= '0') && (ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return ch - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } else if ((ch >= 'a') && (ch <= 'f')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                return ch - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            } else if ((ch >= 'A') && (ch <= 'F')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return ch - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    <T> T[] concat(T[] a, T[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if ((b == null) || (b.length == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        T[] r = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), a.length + b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        System.arraycopy(a, 0, r, 0, a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        System.arraycopy(b, 0, r, a.length, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}