1 /*
2  * Copyright (c) 2019, 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  * @summary Basic test for ClassNotFoundException
27  * @run testng BadValues
28  */
29 package test.java.io.Serializable.records;
30 
31 import java.io.ByteArrayInputStream;
32 import java.io.ByteArrayOutputStream;
33 import java.io.DataOutputStream;
34 import java.io.IOException;
35 import java.io.ObjectInputStream;
36 import org.testng.annotations.Test;
37 import static java.io.ObjectStreamConstants.*;
38 import static java.lang.System.out;
39 import static org.testng.Assert.*;
40 
41 /**
42  * Not directly related to records but provokes surrounding code, and ensures
43  * that the record changes don't break anything.
44  */
45 public class BadValues {
46 
47     /**
48      * Returns a stream of bytes for the given class, uid, and flags. The
49      * stream will have no stream field values.
50      */
byteStreamFor(String className, long uid, byte flags)51     static byte[] byteStreamFor(String className, long uid, byte flags)
52         throws Exception
53     {
54         ByteArrayOutputStream baos = new ByteArrayOutputStream();
55         DataOutputStream dos = new DataOutputStream(baos);
56         dos.writeShort(STREAM_MAGIC);
57         dos.writeShort(STREAM_VERSION);
58         dos.writeByte(TC_OBJECT);
59         dos.writeByte(TC_CLASSDESC);
60         dos.writeUTF(className);
61         dos.writeLong(uid);
62         dos.writeByte(flags);
63         dos.writeShort(0);                // number of fields
64         dos.writeByte(TC_ENDBLOCKDATA);   // no annotations
65         dos.writeByte(TC_NULL);           // no superclasses
66         dos.write(TC_ENDBLOCKDATA);       // end block - for SC_WRITE_METHOD
67         dos.close();
68         return baos.toByteArray();
69     }
70 
71     static final Class<ClassNotFoundException> CNFE = ClassNotFoundException.class;
72 
73     @Test
testNotFoundSer()74     public void testNotFoundSer() throws Exception {
75         out.println("\n---");
76         byte[] bytes = byteStreamFor("XxYyZz", 0L, (byte)SC_SERIALIZABLE);
77         Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
78         out.println("caught expected CNFE: " + t);
79     }
80 
81     @Test
testNotFoundSerWr()82     public void testNotFoundSerWr() throws Exception {
83         out.println("\n---");
84         byte[] bytes = byteStreamFor("XxYyZz", 0L, (byte)(SC_SERIALIZABLE | SC_WRITE_METHOD));
85         Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
86         out.println("caught expected CNFE: " + t);
87     }
88 
89     @Test
testNotFoundExt()90     public void testNotFoundExt() throws Exception {
91         out.println("\n---");
92         byte[] bytes = byteStreamFor("AaBbCc", 0L, (byte)SC_EXTERNALIZABLE);
93         Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
94         out.println("caught expected CNFE: " + t);
95     }
96 
97     @Test
testNotFoundExtWr()98     public void testNotFoundExtWr() throws Exception {
99         out.println("\n---");
100         byte[] bytes = byteStreamFor("AaBbCc", 0L, (byte)(SC_SERIALIZABLE | SC_WRITE_METHOD));
101         Throwable t = expectThrows(CNFE, () -> deserialize(bytes));
102         out.println("caught expected CNFE: " + t);
103     }
104 
105     @SuppressWarnings("unchecked")
deserialize(byte[] streamBytes)106     static <T> T deserialize(byte[] streamBytes)
107         throws IOException, ClassNotFoundException
108     {
109         ByteArrayInputStream bais = new ByteArrayInputStream(streamBytes);
110         ObjectInputStream ois  = new ObjectInputStream(bais);
111         return (T) ois.readObject();
112     }
113 }
114