1 /* 2 * Copyright (c) 2014, 2018, 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 8030050 8039082 28 * @summary Validate fields on DnD class deserialization 29 */ 30 31 import java.awt.Point; 32 import java.awt.dnd.DragGestureEvent; 33 import java.awt.dnd.DragGestureRecognizer; 34 import java.awt.dnd.DragSource; 35 import java.awt.event.InputEvent; 36 import java.awt.event.KeyEvent; 37 import java.io.File; 38 import java.io.FileInputStream; 39 import java.io.FileOutputStream; 40 import java.io.IOException; 41 import java.io.InvalidObjectException; 42 import java.io.ObjectInputStream; 43 import java.io.ObjectOutputStream; 44 import java.util.ArrayList; 45 import java.util.stream.Stream; 46 47 import javax.swing.JPanel; 48 49 import static java.awt.dnd.DnDConstants.ACTION_COPY; 50 51 public class BadSerializationTest { 52 53 private static final String[] badSerialized = new String[] { 54 "badAction", 55 "noEvents", 56 "nullComponent", 57 "nullDragSource", 58 "nullOrigin" 59 }; 60 61 private static final String goodSerialized = "good"; 62 main(String[] args)63 public static void main(String[] args) throws Exception { 64 if (args.length > 0 && args[0].equals("-write")) { 65 writeObjects(); //Creates the binary files for the test. 66 } else { 67 String testSrc = System.getProperty("test.src") + File.separator; 68 testReadObject(testSrc + goodSerialized, false); 69 Stream.of(badSerialized).forEach( 70 file -> testReadObject(testSrc + file, true)); 71 } 72 } 73 testReadObject(String filename, boolean expectException)74 private static void testReadObject(String filename, boolean expectException) { 75 Exception exceptionCaught = null; 76 try (FileInputStream fileInputStream = new FileInputStream(filename); 77 ObjectInputStream ois = new ObjectInputStream(fileInputStream)) { 78 ois.readObject(); 79 } catch (InvalidObjectException e) { 80 exceptionCaught = e; 81 } catch (IOException e) { 82 throw new RuntimeException("FAILED: IOException", e); 83 } catch (ClassNotFoundException e) { 84 throw new RuntimeException("FAILED: ClassNotFoundException", e); 85 } 86 if (exceptionCaught != null && !expectException) { 87 throw new RuntimeException("FAILED: UnexpectedException", exceptionCaught); 88 } 89 if (exceptionCaught == null && expectException) { 90 throw new RuntimeException("FAILED: Invalid object was created with no exception"); 91 } 92 } 93 94 /** 95 * Creates the stubs for the test. It is necessary to disable all checks in 96 * the constructors of DragGestureEvent/DragGestureRecognizer before run. 97 */ writeObjects()98 private static void writeObjects() throws Exception { 99 ArrayList<InputEvent> evs = new ArrayList<>(); 100 Point ori = new Point(); 101 102 write(new DragGestureEvent(new NothingNull(), ACTION_COPY, ori, evs), 103 "noEvents"); 104 105 evs.add(new KeyEvent(new JPanel(), 0, 0, 0, 0, 'a', 0)); 106 107 write(new DragGestureEvent(new NullComponent(), ACTION_COPY, ori, evs), 108 "nullComponent"); 109 110 write(new DragGestureEvent(new NothingNull(), 100, ori, evs), 111 "badAction"); 112 113 write(new DragGestureEvent(new NullDragSource(), ACTION_COPY, ori, evs), 114 "nullDragSource"); 115 116 write(new DragGestureEvent(new NothingNull(), ACTION_COPY, null, evs), 117 "nullOrigin"); 118 119 write(new DragGestureEvent(new NothingNull(), ACTION_COPY, ori, evs), 120 "good"); 121 } 122 write(Object obj, String file)123 private static void write(Object obj, String file) throws Exception { 124 try (FileOutputStream fis = new FileOutputStream(file); 125 ObjectOutputStream ois = new ObjectOutputStream(fis)) { 126 ois.writeObject(obj); 127 } 128 } 129 130 public static final class NullDragSource extends DragGestureRecognizer { 131 NullDragSource()132 public NullDragSource() { 133 super(null, new JPanel()); 134 } 135 registerListeners()136 protected void registerListeners() { 137 } 138 unregisterListeners()139 protected void unregisterListeners() { 140 } 141 } 142 143 public static final class NullComponent extends DragGestureRecognizer { 144 NullComponent()145 public NullComponent() { 146 super(new DragSource(), null); 147 } 148 registerListeners()149 protected void registerListeners() { 150 } 151 unregisterListeners()152 protected void unregisterListeners() { 153 } 154 } 155 156 public static final class NothingNull extends DragGestureRecognizer { 157 NothingNull()158 public NothingNull() { 159 super(new DragSource(), new JPanel()); 160 } 161 registerListeners()162 protected void registerListeners() { 163 } 164 unregisterListeners()165 protected void unregisterListeners() { 166 } 167 } 168 } 169