PageRenderTime 28ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/luni/src/test/java/libcore/java/net/OldJarURLConnectionTest.java

https://github.com/MIPS/libcore
Java | 353 lines | 260 code | 64 blank | 29 comment | 3 complexity | 8b7f28faaaf3362234f8e2c91b25b568 MD5 | raw file
  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. package libcore.java.net;
  18. import java.io.BufferedOutputStream;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.JarURLConnection;
  24. import java.net.MalformedURLException;
  25. import java.net.URL;
  26. import java.net.URLConnection;
  27. import java.security.cert.Certificate;
  28. import java.util.Arrays;
  29. import java.util.HashSet;
  30. import java.util.Map;
  31. import java.util.jar.Attributes;
  32. import java.util.jar.JarEntry;
  33. import java.util.jar.JarFile;
  34. import java.util.jar.JarOutputStream;
  35. import java.util.jar.Manifest;
  36. import java.util.zip.ZipEntry;
  37. import java.util.zip.ZipFile;
  38. import tests.support.resource.Support_Resources;
  39. public class OldJarURLConnectionTest extends junit.framework.TestCase {
  40. JarURLConnection juc;
  41. private URL createContent(String jarFile, String inFile)
  42. throws MalformedURLException {
  43. File resources = Support_Resources.createTempFolder();
  44. Support_Resources.copyFile(resources, "net", jarFile);
  45. File file = new File(resources.toString() + "/net/" + jarFile);
  46. return new URL("jar:file:" + file.getPath() + "!/" + inFile);
  47. }
  48. public void test_getAttributes() throws Exception {
  49. URL u = createContent("lf.jar", "swt.dll");
  50. juc = (JarURLConnection) u.openConnection();
  51. java.util.jar.Attributes a = juc.getAttributes();
  52. assertEquals("Returned incorrect Attributes", "SHA MD5", a
  53. .get(new java.util.jar.Attributes.Name("Digest-Algorithms")));
  54. URL invURL = createContent("InvalidJar.jar", "Test.class");
  55. JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
  56. try {
  57. juConn.getAttributes();
  58. fail("IOException was not thrown.");
  59. } catch(java.io.IOException io) {
  60. //expected
  61. }
  62. }
  63. public void test_getCertificates() throws Exception {
  64. URL u = createContent("TestCodeSigners.jar", "Test.class");
  65. juc = (JarURLConnection) u.openConnection();
  66. assertNull(juc.getCertificates());
  67. JarEntry je = juc.getJarEntry();
  68. JarFile jf = juc.getJarFile();
  69. InputStream is = jf.getInputStream(je);
  70. is.skip(je.getSize());
  71. Certificate [] certs = juc.getCertificates();
  72. assertEquals(3, certs.length);
  73. URL invURL = createContent("InvalidJar.jar", "Test.class");
  74. JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
  75. try {
  76. juConn.getCertificates();
  77. fail("IOException was not thrown.");
  78. } catch(java.io.IOException io) {
  79. //expected
  80. }
  81. }
  82. public void test_getManifest() throws Exception {
  83. URL u = createContent("lf.jar", "swt.dll");
  84. juc = (JarURLConnection) u.openConnection();
  85. Manifest manifest = juc.getManifest();
  86. Map<String, Attributes> attr = manifest.getEntries();
  87. assertEquals(new HashSet<String>(Arrays.asList("plus.bmp", "swt.dll")),
  88. attr.keySet());
  89. URL invURL = createContent("InvalidJar.jar", "Test.class");
  90. JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
  91. try {
  92. juConn.getManifest();
  93. fail("IOException was not thrown.");
  94. } catch(java.io.IOException io) {
  95. //expected
  96. }
  97. }
  98. public void test_getEntryName() throws Exception {
  99. URL u = createContent("lf.jar", "plus.bmp");
  100. juc = (JarURLConnection) u.openConnection();
  101. assertEquals("Returned incorrect entryName", "plus.bmp", juc
  102. .getEntryName());
  103. u = createContent("lf.jar", "");
  104. juc = (JarURLConnection) u.openConnection();
  105. assertNull("Returned incorrect entryName", juc.getEntryName());
  106. // Regression test for harmony-3053
  107. URL url = new URL("jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
  108. assertEquals("foo.jar!/Bugs/HelloWorld.class",((JarURLConnection)url.openConnection()).getEntryName());
  109. }
  110. public void test_getJarEntry() throws Exception {
  111. URL u = createContent("lf.jar", "plus.bmp");
  112. juc = (JarURLConnection) u.openConnection();
  113. assertEquals("Returned incorrect JarEntry", "plus.bmp", juc
  114. .getJarEntry().getName());
  115. u = createContent("lf.jar", "");
  116. juc = (JarURLConnection) u.openConnection();
  117. assertNull("Returned incorrect JarEntry", juc.getJarEntry());
  118. URL invURL = createContent("InvalidJar.jar", "Test.class");
  119. JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
  120. try {
  121. juConn.getJarEntry();
  122. fail("IOException was not thrown.");
  123. } catch(java.io.IOException io) {
  124. //expected
  125. }
  126. }
  127. public void test_getJarFile() throws IOException {
  128. URL url = createContent("lf.jar", "missing");
  129. JarURLConnection connection = (JarURLConnection) url.openConnection();
  130. try {
  131. connection.connect();
  132. fail("Did not throw exception on connect");
  133. } catch (IOException e) {
  134. // expected
  135. }
  136. try {
  137. connection.getJarFile();
  138. fail("Did not throw exception after connect");
  139. } catch (IOException e) {
  140. // expected
  141. }
  142. URL invURL = createContent("InvalidJar.jar", "Test.class");
  143. JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
  144. try {
  145. juConn.getJarFile();
  146. fail("IOException was not thrown.");
  147. } catch(java.io.IOException io) {
  148. //expected
  149. }
  150. File resources = Support_Resources.createTempFolder();
  151. Support_Resources.copyFile(resources, null, "hyts_att.jar");
  152. File file = new File(resources.toString() + "/hyts_att.jar");
  153. URL fUrl1 = new URL("jar:file:" + file.getPath() + "!/");
  154. JarURLConnection con1 = (JarURLConnection) fUrl1.openConnection();
  155. ZipFile jf1 = con1.getJarFile();
  156. JarURLConnection con2 = (JarURLConnection) fUrl1.openConnection();
  157. ZipFile jf2 = con2.getJarFile();
  158. assertTrue("file: JarFiles not the same", jf1 == jf2);
  159. jf1.close();
  160. assertTrue("File should exist", file.exists());
  161. fUrl1 = createContent("lf.jar", "");
  162. con1 = (JarURLConnection) fUrl1.openConnection();
  163. jf1 = con1.getJarFile();
  164. con2 = (JarURLConnection) fUrl1.openConnection();
  165. jf2 = con2.getJarFile();
  166. assertTrue("http: JarFiles not the same", jf1 == jf2);
  167. jf1.close();
  168. }
  169. public void test_getJarFile29() throws Exception {
  170. File jarFile = File.createTempFile("1+2 3", "test.jar");
  171. jarFile.deleteOnExit();
  172. JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile));
  173. out.putNextEntry(new ZipEntry("test"));
  174. out.closeEntry();
  175. out.close();
  176. JarURLConnection conn = (JarURLConnection) new URL("jar:file:"
  177. + jarFile.getAbsolutePath().replaceAll(" ", "%20") + "!/")
  178. .openConnection();
  179. conn.getJarFile().entries();
  180. }
  181. //Regression for HARMONY-3436
  182. public void test_setUseCaches() throws Exception {
  183. File resources = Support_Resources.createTempFolder();
  184. Support_Resources.copyFile(resources, null, "hyts_att.jar");
  185. File file = new File(resources.toString() + "/hyts_att.jar");
  186. URL url = new URL("jar:file:" + file.getPath() + "!/HasAttributes.txt");
  187. JarURLConnection connection = (JarURLConnection) url.openConnection();
  188. connection.setUseCaches(false);
  189. connection.getInputStream();
  190. InputStream in = connection.getInputStream();
  191. JarFile jarFile1 = connection.getJarFile();
  192. JarEntry jarEntry1 = connection.getJarEntry();
  193. in.read();
  194. in.close();
  195. JarFile jarFile2 = connection.getJarFile();
  196. JarEntry jarEntry2 = connection.getJarEntry();
  197. assertSame(jarFile1, jarFile2);
  198. assertSame(jarEntry1, jarEntry2);
  199. try {
  200. connection.getInputStream();
  201. fail("should throw IllegalStateException");
  202. } catch (IllegalStateException e) {
  203. // expected
  204. }
  205. }
  206. public void test_getJarFileURL() throws Exception {
  207. URL u = createContent("lf.jar", "plus.bmp");
  208. URL fileURL = new URL(u.getPath().substring(0, u.getPath().indexOf("!")));
  209. juc = (JarURLConnection) u.openConnection();
  210. assertTrue("Returned incorrect file URL", juc.getJarFileURL().equals(
  211. fileURL));
  212. // Regression test for harmony-3053
  213. URL url = new URL("jar:file:///bar.jar!/foo.jar!/Bugs/HelloWorld.class");
  214. assertEquals("file:/bar.jar",((JarURLConnection)url.openConnection()).getJarFileURL().toString());
  215. }
  216. public void test_getMainAttributes() throws Exception {
  217. URL u = createContent("lf.jar", "swt.dll");
  218. juc = (JarURLConnection) u.openConnection();
  219. java.util.jar.Attributes a = juc.getMainAttributes();
  220. assertEquals("Returned incorrect Attributes", "1.0", a
  221. .get(java.util.jar.Attributes.Name.MANIFEST_VERSION));
  222. URL invURL = createContent("InvalidJar.jar", "Test.class");
  223. JarURLConnection juConn = (JarURLConnection) invURL.openConnection();
  224. try {
  225. juConn.getMainAttributes();
  226. fail("IOException was not thrown.");
  227. } catch(java.io.IOException io) {
  228. //expected
  229. }
  230. }
  231. public void test_getInputStream_DeleteJarFileUsingURLConnection()
  232. throws Exception {
  233. String entry = "text.txt";
  234. String cts = System.getProperty("java.io.tmpdir");
  235. File tmpDir = new File(cts);
  236. File jarFile = File.createTempFile("file", ".jar", tmpDir);
  237. String jarFileName = jarFile.getPath();
  238. FileOutputStream jarFileOutputStream = new FileOutputStream(jarFileName);
  239. JarOutputStream out = new JarOutputStream(new BufferedOutputStream(
  240. jarFileOutputStream));
  241. JarEntry jarEntry = new JarEntry(entry);
  242. out.putNextEntry(jarEntry);
  243. out.write(new byte[] { 'a', 'b', 'c' });
  244. out.close();
  245. URL url = new URL("jar:file:" + jarFileName + "!/" + entry);
  246. URLConnection conn = url.openConnection();
  247. conn.setUseCaches(false);
  248. InputStream is = conn.getInputStream();
  249. is.close();
  250. assertTrue(jarFile.delete());
  251. }
  252. public void test_Constructor() {
  253. try {
  254. String jarFileName = "file.jar";
  255. String entry = "text.txt";
  256. URL url = new URL("jar:file:" + jarFileName + "!/" + entry);
  257. TestJarURLConnection jarConn = new TestJarURLConnection(url);
  258. assertEquals(new URL("file:file.jar"), jarConn.getJarFileURL());
  259. } catch(MalformedURLException me) {
  260. fail("MalformedURLException was thrown.");
  261. }
  262. try {
  263. URL [] urls = {new URL("file:file.jar"),
  264. new URL("http://foo.com/foo/foo.jar")};
  265. for(URL url : urls) {
  266. try {
  267. new TestJarURLConnection(url);
  268. fail("MalformedURLException was not thrown.");
  269. } catch(MalformedURLException me) {
  270. //expected
  271. }
  272. }
  273. } catch(MalformedURLException me) {
  274. fail("MalformedURLException was thrown.");
  275. }
  276. }
  277. class TestJarURLConnection extends JarURLConnection {
  278. protected TestJarURLConnection(URL arg0) throws MalformedURLException {
  279. super(arg0);
  280. }
  281. @Override
  282. public JarFile getJarFile() throws IOException {
  283. return null;
  284. }
  285. @Override
  286. public void connect() throws IOException {
  287. }
  288. }
  289. }