Path: blob/master/test/jdk/java/net/UnixDomainSocketAddress/UnixDomainSocketAddressSerializationTest.java
41149 views
/*1* Copyright (c) 2020, 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 org.testng.annotations.Test;2425import java.io.ByteArrayInputStream;26import java.io.ByteArrayOutputStream;27import java.io.DataOutputStream;28import java.io.IOException;29import java.io.InvalidObjectException;30import java.io.ObjectInputStream;31import java.io.ObjectOutputStream;32import java.io.ObjectStreamClass;33import java.io.Serializable;34import java.net.UnixDomainSocketAddress;35import java.nio.file.Path;36import static java.io.ObjectStreamConstants.*;37import static org.testng.Assert.assertEquals;38import static org.testng.Assert.assertTrue;39import static org.testng.Assert.expectThrows;4041/*42* @test43* @summary UnixDomainSocketAddress serialization test44* @run testng/othervm UnixDomainSocketAddressSerializationTest45*/4647@Test48public class UnixDomainSocketAddressSerializationTest {49private static final UnixDomainSocketAddress addr =50UnixDomainSocketAddress.of(Path.of("test.sock"));5152public static void test() throws Exception {53assertTrue(addr instanceof Serializable);5455byte[] serialized = serialize(addr);56assertTrue(serialized.length > 0);5758UnixDomainSocketAddress deserialized =59deserialize(serialized, UnixDomainSocketAddress.class);60assertEquals(deserialized.getPath(), addr.getPath());61assertEquals(deserialized.toString(), addr.toString());62assertEquals(deserialized.hashCode(), addr.hashCode());63assertEquals(deserialized, addr);64}6566static final Class<InvalidObjectException> IOE = InvalidObjectException.class;67static final Class<NullPointerException> NPE = NullPointerException.class;6869/** Tests that UnixDomainSocketAddress in the byte-stream is disallowed. */70public static void testUnixDomainSocketAddressInStream() throws Exception {71long suid = ObjectStreamClass.lookup(UnixDomainSocketAddress.class).getSerialVersionUID();72byte[] bytes = byteStreamFor(UnixDomainSocketAddress.class.getName(), suid);73expectThrows(IOE, () -> deserialize(bytes, UnixDomainSocketAddress.class));74}7576/** Tests that SerialProxy with a null/absent path value in the byte-stream is disallowed. */77public static void testSerialProxyNoStreamValues() throws Exception {78Class<?> c = Class.forName("java.net.UnixDomainSocketAddress$Ser");79long suid = ObjectStreamClass.lookup(c).getSerialVersionUID();80byte[] bytes = byteStreamFor(c.getName(), suid);81expectThrows(NPE, () -> deserialize(bytes, UnixDomainSocketAddress.class));82}8384private static <T extends Serializable> byte[] serialize(T t)85throws IOException {86ByteArrayOutputStream bos = new ByteArrayOutputStream();87ObjectOutputStream oos = new ObjectOutputStream(bos);88oos.writeObject(t);89oos.flush();90oos.close();91return bos.toByteArray();92}9394private static <T extends Serializable> T deserialize(byte[] b, Class<T> cl)95throws IOException, ClassNotFoundException {96try (ObjectInputStream ois =97new ObjectInputStream(new ByteArrayInputStream(b))) {98Object o = ois.readObject();99return cl.cast(o);100}101}102103/**104* Returns a stream with the given classname and suid. The stream will have105* no stream field values.106*/107static byte[] byteStreamFor(String classname, long suid) throws Exception {108ByteArrayOutputStream baos = new ByteArrayOutputStream();109DataOutputStream dos = new DataOutputStream(baos);110dos.writeShort(STREAM_MAGIC);111dos.writeShort(STREAM_VERSION);112dos.writeByte(TC_OBJECT);113dos.writeByte(TC_CLASSDESC);114dos.writeUTF(classname);115dos.writeLong(suid);116dos.writeByte(SC_SERIALIZABLE);117dos.writeShort(0); // number of stream fields118dos.writeByte(TC_ENDBLOCKDATA); // no annotations119dos.writeByte(TC_NULL); // no superclasses120dos.write(TC_ENDBLOCKDATA); // end block - for SC_WRITE_METHOD121dos.close();122return baos.toByteArray();123}124}125126127