1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
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  *
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.
22  */
23 
24 /*
25  * @test
26  * @bug 8225037
27  * @library /test/lib
28  * @summary Basic test for java.net.JarURLConnection default behavior
29  * @run testng/othervm TestDefaultBehavior
30  */
31 
32 import java.io.IOException;
33 import java.net.MalformedURLException;
34 import java.net.JarURLConnection;
35 import java.net.URI;
36 import java.net.URL;
37 import java.net.URLConnection;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.util.jar.JarFile;
41 import jdk.test.lib.util.JarUtils;
42 import org.testng.annotations.BeforeTest;
43 import org.testng.annotations.Test;
44 import static org.testng.Assert.assertEquals;
45 import static org.testng.Assert.assertNotNull;
46 
47 public class TestDefaultBehavior {
48 
49     // Disable caching and create three jar files:
50     //   1. jar without a manifest
51     //   2. jar with a manifest
52     //   3. jar with manifest that includes an entry attribute
53     @BeforeTest
setup()54     public void setup() throws Exception {
55         URLConnection.setDefaultUseCaches("jar", false);
56         URLConnection.setDefaultUseCaches("file", false);
57 
58         Path foo = Path.of("foo.txt");
59         Files.writeString(foo, "Hello there");
60 
61         Files.createDirectory(Path.of("META-INF"));
62         Path manifest = Path.of("META-INF/MANIFEST.MF");
63         Files.writeString(manifest, "Manifest-Version: 5.5\n");
64 
65         JarUtils.createJarFile(Path.of("test.jar"), Path.of("."), foo);
66         JarUtils.createJarFile(Path.of("testWithManifest.jar"), Path.of("."), manifest, foo);
67 
68         Files.writeString(manifest, "Manifest-Version: 7.7\n\n" +       // main-section
69                                          "Name: foo.txt\nGreeting: true\n"); // individual-section
70         JarUtils.createJarFile(Path.of("testWithManifestAndAttr.jar"), Path.of("."), manifest, foo);
71     }
72 
73     @Test
noEntry()74     public void noEntry() throws Exception {
75         URI fileURI = Path.of("test.jar").toUri();
76         URL jarFileURL = URI.create("jar:" + fileURI + "!/").toURL();
77         JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);
78 
79         assertEquals(jarURLConnection.getAttributes(), null);
80         assertEquals(jarURLConnection.getCertificates(), null);
81         assertEquals(jarURLConnection.getEntryName(), null);
82         assertEquals(jarURLConnection.getJarEntry(), null);
83         assertNotNull(jarURLConnection.getJarFile());
84         assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());
85         assertEquals(jarURLConnection.getMainAttributes(), null);
86         assertEquals(jarURLConnection.getManifest(), null);
87     }
88 
89     @Test
withEntry()90     public void withEntry() throws Exception {
91         URI fileURI = Path.of("test.jar").toUri();
92         URL jarFileURL = URI.create("jar:" + fileURI + "!/foo.txt").toURL();
93         JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);
94 
95         assertEquals(jarURLConnection.getAttributes(), null);
96         assertEquals(jarURLConnection.getCertificates(), null);
97         assertEquals(jarURLConnection.getEntryName(), "foo.txt");
98         assertEquals(jarURLConnection.getJarEntry().getName(), "foo.txt");
99         assertNotNull(jarURLConnection.getJarFile());
100         assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());
101         assertEquals(jarURLConnection.getMainAttributes(), null);
102         assertEquals(jarURLConnection.getManifest(), null);
103     }
104 
105     @Test
manifestNoEntry()106     public void manifestNoEntry() throws Exception {
107         URI fileURI = Path.of("testWithManifest.jar").toUri();
108         URL jarFileURL = URI.create("jar:" + fileURI + "!/").toURL();
109         JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);
110 
111         assertEquals(jarURLConnection.getAttributes(), null);
112         assertEquals(jarURLConnection.getCertificates(), null);
113         assertEquals(jarURLConnection.getEntryName(), null);
114         assertEquals(jarURLConnection.getJarEntry(), null);
115         assertNotNull(jarURLConnection.getJarFile());
116         assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());
117         assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "5.5");
118         assertNotNull(jarURLConnection.getManifest());
119     }
120 
121     @Test
manifestWithEntry()122     public void manifestWithEntry() throws Exception {
123         URI fileURI = Path.of("testWithManifest.jar").toUri();
124         URL jarFileURL = URI.create("jar:" + fileURI + "!/foo.txt").toURL();
125         JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);
126 
127         assertEquals(jarURLConnection.getAttributes(), null);
128         assertEquals(jarURLConnection.getCertificates(), null);
129         assertEquals(jarURLConnection.getEntryName(), "foo.txt");
130         assertEquals(jarURLConnection.getJarEntry().getName(), "foo.txt");
131         assertNotNull(jarURLConnection.getJarFile());
132         assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());
133         assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "5.5");
134         assertNotNull(jarURLConnection.getManifest());
135     }
136 
137     @Test
manifestNoEntryAttr()138     public void manifestNoEntryAttr() throws Exception {
139         URI fileURI = Path.of("testWithManifestAndAttr.jar").toUri();
140         URL jarFileURL = URI.create("jar:" + fileURI + "!/").toURL();
141         JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);
142 
143         assertEquals(jarURLConnection.getAttributes(), null);
144         assertEquals(jarURLConnection.getCertificates(), null);
145         assertEquals(jarURLConnection.getEntryName(), null);
146         assertEquals(jarURLConnection.getJarEntry(), null);
147         assertNotNull(jarURLConnection.getJarFile());
148         assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());
149         assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "7.7");
150         assertNotNull(jarURLConnection.getManifest());
151     }
152 
153     @Test
manifestWithEntryAttr()154     public void manifestWithEntryAttr() throws Exception {
155         URI fileURI = Path.of("testWithManifestAndAttr.jar").toUri();
156         URL jarFileURL = URI.create("jar:" + fileURI + "!/foo.txt").toURL();
157         JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);
158 
159         assertEquals(jarURLConnection.getAttributes().getValue("Greeting"), "true");
160         assertEquals(jarURLConnection.getCertificates(), null);
161         assertEquals(jarURLConnection.getEntryName(), "foo.txt");
162         assertEquals(jarURLConnection.getJarEntry().getName(), "foo.txt");
163         assertNotNull(jarURLConnection.getJarFile());
164         assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());
165         assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "7.7");
166         assertNotNull(jarURLConnection.getManifest());
167     }
168 
169     // A minimal JarURLConnection
170     static class CustomJarURLConnection extends JarURLConnection {
171         private final URL jarFileURL;
172         private JarFile jarFile;
173 
CustomJarURLConnection(URL url)174         CustomJarURLConnection(URL url) throws MalformedURLException {
175             super(url);
176             jarFileURL = url;
177         }
178 
179         @Override
getJarFile()180         public JarFile getJarFile() throws IOException {
181             if (jarFile == null)
182                 connect();
183             return jarFile;
184         }
185 
186         @Override
connect()187         public void connect() throws IOException {
188             jarFile = ((JarURLConnection)jarFileURL.openConnection()).getJarFile();
189         }
190     }
191 }
192