Path: blob/master/test/jdk/sun/net/www/protocol/jar/B4957695.java
41159 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 495769526* @run main/othervm -Djava.io.tmpdir=. B495769527* @summary URLJarFile.retrieve does not delete tmpFile on IOException28*/2930import java.io.*;31import java.net.*;3233public class B4957695 {3435static Server server;3637static class Server extends Thread {38final ServerSocket srv;39static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'};4041Server(ServerSocket s) {42srv = s;43}4445void readOneRequest(InputStream is) throws IOException {46int requestEndCount = 0, r;47while ((r = is.read()) != -1) {48if (r == requestEnd[requestEndCount]) {49requestEndCount++;50if (requestEndCount == 4) {51break;52}53} else {54requestEndCount = 0;55}56}57}5859public void run() {60try (Socket s = srv.accept()) {61// read HTTP request from client62readOneRequest(s.getInputStream());63try (OutputStreamWriter ow =64new OutputStreamWriter((s.getOutputStream()))) {65FileInputStream fin = new FileInputStream(new File(66System.getProperty("test.src", "."), "foo1.jar"));67int length = fin.available();68byte[] b = new byte[length-10];69fin.read(b, 0, length-10);70ow.write("HTTP/1.0 200 OK\r\n");7172// Note: The client expects length bytes.73ow.write("Content-Length: " + length + "\r\n");74ow.write("Content-Type: text/html\r\n");75ow.write("\r\n");7677// Note: The (buggy) server only sends length-10 bytes.78ow.write(new String(b));79ow.flush();80}81} catch (IOException e) {82e.printStackTrace();83}84}85}8687static void read (InputStream is) throws IOException {88int c,len=0;89while ((c=is.read()) != -1) {90len += c;91}92System.out.println ("read " + len + " bytes");93}9495public static void main (String[] args) throws Exception {96String tmpdir = System.getProperty("java.io.tmpdir");97String[] list1 = listTmpFiles(tmpdir);98InetAddress localHost = InetAddress.getByName("localhost");99InetSocketAddress address = new InetSocketAddress(localHost, 0);100ServerSocket serverSocket = new ServerSocket();101serverSocket.bind(address);102server = new Server(serverSocket);103server.start();104int port = serverSocket.getLocalPort();105System.out.println ("Server: listening on port: " + port);106URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT");107try {108URLConnection urlc = url.openConnection ();109InputStream is = urlc.getInputStream();110read (is);111is.close();112} catch (IOException e) {113System.out.println ("Received IOException as expected: " + e);114} finally {115try {serverSocket.close();} catch (IOException x) {}116}117String[] list2 = listTmpFiles(tmpdir);118if (!sameList (list1, list2)) {119throw new RuntimeException ("some jar_cache files left behind");120}121}122123static String[] listTmpFiles (String d) {124File dir = new File (d);125return dir.list (new FilenameFilter () {126public boolean accept (File dr, String name) {127return (name.startsWith ("jar_cache"));128}129});130}131132static boolean sameList (String[] list1, String[] list2) {133if (list1.length != list2.length) {134return false;135}136for (int i=0; i<list1.length; i++) {137String s1 = list1[i];138String s2 = list2[i];139if ((s1 == null && s2 != null)) {140return false;141} else if ((s2 == null && s1 != null)) {142return false;143} else if (s1 == null) {144return true;145} else if (!s1.equals(s2)) {146return false;147}148}149return true;150}151}152153154