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