1 /*
   2  * Copyright (c) 2008, 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 6691503
  27  * @summary Checks that there is no opportunity for a malicious applet
  28  * to show a popup menu which has whole screen size.
  29  * a heaviweight popup menu is shown from an applet.
  30  * @author Mikhail Lapshin
  31  * @run main bug6691503
  32  */
  33 
  34 import javax.swing.*;
  35 import java.awt.*;
  36 
  37 public class bug6691503 {
  38     private JPopupMenu popupMenu;
  39     private JFrame frame;
  40     private boolean isAlwaysOnTop1 = false;
  41     private boolean isAlwaysOnTop2 = true;
  42 
  43     public static void main(String[] args) {
  44         bug6691503 test = new bug6691503();
  45         test.setupUI();
  46         test.testApplication();
  47         test.testApplet();
  48         test.checkResult();
  49         test.stopEDT();
  50     }
  51 
  52     private void setupUI() {
  53         SwingUtilities.invokeLater(new Runnable() {
  54             public void run() {
  55                 frame = new JFrame();
  56                 frame.setVisible(true);
  57                 popupMenu = new JPopupMenu();
  58                 JMenuItem click = new JMenuItem("Click");
  59                 popupMenu.add(click);
  60             }
  61         });
  62     }
  63 
  64     private void testApplication() {
  65         SwingUtilities.invokeLater(new Runnable() {
  66             public void run() {
  67                 popupMenu.show(frame, 0, 0);
  68                 Window popupWindow = (Window)
  69                         (popupMenu.getParent().getParent().getParent().getParent());
  70                 isAlwaysOnTop1 = popupWindow.isAlwaysOnTop();
  71                 System.out.println(
  72                         "Application: popupWindow.isAlwaysOnTop() = " + isAlwaysOnTop1);
  73                 popupMenu.setVisible(false);
  74             }
  75         });
  76     }
  77 
  78     private void testApplet() {
  79         SwingUtilities.invokeLater(new Runnable() {
  80             public void run() {
  81                 System.setSecurityManager(new SecurityManager());
  82                 popupMenu.show(frame, 0, 0);
  83                 Window popupWindow = (Window)
  84                         (popupMenu.getParent().getParent().getParent().getParent());
  85                 isAlwaysOnTop2 = popupWindow.isAlwaysOnTop();
  86                 System.out.println(
  87                         "Applet: popupWindow.isAlwaysOnTop() = " + isAlwaysOnTop2);
  88                 popupMenu.setVisible(false);
  89             }
  90         });
  91     }
  92 
  93     private void checkResult() {
  94         try {
  95             Robot robot = new Robot();
  96             robot.waitForIdle();
  97         }catch(Exception ex) {
  98             ex.printStackTrace();
  99             throw new RuntimeException("Unexpected failure");
 100         }
 101         if (!isAlwaysOnTop1 || isAlwaysOnTop2) {
 102             throw new RuntimeException("Malicious applet can show always-on-top " +
 103                     "popup menu which has whole screen size");
 104         }
 105         System.out.println("Test passed");
 106     }
 107 
 108     private void stopEDT() {
 109         SwingUtilities.invokeLater(new Runnable() {
 110             public void run() {
 111                 frame.dispose();
 112             }
 113         });
 114     }
 115 }