Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/jar/B4957695.java
38867 views
/*1* Copyright (c) 2003, 2012, 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* @summary URLJarFile.retrieve does not delete tmpFile on IOException27*/2829import java.io.*;30import java.net.*;3132public class B4957695 {3334static Server server;3536static class Server extends Thread {37final ServerSocket srv;38static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n'};3940Server(ServerSocket s) {41srv = s;42}4344void readOneRequest(InputStream is) throws IOException {45int requestEndCount = 0, r;46while ((r = is.read()) != -1) {47if (r == requestEnd[requestEndCount]) {48requestEndCount++;49if (requestEndCount == 4) {50break;51}52} else {53requestEndCount = 0;54}55}56}5758public void run() {59try (Socket s = srv.accept()) {60// read HTTP request from client61readOneRequest(s.getInputStream());62try (OutputStreamWriter ow =63new OutputStreamWriter((s.getOutputStream()))) {64FileInputStream fin = new FileInputStream(new File(65System.getProperty("test.src", "."), "foo1.jar"));66int length = fin.available();67byte[] b = new byte[length-10];68fin.read(b, 0, length-10);69ow.write("HTTP/1.0 200 OK\r\n");7071// Note: The client expects length bytes.72ow.write("Content-Length: " + length + "\r\n");73ow.write("Content-Type: text/html\r\n");74ow.write("\r\n");7576// Note: The (buggy) server only sends length-10 bytes.77ow.write(new String(b));78ow.flush();79}80} catch (IOException e) {81e.printStackTrace();82}83}84}8586static void read (InputStream is) throws IOException {87int c,len=0;88while ((c=is.read()) != -1) {89len += c;90}91System.out.println ("read " + len + " bytes");92}9394public static void main (String[] args) throws Exception {95String tmpdir = System.getProperty("java.io.tmpdir");96String[] list1 = listTmpFiles(tmpdir);97ServerSocket serverSocket = new ServerSocket(0);98server = new Server(serverSocket);99server.start();100int port = serverSocket.getLocalPort();101System.out.println ("Server: listening on port: " + port);102URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT");103try {104URLConnection urlc = url.openConnection ();105InputStream is = urlc.getInputStream();106read (is);107is.close();108} catch (IOException e) {109System.out.println ("Received IOException as expected");110}111String[] list2 = listTmpFiles(tmpdir);112if (!sameList (list1, list2)) {113throw new RuntimeException ("some jar_cache files left behind");114}115}116117static String[] listTmpFiles (String d) {118File dir = new File (d);119return dir.list (new FilenameFilter () {120public boolean accept (File dr, String name) {121return (name.startsWith ("jar_cache"));122}123});124}125126static boolean sameList (String[] list1, String[] list2) {127if (list1.length != list2.length) {128return false;129}130for (int i=0; i<list1.length; i++) {131String s1 = list1[i];132String s2 = list2[i];133if ((s1 == null && s2 != null)) {134return false;135} else if ((s2 == null && s1 != null)) {136return false;137} else if (s1 == null) {138return true;139} else if (!s1.equals(s2)) {140return false;141}142}143return true;144}145}146147148