test/jdk/java/io/Serializable/serialFilter/CheckArrayTest.java
author rriggs
Fri, 25 May 2018 13:29:25 -0400
changeset 50271 697b49a04e19
child 52427 3c6aa484536c
permissions -rw-r--r--
8203368: ObjectInputStream filterCheck method throws NullPointerException Reviewed-by: bpb, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50271
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     1
/*
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     4
 *
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     8
 *
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    13
 * accompanied this code).
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    14
 *
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    18
 *
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    21
 * questions.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    22
 */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    23
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    24
import java.io.ByteArrayInputStream;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    25
import java.io.IOException;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    26
import java.io.InputStream;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    27
import java.io.ObjectInputFilter;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    28
import java.io.ObjectInputStream;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    29
import java.io.InvalidClassException;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    30
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    31
import java.util.Hashtable;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    32
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    33
import jdk.internal.misc.SharedSecrets;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    34
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    35
import org.testng.annotations.BeforeClass;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    36
import org.testng.annotations.DataProvider;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    37
import org.testng.annotations.Test;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    38
import org.testng.Assert;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    39
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    40
/* @test
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    41
 * @build CheckArrayTest SerialFilterTest
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    42
 * @bug 8203368
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    43
 * @modules java.base/jdk.internal.misc
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    44
 * @run testng CheckArrayTest
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    45
 *
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    46
 * @summary Test the SharedSecret access to ObjectInputStream.checkArray works
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    47
 *      with overridden subclasses.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    48
 */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    49
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    50
/**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    51
 * Verify that the SharedSecret access to the OIS checkAccess method
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    52
 * does not fail with NPE in the case where ObjectInputStream is subclassed.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    53
 * The checkAccess method is called from various aggregate types in java.util
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    54
 * to check array sizes during deserialization via the ObjectInputFilter attached the stream.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    55
 * The filterCheck must be resilent to an InputStream not being available (only the subclass knows).
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    56
 */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    57
public class CheckArrayTest {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    58
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    59
    @DataProvider(name = "Patterns")
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    60
    Object[][] patterns() {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    61
        return new Object[][]{
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    62
                new Object[]{"maxarray=10", 10, new String[10]},    // successful
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    63
                new Object[]{"maxarray=10", 11, new String[11]},    // exception expected
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    64
        };
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    65
    }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    66
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    67
    /**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    68
     * Test SharedSecrets checkArray with unmodified ObjectInputStream.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    69
     */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    70
    @Test(dataProvider = "Patterns")
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    71
    public void normalOIS(String pattern, int arraySize, Object[] array) throws IOException {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    72
        ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    73
        byte[] bytes = SerialFilterTest.writeObjects(array);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    74
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    75
             ObjectInputStream ois = new ObjectInputStream(bais)) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    76
            // Check the arraysize against the filter
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    77
            try {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    78
                ois.setObjectInputFilter(filter);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    79
                SharedSecrets.getJavaObjectInputStreamAccess()
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    80
                        .checkArray(ois, array.getClass(), arraySize);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    81
                Assert.assertTrue(array.length >= arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    82
                        "Should have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    83
            } catch (InvalidClassException ice) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    84
                Assert.assertFalse(array.length > arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    85
                        "Should NOT have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    86
            }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    87
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    88
    }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    89
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    90
    /**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    91
     * Test SharedSecrets checkArray with an ObjectInputStream subclassed to
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    92
     * handle all input stream functions.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    93
     */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    94
    @Test(dataProvider = "Patterns")
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    95
    public void subclassedOIS(String pattern, int arraySize, Object[] array) throws IOException {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    96
        byte[] bytes = SerialFilterTest.writeObjects(array);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    97
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    98
             ObjectInputStream ois = new MyInputStream(bais)) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    99
            // Check the arraysize against the filter
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   100
            ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   101
            ois.setObjectInputFilter(filter);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   102
            SharedSecrets.getJavaObjectInputStreamAccess()
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   103
                    .checkArray(ois, array.getClass(), arraySize);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   104
            Assert.assertTrue(array.length >= arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   105
                    "Should have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   106
        } catch (InvalidClassException ice) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   107
            Assert.assertFalse(array.length > arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   108
                    "Should NOT have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   109
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   110
    }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   111
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   112
    /**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   113
     * Subclass OIS to disable all input stream functions of the OIS.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   114
     */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   115
    static class MyInputStream extends ObjectInputStream {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   116
        MyInputStream(InputStream is) throws IOException {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   117
            super();
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   118
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   119
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   120
        public void close() {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   121
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   122
    }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   123
}