test/jdk/java/io/Serializable/serialFilter/CheckArrayTest.java
author mchung
Tue, 06 Nov 2018 10:01:16 -0800
changeset 52427 3c6aa484536c
parent 50271 697b49a04e19
permissions -rw-r--r--
8211122: Reduce the number of internal classes made accessible to jdk.unsupported Reviewed-by: alanb, dfuchs, kvn
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
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50271
diff changeset
    31
import jdk.internal.access.SharedSecrets;
50271
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 org.testng.annotations.DataProvider;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    34
import org.testng.annotations.Test;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    35
import org.testng.Assert;
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    36
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    37
/* @test
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    38
 * @build CheckArrayTest SerialFilterTest
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    39
 * @bug 8203368
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50271
diff changeset
    40
 * @modules java.base/jdk.internal.access
50271
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    41
 * @run testng CheckArrayTest
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    42
 *
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    43
 * @summary Test the SharedSecret access to ObjectInputStream.checkArray works
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    44
 *      with overridden subclasses.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    45
 */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    46
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    47
/**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    48
 * Verify that the SharedSecret access to the OIS checkAccess method
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    49
 * does not fail with NPE in the case where ObjectInputStream is subclassed.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    50
 * The checkAccess method is called from various aggregate types in java.util
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    51
 * to check array sizes during deserialization via the ObjectInputFilter attached the stream.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    52
 * 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
    53
 */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    54
public class CheckArrayTest {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    55
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    56
    @DataProvider(name = "Patterns")
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    57
    Object[][] patterns() {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    58
        return new Object[][]{
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    59
                new Object[]{"maxarray=10", 10, new String[10]},    // successful
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    60
                new Object[]{"maxarray=10", 11, new String[11]},    // exception expected
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    61
        };
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    62
    }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    63
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    64
    /**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    65
     * Test SharedSecrets checkArray with unmodified ObjectInputStream.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    66
     */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    67
    @Test(dataProvider = "Patterns")
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    68
    public void normalOIS(String pattern, int arraySize, Object[] array) throws IOException {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    69
        ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    70
        byte[] bytes = SerialFilterTest.writeObjects(array);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    71
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    72
             ObjectInputStream ois = new ObjectInputStream(bais)) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    73
            // Check the arraysize against the filter
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    74
            try {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    75
                ois.setObjectInputFilter(filter);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    76
                SharedSecrets.getJavaObjectInputStreamAccess()
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    77
                        .checkArray(ois, array.getClass(), arraySize);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    78
                Assert.assertTrue(array.length >= arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    79
                        "Should have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    80
            } catch (InvalidClassException ice) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    81
                Assert.assertFalse(array.length > arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    82
                        "Should NOT have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    83
            }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    84
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    85
    }
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
     * Test SharedSecrets checkArray with an ObjectInputStream subclassed to
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    89
     * handle all input stream functions.
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(dataProvider = "Patterns")
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    92
    public void subclassedOIS(String pattern, int arraySize, Object[] array) throws IOException {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    93
        byte[] bytes = SerialFilterTest.writeObjects(array);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    94
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    95
             ObjectInputStream ois = new MyInputStream(bais)) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    96
            // Check the arraysize against the filter
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    97
            ObjectInputFilter filter = ObjectInputFilter.Config.createFilter(pattern);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    98
            ois.setObjectInputFilter(filter);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
    99
            SharedSecrets.getJavaObjectInputStreamAccess()
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   100
                    .checkArray(ois, array.getClass(), arraySize);
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   101
            Assert.assertTrue(array.length >= arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   102
                    "Should have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   103
        } catch (InvalidClassException ice) {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   104
            Assert.assertFalse(array.length > arraySize,
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   105
                    "Should NOT have thrown InvalidClassException due to array size");
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   106
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   107
    }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   108
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   109
    /**
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   110
     * Subclass OIS to disable all input stream functions of the OIS.
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   111
     */
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   112
    static class MyInputStream extends ObjectInputStream {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   113
        MyInputStream(InputStream is) throws IOException {
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   114
            super();
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   115
        }
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   116
697b49a04e19 8203368: ObjectInputStream filterCheck method throws NullPointerException
rriggs
parents:
diff changeset
   117
        public void close() {
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
}