Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/UnixDomainSocketAddress/UnixDomainSocketAddressSerializationTest.java
41149 views
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.net.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
static final Class<NullPointerException> NPE = NullPointerException.class;
69
70
/** Tests that UnixDomainSocketAddress in the byte-stream is disallowed. */
71
public static void testUnixDomainSocketAddressInStream() throws Exception {
72
long suid = ObjectStreamClass.lookup(UnixDomainSocketAddress.class).getSerialVersionUID();
73
byte[] bytes = byteStreamFor(UnixDomainSocketAddress.class.getName(), suid);
74
expectThrows(IOE, () -> deserialize(bytes, UnixDomainSocketAddress.class));
75
}
76
77
/** Tests that SerialProxy with a null/absent path value in the byte-stream is disallowed. */
78
public static void testSerialProxyNoStreamValues() throws Exception {
79
Class<?> c = Class.forName("java.net.UnixDomainSocketAddress$Ser");
80
long suid = ObjectStreamClass.lookup(c).getSerialVersionUID();
81
byte[] bytes = byteStreamFor(c.getName(), suid);
82
expectThrows(NPE, () -> deserialize(bytes, UnixDomainSocketAddress.class));
83
}
84
85
private static <T extends Serializable> byte[] serialize(T t)
86
throws IOException {
87
ByteArrayOutputStream bos = new ByteArrayOutputStream();
88
ObjectOutputStream oos = new ObjectOutputStream(bos);
89
oos.writeObject(t);
90
oos.flush();
91
oos.close();
92
return bos.toByteArray();
93
}
94
95
private static <T extends Serializable> T deserialize(byte[] b, Class<T> cl)
96
throws IOException, ClassNotFoundException {
97
try (ObjectInputStream ois =
98
new ObjectInputStream(new ByteArrayInputStream(b))) {
99
Object o = ois.readObject();
100
return cl.cast(o);
101
}
102
}
103
104
/**
105
* Returns a stream with the given classname and suid. The stream will have
106
* no stream field values.
107
*/
108
static byte[] byteStreamFor(String classname, long suid) throws Exception {
109
ByteArrayOutputStream baos = new ByteArrayOutputStream();
110
DataOutputStream dos = new DataOutputStream(baos);
111
dos.writeShort(STREAM_MAGIC);
112
dos.writeShort(STREAM_VERSION);
113
dos.writeByte(TC_OBJECT);
114
dos.writeByte(TC_CLASSDESC);
115
dos.writeUTF(classname);
116
dos.writeLong(suid);
117
dos.writeByte(SC_SERIALIZABLE);
118
dos.writeShort(0); // number of stream fields
119
dos.writeByte(TC_ENDBLOCKDATA); // no annotations
120
dos.writeByte(TC_NULL); // no superclasses
121
dos.write(TC_ENDBLOCKDATA); // end block - for SC_WRITE_METHOD
122
dos.close();
123
return baos.toByteArray();
124
}
125
}
126
127