1 /*
2  * Copyright (c) 2005, 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 /* @test
25  * @summary it is new version of old test which was under
26  *          /src/share/test/serialization/psiotest.java
27  *          Test validation callbacks
28  */
29 
30 import java.io.*;
31 
32 public class ValidateClass {
main(String argv[])33     public static void main (String argv[]) {
34         System.err.println("\nRegression test for validation of callbacks \n");
35 
36         FileInputStream istream = null;
37         try {
38 
39             FileOutputStream ostream = new FileOutputStream("psiotest4.tmp");
40             ObjectOutputStream p = new ObjectOutputStream(ostream);
41 
42             /* Catch the expected exception and
43              * complain if it does not occur.
44              */
45 
46             // Serialize a bunch of objects that will be validated when read
47             // Make a list of classes with intermingled priorities
48             Validator vc = new Validator(0, null);
49             vc = new Validator(2, vc);
50             vc = new Validator(0, vc);
51             vc = new Validator(3, vc);
52             vc = new Validator(Integer.MIN_VALUE, vc);
53             vc = new Validator(1, vc);
54             vc = new Validator(1, vc);
55             vc = new Validator(0, vc);
56 
57             p.writeObject(vc);
58             p.flush();
59             ostream.close();
60 
61             istream = new FileInputStream("psiotest4.tmp");
62             ObjectInputStream q = new ObjectInputStream(istream);
63 
64             Validator vc_u;
65 
66             vc_u = (Validator)q.readObject();
67             if (Validator.validated != Integer.MIN_VALUE) {
68                 System.err.println("\nTEST FAILED: Validation callbacks did " +
69                     "not complete.");
70                 throw new Error();
71             }
72             istream.close();
73             System.err.println("\nTEST PASSED");
74         } catch (Exception e) {
75             System.err.print("TEST FAILED: ");
76             e.printStackTrace();
77             throw new Error();
78         }
79     }
80 }
81 
82 class MissingWriterClass implements java.io.Serializable {
83     private static final long serialVersionUID = 1L;
84     int i = 77;
85 
writeObject(ObjectOutputStream pw)86     private void writeObject(ObjectOutputStream pw) throws IOException {
87         pw.writeInt(i);
88     }
89 }
90 
91 class MissingReaderClass implements java.io.Serializable {
92     private static final long serialVersionUID = 1L;
93     int i = 77;
94 
readObject(ObjectInputStream pr)95     private void readObject(ObjectInputStream pr) throws IOException {
96         i = pr.readInt();
97     }
98 }
99 
100 
101 class Validator implements ObjectInputValidation, java.io.Serializable  {
102     private static final long serialVersionUID = 1L;
103 
104     static int validated = Integer.MAX_VALUE; // Last value validated
105     int priority;
106     Validator next = null;
107 
Validator(int prio, Validator n)108     public Validator(int prio, Validator n) {
109         priority = prio;
110         next = n;
111     }
112 
113     // Handle serialization/deserialization
writeObject(ObjectOutputStream pw)114     private void writeObject(ObjectOutputStream pw) throws IOException {
115         pw.writeInt(priority);
116         pw.writeObject(next);
117     }
118 
readObject(ObjectInputStream pr)119     private void readObject(ObjectInputStream pr)
120         throws IOException, ClassNotFoundException
121     {
122         priority = pr.readInt();
123         next = (Validator)pr.readObject();
124 
125         pr.registerValidation(this, priority);
126     }
127 
validateObject()128     public void validateObject() throws InvalidObjectException {
129         if (validated < priority) {
130             System.err.println("\nTEST FAILED: Validations called out " +
131                 "of order: Previous priority: " + validated + " < " +
132                 "new priority: " + priority);
133             throw new Error();
134         }
135         validated = priority;
136     }
137 }
138