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