author | mfang |
Wed, 17 Aug 2011 14:18:26 -0700 | |
changeset 10294 | 8fcdae2a7ec7 |
parent 7668 | d4a77089c587 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 4092038 |
|
27 |
* @summary TCP Urgent data support |
|
28 |
* |
|
29 |
*/ |
|
30 |
||
31 |
import java.net.*; |
|
32 |
import java.io.*; |
|
33 |
||
34 |
public class UrgentDataTest { |
|
35 |
||
36 |
Object opref; |
|
37 |
boolean isClient, isServer; |
|
38 |
String clHost; |
|
39 |
ServerSocket listener; |
|
40 |
Socket client, server; |
|
41 |
int clPort; |
|
42 |
InputStream clis, sis; |
|
43 |
OutputStream clos, sos; |
|
44 |
||
45 |
static void usage () { |
|
46 |
System.out.println (" usage: java UrgentDataTest <runs client and server together>"); |
|
47 |
System.out.println (" usage: java UrgentDataTest -server <runs server alone>"); |
|
48 |
System.out.println (" usage: java UrgentDataTest -client host port <runs client and connects to"+ |
|
49 |
" specified server>"); |
|
50 |
} |
|
51 |
||
52 |
public static void main (String args[]) { |
|
53 |
try { |
|
54 |
UrgentDataTest test = new UrgentDataTest (); |
|
55 |
if (args.length == 0) { |
|
56 |
test.listener = new ServerSocket (0); |
|
57 |
test.isClient = true; |
|
58 |
test.isServer = true; |
|
59 |
test.clHost = "127.0.0.1"; |
|
60 |
test.clPort = test.listener.getLocalPort(); |
|
61 |
test.run(); |
|
62 |
} else if (args[0].equals ("-server")) { |
|
63 |
test.listener = new ServerSocket (0); |
|
64 |
System.out.println ("Server listening on port " + test.listener.getLocalPort()); |
|
65 |
test.isClient = false; |
|
66 |
test.isServer = true; |
|
67 |
test.run(); |
|
68 |
System.out.println ("Server: Completed OK"); |
|
69 |
} else if (args[0].equals ("-client")) { |
|
70 |
test.isClient = true; |
|
71 |
test.isServer = false; |
|
72 |
test.clHost = args [1]; |
|
73 |
test.clPort = Integer.parseInt (args[2]); |
|
74 |
test.run(); |
|
75 |
System.out.println ("Client: Completed OK"); |
|
76 |
} else { |
|
77 |
usage (); |
|
78 |
} |
|
79 |
} |
|
80 |
catch (ArrayIndexOutOfBoundsException e) { |
|
81 |
usage(); |
|
82 |
} |
|
83 |
catch (NumberFormatException e) { |
|
84 |
usage(); |
|
85 |
} |
|
86 |
catch (Exception e) { |
|
87 |
e.printStackTrace (); |
|
88 |
throw new RuntimeException ("Exception caught"); |
|
89 |
} |
|
90 |
} |
|
91 |
||
92 |
public void run () throws Exception { |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
93 |
try { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
94 |
if (isClient) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
95 |
client = new Socket (clHost, clPort); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
96 |
clis = client.getInputStream(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
97 |
clos = client.getOutputStream(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
98 |
client.setOOBInline (true); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
99 |
if (client.getOOBInline() != true) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
100 |
throw new RuntimeException ("Setting OOBINLINE failed"); |
2 | 101 |
} |
102 |
} |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
103 |
if (isServer) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
104 |
server = listener.accept (); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
105 |
sis = server.getInputStream(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
106 |
sos = server.getOutputStream(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
107 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
108 |
if (isClient) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
109 |
clos.write ("Hello".getBytes ()); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
110 |
client.sendUrgentData (100); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
111 |
clos.write ("world".getBytes ()); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
112 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
113 |
// read Hello world from server (during which oob byte must have been dropped) |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
114 |
String s = "Helloworld"; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
115 |
if (isServer) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
116 |
for (int y=0; y<s.length(); y++) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
117 |
int c = sis.read (); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
118 |
if (c != (int)s.charAt (y)) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
119 |
throw new RuntimeException ("Unexpected character read"); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
120 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
121 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
122 |
// Do the same from server to client |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
123 |
sos.write ("Hello".getBytes ()); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
124 |
server.sendUrgentData (101); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
125 |
sos.write ("World".getBytes ()); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
126 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
127 |
if (isClient) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
128 |
// read Hello world from client (during which oob byte must have been read) |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
129 |
s="Hello"; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
130 |
for (int y=0; y<s.length(); y++) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
131 |
int c = clis.read (); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
132 |
if (c != (int)s.charAt (y)) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
133 |
throw new RuntimeException ("Unexpected character read"); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
134 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
135 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
136 |
if (clis.read() != 101) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
137 |
throw new RuntimeException ("OOB byte not received"); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
138 |
} |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
139 |
s="World"; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
140 |
for (int y=0; y<s.length(); y++) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
141 |
int c = clis.read (); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
142 |
if (c != (int)s.charAt (y)) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
143 |
throw new RuntimeException ("Unexpected character read"); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
144 |
} |
2 | 145 |
} |
146 |
} |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
147 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
148 |
if (listener != null) listener.close(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
149 |
if (client != null) client.close (); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
150 |
if (server != null) server.close (); |
2 | 151 |
} |
152 |
} |
|
153 |
} |