jdk/test/java/security/Provider/DefaultProviderList.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 38949 efeb06a73871
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
38454
f196252337c7 8157469: DefaultProviderList.java fails with no provider class apple.security.AppleProvider found
rhalade
parents: 36511
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    26
 * @bug 7191662 8157469 8157489
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @summary Ensure non-java.base providers can be found by ServiceLoader
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
 * @author Valerie Peng
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
38454
f196252337c7 8157469: DefaultProviderList.java fails with no provider class apple.security.AppleProvider found
rhalade
parents: 36511
diff changeset
    31
import java.security.Provider;
f196252337c7 8157469: DefaultProviderList.java fails with no provider class apple.security.AppleProvider found
rhalade
parents: 36511
diff changeset
    32
import java.security.Security;
f196252337c7 8157469: DefaultProviderList.java fails with no provider class apple.security.AppleProvider found
rhalade
parents: 36511
diff changeset
    33
import java.util.Arrays;
f196252337c7 8157469: DefaultProviderList.java fails with no provider class apple.security.AppleProvider found
rhalade
parents: 36511
diff changeset
    34
import java.util.Iterator;
f196252337c7 8157469: DefaultProviderList.java fails with no provider class apple.security.AppleProvider found
rhalade
parents: 36511
diff changeset
    35
import java.util.ServiceLoader;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
public class DefaultProviderList {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
    public static void main(String[] args) throws Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
        Provider[] defaultProvs = Security.getProviders();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
        System.out.println("Providers: " + Arrays.asList(defaultProvs));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
        System.out.println();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        ServiceLoader<Provider> sl = ServiceLoader.load(Provider.class);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        boolean failed = false;
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    46
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    47
        Module baseMod = Object.class.getModule();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    48
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    49
        // Test#1: check that all non-base security providers can be found
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    50
        // through ServiceLoader
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        for (Provider p : defaultProvs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            String pName = p.getName();
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    53
            Class pClass = p.getClass();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    54
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    55
            if (pClass.getModule() != baseMod) {
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    56
                String pClassName = pClass.getName();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    57
                Iterator<Provider> provIter = sl.iterator();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    58
                boolean found = false;
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    59
                while (provIter.hasNext()) {
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    60
                    Provider pFromSL = provIter.next();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    61
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    62
                    // check for match by class name because PKCS11 provider
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    63
                    // will have a different name after being configured.
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    64
                    if (pFromSL.getClass().getName().equals(pClassName)) {
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    65
                        found = true;
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    66
                        System.out.println("SL found provider " + pName);
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    67
                        break;
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    68
                    }
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    69
                }
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    70
                if (!found) {
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    71
                    failed = true;
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    72
                    System.out.println("Error: SL cannot find provider " +
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    73
                        pName);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            }
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    76
        }
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    77
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    78
        // Test#2: check that all security providers found through ServiceLoader
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    79
        // are not from base module
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    80
        Iterator<Provider> provIter = sl.iterator();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    81
        while (provIter.hasNext()) {
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    82
            Provider pFromSL = provIter.next();
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    83
            if (pFromSL.getClass().getModule() == baseMod) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                failed = true;
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    85
                System.out.println("Error: base provider " +
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    86
                    pFromSL.getName() + " loaded by SL");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
        }
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    89
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        if (!failed) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
            System.out.println("Test Passed");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
        } else {
38949
efeb06a73871 8157489: AppleProvider in java.base/macosx/classes/module-info.java.extra
valeriep
parents: 38454
diff changeset
    93
            throw new Exception("One or more tests failed");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
}