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