test/jdk/sun/net/www/protocol/jar/B4957695.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
child 58108 5302477c8285
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary URLJarFile.retrieve does not delete tmpFile on IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class B4957695 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    static void read (InputStream is) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        int c,len=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        while ((c=is.read()) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            len += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.out.println ("read " + len + " bytes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        String tmpdir = System.getProperty("java.io.tmpdir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println ("Server: listening on port: " + port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        URL url = new URL ("jar:http://localhost:"+port+"!/COPYRIGHT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            URLConnection urlc = url.openConnection ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            InputStream is = urlc.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            read (is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.out.println ("Received IOException as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String[] list2 = listTmpFiles(tmpdir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (!sameList (list1, list2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new RuntimeException ("some jar_cache files left behind");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    static String[] listTmpFiles (String d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        File dir = new File (d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return dir.list (new FilenameFilter () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            public boolean accept (File dr, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                return (name.startsWith ("jar_cache"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static boolean sameList (String[] list1, String[] list2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (list1.length != list2.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (int i=0; i<list1.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            String s1 = list1[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            String s2 = list2[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if ((s1 == null && s2 != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if ((s2 == null && s1 != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            } else if (s1 == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else if (!s1.equals(s2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}