author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
child 58108 | 5302477c8285 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
41593
f68910c5c9f5
8146257: sun/net/www/protocol/jar/B4957695.java fails intermittently with java.lang.RuntimeException: some jar_cache files left behind
amlu
parents:
12541
diff
changeset
|
2 |
* Copyright (c) 2003, 2016, 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 4957695 |
|
41593
f68910c5c9f5
8146257: sun/net/www/protocol/jar/B4957695.java fails intermittently with java.lang.RuntimeException: some jar_cache files left behind
amlu
parents:
12541
diff
changeset
|
27 |
* @run main/othervm -Djava.io.tmpdir=. B4957695 |
2 | 28 |
* @summary URLJarFile.retrieve does not delete tmpFile on IOException |
29 |
*/ |
|
30 |
||
31 |
import java.io.*; |
|
32 |
import java.net.*; |
|
33 |
||
34 |
public class B4957695 { |
|
35 |
||
12447
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
36 |
static Server server; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
37 |
|
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
38 |
static class Server extends Thread { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
39 |
final ServerSocket srv; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
40 |
static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'}; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
41 |
|
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
42 |
Server(ServerSocket s) { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
43 |
srv = s; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
44 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
45 |
|
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
46 |
void readOneRequest(InputStream is) throws IOException { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
47 |
int requestEndCount = 0, r; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
48 |
while ((r = is.read()) != -1) { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
49 |
if (r == requestEnd[requestEndCount]) { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
50 |
requestEndCount++; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
51 |
if (requestEndCount == 4) { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
52 |
break; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
53 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
54 |
} else { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
55 |
requestEndCount = 0; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
56 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
57 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
58 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
59 |
|
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
60 |
public void run() { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
61 |
try (Socket s = srv.accept()) { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
62 |
// read HTTP request from client |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
63 |
readOneRequest(s.getInputStream()); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
64 |
try (OutputStreamWriter ow = |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
65 |
new OutputStreamWriter((s.getOutputStream()))) { |
12541
de11282aa666
7162385: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing again
khazra
parents:
12447
diff
changeset
|
66 |
FileInputStream fin = new FileInputStream(new File( |
de11282aa666
7162385: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing again
khazra
parents:
12447
diff
changeset
|
67 |
System.getProperty("test.src", "."), "foo1.jar")); |
12447
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
68 |
int length = fin.available(); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
69 |
byte[] b = new byte[length-10]; |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
70 |
fin.read(b, 0, length-10); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
71 |
ow.write("HTTP/1.0 200 OK\r\n"); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
72 |
|
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
73 |
// Note: The client expects length bytes. |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
74 |
ow.write("Content-Length: " + length + "\r\n"); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
75 |
ow.write("Content-Type: text/html\r\n"); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
76 |
ow.write("\r\n"); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
77 |
|
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
78 |
// Note: The (buggy) server only sends length-10 bytes. |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
79 |
ow.write(new String(b)); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
80 |
ow.flush(); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
81 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
82 |
} catch (IOException e) { |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
83 |
e.printStackTrace(); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
84 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
85 |
} |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
86 |
} |
2 | 87 |
|
88 |
static void read (InputStream is) throws IOException { |
|
89 |
int c,len=0; |
|
90 |
while ((c=is.read()) != -1) { |
|
91 |
len += c; |
|
92 |
} |
|
93 |
System.out.println ("read " + len + " bytes"); |
|
94 |
} |
|
95 |
||
96 |
public static void main (String[] args) throws Exception { |
|
97 |
String tmpdir = System.getProperty("java.io.tmpdir"); |
|
98 |
String[] list1 = listTmpFiles(tmpdir); |
|
12447
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
99 |
ServerSocket serverSocket = new ServerSocket(0); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
100 |
server = new Server(serverSocket); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
101 |
server.start(); |
92676a77a667
7152856: TEST_BUG: sun/net/www/protocol/jar/B4957695.java failing on Windows
khazra
parents:
5506
diff
changeset
|
102 |
int port = serverSocket.getLocalPort(); |
2 | 103 |
System.out.println ("Server: listening on port: " + port); |
104 |
URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT"); |
|
105 |
try { |
|
106 |
URLConnection urlc = url.openConnection (); |
|
107 |
InputStream is = urlc.getInputStream(); |
|
108 |
read (is); |
|
109 |
is.close(); |
|
110 |
} catch (IOException e) { |
|
111 |
System.out.println ("Received IOException as expected"); |
|
112 |
} |
|
113 |
String[] list2 = listTmpFiles(tmpdir); |
|
114 |
if (!sameList (list1, list2)) { |
|
115 |
throw new RuntimeException ("some jar_cache files left behind"); |
|
116 |
} |
|
117 |
} |
|
118 |
||
119 |
static String[] listTmpFiles (String d) { |
|
120 |
File dir = new File (d); |
|
121 |
return dir.list (new FilenameFilter () { |
|
122 |
public boolean accept (File dr, String name) { |
|
123 |
return (name.startsWith ("jar_cache")); |
|
124 |
} |
|
125 |
}); |
|
126 |
} |
|
127 |
||
128 |
static boolean sameList (String[] list1, String[] list2) { |
|
129 |
if (list1.length != list2.length) { |
|
130 |
return false; |
|
131 |
} |
|
132 |
for (int i=0; i<list1.length; i++) { |
|
133 |
String s1 = list1[i]; |
|
134 |
String s2 = list2[i]; |
|
135 |
if ((s1 == null && s2 != null)) { |
|
136 |
return false; |
|
137 |
} else if ((s2 == null && s1 != null)) { |
|
138 |
return false; |
|
139 |
} else if (s1 == null) { |
|
140 |
return true; |
|
141 |
} else if (!s1.equals(s2)) { |
|
142 |
return false; |
|
143 |
} |
|
144 |
} |
|
145 |
return true; |
|
146 |
} |
|
147 |
} |