• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2013, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  * Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos
28  *
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions are met:
33  *
34  *  * Redistributions of source code must retain the above copyright notice,
35  *    this list of conditions and the following disclaimer.
36  *
37  *  * Redistributions in binary form must reproduce the above copyright notice,
38  *    this list of conditions and the following disclaimer in the documentation
39  *    and/or other materials provided with the distribution.
40  *
41  *  * Neither the name of JSR-310 nor the names of its contributors
42  *    may be used to endorse or promote products derived from this software
43  *    without specific prior written permission.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
49  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
50  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
52  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
53  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
54  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
55  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 package tck.java.time;
58 
59 import static org.testng.Assert.assertEquals;
60 import static org.testng.Assert.assertSame;
61 import static org.testng.Assert.fail;
62 
63 import java.io.ByteArrayInputStream;
64 import java.io.ByteArrayOutputStream;
65 import java.io.DataInputStream;
66 import java.io.DataOutputStream;
67 import java.io.IOException;
68 import java.io.ObjectInputStream;
69 import java.io.ObjectOutputStream;
70 import java.io.ObjectStreamConstants;
71 import java.io.Serializable;
72 import java.lang.reflect.Field;
73 import java.util.Formatter;
74 
75 /**
76  * Base test class.
77  */
78 public abstract class AbstractTCKTest {
79 
isIsoLeap(long year)80     protected static boolean isIsoLeap(long year) {
81         if (year % 4 != 0) {
82             return false;
83         }
84         if (year % 100 == 0 && year % 400 != 0) {
85             return false;
86         }
87         return true;
88     }
89 
assertSerializable(Object object)90     protected static void assertSerializable(Object object) throws IOException, ClassNotFoundException {
91         assertEquals(object instanceof Serializable, true);
92         Object deserializedObject = writeThenRead(object);
93         assertEquals(deserializedObject, object);
94     }
95 
assertSerializableSame(Object object)96     protected static void assertSerializableSame(Object object) throws IOException, ClassNotFoundException {
97         assertEquals(object instanceof Serializable, true);
98         Object deserializedObject = writeThenRead(object);
99         assertSame(deserializedObject, object);
100     }
101 
writeThenRead(Object object)102     private static Object writeThenRead(Object object) throws IOException, ClassNotFoundException {
103         ByteArrayOutputStream baos = new ByteArrayOutputStream();
104         try (ObjectOutputStream oos = new ObjectOutputStream(baos) ) {
105             oos.writeObject(object);
106         }
107         try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) {
108             return ois.readObject();
109         }
110     }
111 
assertSerializedBySer(Object object, byte[] expectedBytes, byte[]... matches)112     protected static void assertSerializedBySer(Object object, byte[] expectedBytes, byte[]... matches) throws Exception {
113         String serClass = object.getClass().getPackage().getName() + ".Ser";
114         Class<?> serCls = Class.forName(serClass);
115         Field field = serCls.getDeclaredField("serialVersionUID");
116         field.setAccessible(true);
117         long serVer = (Long) field.get(null);
118         ByteArrayOutputStream baos = new ByteArrayOutputStream();
119         try (ObjectOutputStream oos = new ObjectOutputStream(baos) ) {
120             oos.writeObject(object);
121         }
122         byte[] bytes = baos.toByteArray();
123         ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
124         try (DataInputStream dis = new DataInputStream(bais)) {
125             assertEquals(dis.readShort(), ObjectStreamConstants.STREAM_MAGIC);
126             assertEquals(dis.readShort(), ObjectStreamConstants.STREAM_VERSION);
127             assertEquals(dis.readByte(), ObjectStreamConstants.TC_OBJECT);
128             assertEquals(dis.readByte(), ObjectStreamConstants.TC_CLASSDESC);
129             assertEquals(dis.readUTF(), serClass);
130             assertEquals(dis.readLong(), serVer);
131             assertEquals(dis.readByte(), ObjectStreamConstants.SC_EXTERNALIZABLE | ObjectStreamConstants.SC_BLOCK_DATA);
132             assertEquals(dis.readShort(), 0);  // number of fields
133             assertEquals(dis.readByte(), ObjectStreamConstants.TC_ENDBLOCKDATA);  // end of classdesc
134             assertEquals(dis.readByte(), ObjectStreamConstants.TC_NULL);  // no superclasses
135             if (expectedBytes.length < 256) {
136                 assertEquals(dis.readByte(), ObjectStreamConstants.TC_BLOCKDATA);
137                 assertEquals(dis.readUnsignedByte(), expectedBytes.length, "blockdata length incorrect");
138             } else {
139                 assertEquals(dis.readByte(), ObjectStreamConstants.TC_BLOCKDATALONG);
140                 assertEquals(dis.readInt(), expectedBytes.length, "blockdatalong length incorrect");
141             }
142             byte[] input = new byte[expectedBytes.length];
143             dis.readFully(input);
144             assertEquals(input, expectedBytes);
145             if (matches.length > 0) {
146                 for (byte[] match : matches) {
147                     boolean matched = false;
148                     while (matched == false) {
149                         try {
150                             dis.mark(1000);
151                             byte[] possible = new byte[match.length];
152                             dis.readFully(possible);
153                             assertEquals(possible, match);
154                             matched = true;
155                         } catch (AssertionError ex) {
156                             dis.reset();
157                             dis.readByte();  // ignore
158                         }
159                     }
160                 }
161             } else {
162                 assertEquals(dis.readByte(), ObjectStreamConstants.TC_ENDBLOCKDATA);  // end of blockdata
163                 assertEquals(dis.read(), -1);
164             }
165         }
166     }
167 
168     /**
169      * Verify the class cannot be deserialized from a handcoded stream.
170      * Fail if the deserialization does <em>not</em> throw an Exception.
171      * @param serClass the class to embed in the handcoded stream
172      * @throws Exception if an unexpected condition occurs
173      */
assertNotSerializable(Class<?> serClass)174     protected static void assertNotSerializable(Class<?> serClass) throws Exception {
175         Field field = serClass.getDeclaredField("serialVersionUID");
176         field.setAccessible(true);
177         long serVer = (Long) field.get(null);
178         ByteArrayOutputStream baos = new ByteArrayOutputStream();
179         try (DataOutputStream out = new DataOutputStream(baos)) {
180             out.writeShort(ObjectStreamConstants.STREAM_MAGIC);
181             out.writeShort(ObjectStreamConstants.STREAM_VERSION);
182             out.writeByte(ObjectStreamConstants.TC_OBJECT);
183             out.writeByte(ObjectStreamConstants.TC_CLASSDESC);
184             out.writeUTF(serClass.getName());
185             out.writeLong(serVer);
186             out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE);   // Flags ObjectStreamConstants
187             out.writeShort(0);  // number of fields
188             out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA);
189             out.writeByte(ObjectStreamConstants.TC_NULL);  // no superclasses
190         }
191 
192         byte[] bytes = baos.toByteArray();
193 
194         try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
195             ObjectInputStream in = new ObjectInputStream(bis)) {
196             Object o = in.readObject();
197         } catch (Exception ioe) {
198             // Expected exception
199             return;
200         }
201         fail("Class should not be deserializable " + serClass.getName());
202     }
203 
204 
205     /**
206      * Utility method to dump a byte array in a java syntax.
207      * @param bytes and array of bytes
208      * @return a string containing the bytes formatted in java syntax
209      */
dumpSerialStream(byte[] bytes)210     protected static String dumpSerialStream(byte[] bytes) {
211         StringBuilder sb = new StringBuilder(bytes.length * 5);
212         Formatter fmt = new Formatter(sb);
213         fmt.format("    byte[] bytes = {" );
214         final int linelen = 10;
215         for (int i = 0; i < bytes.length; i++) {
216             if (i % linelen == 0) {
217                 fmt.format("%n        ");
218             }
219             fmt.format(" %3d,", bytes[i] & 0xff);
220             if ((i % linelen) == (linelen-1) || i == bytes.length - 1) {
221                 fmt.format("  /*");
222                 int s = i / linelen * linelen;
223                 int k = i % linelen;
224                 for (int j = 0; j <= k && s + j < bytes.length; j++) {
225                     fmt.format(" %c", bytes[s + j] & 0xff);
226                 }
227                 fmt.format(" */");
228             }
229         }
230         fmt.format("%n    };%n");
231         return sb.toString();
232     }
233 }
234