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 8038436
  27  * @summary Test for changes in 8038436
  28  * @compile -XDignore.symbol.file Bug8038436.java
  29  * @run main/othervm Bug8038436 -Djava.ext.dirs=foo security
  30  * @run main/othervm Bug8038436 -Djava.locale.providers=JRE availlocs
  31  */
  32 
  33 import java.security.*;
  34 import java.text.*;
  35 import java.util.*;
  36 import java.util.stream.*;
  37 import sun.util.locale.provider.*;
  38 
  39 public class Bug8038436 {
  40     public static void main(String[] args) {
  41         switch (args[1]) {
  42         case "security":
  43             securityTests();
  44             break;
  45         case "availlocs":
  46             availableLocalesTests();
  47             break;
  48         default:
  49             throw new RuntimeException("no test was specified.");
  50         }
  51     }
  52 
  53     private static void securityTests() {
  54         Policy.setPolicy(new MyPolicy());
  55         System.setSecurityManager(new SecurityManager());
  56 
  57         /*
  58          * Test for AccessClassInPackage security exception. Confirms that
  59          * exeption won't be thrown if an application sets a Permission that 
  60          * does not allow any RuntimePermission, on loading LocaleDataMetaInfo
  61          * from jdk.localedata module.
  62          */
  63         System.out.println(new Formatter(Locale.JAPAN).format("%1$tB %1$te, %1$tY",
  64                            new GregorianCalendar()));
  65 
  66         /*
  67          * Check only English/ROOT locales are returned if the jdk.localedata
  68          * module is not installed (implied by "java.ext.dirs" set to "foo").
  69          */
  70         if (Arrays.asList(Locale.getAvailableLocales())
  71                 .stream()
  72                 .anyMatch(l -> l != Locale.ROOT && l.getLanguage() != "en")) {
  73             throw new RuntimeException("non English locale(s) included in available locales");   
  74         }
  75     }
  76 
  77 
  78     static class MyPolicy extends Policy {
  79         final PermissionCollection perms = new Permissions();
  80 
  81         MyPolicy() {
  82             // allows no RuntimePermission
  83         }
  84 
  85         public PermissionCollection getPermissions(ProtectionDomain domain) {
  86             return perms;
  87         }
  88 
  89         public PermissionCollection getPermissions(CodeSource codesource) {
  90             return perms;
  91         }
  92 
  93         public boolean implies(ProtectionDomain domain, Permission perm) {
  94             return perms.implies(perm);
  95         }
  96     }
  97 
  98     static final String[] bipLocs = ("ar, ar-JO, ar-LB, ar-SY, be, be-BY, bg, " +
  99         "bg-BG, ca, ca-ES, cs, cs-CZ, da, da-DK, de, de-AT, de-CH, de-DE, " +
 100         "de-LU, el, el-CY, el-GR, en, en-AU, en-CA, en-GB, en-IE, en-IN, " +
 101         "en-MT, en-NZ, en-PH, en-SG, en-US, en-ZA, es, es-AR, es-BO, es-CL, " +
 102         "es-CO, es-CR, es-DO, es-EC, es-ES, es-GT, es-HN, es-MX, es-NI, " +
 103         "es-PA, es-PE, es-PR, es-PY, es-SV, es-US, es-UY, es-VE, et, et-EE, " +
 104         "fi, fi-FI, fr, fr-BE, fr-CA, fr-CH, fr-FR, ga, ga-IE, he, he-IL, " +
 105         "hi-IN, hr, hr-HR, hu, hu-HU, id, id-ID, is, is-IS, it, it-CH, it-IT, " +
 106         "ja, ja-JP, ko, ko-KR, lt, lt-LT, lv, lv-LV, mk, mk-MK, ms, ms-MY, mt, " +
 107         "mt-MT, nl, nl-BE, nl-NL, no, no-NO, no-NO, pl, pl-PL, pt, pt-BR, " +
 108         "pt-PT, ro, ro-RO, ru, ru-RU, sk, sk-SK, sl, sl-SI, sq, sq-AL, sr, " +
 109         "sr-BA, sr-CS, sr-Latn, sr-Latn-ME, sr-ME, sr-RS, sv, sv-SE, th, th-TH, " +
 110         "tr, tr-TR, uk, uk-UA, und, vi, vi-VN, zh, zh-CN, zh-HK, zh-Hans-CN, " +
 111         "zh-Hans-SG, zh-Hant-HK, zh-Hant-TW, zh-SG, zh-TW, ").split(",\\s*");
 112     static final String[] dfpLocs = bipLocs;
 113     static final String[] datefspLocs = bipLocs;
 114     static final String[] decimalfspLocs = bipLocs;
 115     static final String[] calnpLocs = bipLocs;
 116     static final String[] cpLocs = ("ar, be, bg, ca, cs, da, el, es, et, fi, " +
 117         "fr, he, hi, hr, hu, is, ja, ko, lt, lv, mk, no, pl, ro, ru, sk, sl, " +
 118         "sq, sr, sr-Latn, sv, th, tr, uk, und, vi, zh, zh-HK, zh-Hant-HK, " +
 119         "zh-Hant-TW, zh-TW, ").split(",\\s*");
 120     static final String[] nfpLocs = ("ar, ar-AE, ar-BH, ar-DZ, ar-EG, ar-IQ, " +
 121         "ar-JO, ar-KW, ar-LB, ar-LY, ar-MA, ar-OM, ar-QA, ar-SA, ar-SD, ar-SY, " +
 122         "ar-TN, ar-YE, be, be-BY, bg, bg-BG, ca, ca-ES, cs, cs-CZ, da, da-DK, " +
 123         "de, de-AT, de-CH, de-DE, de-GR, de-LU, el, el-CY, el-GR, en, en-AU, " +
 124         "en-CA, en-GB, en-IE, en-IN, en-MT, en-NZ, en-PH, en-SG, en-US, en-ZA, " +
 125         "es, es-AR, es-BO, es-CL, es-CO, es-CR, es-CU, es-DO, es-EC, es-ES, " +
 126         "es-GT, es-HN, es-MX, es-NI, es-PA, es-PE, es-PR, es-PY, es-SV, es-US, " +
 127         "es-UY, es-VE, et, et-EE, fi, fi-FI, fr, fr-BE, fr-CA, fr-CH, fr-FR, " +
 128         "fr-LU, ga, ga-IE, he, he-IL, hi, hi-IN, hr, hr-HR, hu, hu-HU, id, " +
 129         "id-ID, is, is-IS, it, it-CH, it-IT, ja, ja-JP, " +
 130         "ja-JP-u-ca-japanese-x-lvariant-JP, ko, ko-KR, lt, lt-LT, lv, lv-LV, " +
 131         "mk, mk-MK, ms, ms-MY, mt, mt-MT, nb-NO, nl, nl-BE, nl-NL, nn-NO, " +
 132         "nn-NO, no, no-NO, pl, pl-PL, pt, pt-BR, pt-PT, ro, ro-RO, ru, ru-RU, " +
 133         "sk, sk-SK, sl, sl-SI, sq, sq-AL, sr, sr-BA, sr-CS, sr-Latn, " +
 134         "sr-Latn-BA, sr-Latn-ME, sr-Latn-RS, sr-ME, sr-RS, sv, sv-SE, th, " +
 135         "th-TH, th-TH-u-nu-thai-x-lvariant-TH, tr, tr-TR, uk, uk-UA, und, vi, " +
 136         "vi-VN, zh, zh-CN, zh-HK, zh-Hans-CN, zh-Hans-SG, zh-Hant-HK, " +
 137         "zh-Hant-TW, zh-SG, zh-TW, ").split(",\\s*");
 138     static final String[] currencynpLocs = ("ar-AE, ar-BH, ar-DZ, ar-EG, ar-IQ, " +
 139         "ar-JO, ar-KW, ar-LB, ar-LY, ar-MA, ar-OM, ar-QA, ar-SA, ar-SD, ar-SY, " +
 140         "ar-TN, ar-YE, be-BY, bg-BG, ca-ES, cs-CZ, da-DK, de, de-AT, de-CH, " +
 141         "de-DE, de-GR, de-LU, el-CY, el-GR, en-AU, en-CA, en-GB, en-IE, en-IN, " +
 142         "en-MT, en-NZ, en-PH, en-SG, en-US, en-ZA, es, es-AR, es-BO, es-CL, " +
 143         "es-CO, es-CR, es-CU, es-DO, es-EC, es-ES, es-GT, es-HN, es-MX, es-NI, " +
 144         "es-PA, es-PE, es-PR, es-PY, es-SV, es-US, es-UY, es-VE, et-EE, fi-FI, " +
 145         "fr, fr-BE, fr-CA, fr-CH, fr-FR, fr-LU, ga-IE, he-IL, hi-IN, hr-HR, " +
 146         "hu-HU, id-ID, is-IS, it, it-CH, it-IT, ja, ja-JP, ko, ko-KR, lt-LT, " +
 147         "lv-LV, mk-MK, ms-MY, mt-MT, nl-BE, nl-NL, no-NO, pl-PL, pt, pt-BR, " +
 148         "pt-PT, ro-RO, ru-RU, sk-SK, sl-SI, sq-AL, sr-BA, sr-CS, sr-Latn-BA, " +
 149         "sr-Latn-ME, sr-Latn-RS, sr-ME, sr-RS, sv, sv-SE, th-TH, tr-TR, uk-UA, " +
 150         "und, vi-VN, zh-CN, zh-HK, zh-Hans-CN, zh-Hans-SG, zh-Hant-HK, " +
 151         "zh-Hant-TW, zh-SG, zh-TW, ").split(",\\s*");
 152     static final String[] lnpLocs = ("ar, be, bg, ca, cs, da, de, el, el-CY, " +
 153         "en, en-MT, en-PH, en-SG, es, es-US, et, fi, fr, ga, he, hi, hr, hu, " +
 154         "id, is, it, ja, ko, lt, lv, mk, ms, mt, nl, no, no-NO, pl, pt, pt-BR, " +
 155         "pt-PT, ro, ru, sk, sl, sq, sr, sr-Latn, sv, th, tr, uk, und, vi, zh, " +
 156         "zh-HK, zh-Hans-SG, zh-Hant-HK, zh-Hant-TW, zh-SG, zh-TW, ").split(",\\s*");
 157     static final String[] tznpLocs = ("de, en, en-CA, en-GB, en-IE, es, fr, hi, " +
 158         "it, ja, ko, pt-BR, sv, und, zh-CN, zh-HK, zh-Hans-CN, zh-Hant-HK, " +
 159         "zh-Hant-TW, zh-TW, ").split(",\\s*");
 160     static final String[] caldpLocs = ("ar, be, bg, ca, cs, da, de, el, el-CY, " +
 161         "en, en-GB, en-IE, en-MT, es, es-ES, es-US, et, fi, fr, fr-CA, he, hi, " +
 162         "hr, hu, id-ID, is, it, ja, ko, lt, lv, mk, ms-MY, mt, mt-MT, nl, no, " +
 163         "pl, pt, pt-BR, pt-PT, ro, ru, sk, sl, sq, sr, sr-Latn-BA, sr-Latn-ME, " +
 164         "sr-Latn-RS, sv, th, tr, uk, und, vi, zh, ").split(",\\s*");
 165     static final String[] calpLocs = caldpLocs;
 166 
 167     /*
 168      * Validate whether JRE's *Providers return supported locales list based on
 169      * their actual resource bundle exsistence. The above golden data
 170      * are manually extracted, so they need to be updated if new locale
 171      * data resource bundle were added.
 172      */
 173     private static void availableLocalesTests() {
 174         LocaleProviderAdapter jre = LocaleProviderAdapter.forJRE();
 175 
 176         checkAvailableLocales("BreakIteratorProvider",
 177             jre.getBreakIteratorProvider().getAvailableLocales(), bipLocs);
 178         checkAvailableLocales("CollatorProvider",
 179             jre.getCollatorProvider().getAvailableLocales(), cpLocs);
 180         checkAvailableLocales("DateFormatProvider",
 181             jre.getDateFormatProvider().getAvailableLocales(), dfpLocs);
 182         checkAvailableLocales("DateFormatSymbolsProvider",
 183             jre.getDateFormatSymbolsProvider().getAvailableLocales(), datefspLocs);
 184         checkAvailableLocales("DecimalFormatSymbolsProvider",
 185             jre.getDecimalFormatSymbolsProvider().getAvailableLocales(), decimalfspLocs);
 186         checkAvailableLocales("NumberFormatProvider",
 187             jre.getNumberFormatProvider().getAvailableLocales(), nfpLocs);
 188         checkAvailableLocales("CurrencyNameProvider",
 189             jre.getCurrencyNameProvider().getAvailableLocales(), currencynpLocs);
 190         checkAvailableLocales("LocaleNameProvider", 
 191             jre.getLocaleNameProvider().getAvailableLocales(), lnpLocs);
 192         checkAvailableLocales("TimeZoneNameProvider",
 193             jre.getTimeZoneNameProvider().getAvailableLocales(), tznpLocs);
 194         checkAvailableLocales("CalendarDataProvider",
 195             jre.getCalendarDataProvider().getAvailableLocales(), caldpLocs);
 196         checkAvailableLocales("CalendarNameProvider",
 197             jre.getCalendarNameProvider().getAvailableLocales(), calnpLocs);
 198         checkAvailableLocales("CalendarProvider",   
 199             jre.getCalendarProvider().getAvailableLocales(), calpLocs);
 200     }
 201 
 202     private static void checkAvailableLocales(String testName, Locale[] got, String[] expected) {
 203         System.out.println("Testing available locales for " + testName);
 204         List<Locale> gotList = Arrays.asList(got).stream()
 205             .map(Locale::toLanguageTag)
 206             .sorted()
 207             .map(Locale::forLanguageTag)
 208             .collect(Collectors.toList());
 209         List<Locale> expectedList = Arrays.asList(expected).stream()
 210             .map(Locale::forLanguageTag)
 211             .collect(Collectors.toList());
 212 
 213         if (!gotList.equals(expectedList)) {
 214             throw new RuntimeException("\n" + gotList.toString() + "\n is not equal to \n" +
 215                                        expectedList.toString());
 216         }
 217     }
 218 }