1 /* 2 * Copyright (c) 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 import java.io.ByteArrayInputStream; 25 import java.io.IOException; 26 import java.io.InputStream; 27 import java.io.ObjectInputFilter; 28 import java.io.ObjectInputStream; 29 import java.io.InvalidClassException; 30 31 import java.util.Hashtable; 32 33 import jdk.internal.misc.SharedSecrets; 34 35 import org.testng.annotations.BeforeClass; 36 import org.testng.annotations.DataProvider; 37 import org.testng.annotations.Test; 38 import org.testng.Assert; 39 40 /* @test 41 * @build CheckArrayTest SerialFilterTest 42 * @bug 8203368 43 * @modules java.base/jdk.internal.misc 44 * @run testng CheckArrayTest 45 * 46 * @summary Test the SharedSecret access to ObjectInputStream.checkArray works 47 * with overridden subclasses. 48 */ 49 50 /** 51 * Verify that the SharedSecret access to the OIS checkAccess method 52 * does not fail with NPE in the case where ObjectInputStream is subclassed. 53 * The checkAccess method is called from various aggregate types in java.util 54 * to check array sizes during deserialization via the ObjectInputFilter attached the stream. 55 * The filterCheck must be resilent to an InputStream not being available (only the subclass knows). 56 */ 57 public class CheckArrayTest { 58 59 @DataProvider(name = "Patterns") 60 Object[][] patterns() { 61 return new Object[][]{ 62 new Object[]{"maxarray=10", 10, new String[10]}, // successful 63 new Object[]{"maxarray=10", 11, new String[11]}, // exception expected 64 }; 65 } 66 67 /** 68 * Test SharedSecrets checkArray with unmodified ObjectInputStream. 69 */ 70 @Test(dataProvider = "Patterns") 71 public void normalOIS(String pattern, int arraySize, Object[] array) throws IOException { 72 ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern); 73 byte[] bytes = SerialFilterTest.writeObjects(array); 74 try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 75 ObjectInputStream ois = new ObjectInputStream(bais)) { 76 // Check the arraysize against the filter 77 try { 78 ois.setObjectInputFilter(filter); 79 SharedSecrets.getJavaObjectInputStreamAccess() 80 .checkArray(ois, array.getClass(), arraySize); 81 Assert.assertTrue(array.length >= arraySize, 82 "Should have thrown InvalidClassException due to array size"); 83 } catch (InvalidClassException ice) { 84 Assert.assertFalse(array.length > arraySize, 85 "Should NOT have thrown InvalidClassException due to array size"); 86 } 87 } 88 } 89 90 /** 91 * Test SharedSecrets checkArray with an ObjectInputStream subclassed to 92 * handle all input stream functions. 93 */ 94 @Test(dataProvider = "Patterns") 95 public void subclassedOIS(String pattern, int arraySize, Object[] array) throws IOException { 96 byte[] bytes = SerialFilterTest.writeObjects(array); 97 try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 98 ObjectInputStream ois = new MyInputStream(bais)) { 99 // Check the arraysize against the filter 100 ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern); 101 ois.setObjectInputFilter(filter); 102 SharedSecrets.getJavaObjectInputStreamAccess() 103 .checkArray(ois, array.getClass(), arraySize); 104 Assert.assertTrue(array.length >= arraySize, 105 "Should have thrown InvalidClassException due to array size"); 106 } catch (InvalidClassException ice) { 107 Assert.assertFalse(array.length > arraySize, 108 "Should NOT have thrown InvalidClassException due to array size"); 109 } 110 } 111 112 /** 113 * Subclass OIS to disable all input stream functions of the OIS. 114 */ 115 static class MyInputStream extends ObjectInputStream { 116 MyInputStream(InputStream is) throws IOException { 117 super(); 118 } 119 120 public void close() { 121 } 122 } 123 }