1 /* 2 * Copyright (c) 2012, 2017, 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.util.Formatter; 73 import java.util.Map; 74 75 /** 76 * Base test class. 77 */ 78 public abstract class AbstractTCKTest { 79 80 /** 81 * Map from package name to the serialVersionUID of the .Ser class for the package. 82 */ 83 private static Map<String, Long> serialVersionUIDs = Map.of( 84 "java.time", -7683839454370182990L, 85 "java.time.chrono", -6103370247208168577L, 86 "java.time.zone", -8885321777449118786L 87 ); 88 89 /** 90 * Returns the serialVersionUID for the class. 91 * The SUIDs are defined by the specification for each class. 92 * @param serClass the class to return the SUID of 93 * @return returns the serialVersionUID for the class 94 */ getSUID(Class<?> serClass)95 public final static long getSUID(Class<?> serClass) { 96 String pkgName = serClass.getPackageName(); 97 return serialVersionUIDs.get(pkgName); 98 } 99 100 isIsoLeap(long year)101 protected static boolean isIsoLeap(long year) { 102 if (year % 4 != 0) { 103 return false; 104 } 105 if (year % 100 == 0 && year % 400 != 0) { 106 return false; 107 } 108 return true; 109 } 110 assertSerializable(Object object)111 protected static void assertSerializable(Object object) throws IOException, ClassNotFoundException { 112 assertEquals(object instanceof Serializable, true); 113 Object deserializedObject = writeThenRead(object); 114 assertEquals(deserializedObject, object); 115 } 116 assertSerializableSame(Object object)117 protected static void assertSerializableSame(Object object) throws IOException, ClassNotFoundException { 118 assertEquals(object instanceof Serializable, true); 119 Object deserializedObject = writeThenRead(object); 120 assertSame(deserializedObject, object); 121 } 122 writeThenRead(Object object)123 private static Object writeThenRead(Object object) throws IOException, ClassNotFoundException { 124 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 125 try (ObjectOutputStream oos = new ObjectOutputStream(baos) ) { 126 oos.writeObject(object); 127 } 128 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { 129 return ois.readObject(); 130 } 131 } 132 assertSerializedBySer(Object object, byte[] expectedBytes, byte[]... matches)133 protected static void assertSerializedBySer(Object object, byte[] expectedBytes, byte[]... matches) throws Exception { 134 String serClass = object.getClass().getPackage().getName() + ".Ser"; 135 long serVer = getSUID(object.getClass()); 136 137 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 138 try (ObjectOutputStream oos = new ObjectOutputStream(baos) ) { 139 oos.writeObject(object); 140 } 141 byte[] bytes = baos.toByteArray(); 142 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 143 try (DataInputStream dis = new DataInputStream(bais)) { 144 assertEquals(dis.readShort(), ObjectStreamConstants.STREAM_MAGIC); 145 assertEquals(dis.readShort(), ObjectStreamConstants.STREAM_VERSION); 146 assertEquals(dis.readByte(), ObjectStreamConstants.TC_OBJECT); 147 assertEquals(dis.readByte(), ObjectStreamConstants.TC_CLASSDESC); 148 assertEquals(dis.readUTF(), serClass); 149 assertEquals(dis.readLong(), serVer); 150 assertEquals(dis.readByte(), ObjectStreamConstants.SC_EXTERNALIZABLE | ObjectStreamConstants.SC_BLOCK_DATA); 151 assertEquals(dis.readShort(), 0); // number of fields 152 assertEquals(dis.readByte(), ObjectStreamConstants.TC_ENDBLOCKDATA); // end of classdesc 153 assertEquals(dis.readByte(), ObjectStreamConstants.TC_NULL); // no superclasses 154 if (expectedBytes.length < 256) { 155 assertEquals(dis.readByte(), ObjectStreamConstants.TC_BLOCKDATA); 156 assertEquals(dis.readUnsignedByte(), expectedBytes.length, "blockdata length incorrect"); 157 } else { 158 assertEquals(dis.readByte(), ObjectStreamConstants.TC_BLOCKDATALONG); 159 assertEquals(dis.readInt(), expectedBytes.length, "blockdatalong length incorrect"); 160 } 161 byte[] input = new byte[expectedBytes.length]; 162 dis.readFully(input); 163 assertEquals(input, expectedBytes); 164 if (matches.length > 0) { 165 for (byte[] match : matches) { 166 boolean matched = false; 167 while (matched == false) { 168 try { 169 dis.mark(1000); 170 byte[] possible = new byte[match.length]; 171 dis.readFully(possible); 172 assertEquals(possible, match); 173 matched = true; 174 } catch (AssertionError ex) { 175 dis.reset(); 176 dis.readByte(); // ignore 177 } 178 } 179 } 180 } else { 181 assertEquals(dis.readByte(), ObjectStreamConstants.TC_ENDBLOCKDATA); // end of blockdata 182 assertEquals(dis.read(), -1); 183 } 184 } 185 } 186 187 /** 188 * Verify the class cannot be deserialized from a handcoded stream. 189 * Fail if the deserialization does <em>not</em> throw an Exception. 190 * @param serClass the class to embed in the handcoded stream 191 * @throws Exception if an unexpected condition occurs 192 */ assertNotSerializable(Class<?> serClass)193 protected static void assertNotSerializable(Class<?> serClass) throws Exception { 194 long serVer = getSUID(serClass); 195 196 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 197 try (DataOutputStream out = new DataOutputStream(baos)) { 198 out.writeShort(ObjectStreamConstants.STREAM_MAGIC); 199 out.writeShort(ObjectStreamConstants.STREAM_VERSION); 200 out.writeByte(ObjectStreamConstants.TC_OBJECT); 201 out.writeByte(ObjectStreamConstants.TC_CLASSDESC); 202 out.writeUTF(serClass.getName()); 203 out.writeLong(serVer); 204 out.writeByte(ObjectStreamConstants.SC_SERIALIZABLE); // Flags ObjectStreamConstants 205 out.writeShort(0); // number of fields 206 out.writeByte(ObjectStreamConstants.TC_ENDBLOCKDATA); 207 out.writeByte(ObjectStreamConstants.TC_NULL); // no superclasses 208 } 209 210 byte[] bytes = baos.toByteArray(); 211 212 try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes); 213 ObjectInputStream in = new ObjectInputStream(bis)) { 214 Object o = in.readObject(); 215 } catch (Exception ioe) { 216 // Expected exception 217 return; 218 } 219 fail("Class should not be deserializable " + serClass.getName()); 220 } 221 222 /** 223 * Utility method to dump a byte array in a java syntax. 224 * @param bytes and array of bytes 225 * @return a string containing the bytes formatted in java syntax 226 */ dumpSerialStream(byte[] bytes)227 protected static String dumpSerialStream(byte[] bytes) { 228 StringBuilder sb = new StringBuilder(bytes.length * 5); 229 Formatter fmt = new Formatter(sb); 230 fmt.format(" byte[] bytes = {" ); 231 final int linelen = 10; 232 for (int i = 0; i < bytes.length; i++) { 233 if (i % linelen == 0) { 234 fmt.format("%n "); 235 } 236 fmt.format(" %3d,", bytes[i] & 0xff); 237 if ((i % linelen) == (linelen-1) || i == bytes.length - 1) { 238 fmt.format(" /*"); 239 int s = i / linelen * linelen; 240 int k = i % linelen; 241 for (int j = 0; j <= k && s + j < bytes.length; j++) { 242 fmt.format(" %c", bytes[s + j] & 0xff); 243 } 244 fmt.format(" */"); 245 } 246 } 247 fmt.format("%n };%n"); 248 return sb.toString(); 249 } 250 } 251