SetDefaultSecurityTest.java revision 8729:0242fce0f717
147558Sache/*
247558Sache * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
347558Sache * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4136644Sache *
547558Sache * This code is free software; you can redistribute it and/or modify it
647558Sache * under the terms of the GNU General Public License version 2 only, as
747558Sache * published by the Free Software Foundation.
847558Sache *
947558Sache * This code is distributed in the hope that it will be useful, but WITHOUT
1047558Sache * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1147558Sache * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1247558Sache * version 2 for more details (a copy is included in the LICENSE file that
1347558Sache * accompanied this code).
1447558Sache *
1547558Sache * You should have received a copy of the GNU General Public License version
1647558Sache * 2 along with this work; if not, write to the Free Software Foundation,
1747558Sache * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1858310Sache *
1958310Sache * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20136644Sache * or visit www.oracle.com if you need additional information or have any
21136644Sache * questions.
22136644Sache */
23136644Sache
24136644Sache/*
25136644Sache * @test
26136644Sache * @bug 8001029
27136644Sache * @summary Make sure that TimeZone.setDefault throws a SecurityException if the
2847558Sache *          security manager doesn't permit.
2947558Sache * @run main/othervm SetDefaultSecurityTest
3047558Sache */
3147558Sache
3247558Sacheimport java.util.SimpleTimeZone;
3347558Sacheimport java.util.TimeZone;
3447558Sache
3547558Sachepublic class SetDefaultSecurityTest {
3647558Sache    static final TimeZone NOWHERE = new SimpleTimeZone(Integer.MAX_VALUE, "Nowhere");
3747558Sache
3847558Sache    public static void main(String[] args)   {
3947558Sache        TimeZone defaultZone = TimeZone.getDefault();
4047558Sache
4147558Sache        // Make sure that TimeZone.setDefault works for trusted code
4247558Sache        TimeZone.setDefault(NOWHERE);
4347558Sache        if (!NOWHERE.equals(TimeZone.getDefault())) {
4447558Sache            new RuntimeException("TimeZone.setDefault doesn't work for trusted code.");
4547558Sache        }
4647558Sache        // Restore defaultZone
4775406Sache        TimeZone.setDefault(defaultZone);
4847558Sache        if (!defaultZone.equals(TimeZone.getDefault())) {
4947558Sache            new RuntimeException("TimeZone.setDefault doesn't restore defaultZone.");
5047558Sache        }
5147558Sache
5247558Sache        // Install a SecurityManager.
5347558Sache        System.setSecurityManager(new SecurityManager());
5447558Sache        try {
5575406Sache            TimeZone.setDefault(NOWHERE);
56157184Sache            throw new RuntimeException("TimeZone.setDefault doesn't throw a SecurityException.");
5747558Sache        } catch (SecurityException se) {
58136644Sache            // OK
59136644Sache        }
6047558Sache        TimeZone tz = TimeZone.getDefault();
61119610Sache        if (!defaultZone.equals(tz)) {
62119610Sache            throw new RuntimeException("Default TimeZone changed: " + tz);
63119610Sache        }
6447558Sache    }
6547558Sache}
6647558Sache