test/jdk/java/util/EnumSet/BogusEnumSet.java
author plevart
Sat, 13 Jul 2019 10:43:45 +0000
changeset 55697 7fd4446c02ee
parent 48230 d0e8542ef650
permissions -rw-r--r--
8227368: EnumSet.class serialization broken in JDK 9+ Reviewed-by: smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     1
/*
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     3
 *
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     6
 * published by the Free Software Foundation.
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     7
 *
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    12
 * accompanied this code).
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    13
 *
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    17
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1088
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1088
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1088
diff changeset
    20
 * questions.
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    21
 */
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    22
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    23
/*
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    24
 * @test
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    25
 * @bug     6739302
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    26
 * @summary Check that deserialization preserves EnumSet integrity
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    27
 * @author  Josh Bloch
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    28
 */
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    29
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    30
import java.util.*;
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    31
import java.io.*;
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    32
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    33
public class BogusEnumSet {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    34
    public static void main(String[] args) throws Throwable {
55697
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    35
        // This test tries to deserialize a bogus stream produced with
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    36
        // hypothetical EnumSet without a writeReplace() method - i.e.
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    37
        // not using serialization proxy pattern. It tests that such
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    38
        // stream is not accepted as valid stream - the EnumSet class
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    39
        // declares a readObject() method which throws exception.
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    40
        byte[] serializedForm  = {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    41
            (byte)0xac, (byte)0xed, 0x0, 0x5, 0x73, 0x72, 0x0, 0x18,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    42
            0x6a,  0x61,  0x76,  0x61, 0x2e,  0x75,  0x74,  0x69,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    43
            0x6c,  0x2e, 0x52, 0x65, 0x67, 0x75, 0x6c, 0x61, 0x72, 0x45,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    44
            0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0x2f, 0x58, 0x6f, (byte)0xc7,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    45
            0x7e, (byte)0xb0, (byte)0xd0, 0x7e, 0x2, 0x0, 0x1, 0x4a, 0x0, 0x8,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    46
            0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x78, 0x72, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    47
            0x11, 0x6a,  0x61,  0x76,  0x61, 0x2e,  0x75,  0x74,  0x69,
55697
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    48
            0x6c,  0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x53, 0x65, 0x74, 0xe,
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    49
            0x3, 0x21, 0x6a, (byte)0xcd, (byte)0x8c, 0x29, (byte)0xdd, 0x2,
7fd4446c02ee 8227368: EnumSet.class serialization broken in JDK 9+
plevart
parents: 48230
diff changeset
    50
            0x0, 0x2, 0x4c, 0x0, 0xb, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74,
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    51
            0x54, 0x79, 0x70, 0x65, 0x74, 0x0, 0x11, 0x4c, 0x6a, 0x61, 0x76,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    52
            0x61, 0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x43, 0x6c, 0x61, 0x73,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    53
            0x73, 0x3b, 0x5b, 0x0, 0x8, 0x75, 0x6e, 0x69, 0x76, 0x65, 0x72,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    54
            0x73, 0x65, 0x74, 0x0, 0x11, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    55
            0x2f, 0x6c, 0x61, 0x6e, 0x67, 0x2f, 0x45, 0x6e, 0x75, 0x6d, 0x3b,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    56
            0x78, 0x70, 0x76, 0x72, 0x0, 0x16, 0x6a, 0x61, 0x76, 0x61, 0x2e,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    57
            0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x54, 0x68, 0x72, 0x65, 0x61,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    58
            0x64, 0x24, 0x53, 0x74, 0x61, 0x74, 0x65, 0x0, 0x0, 0x0, 0x0, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    59
            0x0, 0x0, 0x0, 0x12, 0x0, 0x0, 0x78, 0x72, 0x0, 0xe, 0x6a, 0x61,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    60
            0x76, 0x61, 0x2e, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x45, 0x6e, 0x75,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    61
            0x6d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x12, 0x0, 0x0, 0x78,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    62
            0x70, 0x75, 0x72, 0x0, 0x19, 0x5b, 0x4c, 0x6a, 0x61, 0x76, 0x61,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    63
            0x2e, 0x6c, 0x61, 0x6e, 0x67, 0x2e, 0x54, 0x68, 0x72, 0x65, 0x61,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    64
            0x64, 0x24, 0x53, 0x74, 0x61, 0x74, 0x65, 0x3b, 0x68, (byte)0xa3,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    65
            (byte)0xb5, (byte)0xd5, 0x11, 0x7d, 0x1b, (byte)0xb3, 0x2, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    66
            0x0, 0x78, 0x70, 0x0, 0x0, 0x0, 0x6, 0x7e, 0x71, 0x0, 0x7e, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    67
            0x5, 0x74, 0x0, 0x3, 0x4e, 0x45, 0x57, 0x7e, 0x71, 0x0, 0x7e, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    68
            0x5, 0x74, 0x0, 0x8, 0x52, 0x55, 0x4e, 0x4e, 0x41, 0x42, 0x4c, 0x45,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    69
            0x7e, 0x71, 0x0, 0x7e, 0x0, 0x5, 0x74, 0x0, 0x7, 0x42, 0x4c, 0x4f,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    70
            0x43, 0x4b, 0x45, 0x44, 0x7e, 0x71, 0x0, 0x7e, 0x0, 0x5, 0x74, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    71
            0x7, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    72
            0x7e, 0x0, 0x5, 0x74, 0x0, 0xd, 0x54, 0x49, 0x4d, 0x45, 0x44,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    73
            0x5f, 0x57, 0x41, 0x49, 0x54, 0x49, 0x4e, 0x47, 0x7e, 0x71, 0x0,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    74
            0x7e, 0x0, 0x5, 0x74, 0x0, 0xa, 0x54, 0x45, 0x52, 0x4d, 0x49,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    75
            0x4e, 0x41, 0x54, 0x45, 0x44, (byte)0xff, (byte)0xff, (byte)0xff,
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    76
            (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    77
        };
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    78
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    79
        try {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    80
          // Should fail, but instead creates corrupt EnumSet
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    81
          @SuppressWarnings("unchecked")
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    82
          EnumSet<Thread.State> es = (EnumSet<Thread.State>)
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    83
              deserialize(serializedForm);
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    84
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    85
          // Demonstrates corruption
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    86
          System.out.println("Enum size: " + Thread.State.values().length); // 6
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    87
          System.out.println("Set size: " + es.size()); // 64
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    88
          System.out.println("Set: " + es); // Throws IndexOutOfBoundsException
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    89
          throw new AssertionError("Expected exception InvalidObjectException not thrown");
27785
29d4cb4a8f5e 8065998: Avoid use of _ as a one-character identifier
jlahoda
parents: 5506
diff changeset
    90
        } catch (java.io.InvalidObjectException expected) { /* OK */ }
1088
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    91
    }
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    92
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    93
    private static Object deserialize(byte[] sf) throws Throwable {
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    94
      return new ObjectInputStream(
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    95
          new ByteArrayInputStream(sf))
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    96
          .readObject();
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    97
    }
aa57bc6c9b3c 6739302: Check that deserialization preserves EnumSet integrity
martin
parents:
diff changeset
    98
}