OldJarURLConnectionTest.java revision 51a2a0fe51532ca5f3ce872295e6de253eac07bf
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package libcore.java.net;
19
20import java.io.BufferedOutputStream;
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.InputStream;
25import java.net.JarURLConnection;
26import java.net.MalformedURLException;
27import java.net.URL;
28import java.net.URLConnection;
29import java.security.cert.Certificate;
30import java.util.Arrays;
31import java.util.HashSet;
32import java.util.Map;
33import java.util.jar.Attributes;
34import java.util.jar.JarEntry;
35import java.util.jar.JarFile;
36import java.util.jar.JarOutputStream;
37import java.util.jar.Manifest;
38import java.util.zip.ZipEntry;
39import java.util.zip.ZipFile;
40import tests.support.resource.Support_Resources;
41
42public class OldJarURLConnectionTest extends junit.framework.TestCase {
43
44    JarURLConnection juc;
45
46    private URL createContent(String jarFile, String inFile)
47                                                throws MalformedURLException {
48
49        File resources = Support_Resources.createTempFolder();
50
51        Support_Resources.copyFile(resources, "net", jarFile);
52        File file = new File(resources.toString() + "/net/" + jarFile);
53
54        return new URL("jar:file:" + file.getPath() + "!/" + inFile);
55    }
56
57    public void test_getAttributes() throws Exception {
58        URL u = createContent("lf.jar", "swt.dll");
59        juc = (JarURLConnection) u.openConnection();
60        java.util.jar.Attributes a = juc.getAttributes();
61        assertEquals("Returned incorrect Attributes", "SHA MD5", a
62                .get(new java.util.jar.Attributes.Name("Digest-Algorithms")));
63        URL invURL = createContent("InvalidJar.jar", "Test.class");
64
65        JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
66        try {
67            juConn.getAttributes();
68            fail("IOException was not thrown.");
69        } catch(java.io.IOException io) {
70            //expected
71        }
72    }
73
74    public void test_getCertificates() throws Exception {
75        URL u = createContent("TestCodeSigners.jar", "Test.class");
76
77        juc = (JarURLConnection) u.openConnection();
78        assertNull(juc.getCertificates());
79
80        JarEntry je = juc.getJarEntry();
81        JarFile jf = juc.getJarFile();
82        InputStream is = jf.getInputStream(je);
83        is.skip(je.getSize());
84
85        Certificate [] certs = juc.getCertificates();
86        assertEquals(3, certs.length);
87
88        URL invURL = createContent("InvalidJar.jar", "Test.class");
89
90        JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
91        try {
92            juConn.getCertificates();
93            fail("IOException was not thrown.");
94        } catch(java.io.IOException io) {
95            //expected
96        }
97    }
98
99    public void test_getManifest() throws Exception {
100        URL u = createContent("lf.jar", "swt.dll");
101
102        juc = (JarURLConnection) u.openConnection();
103        Manifest manifest = juc.getManifest();
104        Map<String, Attributes> attr = manifest.getEntries();
105        assertEquals(new HashSet<String>(Arrays.asList("plus.bmp", "swt.dll")),
106                attr.keySet());
107
108        URL invURL = createContent("InvalidJar.jar", "Test.class");
109
110        JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
111        try {
112            juConn.getManifest();
113            fail("IOException was not thrown.");
114        } catch(java.io.IOException io) {
115            //expected
116        }
117    }
118
119    public void test_getEntryName() throws Exception {
120        URL u = createContent("lf.jar", "plus.bmp");
121
122        juc = (JarURLConnection) u.openConnection();
123        assertEquals("Returned incorrect entryName", "plus.bmp", juc
124                .getEntryName());
125
126        u = createContent("lf.jar", "");
127
128        juc = (JarURLConnection) u.openConnection();
129        assertNull("Returned incorrect entryName", juc.getEntryName());
130
131        // Regression test for harmony-3053
132        URL url = new URL("jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
133        assertEquals("foo.jar!/Bugs/HelloWorld.class",((JarURLConnection)url.openConnection()).getEntryName());
134    }
135
136    public void test_getJarEntry() throws Exception {
137        URL u = createContent("lf.jar", "plus.bmp");
138
139        juc = (JarURLConnection) u.openConnection();
140        assertEquals("Returned incorrect JarEntry", "plus.bmp", juc
141                .getJarEntry().getName());
142
143        u = createContent("lf.jar", "");
144
145        juc = (JarURLConnection) u.openConnection();
146        assertNull("Returned incorrect JarEntry", juc.getJarEntry());
147
148        URL invURL = createContent("InvalidJar.jar", "Test.class");
149
150        JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
151        try {
152            juConn.getJarEntry();
153            fail("IOException was not thrown.");
154        } catch(java.io.IOException io) {
155            //expected
156        }
157    }
158
159    public void test_getJarFile() throws IOException {
160        URL url = createContent("lf.jar", "missing");
161
162        JarURLConnection connection = (JarURLConnection) url.openConnection();
163        try {
164            connection.connect();
165            fail("Did not throw exception on connect");
166        } catch (IOException e) {
167            // expected
168        }
169
170        try {
171            connection.getJarFile();
172            fail("Did not throw exception after connect");
173        } catch (IOException e) {
174            // expected
175        }
176
177        URL invURL = createContent("InvalidJar.jar", "Test.class");
178
179        JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
180        try {
181            juConn.getJarFile();
182            fail("IOException was not thrown.");
183        } catch(java.io.IOException io) {
184            //expected
185        }
186
187        File resources = Support_Resources.createTempFolder();
188
189        Support_Resources.copyFile(resources, null, "hyts_att.jar");
190        File file = new File(resources.toString() + "/hyts_att.jar");
191        URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
192        JarURLConnection con1 = (JarURLConnection) fUrl1.openConnection();
193        ZipFile jf1 = con1.getJarFile();
194        JarURLConnection con2 = (JarURLConnection) fUrl1.openConnection();
195        ZipFile jf2 = con2.getJarFile();
196        assertTrue("file: JarFiles not the same", jf1 == jf2);
197        jf1.close();
198        assertTrue("File should exist", file.exists());
199
200        fUrl1 = createContent("lf.jar", "");
201
202        con1 = (JarURLConnection) fUrl1.openConnection();
203        jf1 = con1.getJarFile();
204        con2 = (JarURLConnection) fUrl1.openConnection();
205        jf2 = con2.getJarFile();
206        assertTrue("http: JarFiles not the same", jf1 == jf2);
207        jf1.close();
208    }
209
210    public void test_getJarFile29() throws Exception {
211        File jarFile = File.createTempFile("1+2 3", "test.jar");
212        jarFile.deleteOnExit();
213        JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile));
214        out.putNextEntry(new ZipEntry("test"));
215        out.closeEntry();
216        out.close();
217
218        JarURLConnection conn = (JarURLConnection) new URL("jar:file:"
219                + jarFile.getAbsolutePath().replaceAll(" ", "%20") + "!/")
220                .openConnection();
221        conn.getJarFile().entries();
222    }
223
224    //Regression for HARMONY-3436
225    public void test_setUseCaches() throws Exception {
226        File resources = Support_Resources.createTempFolder();
227        Support_Resources.copyFile(resources, null, "hyts_att.jar");
228        File file = new File(resources.toString() + "/hyts_att.jar");
229        URL url = new URL("jar:file:" + file.getPath() + "!/HasAttributes.txt");
230
231        JarURLConnection connection = (JarURLConnection) url.openConnection();
232        connection.setUseCaches(false);
233        connection.getInputStream();
234        InputStream in = connection.getInputStream();
235        JarFile jarFile1 = connection.getJarFile();
236        JarEntry jarEntry1 = connection.getJarEntry();
237        in.read();
238        in.close();
239        JarFile jarFile2 = connection.getJarFile();
240        JarEntry jarEntry2 = connection.getJarEntry();
241        assertSame(jarFile1, jarFile2);
242        assertSame(jarEntry1, jarEntry2);
243
244        try {
245            connection.getInputStream();
246            fail("should throw IllegalStateException");
247        } catch (IllegalStateException e) {
248            // expected
249        }
250    }
251
252    public void test_getJarFileURL() throws Exception {
253        URL u = createContent("lf.jar", "plus.bmp");
254
255        URL fileURL = new URL(u.getPath().substring(0, u.getPath().indexOf("!")));
256
257        juc = (JarURLConnection) u.openConnection();
258        assertTrue("Returned incorrect file URL", juc.getJarFileURL().equals(
259                fileURL));
260
261        URL url = new URL("jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
262        String jarFileUrl = ((JarURLConnection) url.openConnection()).getJarFileURL().toString();
263        // The RI omits the empty authority "//" but the RFC doesn't say this is necessary
264        assertTrue(jarFileUrl.equals("file:///bar.jar") || jarFileUrl.equals("file:/bar.jar"));
265    }
266
267    public void test_getMainAttributes() throws Exception {
268        URL u = createContent("lf.jar", "swt.dll");
269
270        juc = (JarURLConnection) u.openConnection();
271        java.util.jar.Attributes a = juc.getMainAttributes();
272        assertEquals("Returned incorrect Attributes", "1.0", a
273                .get(java.util.jar.Attributes.Name.MANIFEST_VERSION));
274
275        URL invURL = createContent("InvalidJar.jar", "Test.class");
276
277        JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
278        try {
279            juConn.getMainAttributes();
280            fail("IOException was not thrown.");
281        } catch(java.io.IOException io) {
282            //expected
283        }
284    }
285
286    public void test_getInputStream_DeleteJarFileUsingURLConnection()
287            throws Exception {
288        String entry = "text.txt";
289        String cts = System.getProperty("java.io.tmpdir");
290        File tmpDir = new File(cts);
291        File jarFile = File.createTempFile("file", ".jar", tmpDir);
292        String jarFileName = jarFile.getPath();
293        FileOutputStream jarFileOutputStream = new FileOutputStream(jarFileName);
294        JarOutputStream out = new JarOutputStream(new BufferedOutputStream(
295                jarFileOutputStream));
296        JarEntry jarEntry = new JarEntry(entry);
297        out.putNextEntry(jarEntry);
298        out.write(new byte[] { 'a', 'b', 'c' });
299        out.close();
300
301        URL url = new URL("jar:file:" + jarFileName + "!/" + entry);
302        URLConnection conn = url.openConnection();
303        conn.setUseCaches(false);
304        InputStream is = conn.getInputStream();
305        is.close();
306
307        assertTrue(jarFile.delete());
308    }
309
310    public void test_Constructor() {
311        try {
312            String jarFileName = "file.jar";
313            String entry = "text.txt";
314            URL url = new URL("jar:file:" + jarFileName + "!/" + entry);
315            TestJarURLConnection jarConn = new TestJarURLConnection(url);
316            assertEquals(new URL("file:file.jar"), jarConn.getJarFileURL());
317        } catch(MalformedURLException me) {
318            fail("MalformedURLException was thrown.");
319        }
320
321        try {
322            URL [] urls = {new URL("file:file.jar"),
323                           new URL("http://foo.com/foo/foo.jar")};
324
325            for(URL url : urls) {
326                try {
327                    new TestJarURLConnection(url);
328                    fail("MalformedURLException was not thrown.");
329                } catch(MalformedURLException me) {
330                    //expected
331                }
332            }
333        } catch(MalformedURLException me) {
334            fail("MalformedURLException was thrown.");
335        }
336    }
337
338    class TestJarURLConnection extends JarURLConnection {
339
340        protected TestJarURLConnection(URL arg0) throws MalformedURLException {
341            super(arg0);
342        }
343
344        @Override
345        public JarFile getJarFile() throws IOException {
346            return null;
347        }
348
349        @Override
350        public void connect() throws IOException {
351
352        }
353    }
354}
355
356