Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/AddressTest.java
41152 views
1
/*
2
* Copyright (c) 2001, 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
/*
25
* @test
26
* @bug 4507501
27
* @library /test/lib
28
* @summary Test various methods that should throw IAE when passed improper
29
* SocketAddress
30
* @run main AddressTest
31
* @run main/othervm -Djava.net.preferIPv4Stack=true AddressTest
32
* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl AddressTest
33
*/
34
35
import java.net.*;
36
import jdk.test.lib.net.IPSupport;
37
38
public class AddressTest {
39
class MySocketAddress extends SocketAddress {
40
public MySocketAddress() {
41
}
42
}
43
44
public AddressTest() throws Exception {
45
SocketAddress addr = new MySocketAddress();
46
Socket soc = new Socket();
47
ServerSocket serv = new ServerSocket();
48
DatagramSocket ds = new DatagramSocket((SocketAddress)null);
49
DatagramPacket pac = new DatagramPacket(new byte[20], 20);
50
MulticastSocket mul = new MulticastSocket((SocketAddress) null);
51
boolean ok = false;
52
try {
53
soc.bind(addr);
54
} catch (IllegalArgumentException e) {
55
ok = true;
56
} catch (Exception e2) {
57
}
58
if (!ok)
59
throw new RuntimeException("Socket.bind should throw IllegalArgumentException!");
60
61
ok = false;
62
soc.close();
63
soc = new Socket();
64
try {
65
soc.connect(addr, 100);
66
} catch (IllegalArgumentException e) {
67
ok = true;
68
} catch (Exception e2) {
69
}
70
if (!ok)
71
throw new RuntimeException("Socket.connect should throw IllegalArgumentException!");
72
73
ok = false;
74
try {
75
serv.bind(addr);
76
} catch (IllegalArgumentException e) {
77
ok = true;
78
} catch (Exception e2) {
79
}
80
if (!ok)
81
throw new RuntimeException("ServerSocket.bind should throw IllegalArgumentException!");
82
83
ok = false;
84
85
try {
86
pac.setSocketAddress(addr);
87
} catch (IllegalArgumentException e) {
88
ok = true;
89
} catch (Exception e2) {
90
}
91
92
if (!ok)
93
throw new RuntimeException("DatagramPacket.setSocketAddress should throw IllegalArgumentException");
94
95
ok = false;
96
97
try {
98
ds.bind(addr);
99
} catch (IllegalArgumentException e) {
100
ok = true;
101
} catch (Exception e2) {
102
}
103
104
if (!ok)
105
throw new RuntimeException("DatagramSocket.bind should throw IllegalArgumentException");
106
107
ok = false;
108
109
try {
110
ds.connect(addr);
111
} catch (IllegalArgumentException e) {
112
ok = true;
113
} catch (Exception e2) {
114
}
115
116
if (!ok)
117
throw new RuntimeException("DatagramSocket.connect should throw IllegalArgumentException");
118
119
ok = false;
120
121
try {
122
mul.bind(addr);
123
} catch (IllegalArgumentException e) {
124
ok = true;
125
} catch (Exception e2) {
126
}
127
128
if (!ok)
129
throw new RuntimeException("MulticastSocket.bind should throw IllegalArgumentException");
130
131
ok = false;
132
133
mul.close();
134
mul = new MulticastSocket(new InetSocketAddress(0));
135
try {
136
mul.joinGroup(addr, null);
137
} catch (IllegalArgumentException e) {
138
ok = true;
139
} catch (Exception e2) {
140
}
141
142
if (!ok)
143
throw new RuntimeException("MulticastSocket.joinGroup should throw IllegalArgumentException");
144
145
ok = false;
146
try {
147
mul.leaveGroup(addr, null);
148
} catch (IllegalArgumentException e) {
149
ok = true;
150
} catch (Exception e2) {
151
}
152
153
if (!ok)
154
throw new RuntimeException("MulticastSocket.leaveGroup should throw IllegalArgumentException");
155
156
}
157
158
public static void main(String[] args) throws Exception {
159
IPSupport.throwSkippedExceptionIfNonOperational();
160
new AddressTest();
161
}
162
}
163
164