1 /*
   2  * Copyright (c) 1998, 1999, 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 4146462
  27  * @summary Priority inversion prevents call to the genSeed method from
  28  *      returning
  29  *
  30  * if the test returns, then it passed.
  31  * if the test never returns (hangs forever), then it failed.
  32  * @key randomness
  33  */
  34 
  35 import java.security.SecureRandom;
  36 
  37 public class Priority_Inversion {
  38     public static void main(String args[]) {
  39         int deltaPriority = 1;
  40 
  41         if (args.length == 1) {
  42             try {
  43                 deltaPriority = Integer.parseInt(args[0]);
  44             }
  45             catch (NumberFormatException nfe) {
  46                 System.err.println
  47                         ("Sorry, \"" + args[0] + "\" is not a number");
  48                 System.exit(1);
  49             }
  50         }
  51 
  52         RandomTest rand = new RandomTest();
  53         InvertTest invert = new InvertTest(deltaPriority, rand);
  54         rand.start();
  55         invert.start();
  56     }
  57 }
  58 
  59 class RandomTest extends Thread {
  60     public synchronized void run() {
  61         System.out.println("Start priority " + getPriority());
  62         // The following should take over a second
  63         SecureRandom rand = new SecureRandom();
  64         rand.nextBytes(new byte[5]);
  65     }
  66 
  67     void invertPriority() {
  68         System.out.println("Waiting ..., priority " +
  69                 Thread.currentThread().getPriority());
  70         synchronized(this) {
  71         }
  72         System.out.println("Released Lock");
  73     }
  74 }
  75 class InvertTest extends Thread {
  76     private int delta;
  77     private RandomTest rand;
  78 
  79     InvertTest(int delta, RandomTest rand) {
  80         this.delta = delta;
  81         this.rand = rand;
  82     }
  83 
  84     public void run() {
  85         setPriority(getPriority() + delta);
  86         try {
  87             sleep(500);
  88         }
  89         catch (InterruptedException ie) { }
  90         rand.invertPriority();
  91     }
  92 }