Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Inet6Address/serialize/Inet6AddressSerTest.java
41152 views
1
/*
2
* Copyright (c) 2018, 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 java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.DataOutputStream;
27
import java.io.IOException;
28
import java.io.InvalidObjectException;
29
import java.io.ObjectInputStream;
30
import java.io.ObjectStreamConstants;
31
import static java.io.ObjectStreamConstants.STREAM_MAGIC;
32
import static java.io.ObjectStreamConstants.TC_CLASSDESC;
33
import static java.io.ObjectStreamConstants.TC_ENDBLOCKDATA;
34
import static java.io.ObjectStreamConstants.TC_NULL;
35
import static java.io.ObjectStreamConstants.TC_OBJECT;
36
import java.net.Inet6Address;
37
/**
38
* @test
39
* @bug 8194676
40
* @summary NullPointerException is thrown if ipaddress is not set.
41
* @run main Inet6AddressSerTest
42
*/
43
public class Inet6AddressSerTest implements ObjectStreamConstants {
44
45
static class PayloadTest {
46
47
private static byte[] serialize(String className) throws IOException {
48
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
49
DataOutputStream dos = new DataOutputStream(baos)) {
50
// stream headers
51
dos.writeShort(STREAM_MAGIC);
52
dos.writeShort(5); // version
53
54
// Inet6Address
55
dos.writeByte(TC_OBJECT);
56
dos.writeByte(TC_CLASSDESC);
57
dos.writeUTF(className);
58
dos.writeLong(6880410070516793377L);
59
dos.writeByte(2);
60
dos.writeShort(0);
61
dos.writeByte(TC_ENDBLOCKDATA);
62
dos.writeByte(TC_NULL);
63
return baos.toByteArray();
64
}
65
}
66
67
private static Object deserialize(final byte[] buffer)
68
throws IOException, ClassNotFoundException {
69
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer))) {
70
return ois.readObject();
71
}
72
}
73
74
void test(String className) throws IOException, ClassNotFoundException {
75
deserialize(serialize(className));
76
}
77
}
78
79
public static void main(String[] args) throws IOException, ClassNotFoundException {
80
try {
81
new PayloadTest().test(Inet6Address.class.getName());
82
throw new RuntimeException("Expected exception not raised");
83
} catch (InvalidObjectException ioe) {
84
if (ioe.getMessage().contains("invalid address length")) {
85
System.out.println(String.format("Got expected exception: %s", ioe));
86
} else {
87
throw new RuntimeException("Expected exception not raised");
88
}
89
} catch (RuntimeException re) {
90
throw new RuntimeException("Expected exception not raised");
91
}
92
}
93
}
94
95