/** @throws IOException java.util.jar.JarFile#getJarEntry(java.lang.String) */ public void test_getJarEntryLjava_lang_String() throws IOException { try { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName)); assertEquals("Error in returned entry", 311, jarFile.getJarEntry(entryName).getSize()); jarFile.close(); } catch (Exception e) { fail("Exception during test: " + e.toString()); } Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName)); Enumeration<JarEntry> enumeration = jarFile.entries(); assertTrue(enumeration.hasMoreElements()); while (enumeration.hasMoreElements()) { JarEntry je = enumeration.nextElement(); jarFile.getJarEntry(je.getName()); } enumeration = jarFile.entries(); assertTrue(enumeration.hasMoreElements()); JarEntry je = enumeration.nextElement(); try { jarFile.close(); jarFile.getJarEntry(je.getName()); // fail("IllegalStateException expected."); } catch (IllegalStateException ee) { // Per documentation exception // may be thrown. // expected } }
/** java.util.jar.JarFile#JarFile(java.io.File, boolean, int) */ public void test_ConstructorLjava_io_FileZI() { try { JarFile jarFile = new JarFile(new File("Wrong.file"), true, ZipFile.OPEN_READ); fail("Should throw IOException"); } catch (IOException e) { // expected } try { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName), false, ZipFile.OPEN_READ); } catch (IOException e) { fail("Should not throw IOException"); } try { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile( new File(resources, jarName), false, ZipFile.OPEN_READ | ZipFile.OPEN_DELETE + 33); fail("Should throw IllegalArgumentException"); } catch (IOException e) { fail("Should not throw IOException"); } catch (IllegalArgumentException e) { // expected } }
public void test_equals() throws IOException { Manifest manifest1 = new Manifest(new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")).openStream()); Manifest manifest2 = new Manifest(new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")).openStream()); Manifest manifest3 = new Manifest(); assertTrue(manifest1.equals(manifest1)); assertTrue(manifest1.equals(manifest2)); assertFalse(manifest1.equals(manifest3)); assertFalse(manifest1.equals(this)); }
Package getTestPackage(String resourceJar, String className) throws Exception { Support_Resources.copyFile(resources, "Package", resourceJar); URL resourceURL = new URL("file:/" + resPath + "/Package/" + resourceJar); URLClassLoader ucl = new URLClassLoader(new URL[] {resourceURL}, null); return Class.forName(className, true, ucl).getPackage(); }
private void checkSignedJar(String jarName) throws Exception { Support_Resources.copyFile(resources, null, jarName); File file = new File(resources, jarName); boolean foundCerts = false; JarFile jarFile = new JarFile(file, true); try { Enumeration<JarEntry> e = jarFile.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); InputStream is = jarFile.getInputStream(entry); is.skip(100000); is.close(); Certificate[] certs = entry.getCertificates(); if (certs != null && certs.length > 0) { foundCerts = true; break; } } } finally { jarFile.close(); } assertTrue( "No certificates found during signed jar test for jar \"" + jarName + "\"", foundCerts); }
private Certificate[] getSignedJarCerts(String jarName, boolean chainCheck) throws Exception { Support_Resources.copyFile(resources, null, jarName); File file = new File(resources, jarName); Certificate[] foundCerts = null; JarFile jarFile = new JarFile(file, true, ZipFile.OPEN_READ, chainCheck); try { Enumeration<JarEntry> e = jarFile.entries(); while (e.hasMoreElements()) { JarEntry entry = e.nextElement(); InputStream is = jarFile.getInputStream(entry); // Skip bytes because we have to read the entire file for it to read signatures. is.skip(entry.getSize()); is.close(); Certificate[] certs = entry.getCertificates(); if (certs != null && certs.length > 0) { foundCerts = certs; break; } } } finally { jarFile.close(); } return foundCerts; }
public void test_1562() throws Exception { Manifest man = new Manifest(); Attributes att = man.getMainAttributes(); att.put(Attributes.Name.MANIFEST_VERSION, "1.0"); att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo"); File outputZip = File.createTempFile("hyts_", ".zip"); outputZip.deleteOnExit(); ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(outputZip)); File resources = Support_Resources.createTempFolder(); for (String zipClass : new String[] {"Foo", "Bar"}) { zout.putNextEntry(new ZipEntry("foo/bar/execjartest/" + zipClass + ".class")); zout.write(getResource(resources, "hyts_" + zipClass + ".ser")); } zout.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); man.write(zout); zout.close(); // set up the VM parameters String[] args = new String[] {"-jar", outputZip.getAbsolutePath()}; // execute the JAR and read the result String res = Support_Exec.execJava(args, null, false); assertTrue("Error executing ZIP : result returned was incorrect.", res.startsWith("FOOBAR")); }
/** * tests case when Main-Class is not in the zip launched but in another zip referenced by * Class-Path * * @throws Exception in case of troubles */ public void test_main_class_in_another_zip() throws Exception { File fooZip = File.createTempFile("hyts_", ".zip"); File barZip = File.createTempFile("hyts_", ".zip"); fooZip.deleteOnExit(); barZip.deleteOnExit(); // create the manifest Manifest man = new Manifest(); Attributes att = man.getMainAttributes(); att.put(Attributes.Name.MANIFEST_VERSION, "1.0"); att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo"); att.put(Attributes.Name.CLASS_PATH, fooZip.getName()); File resources = Support_Resources.createTempFolder(); ZipOutputStream zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip)); zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class")); zoutFoo.write(getResource(resources, "hyts_Foo.ser")); zoutFoo.close(); ZipOutputStream zoutBar = new ZipOutputStream(new FileOutputStream(barZip)); zoutBar.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); man.write(zoutBar); zoutBar.putNextEntry(new ZipEntry("foo/bar/execjartest/Bar.class")); zoutBar.write(getResource(resources, "hyts_Bar.ser")); zoutBar.close(); String[] args = new String[] {"-jar", barZip.getAbsolutePath()}; // execute the JAR and read the result String res = Support_Exec.execJava(args, null, false); assertTrue("Error executing JAR : result returned was incorrect.", res.startsWith("FOOBAR")); }
/* The jar is intact, then everything is all right. */ public void test_JarFile_Integrate_Jar() throws IOException { String modifiedJarName = "Integrate.jar"; Support_Resources.copyFile(resources, null, modifiedJarName); JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); jarFile.getInputStream(zipEntry).skip(Long.MAX_VALUE); } }
/* * The jar created by 1.4 which does not provide a * algorithm-Digest-Manifest-Main-Attributes entry in .SF file. */ public void test_Jar_created_before_java_5() throws IOException { String modifiedJarName = "Created_by_1_4.jar"; Support_Resources.copyFile(resources, null, modifiedJarName); JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); jarFile.getInputStream(zipEntry); } }
public void test_close() throws IOException { String modifiedJarName = "Modified_SF_EntryAttributes.jar"; Support_Resources.copyFile(resources, null, modifiedJarName); JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true); Enumeration<JarEntry> entries = jarFile.entries(); jarFile.close(); jarFile.close(); // Can not check IOException }
public void test_entries2() throws Exception { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName)); Enumeration<JarEntry> enumeration = jarFile.entries(); jarFile.close(); try { enumeration.hasMoreElements(); fail("hasMoreElements() did not detect a closed jar file"); } catch (IllegalStateException e) { } Support_Resources.copyFile(resources, null, jarName); jarFile = new JarFile(new File(resources, jarName)); enumeration = jarFile.entries(); jarFile.close(); try { enumeration.nextElement(); fail("nextElement() did not detect closed jar file"); } catch (IllegalStateException e) { } }
private static byte[] getResource(File tempDir, String resourceName) throws IOException { Support_Resources.copyFile(tempDir, null, resourceName); File resourceFile = new File(tempDir, resourceName); resourceFile.deleteOnExit(); // read whole resource data into memory byte[] resourceBody = new byte[(int) resourceFile.length()]; FileInputStream fis = new FileInputStream(resourceFile); fis.read(resourceBody); fis.close(); return resourceBody; }
/* * If another entry is inserted into Manifest, no security exception will be * thrown out. */ public void test_Inserted_Entry_Manifest_with_DigestCode() throws IOException { String modifiedJarName = "Inserted_Entry_Manifest_with_DigestCode.jar"; Support_Resources.copyFile(resources, null, modifiedJarName); JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true); Enumeration<JarEntry> entries = jarFile.entries(); int count = 0; while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); jarFile.getInputStream(zipEntry); count++; } assertEquals(5, count); }
public void test_clone() throws IOException { Manifest emptyManifest = new Manifest(); Manifest emptyClone = (Manifest) emptyManifest.clone(); assertTrue("Should have no entries", emptyClone.getEntries().isEmpty()); assertTrue("Should have no main attributes", emptyClone.getMainAttributes().isEmpty()); assertEquals(emptyClone, emptyManifest); assertEquals(emptyManifest.clone().getClass().getName(), "java.util.jar.Manifest"); Manifest manifest = new Manifest(new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")).openStream()); Manifest manifestClone = (Manifest) manifest.clone(); manifestClone.getMainAttributes(); checkManifest(manifestClone); }
/* * If the content of the .SA file is modified, no matter what it resides, * JarFile.getInputStream of any JarEntry will throw Security Exception. */ public void test_JarFile_Modified_SF_EntryAttributes() throws IOException { String modifiedJarName = "Modified_SF_EntryAttributes.jar"; Support_Resources.copyFile(resources, null, modifiedJarName); JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); try { jarFile.getInputStream(zipEntry); fail("should throw Security Exception"); } catch (SecurityException e) { // desired } } }
/** java.util.jar.JarFile#JarFile(java.io.File, boolean) */ public void test_ConstructorLjava_io_FileZ() { try { JarFile jarFile = new JarFile(new File("Wrong.file"), true); fail("Should throw IOException"); } catch (IOException e) { // expected } try { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName), false); } catch (IOException e) { fail("Should not throw IOException"); } }
/** java.util.jar.JarFile#JarFile(java.lang.String, boolean) */ public void test_ConstructorLjava_lang_StringZ() { try { JarFile jarFile = new JarFile("Wrong.file", false); fail("Should throw IOException"); } catch (IOException e) { // expected } try { Support_Resources.copyFile(resources, null, jarName); String fileName = (new File(resources, jarName)).getCanonicalPath(); JarFile jarFile = new JarFile(fileName, true); } catch (IOException e) { fail("Should not throw IOException"); } }
/** java.util.jar.JarFile#entries() */ public void test_entries() throws Exception { /* * Note only (and all of) the following should be contained in the file * META-INF/ META-INF/MANIFEST.MF foo/ foo/bar/ foo/bar/A.class Blah.txt */ Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName)); Enumeration<JarEntry> e = jarFile.entries(); int i; for (i = 0; e.hasMoreElements(); i++) { e.nextElement(); } assertEquals(jarFile.size(), i); jarFile.close(); assertEquals(6, i); }
// Regression test for issue introduced by HARMONY-4569: signed archives containing files with // size 0 could not get verified. public void testJarVerificationEmptyEntry() throws IOException { Support_Resources.copyFile(resources, null, emptyEntryJar); File f = new File(resources, emptyEntryJar); JarFile jarFile = new JarFile(f); ZipEntry zipEntry = jarFile.getJarEntry(emptyEntry1); int res = jarFile.getInputStream(zipEntry).read(new byte[100], 0, 100); assertEquals("Wrong length of empty jar entry", -1, res); zipEntry = jarFile.getJarEntry(emptyEntry2); res = jarFile.getInputStream(zipEntry).read(new byte[100], 0, 100); assertEquals("Wrong length of empty jar entry", -1, res); zipEntry = jarFile.getJarEntry(emptyEntry3); res = jarFile.getInputStream(zipEntry).read(); assertEquals("Wrong length of empty jar entry", -1, res); }
/** The jar is intact, but the entry object is modified. */ public void testJarVerificationModifiedEntry() throws IOException { Support_Resources.copyFile(resources, null, integrateJar); File f = new File(resources, integrateJar); JarFile jarFile = new JarFile(f); ZipEntry zipEntry = jarFile.getJarEntry(integrateJarEntry); zipEntry.setSize(zipEntry.getSize() + 1); jarFile.getInputStream(zipEntry).skip(Long.MAX_VALUE); jarFile = new JarFile(f); zipEntry = jarFile.getJarEntry(integrateJarEntry); zipEntry.setSize(zipEntry.getSize() - 1); try { // jarFile.getInputStream(zipEntry).skip(Long.MAX_VALUE); jarFile.getInputStream(zipEntry).read(new byte[5000], 0, 5000); fail("SecurityException expected"); } catch (SecurityException e) { // desired } }
/* * The content of Test.class is modified, jarFile.getInputStream will not * throw security Exception, but it will anytime before the inputStream got * from getInputStream method has been read to end. */ public void test_JarFile_Modified_Class() throws IOException { String modifiedJarName = "Modified_Class.jar"; Support_Resources.copyFile(resources, null, modifiedJarName); JarFile jarFile = new JarFile(new File(resources, modifiedJarName), true); Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); jarFile.getInputStream(zipEntry); } /* The content of Test.class has been tampered. */ ZipEntry zipEntry = jarFile.getEntry("Test.class"); InputStream in = jarFile.getInputStream(zipEntry); byte[] buffer = new byte[1024]; try { while (in.available() > 0) { in.read(buffer); } fail("SecurityException expected"); } catch (SecurityException e) { // desired } }
public void test_writeLjava_io_OutputStream() throws IOException { byte b[]; Manifest manifest1 = null; Manifest manifest2 = null; try { manifest1 = new Manifest(new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")).openStream()); } catch (MalformedURLException e) { fail("Malformed URL"); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); manifest1.write(baos); b = baos.toByteArray(); File f = File.createTempFile("111", "111"); FileOutputStream fos = new FileOutputStream(f); fos.close(); try { manifest1.write(fos); fail("IOException expected"); } catch (IOException e) { // expected } f.delete(); ByteArrayInputStream bais = new ByteArrayInputStream(b); try { manifest2 = new Manifest(bais); } catch (MalformedURLException e) { fail("Malformed URL"); } assertTrue(manifest1.equals(manifest2)); }
/** @tests java.security.cert.X509Certificate#getExtensionValue(java.lang.String) */ public void test_getExtensionValueLjava_lang_String() throws Exception { InputStream is = Support_Resources.getResourceStream("hyts_certificate_PEM.txt"); CertificateFactory certFact = CertificateFactory.getInstance("X509"); X509Certificate pemCert = (X509Certificate) certFact.generateCertificate(is); Vector<String> extensionOids = new Vector<String>(); extensionOids.addAll(pemCert.getCriticalExtensionOIDs()); extensionOids.addAll(pemCert.getNonCriticalExtensionOIDs()); Iterator i = extensionOids.iterator(); while (i.hasNext()) { String oid = (String) i.next(); byte[] value = pemCert.getExtensionValue(oid); if (value != null && value.length > 0) { // check that it is an encoded as a OCTET STRING assertEquals( "The extension value for the oid " + oid + " was not encoded as an OCTET STRING", 0x04, value[0]); } } }
/** * @throws IOException * @tests java.util.Properties#load(java.io.InputStream) */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Doesn't verify IOException, IllegalArgumentException.", method = "load", args = {java.io.InputStream.class}) public void test_loadLjava_io_InputStream_subtest0() throws IOException { InputStream is = Support_Resources.getStream("hyts_PropertiesTest.properties"); Properties props = new Properties(); props.load(is); is.close(); assertEquals("1", "\n \t \f", props.getProperty(" \r")); assertEquals("2", "a", props.getProperty("a")); assertEquals("3", "bb as,dn ", props.getProperty("b")); assertEquals("4", ":: cu", props.getProperty("c\r \t\nu")); assertEquals("5", "bu", props.getProperty("bu")); assertEquals("6", "d\r\ne=e", props.getProperty("d")); assertEquals("7", "fff", props.getProperty("f")); assertEquals("8", "g", props.getProperty("g")); assertEquals("9", "", props.getProperty("h h")); assertEquals("10", "i=i", props.getProperty(" ")); assertEquals("11", " j", props.getProperty("j")); assertEquals("12", " c", props.getProperty("space")); assertEquals("13", "\\", props.getProperty("dblbackslash")); }
@Override protected void setUp() { resources = Support_Resources.createTempFolder(); resPath = resources.toString(); if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') resPath = resPath.substring(1); }
/** java.util.jar.JarFile#getManifest() */ public void test_getManifest() { // Test for method java.util.jar.Manifest // java.util.jar.JarFile.getManifest() try { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName)); assertNotNull("Error--Manifest not returned", jarFile.getManifest()); jarFile.close(); } catch (Exception e) { fail("Exception during 1st test: " + e.toString()); } try { Support_Resources.copyFile(resources, null, jarName2); JarFile jarFile = new JarFile(new File(resources, jarName2)); assertNull("Error--should have returned null", jarFile.getManifest()); jarFile.close(); } catch (Exception e) { fail("Exception during 2nd test: " + e.toString()); } try { // jarName3 was created using the following test Support_Resources.copyFile(resources, null, jarName3); JarFile jarFile = new JarFile(new File(resources, jarName3)); assertNotNull("Should find manifest without verifying", jarFile.getManifest()); jarFile.close(); } catch (Exception e) { fail("Exception during 3rd test: " + e.toString()); } try { // this is used to create jarName3 used in the previous test Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.put(new Attributes.Name("Manifest-Version"), "1.0"); ByteArrayOutputStream manOut = new ByteArrayOutputStream(); manifest.write(manOut); byte[] manBytes = manOut.toByteArray(); File file = File.createTempFile("hyts_manifest1", ".jar"); JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(file.getAbsolutePath())); ZipEntry entry = new ZipEntry("META-INF/"); entry.setSize(0); jarOut.putNextEntry(entry); entry = new ZipEntry(JarFile.MANIFEST_NAME); entry.setSize(manBytes.length); jarOut.putNextEntry(entry); jarOut.write(manBytes); entry = new ZipEntry("myfile"); entry.setSize(1); jarOut.putNextEntry(entry); jarOut.write(65); jarOut.close(); JarFile jar = new JarFile(file.getAbsolutePath(), false); assertNotNull("Should find manifest without verifying", jar.getManifest()); jar.close(); file.delete(); } catch (IOException e) { fail("IOException 3"); } try { Support_Resources.copyFile(resources, null, jarName2); JarFile jF = new JarFile(new File(resources, jarName2)); jF.close(); jF.getManifest(); fail("FAILED: expected IllegalStateException"); } catch (IllegalStateException ise) { // expected; } catch (Exception e) { fail("Exception during 4th test: " + e.toString()); } Support_Resources.copyFile(resources, null, "Broken_manifest.jar"); JarFile jf; try { jf = new JarFile(new File(resources, "Broken_manifest.jar")); jf.getManifest(); fail("IOException expected."); } catch (IOException e) { // expected. } }
/** * tests Class-Path entry in manifest * * @throws Exception in case of troubles */ public void test_zip_class_path() throws Exception { File fooZip = File.createTempFile("hyts_", ".zip"); File barZip = File.createTempFile("hyts_", ".zip"); fooZip.deleteOnExit(); barZip.deleteOnExit(); // create the manifest Manifest man = new Manifest(); Attributes att = man.getMainAttributes(); att.put(Attributes.Name.MANIFEST_VERSION, "1.0"); att.put(Attributes.Name.MAIN_CLASS, "foo.bar.execjartest.Foo"); att.put(Attributes.Name.CLASS_PATH, barZip.getName()); File resources = Support_Resources.createTempFolder(); ZipOutputStream zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip)); zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); man.write(zoutFoo); zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class")); zoutFoo.write(getResource(resources, "hyts_Foo.ser")); zoutFoo.close(); ZipOutputStream zoutBar = new ZipOutputStream(new FileOutputStream(barZip)); zoutBar.putNextEntry(new ZipEntry("foo/bar/execjartest/Bar.class")); zoutBar.write(getResource(resources, "hyts_Bar.ser")); zoutBar.close(); String[] args = new String[] {"-jar", fooZip.getAbsolutePath()}; // execute the JAR and read the result String res = Support_Exec.execJava(args, null, false); assertTrue("Error executing JAR : result returned was incorrect.", res.startsWith("FOOBAR")); // rewrite manifest so it contains not only reference to bar but useless entries as well att.put(Attributes.Name.CLASS_PATH, "xx yy zz " + barZip.getName()); zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip)); zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); man.write(zoutFoo); zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class")); zoutFoo.write(getResource(resources, "hyts_Foo.ser")); zoutFoo.close(); // execute the JAR and read the result res = Support_Exec.execJava(args, null, false); assertTrue("Error executing JAR : result returned was incorrect.", res.startsWith("FOOBAR")); // play with relative file names - put relative path as ../<parent dir name>/xx.zip att.put( Attributes.Name.CLASS_PATH, ".." + File.separator + barZip.getParentFile().getName() + File.separator + barZip.getName()); zoutFoo = new ZipOutputStream(new FileOutputStream(fooZip)); zoutFoo.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); man.write(zoutFoo); zoutFoo.putNextEntry(new ZipEntry("foo/bar/execjartest/Foo.class")); zoutFoo.write(getResource(resources, "hyts_Foo.ser")); zoutFoo.close(); // execute the ZIP and read the result res = Support_Exec.execJava(args, null, false); assertTrue("Error executing JAR : result returned was incorrect.", res.startsWith("FOOBAR")); }
// This test doesn't pass on RI. If entry size is set up incorrectly, // SecurityException is thrown. But SecurityException is thrown on RI only // if jar file is signed incorrectly. public void test_getInputStreamLjava_util_jar_JarEntry_subtest0() throws Exception { File signedFile = null; try { Support_Resources.copyFile(resources, null, jarName4); signedFile = new File(resources, jarName4); } catch (Exception e) { fail("Failed to create local file 2: " + e); } try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); InputStream in = jar.getInputStream(entry); in.read(); } catch (Exception e) { fail("Exception during test 3: " + e); } try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); InputStream in = jar.getInputStream(entry); // BEGIN android-added byte[] dummy = getAllBytesFromStream(in); // END android-added assertNull("found certificates", entry.getCertificates()); } catch (Exception e) { fail("Exception during test 4: " + e); } try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); entry.setSize(1076); InputStream in = jar.getInputStream(entry); // BEGIN android-added byte[] dummy = getAllBytesFromStream(in); // END android-added fail("SecurityException should be thrown."); } catch (SecurityException e) { // expected } catch (Exception e) { fail("Exception during test 5: " + e); } try { Support_Resources.copyFile(resources, null, jarName5); signedFile = new File(resources, jarName5); } catch (Exception e) { fail("Failed to create local file 5: " + e); } try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); InputStream in = jar.getInputStream(entry); fail("SecurityException should be thrown."); } catch (SecurityException e) { // expected } catch (Exception e) { fail("Exception during test 5: " + e); } // SHA1 digest, SHA256withRSA signed JAR checkSignedJar(jarName6); // SHA-256 digest, SHA256withRSA signed JAR checkSignedJar(jarName7); // SHA-512 digest, SHA512withECDSA signed JAR checkSignedJar(jarName8); // JAR with a signature that has PKCS#7 Authenticated Attributes checkSignedJar(authAttrsJar); }
/** @throws IOException java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry) */ public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException { File localFile = null; try { Support_Resources.copyFile(resources, null, jarName); localFile = new File(resources, jarName); } catch (Exception e) { fail("Failed to create local file: " + e); } byte[] b = new byte[1024]; try { JarFile jf = new JarFile(localFile); java.io.InputStream is = jf.getInputStream(jf.getEntry(entryName)); // BEGIN android-removed // jf.close(); // END android-removed assertTrue("Returned invalid stream", is.available() > 0); int r = is.read(b, 0, 1024); is.close(); StringBuffer sb = new StringBuffer(r); for (int i = 0; i < r; i++) { sb.append((char) (b[i] & 0xff)); } String contents = sb.toString(); assertTrue("Incorrect stream read", contents.indexOf("bar") > 0); // BEGIN android-added jf.close(); // END android-added } catch (Exception e) { fail("Exception during test: " + e.toString()); } try { JarFile jf = new JarFile(localFile); InputStream in = jf.getInputStream(new JarEntry("invalid")); assertNull("Got stream for non-existent entry", in); } catch (Exception e) { fail("Exception during test 2: " + e); } try { Support_Resources.copyFile(resources, null, jarName); File signedFile = new File(resources, jarName); JarFile jf = new JarFile(signedFile); JarEntry jre = new JarEntry("foo/bar/A.class"); jf.getInputStream(jre); // InputStream returned in any way, exception can be thrown in case // of reading from this stream only. // fail("Should throw ZipException"); } catch (ZipException ee) { // expected } try { Support_Resources.copyFile(resources, null, jarName); File signedFile = new File(resources, jarName); JarFile jf = new JarFile(signedFile); JarEntry jre = new JarEntry("foo/bar/A.class"); jf.close(); jf.getInputStream(jre); // InputStream returned in any way, exception can be thrown in case // of reading from this stream only. // The same for IOException fail("Should throw IllegalStateException"); } catch (IllegalStateException ee) { // expected } }