1 /* 2 * Copyright (c) 2018, 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 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayOutputStream; 28 import java.io.DataOutputStream; 29 import java.io.IOException; 30 import java.io.InvalidObjectException; 31 import java.io.ObjectInputStream; 32 import java.io.ObjectStreamConstants; 33 import static java.io.ObjectStreamConstants.STREAM_MAGIC; 34 import static java.io.ObjectStreamConstants.TC_CLASSDESC; 35 import static java.io.ObjectStreamConstants.TC_ENDBLOCKDATA; 36 import static java.io.ObjectStreamConstants.TC_NULL; 37 import static java.io.ObjectStreamConstants.TC_OBJECT; 38 import java.net.Inet6Address; 39 /** 40 * @test 41 * @bug 8194676 42 * @summary NullPointerException is thrown if ipaddress is not set. 43 * @run main Inet6AddressSerTest 44 */ 45 public class Inet6AddressSerTest implements ObjectStreamConstants { 46 47 static class PayloadTest { 48 serialize(String className)49 private static byte[] serialize(String className) throws IOException { 50 try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); 51 DataOutputStream dos = new DataOutputStream(baos)) { 52 // stream headers 53 dos.writeShort(STREAM_MAGIC); 54 dos.writeShort(5); // version 55 56 // Inet6Address 57 dos.writeByte(TC_OBJECT); 58 dos.writeByte(TC_CLASSDESC); 59 dos.writeUTF(className); 60 dos.writeLong(6880410070516793377L); 61 dos.writeByte(2); 62 dos.writeShort(0); 63 dos.writeByte(TC_ENDBLOCKDATA); 64 dos.writeByte(TC_NULL); 65 return baos.toByteArray(); 66 } 67 } 68 deserialize(final byte[] buffer)69 private static Object deserialize(final byte[] buffer) 70 throws IOException, ClassNotFoundException { 71 try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer))) { 72 return ois.readObject(); 73 } 74 } 75 test(String className)76 void test(String className) throws IOException, ClassNotFoundException { 77 deserialize(serialize(className)); 78 } 79 } 80 main(String[] args)81 public static void main(String[] args) throws IOException, ClassNotFoundException { 82 try { 83 new PayloadTest().test(Inet6Address.class.getName()); 84 throw new RuntimeException("Expected exception not raised"); 85 } catch (InvalidObjectException ioe) { 86 if (ioe.getMessage().contains("invalid address length")) { 87 System.out.println(String.format("Got expected exception: %s", ioe)); 88 } else { 89 throw new RuntimeException("Expected exception not raised"); 90 } 91 } catch (RuntimeException re) { 92 throw new RuntimeException("Expected exception not raised"); 93 } 94 } 95 } 96