Serialize.java revision 3261
222N/A/*
222N/A * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
222N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
222N/A *
935N/A * This code is free software; you can redistribute it and/or modify it
222N/A * under the terms of the GNU General Public License version 2 only, as
222N/A * published by the Free Software Foundation.
919N/A *
919N/A * This code is distributed in the hope that it will be useful, but WITHOUT
919N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
919N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
919N/A * version 2 for more details (a copy is included in the LICENSE file that
919N/A * accompanied this code).
919N/A *
919N/A * You should have received a copy of the GNU General Public License version
919N/A * 2 along with this work; if not, write to the Free Software Foundation,
919N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
919N/A *
919N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
919N/A * or visit www.oracle.com if you need additional information or have any
919N/A * questions.
919N/A */
919N/A
919N/A/**
222N/A * @test
222N/A * @bug 4921029
222N/A * @bug 6656849
493N/A * @summary java.net.Inet6Address fails to be serialized with IPv6 support
493N/A * @summary NullPointerException thrown while de-serializing IPV6 Address.
222N/A */
970N/A
970N/Aimport java.net.*;
970N/Aimport java.io.*;
970N/Aimport java.util.*;
970N/A
970N/Apublic class Serialize {
970N/A
970N/A public static void main(String[] args)
222N/A throws Exception
837N/A {
222N/A Enumeration nifs = NetworkInterface.getNetworkInterfaces();
911N/A while (nifs.hasMoreElements()) {
911N/A NetworkInterface nif = (NetworkInterface)nifs.nextElement();
911N/A Enumeration addrs = nif.getInetAddresses();
911N/A while (addrs.hasMoreElements()) {
222N/A Object o = addrs.nextElement();
222N/A if (o instanceof Inet6Address) {
222N/A Inet6Address addr = (Inet6Address) o;
222N/A System.out.println ("serializing " + addr);
222N/A if (!test (addr)) {
222N/A throw new RuntimeException ("failed on " + addr.toString());
222N/A }
493N/A
837N/A /* now reconstruct address with string name
222N/A * and test again
222N/A */
837N/A byte[] bytes = addr.getAddress();
837N/A Inet6Address addr1 = Inet6Address.getByAddress ("foo", bytes, nif);
365N/A System.out.println ("serializing " + addr1);
222N/A if (!test (addr1)) {
355N/A throw new RuntimeException ("failed on " + addr1.toString());
355N/A }
355N/A }
355N/A }
837N/A }
837N/A
837N/A ObjectInputStream ois;
837N/A Inet6Address nobj;
837N/A
935N/A // check ::1 object serialised with 1.4.2 is readable
935N/A
935N/A File file = new File (System.getProperty("test.src"), "serial1.4.2.ser");
222N/A ois = new ObjectInputStream(new FileInputStream(file));
837N/A nobj = (Inet6Address) ois.readObject();
222N/A ois.close();
222N/A if (!nobj.equals (InetAddress.getByName ("::1"))) {
222N/A throw new RuntimeException ("old ::1 not deserialized right");
837N/A }
561N/A
365N/A System.out.println(nobj);
851N/A
837N/A // create an address with an unlikely numeric scope_id
365N/A if (!test ((Inet6Address)InetAddress.getByName ("fe80::1%99"))) {
365N/A throw new RuntimeException ("test failed on fe80::1%99");
365N/A }
365N/A
837N/A // Deserialize an Inet6 address with a named interface
561N/A file = new File (System.getProperty("test.src"), "serial-bge0.ser");
561N/A ois = new ObjectInputStream(new FileInputStream(file));
try {
nobj = (Inet6Address) ois.readObject();
} catch (NullPointerException e) {
throw new RuntimeException("6656849 Not fixed: NullPointer when deserializing");
} finally {
ois.close();
}
System.out.println(nobj);
System.out.println("All tests passed");
}
static boolean test (Inet6Address obj) throws Exception {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("i6a1.ser"));
oos.writeObject(obj);
oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("i6a1.ser"));
Inet6Address nobj = (Inet6Address) ois.readObject();
ois.close();
if (nobj.equals(obj)) {
return true;
} else {
return false;
}
}
}