1 /* 2 * Copyright (c) 2008, 2017, 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 4607272 27 * @summary tests tasks can be submitted to a channel group's thread pool. 28 * @library /test/lib bootlib 29 * @build PrivilegedThreadFactory Attack 30 * jdk.test.lib.util.JarUtils 31 * @run driver SetupJar 32 * @run main/othervm -Xbootclasspath/a:privileged.jar AsExecutor 33 */ 34 35 import java.nio.channels.AsynchronousChannelGroup; 36 import java.util.concurrent.CountDownLatch; 37 import java.util.concurrent.Executor; 38 import java.util.concurrent.Executors; 39 import java.util.concurrent.ThreadFactory; 40 41 public class AsExecutor { 42 43 public static void main(String[] args) throws Exception { 44 // create channel groups 45 ThreadFactory factory = new PrivilegedThreadFactory(); 46 AsynchronousChannelGroup group1 = AsynchronousChannelGroup 47 .withFixedThreadPool(5, factory); 48 AsynchronousChannelGroup group2 = AsynchronousChannelGroup 49 .withCachedThreadPool(Executors.newCachedThreadPool(factory), 0); 50 AsynchronousChannelGroup group3 = AsynchronousChannelGroup 51 .withThreadPool(Executors.newFixedThreadPool(10, factory)); 52 53 try { 54 // execute simple tasks 55 testSimpleTask(group1); 56 testSimpleTask(group2); 57 testSimpleTask(group3); 58 59 // install security manager and test again 60 System.setSecurityManager( new SecurityManager() ); 61 testSimpleTask(group1); 62 testSimpleTask(group2); 63 testSimpleTask(group3); 64 65 // attempt to execute tasks that run with only frames from boot 66 // class loader on the stack. 67 testAttackingTask(group1); 68 testAttackingTask(group2); 69 testAttackingTask(group3); 70 } finally { 71 group1.shutdown(); 72 group2.shutdown(); 73 group3.shutdown(); 74 } 75 } 76 77 static void testSimpleTask(AsynchronousChannelGroup group) throws Exception { 78 Executor executor = (Executor)group; 79 final CountDownLatch latch = new CountDownLatch(1); 80 executor.execute(new Runnable() { 81 public void run() { 82 latch.countDown(); 83 } 84 }); 85 latch.await(); 86 } 87 88 static void testAttackingTask(AsynchronousChannelGroup group) throws Exception { 89 Executor executor = (Executor)group; 90 Attack task = new Attack(); 91 executor.execute(task); 92 task.waitUntilDone(); 93 if (!task.failedDueToSecurityException()) 94 throw new RuntimeException("SecurityException expected"); 95 } 96 97 }