Path: blob/master/test/jdk/java/net/Inet6Address/serialize/Inet6AddressSerTest.java
41152 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.DataOutputStream;26import java.io.IOException;27import java.io.InvalidObjectException;28import java.io.ObjectInputStream;29import java.io.ObjectStreamConstants;30import static java.io.ObjectStreamConstants.STREAM_MAGIC;31import static java.io.ObjectStreamConstants.TC_CLASSDESC;32import static java.io.ObjectStreamConstants.TC_ENDBLOCKDATA;33import static java.io.ObjectStreamConstants.TC_NULL;34import static java.io.ObjectStreamConstants.TC_OBJECT;35import java.net.Inet6Address;36/**37* @test38* @bug 819467639* @summary NullPointerException is thrown if ipaddress is not set.40* @run main Inet6AddressSerTest41*/42public class Inet6AddressSerTest implements ObjectStreamConstants {4344static class PayloadTest {4546private static byte[] serialize(String className) throws IOException {47try (ByteArrayOutputStream baos = new ByteArrayOutputStream();48DataOutputStream dos = new DataOutputStream(baos)) {49// stream headers50dos.writeShort(STREAM_MAGIC);51dos.writeShort(5); // version5253// Inet6Address54dos.writeByte(TC_OBJECT);55dos.writeByte(TC_CLASSDESC);56dos.writeUTF(className);57dos.writeLong(6880410070516793377L);58dos.writeByte(2);59dos.writeShort(0);60dos.writeByte(TC_ENDBLOCKDATA);61dos.writeByte(TC_NULL);62return baos.toByteArray();63}64}6566private static Object deserialize(final byte[] buffer)67throws IOException, ClassNotFoundException {68try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer))) {69return ois.readObject();70}71}7273void test(String className) throws IOException, ClassNotFoundException {74deserialize(serialize(className));75}76}7778public static void main(String[] args) throws IOException, ClassNotFoundException {79try {80new PayloadTest().test(Inet6Address.class.getName());81throw new RuntimeException("Expected exception not raised");82} catch (InvalidObjectException ioe) {83if (ioe.getMessage().contains("invalid address length")) {84System.out.println(String.format("Got expected exception: %s", ioe));85} else {86throw new RuntimeException("Expected exception not raised");87}88} catch (RuntimeException re) {89throw new RuntimeException("Expected exception not raised");90}91}92}939495