1 /*
   2  * Copyright (c) 2020, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import org.testng.annotations.Test;
  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.ObjectOutputStream;
  33 import java.io.ObjectStreamClass;
  34 import java.io.Serializable;
  35 import java.nio.channels.UnixDomainSocketAddress;
  36 import java.nio.file.Path;
  37 import static java.io.ObjectStreamConstants.*;
  38 import static org.testng.Assert.assertEquals;
  39 import static org.testng.Assert.assertTrue;
  40 import static org.testng.Assert.expectThrows;
  41 
  42 /*
  43  * @test
  44  * @summary UnixDomainSocketAddress serialization test
  45  * @run testng/othervm UnixDomainSocketAddressSerializationTest
  46  */
  47 
  48 @Test
  49 public class UnixDomainSocketAddressSerializationTest {
  50     private static final UnixDomainSocketAddress addr =
  51             UnixDomainSocketAddress.of(Path.of("test.sock"));
  52 
  53     public static void test() throws Exception {
  54         assertTrue(addr instanceof Serializable);
  55 
  56         byte[] serialized = serialize(addr);
  57         assertTrue(serialized.length > 0);
  58 
  59         UnixDomainSocketAddress deserialized =
  60                 deserialize(serialized, UnixDomainSocketAddress.class);
  61         assertEquals(deserialized.getPath(), addr.getPath());
  62         assertEquals(deserialized.toString(), addr.toString());
  63         assertEquals(deserialized.hashCode(), addr.hashCode());
  64         assertEquals(deserialized, addr);
  65     }
  66 
  67     static final Class<InvalidObjectException> IOE = InvalidObjectException.class;
  68 
  69     /** Tests that UnixDomainSocketAddress in the byte-stream is disallowed. */
  70     public static void testUnixDomainSocketAddressInStream() {
  71         expectThrows(IOE, () -> deserialize(byteStream(), UnixDomainSocketAddress.class));
  72     }
  73 
  74     private static <T extends Serializable> byte[] serialize(T t)
  75             throws IOException {
  76         ByteArrayOutputStream bos = new ByteArrayOutputStream();
  77         ObjectOutputStream oos = new ObjectOutputStream(bos);
  78         oos.writeObject(t);
  79         oos.flush();
  80         oos.close();
  81         return bos.toByteArray();
  82     }
  83 
  84     private static <T extends Serializable> T deserialize(byte[] b, Class<T> cl)
  85             throws IOException, ClassNotFoundException {
  86         try (ObjectInputStream ois =
  87                      new ObjectInputStream(new ByteArrayInputStream(b))) {
  88             Object o = ois.readObject();
  89             return cl.cast(o);
  90         }
  91     }
  92 
  93     static final String CLASSNAME = UnixDomainSocketAddress.class.getName();
  94     static final long SUID = ObjectStreamClass.lookup(UnixDomainSocketAddress.class).getSerialVersionUID();
  95     /**
  96      * Returns a stream with UnixDomainSocketAddress in the stream. The
  97      * stream will have no stream field values.
  98      */
  99     static byte[] byteStream() throws Exception {
 100         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 101         DataOutputStream dos = new DataOutputStream(baos);
 102         dos.writeShort(STREAM_MAGIC);
 103         dos.writeShort(STREAM_VERSION);
 104         dos.writeByte(TC_OBJECT);
 105         dos.writeByte(TC_CLASSDESC);
 106         dos.writeUTF(CLASSNAME);
 107         dos.writeLong(SUID);
 108         dos.writeByte(SC_SERIALIZABLE);
 109         dos.writeShort(0);                // number of stream fields
 110         dos.writeByte(TC_ENDBLOCKDATA);   // no annotations
 111         dos.writeByte(TC_NULL);           // no superclasses
 112         dos.write(TC_ENDBLOCKDATA);       // end block - for SC_WRITE_METHOD
 113         dos.close();
 114         return baos.toByteArray();
 115     }
 116 }