diff options
author | Jesse Wilson <jessewilson@google.com> | 2009-05-21 17:39:08 -0700 |
---|---|---|
committer | Jesse Wilson <jessewilson@google.com> | 2009-06-09 16:09:47 -0700 |
commit | 57995e8186b54515d5a03bf2ab104c3dc247f1b6 (patch) | |
tree | fb4502a727023f0021f949c366304b3a082d03f7 /archive/src/test/java | |
parent | 796290e84a25f1444d43604af31cf872648c583f (diff) | |
download | libcore-57995e8186b54515d5a03bf2ab104c3dc247f1b6.zip libcore-57995e8186b54515d5a03bf2ab104c3dc247f1b6.tar.gz libcore-57995e8186b54515d5a03bf2ab104c3dc247f1b6.tar.bz2 |
Updating archive to Harmony r772995.
Squashed commit of the following:
Adding @TestTargetNew tags
Initial merge of branch 'archive_772995' into archive_dalvik.
Fixed some problems, including InflaterInputStream.available()
and JarFile.skip() bugs.
Conflicts:
libcore/archive/.classpath
libcore/archive/META-INF/MANIFEST.MF
libcore/archive/build.xml
libcore/archive/make/hyproperties.xml
libcore/archive/src/main/java/java/util/jar/Attributes.java
libcore/archive/src/main/java/java/util/jar/JarEntry.java
libcore/archive/src/main/java/java/util/jar/JarException.java
libcore/archive/src/main/java/java/util/jar/JarFile.java
libcore/archive/src/main/java/java/util/jar/JarInputStream.java
libcore/archive/src/main/java/java/util/jar/JarVerifier.java
libcore/archive/src/main/java/java/util/jar/Manifest.java
libcore/archive/src/main/java/java/util/jar/Pack200.java
libcore/archive/src/main/java/java/util/zip/Adler32.java
libcore/archive/src/main/java/java/util/zip/CRC32.java
libcore/archive/src/main/java/java/util/zip/Checksum.java
libcore/archive/src/main/java/java/util/zip/DataFormatException.java
libcore/archive/src/main/java/java/util/zip/Deflater.java
libcore/archive/src/main/java/java/util/zip/DeflaterOutputStream.java
libcore/archive/src/main/java/java/util/zip/GZIPInputStream.java
libcore/archive/src/main/java/java/util/zip/GZIPOutputStream.java
libcore/archive/src/main/java/java/util/zip/Inflater.java
libcore/archive/src/main/java/java/util/zip/InflaterInputStream.java
libcore/archive/src/main/java/java/util/zip/ZipEntry.java
libcore/archive/src/main/java/java/util/zip/ZipException.java
libcore/archive/src/main/java/java/util/zip/ZipFile.java
libcore/archive/src/main/java/java/util/zip/ZipInputStream.java
libcore/archive/src/main/java/java/util/zip/ZipOutputStream.java
libcore/archive/src/main/java/org/apache/harmony/archive/internal/nls/Messages.java
libcore/archive/src/main/native/archive/shared/archiveglob.c
libcore/archive/src/main/native/archive/shared/jarfile.c
libcore/archive/src/main/native/archive/shared/zip.c
libcore/archive/src/main/native/archive/shared/zip.h
libcore/archive/src/main/native/archive/unix/makefile
libcore/archive/src/main/native/archive/windows/makefile
libcore/archive/src/main/native/java_util_zip_Adler32.c
libcore/archive/src/main/native/java_util_zip_CRC32.c
libcore/archive/src/main/native/java_util_zip_Deflater.c
libcore/archive/src/main/native/java_util_zip_Inflater.c
libcore/archive/src/main/native/zip/shared/hyzip.nls
libcore/archive/src/main/native/zip/unix/makefile
libcore/archive/src/main/native/zipsup.h
libcore/archive/src/main/native/zlib/unix/makefile
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesNameTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarInputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarOutputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ManifestTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPInputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java
libcore/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java
commit 946f165f5b592f4453fd8f2c19766921544d38dd
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:12:55 2009 -0700
Strip @Since Android 1.0 from Archive
commit 3498f216d7e826bfc9c4cc7c0da35830ca239367
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:09:05 2009 -0700
Dalvik archive
commit 62e9db90bc6aa6b5d1c897cccdd616d812672677
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:07:51 2009 -0700
Archive 772995
commit d1bf618681d6badf1b50edaf204a083d3912213d
Author: Jesse Wilson <jessewilson@google.com>
Date: Tue May 12 15:06:36 2009 -0700
Archive 527399
Diffstat (limited to 'archive/src/test/java')
10 files changed, 896 insertions, 493 deletions
diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesTest.java index 0a8b037..0b3d2cf 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/AttributesTest.java @@ -421,6 +421,27 @@ public class AttributesTest extends TestCase { assertNull(attribute.get(name)); } + /** + * @tests java.util.jar.Attributes.hashCode() + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "hashCode", + args = {} + ) + public void test_hashCode_consistent_with_map() { + MockAttributes mockAttr = new MockAttributes(); + mockAttr.putValue("1", "one"); + assertEquals(mockAttr.getMap().hashCode(), mockAttr.hashCode()); + } + + private static class MockAttributes extends Attributes { + public Map<Object, Object> getMap() { + return map; + } + } + @TestTargetNew( level = TestLevel.COMPLETE, notes = "", @@ -470,7 +491,7 @@ public class AttributesTest extends TestCase { } @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "hashCode", args = {} diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java index 40eff3b..90144be 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarEntryTest.java @@ -72,6 +72,29 @@ public class JarEntryTest extends TestCase { } /** + * @throws IOException + * @tests java.util.jar.JarEntry#JarEntry(java.util.jar.JarEntry) + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "JarEntry", + args = {java.util.jar.JarEntry.class} + ) + public void test_ConstructorLjava_util_jar_JarEntry_on_null() throws IOException { + JarEntry newJarEntry = new JarEntry(jarFile.getJarEntry(entryName)); + assertNotNull(newJarEntry); + + jarEntry = null; + try { + newJarEntry = new JarEntry(jarEntry); + fail("Should throw NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + } + + /** * @tests java.util.jar.JarEntry#JarEntry(java.util.zip.ZipEntry) */ @TestTargetNew( @@ -163,10 +186,21 @@ public class JarEntryTest extends TestCase { JarEntry jarEntry2 = jarFile.getJarEntry("Test.class"); InputStream in = jarFile.getInputStream(jarEntry1); byte[] buffer = new byte[1024]; + // BEGIN android-changed + // the certificates are non-null too early and in.available() fails + // while (in.available() > 0) { + // assertNull("getCertificates() should be null until the entry is read", + // jarEntry1.getCertificates()); + // assertNull(jarEntry2.getCertificates()); + // in.read(buffer); + // } while (in.read(buffer) >= 0); in.close(); + // END android-changed + assertEquals("the file is fully read", -1, in.read()); assertNotNull(jarEntry1.getCertificates()); assertNotNull(jarEntry2.getCertificates()); + in.close(); } /** @@ -187,8 +221,14 @@ public class JarEntryTest extends TestCase { InputStream in = jarFile.getInputStream(jarEntry); byte[] buffer = new byte[1024]; while (in.available() > 0) { + // BEGIN android-changed + // the code signers are non-null too early + // assertNull("getCodeSigners() should be null until the entry is read", + // jarEntry.getCodeSigners()); + // END android-changed in.read(buffer); } + assertEquals("the file is fully read", -1, in.read()); CodeSigner[] codeSigners = jarEntry.getCodeSigners(); assertEquals(2, codeSigners.length); List<?> certs_bob = codeSigners[0].getSignerCertPath() @@ -240,7 +280,7 @@ public class JarEntryTest extends TestCase { } @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "JarEntry", args = {java.util.jar.JarEntry.class} diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java index 720f78d..96321a4 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarFileTest.java @@ -14,13 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.harmony.archive.tests.java.util.jar; import dalvik.annotation.AndroidOnly; import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetNew; @@ -73,13 +71,17 @@ public class JarFileTest extends TestCase { private final String jarName3 = "hyts_manifest1.jar"; private final String jarName4 = "hyts_signed.jar"; - + private final String jarName5 = "hyts_signed_inc.jar"; + private final String integrateJar = "Integrate.jar"; + private final String entryName = "foo/bar/A.class"; private final String entryName3 = "coucou/FileAccess.class"; + private final String integrateJarEntry = "Test.class"; + private File resources; // custom security manager @@ -102,7 +104,7 @@ public class JarFileTest extends TestCase { * @tests java.util.jar.JarFile#JarFile(java.io.File) */ @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "JarFile", args = {java.io.File.class} @@ -300,6 +302,27 @@ public class JarFileTest extends TestCase { } /** + * Constructs JarFile object. + * + * @tests java.util.jar.JarFile#JarFile(java.io.File) + * @tests java.util.jar.JarFile#JarFile(java.lang.String) + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "JarFile", + args = {java.io.File.class} + ) + public void testConstructor_file() throws IOException { + File f = new File(resources, jarName); + Support_Resources.copyFile(resources, null, jarName); + assertTrue(new JarFile(f).getEntry(entryName).getName().equals( + entryName)); + assertTrue(new JarFile(f.getPath()).getEntry(entryName).getName() + .equals(entryName)); + } + + /** * @tests java.util.jar.JarFile#entries() */ @TestTargetNew( @@ -316,11 +339,11 @@ public class JarFileTest extends TestCase { Support_Resources.copyFile(resources, null, jarName); JarFile jarFile = new JarFile(new File(resources, jarName)); Enumeration<JarEntry> e = jarFile.entries(); - int i = 0; - while (e.hasMoreElements()) { - i++; + int i; + for (i = 0; e.hasMoreElements(); i++) { e.nextElement(); } + assertEquals(jarFile.size(), i); jarFile.close(); assertEquals(6, i); } @@ -336,24 +359,20 @@ public class JarFileTest extends TestCase { JarFile jarFile = new JarFile(new File(resources, jarName)); Enumeration<JarEntry> enumeration = jarFile.entries(); jarFile.close(); - boolean pass = false; try { enumeration.hasMoreElements(); + fail("hasMoreElements() did not detect a closed jar file"); } catch (IllegalStateException e) { - pass = true; } - assertTrue("hasMoreElements did not detect closed jar file", pass); Support_Resources.copyFile(resources, null, jarName); jarFile = new JarFile(new File(resources, jarName)); enumeration = jarFile.entries(); jarFile.close(); - pass = false; try { enumeration.nextElement(); + fail("nextElement() did not detect closed jar file"); } catch (IllegalStateException e) { - pass = true; } - assertTrue("nextElement did not detect closed jar file", pass); } /** @@ -361,7 +380,7 @@ public class JarFileTest extends TestCase { * @tests java.util.jar.JarFile#getJarEntry(java.lang.String) */ @TestTargetNew( - level = TestLevel.COMPLETE, + level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "getEntry", args = {java.lang.String.class} @@ -442,6 +461,92 @@ public class JarFileTest extends TestCase { } } + + /** + * @tests java.util.jar.JarFile#getJarEntry(java.lang.String) + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "getEntry", + args = {java.lang.String.class} + ) + public void testGetJarEntry() throws Exception { + Support_Resources.copyFile(resources, null, jarName); + JarFile jarFile = new JarFile(new File(resources, jarName)); + assertEquals("Error in returned entry", 311, jarFile.getEntry( + entryName).getSize()); + jarFile.close(); + + // tests for signed jars + // test all signed jars in the /Testres/Internal/SignedJars directory + String jarDirUrl = Support_Resources + .getResourceURL("/../internalres/signedjars"); + Vector<String> signedJars = new Vector<String>(); + try { + InputStream is = new URL(jarDirUrl + "/jarlist.txt").openStream(); + while (is.available() > 0) { + StringBuilder linebuff = new StringBuilder(80); // Typical line + // length + done: while (true) { + int nextByte = is.read(); + switch (nextByte) { + case -1: + break done; + case (byte) '\r': + if (linebuff.length() == 0) { + // ignore + } + break done; + case (byte) '\n': + if (linebuff.length() == 0) { + // ignore + } + break done; + default: + linebuff.append((char) nextByte); + } + } + if (linebuff.length() == 0) { + break; + } + String line = linebuff.toString(); + signedJars.add(line); + } + is.close(); + } catch (IOException e) { + // no list of jars found + } + + for (int i = 0; i < signedJars.size(); i++) { + String jarName = signedJars.get(i); + try { + File file = Support_Resources.getExternalLocalFile(jarDirUrl + + "/" + jarName); + jarFile = new JarFile(file, true); + boolean foundCerts = false; + 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; + } + } + assertTrue( + "No certificates found during signed jar test for jar \"" + + jarName + "\"", foundCerts); + } catch (IOException e) { + fail("Exception during signed jar test for jar \"" + jarName + + "\": " + e.toString()); + } + } + } + /** * @tests java.util.jar.JarFile#getManifest() */ @@ -540,85 +645,6 @@ public class JarFileTest extends TestCase { } /** - * @throws IOException - * @tests java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry) - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "getInputStream", - args = {java.util.zip.ZipEntry.class} - ) - 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 - } - } - - /** * @tests java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry) */ @TestTargetNew( @@ -660,7 +686,7 @@ public class JarFileTest extends TestCase { } catch (Exception e) { fail("Exception during test 4: " + e); } - + try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); @@ -682,7 +708,7 @@ public class JarFileTest extends TestCase { } catch (Exception e) { fail("Failed to create local file 5: " + e); } - + try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); @@ -732,7 +758,37 @@ public class JarFileTest extends TestCase { Enumeration<JarEntry> entries = jarFile.entries(); while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); - jarFile.getInputStream(zipEntry); + jarFile.getInputStream(zipEntry).skip(Long.MAX_VALUE); + } + } + + /** + * The jar is intact, but the entry object is modified. + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "getInputStream", + args = {ZipEntry.class} + ) + 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 } } @@ -781,7 +837,6 @@ public class JarFileTest extends TestCase { Enumeration<JarEntry> entries = jarFile.entries(); int count = 0; while (entries.hasMoreElements()) { - ZipEntry zipEntry = entries.nextElement(); jarFile.getInputStream(zipEntry); count++; @@ -818,7 +873,7 @@ public class JarFileTest extends TestCase { while (in.available() > 0) { in.read(buffer); } - fail("should throw Security Exception"); + fail("SecurityException expected"); } catch (SecurityException e) { // desired } @@ -827,7 +882,7 @@ public class JarFileTest extends TestCase { /* * In the Modified.jar, the main attributes of META-INF/MANIFEST.MF is * tampered manually. Hence the RI 5.0 JarFile.getInputStream of any - * JarEntry will throw security exception, but the apache harmony will not. + * JarEntry will throw security exception. */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, @@ -846,7 +901,7 @@ public class JarFileTest extends TestCase { ZipEntry zipEntry = entries.nextElement(); try { jarFile.getInputStream(zipEntry); - fail("should throw Security Exception"); + fail("SecurityException expected"); } catch (SecurityException e) { // desired } @@ -927,4 +982,83 @@ public class JarFileTest extends TestCase { // Can not check IOException } + + /** + * @throws IOException + * @tests java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry) + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getInputStream", + args = {java.util.zip.ZipEntry.class} + ) + 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 + } + } } diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarOutputStreamTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarOutputStreamTest.java index e652137..acdad71 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarOutputStreamTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarOutputStreamTest.java @@ -48,7 +48,7 @@ public class JarOutputStreamTest extends junit.framework.TestCase { method = "putNextEntry", args = {java.util.zip.ZipEntry.class} ) - public void test_putNextEntryLjava_util_zip_ZipEntry() { + public void test_putNextEntryLjava_util_zip_ZipEntry() throws Exception { // testClass file`s actual extension is .class, since having .class // extension files in source dir causes // problems on eclipse, the extension is changed into .ser or it can be @@ -76,35 +76,30 @@ public class JarOutputStreamTest extends junit.framework.TestCase { File outputJar = null; JarOutputStream jout = null; - try { - // open the output jarfile - outputJar = File.createTempFile("hyts_", ".jar"); - jout = new JarOutputStream(new FileOutputStream(outputJar), - newman); - jout.putNextEntry(new JarEntry(entryName)); - } catch (Exception e) { - fail("Error creating JarOutputStream: " + e); - } + // open the output jarfile + outputJar = File.createTempFile("hyts_", ".jar"); + jout = new JarOutputStream(new FileOutputStream(outputJar), + newman); + jout.putNextEntry(new JarEntry(entryName)); + File resources = Support_Resources.createTempFolder(); - try { - // read in the class file, and output it to the jar - Support_Resources.copyFile(resources, null, testClass); - URL jarURL = new URL((new File(resources, testClass)).toURL() - .toString()); - InputStream jis = jarURL.openStream(); - - byte[] bytes = new byte[1024]; - int len; - while ((len = jis.read(bytes)) != -1) { - jout.write(bytes, 0, len); - } - - jout.flush(); - jout.close(); - jis.close(); - } catch (Exception e) { - fail("Error writing JAR file for testing: " + e); + + // read in the class file, and output it to the jar + Support_Resources.copyFile(resources, null, testClass); + URL jarURL = new URL((new File(resources, testClass)).toURL() + .toString()); + InputStream jis = jarURL.openStream(); + + byte[] bytes = new byte[1024]; + int len; + while ((len = jis.read(bytes)) != -1) { + jout.write(bytes, 0, len); } + + jout.flush(); + jout.close(); + jis.close(); + String res = null; // set up the VM parameters String[] args = new String[2]; diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ManifestTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ManifestTest.java index 57e4744..42b2543 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ManifestTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ManifestTest.java @@ -14,12 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.harmony.archive.tests.java.util.jar; -import dalvik.annotation.TestTargetClass; -import dalvik.annotation.TestTargets; +import dalvik.annotation.KnownFailure; import dalvik.annotation.TestLevel; +import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; import java.io.ByteArrayInputStream; @@ -28,14 +27,15 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -import java.net.MalformedURLException; import java.net.URL; +import java.net.MalformedURLException; import java.util.Map; import java.util.jar.Attributes; import java.util.jar.JarFile; import java.util.jar.Manifest; import junit.framework.TestCase; + import tests.support.resource.Support_Resources; @TestTargetClass(Manifest.class) @@ -49,6 +49,10 @@ public class ManifestTest extends TestCase { private Manifest m2; + private final String ATT_ENTRY_NAME = "HasAttributes.txt"; + + private final String MANIFEST_NAME = "manifest/hyts_MANIFEST.MF"; + private File resources; @Override @@ -68,6 +72,19 @@ public class ManifestTest extends TestCase { } } + private Manifest getManifest(String fileName) { + try { + Support_Resources.copyFile(resources, null, fileName); + JarFile jarFile = new JarFile(new File(resources, fileName)); + Manifest m = jarFile.getManifest(); + jarFile.close(); + return m; + } catch (Exception e) { + fail("Exception during setup: " + e.toString()); + return null; + } + } + /** * @tests java.util.jar.Manifest#Manifest() */ @@ -87,264 +104,136 @@ public class ManifestTest extends TestCase { } /** - * @tests java.util.jar.Manifest#Manifest(java.io.InputStream) - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "IOException checking missed.", - method = "Manifest", - args = {java.io.InputStream.class} - ) - public void test_ConstructorLjava_io_InputStream() { - // Test for method java.util.jar.Manifest(java.io.InputStream) - /* - * ByteArrayOutputStream baos = new ByteArrayOutputStream(); - * m2.write(baos); InputSteam is = new ByteArrayInputStream - * (baos.toByteArray()); Manifest myManifest = new Manifest (is); - * assertTrue("Manifests should be equal", myManifest.equals(m2)); - */ - - Manifest manifest = null; - InputStream is = null; - try { - is = new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")) - .openStream(); - } catch (MalformedURLException e1) { - fail("Failed to create InputStream object"); - } catch (IOException e1) { - fail("Failed to create InputStream object"); - } - try { - manifest = new Manifest(is); - } catch (MalformedURLException e) { - fail("Malformed URL"); - } catch (IOException e) { - fail("IOException"); - } - Attributes main = manifest.getMainAttributes(); - assertEquals("Bundle-Name not correct", "ClientSupport", main - .getValue("Bundle-Name")); - assertEquals( - "Bundle-Description not correct", - - "Provides SessionService, AuthenticationService. Extends RegistryService.", - main.getValue("Bundle-Description")); - assertEquals("Bundle-Activator not correct", - "com.ibm.ive.eccomm.client.support.ClientSupportActivator", - main.getValue("Bundle-Activator")); - assertEquals( - "Import-Package not correct", - - "com.ibm.ive.eccomm.client.services.log,com.ibm.ive.eccomm.client.services.registry,com.ibm.ive.eccomm.service.registry; specification-version=1.0.0,com.ibm.ive.eccomm.service.session; specification-version=1.0.0,com.ibm.ive.eccomm.service.framework; specification-version=1.2.0,org.osgi.framework; specification-version=1.0.0,org.osgi.service.log; specification-version=1.0.0,com.ibm.ive.eccomm.flash; specification-version=1.2.0,com.ibm.ive.eccomm.client.xml,com.ibm.ive.eccomm.client.http.common,com.ibm.ive.eccomm.client.http.client", - main.getValue("Import-Package")); - assertEquals( - "Import-Service not correct", - - "org.osgi.service.log.LogReaderServiceorg.osgi.service.log.LogService,com.ibm.ive.eccomm.service.registry.RegistryService", - main.getValue("Import-Service")); - assertEquals( - "Export-Package not correct", - - "com.ibm.ive.eccomm.client.services.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.service.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.common; specification-version=1.0.0,com.ibm.ive.eccomm.client.services.registry.store; specification-version=1.0.0", - main.getValue("Export-Package")); - assertEquals( - "Export-Service not correct", - - "com.ibm.ive.eccomm.service.authentication.AuthenticationService,com.ibm.ive.eccomm.service.session.SessionService", - main.getValue("Export-Service")); - assertEquals("Bundle-Vendor not correct", "IBM", main - .getValue("Bundle-Vendor")); - assertEquals("Bundle-Version not correct", "1.2.0", main - .getValue("Bundle-Version")); - try { - is.close(); - } catch (IOException e1) { - fail("Failed to close InputStream object"); - } - try { - manifest = new Manifest(is); - fail("IOException expected"); - } catch (MalformedURLException e) { - fail("IOException expected"); - } catch (IOException e) { - // expected - } - } - - /** - * @tests java.util.jar.Manifest#clear() + * @tests java.util.jar.Manifest#Manifest(java.util.jar.Manifest) */ @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "clear", - args = {} + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "Manifest", + args = {java.util.jar.Manifest.class} ) - public void test_clear() { - // Test for method void java.util.jar.Manifest.clear() - m2.clear(); - assertTrue("Should have no entries", m2.getEntries().isEmpty()); - assertTrue("Should have no main attributes", m2.getMainAttributes() - .isEmpty()); + public void testCopyingConstructor() throws IOException { + Manifest firstManifest = new Manifest(new URL(Support_Resources + .getURL(MANIFEST_NAME)).openStream()); + Manifest secondManifest = new Manifest(firstManifest); + assertEquals(firstManifest, secondManifest); } /** - * @tests java.util.jar.Manifest#getAttributes(java.lang.String) + * @tests java.util.jar.Manifest#Manifest(Manifest) */ @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "getAttributes", - args = {java.lang.String.class} + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "Manifest", + args = {java.util.jar.Manifest.class} ) - public void test_getAttributesLjava_lang_String() { - // Test for method java.util.jar.Attributes - // java.util.jar.Manifest.getAttributes(java.lang.String) - assertNull("Should not exist", m2.getAttributes("Doesn't Exist")); - assertEquals("Should exist", "OK", m2 - .getAttributes("HasAttributes.txt").get( - new Attributes.Name("MyAttribute"))); + public void test_ConstructorLjava_util_jar_Manifest() { + // Test for method java.util.jar.Manifest() + Manifest emptyManifest = new Manifest(); + Manifest emptyClone = new Manifest(emptyManifest); + assertTrue("Should have no entries", emptyClone.getEntries().isEmpty()); + assertTrue("Should have no main attributes", emptyClone + .getMainAttributes().isEmpty()); + assertEquals(emptyClone, emptyManifest); + assertEquals(emptyClone, emptyManifest.clone()); } - /** - * @tests java.util.jar.Manifest#getEntries() - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "getEntries", - args = {} - ) - public void test_getEntries() { - // Test for method java.util.Map java.util.jar.Manifest.getEntries() - Map<String, Attributes> myMap = m2.getEntries(); - assertNull("Shouldn't exist", myMap.get("Doesn't exist")); - assertEquals("Should exist", "OK", myMap.get("HasAttributes.txt").get( - new Attributes.Name("MyAttribute"))); - + private void assertAttribute(Attributes attr, String name, String value) { + assertEquals("Incorrect " + name, value, attr.getValue(name)); } - /** - * @tests java.util.jar.Manifest#getMainAttributes() - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "getMainAttributes", - args = {} - ) - public void test_getMainAttributes() { - // Test for method java.util.jar.Attributes - // java.util.jar.Manifest.getMainAttributes() - Attributes a = m.getMainAttributes(); - assertEquals("Manifest_Version should return 1.0", "1.0", a - .get(Attributes.Name.MANIFEST_VERSION)); + private void checkManifest(Manifest manifest) { + Attributes main = manifest.getMainAttributes(); + assertAttribute(main, "Bundle-Name", "ClientSupport"); + assertAttribute(main, "Bundle-Description", + "Provides SessionService, AuthenticationService. Extends RegistryService."); + assertAttribute(main, "Bundle-Activator", + "com.ibm.ive.eccomm.client.support.ClientSupportActivator"); + assertAttribute( + main, + "Import-Package", + "com.ibm.ive.eccomm.client.services.log,com.ibm.ive.eccomm.client.services.registry,com.ibm.ive.eccomm.service.registry; specification-version=1.0.0,com.ibm.ive.eccomm.service.session; specification-version=1.0.0,com.ibm.ive.eccomm.service.framework; specification-version=1.2.0,org.osgi.framework; specification-version=1.0.0,org.osgi.service.log; specification-version=1.0.0,com.ibm.ive.eccomm.flash; specification-version=1.2.0,com.ibm.ive.eccomm.client.xml,com.ibm.ive.eccomm.client.http.common,com.ibm.ive.eccomm.client.http.client"); + assertAttribute( + main, + "Import-Service", + "org.osgi.service.log.LogReaderServiceorg.osgi.service.log.LogService,com.ibm.ive.eccomm.service.registry.RegistryService"); + assertAttribute( + main, + "Export-Package", + "com.ibm.ive.eccomm.client.services.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.service.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.common; specification-version=1.0.0,com.ibm.ive.eccomm.client.services.registry.store; specification-version=1.0.0"); + assertAttribute( + main, + "Export-Service", + "com.ibm.ive.eccomm.service.authentication.AuthenticationService,com.ibm.ive.eccomm.service.session.SessionService"); + assertAttribute(main, "Bundle-Vendor", "IBM"); + assertAttribute(main, "Bundle-Version", "1.2.0"); } /** - * @tests {@link java.util.jar.Manifest#read(java.io.InputStream) + * @tests java.util.jar.Manifest#Manifest(java.io.InputStream) */ @TestTargetNew( level = TestLevel.COMPLETE, - notes = "", - method = "read", + notes = "IOException checking missed.", + method = "Manifest", args = {java.io.InputStream.class} ) - public void test_readLjava_io_InputStream() { - // Regression for HARMONY-89 - InputStream is = new InputStreamImpl(); - try { - new Manifest().read(is); - fail("Assert 0: Should have thrown IOException"); - } catch (IOException e) { - // expected - } + public void test_ConstructorLjava_io_InputStream() throws IOException { + Manifest m = getManifest(attJarName); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + m.write(baos); + InputStream is = new ByteArrayInputStream(baos.toByteArray()); + Manifest mCopy = new Manifest(is); + assertEquals(m, mCopy); - Manifest manifest = new Manifest(); - try { - manifest.read(new URL(Support_Resources - .getURL("manifest/hyts_MANIFEST.MF")).openStream()); - } catch (MalformedURLException e) { - fail("Can nor read manifest"); - } catch (IOException e) { - fail("Can nor read manifest"); - } - Attributes main = manifest.getMainAttributes(); - assertEquals("Bundle-Name not correct", "ClientSupport", main - .getValue("Bundle-Name")); - assertEquals( - "Bundle-Description not correct", - - "Provides SessionService, AuthenticationService. Extends RegistryService.", - main.getValue("Bundle-Description")); - assertEquals("Bundle-Activator not correct", - "com.ibm.ive.eccomm.client.support.ClientSupportActivator", - main.getValue("Bundle-Activator")); - assertEquals( - "Import-Package not correct", - - "com.ibm.ive.eccomm.client.services.log,com.ibm.ive.eccomm.client.services.registry,com.ibm.ive.eccomm.service.registry; specification-version=1.0.0,com.ibm.ive.eccomm.service.session; specification-version=1.0.0,com.ibm.ive.eccomm.service.framework; specification-version=1.2.0,org.osgi.framework; specification-version=1.0.0,org.osgi.service.log; specification-version=1.0.0,com.ibm.ive.eccomm.flash; specification-version=1.2.0,com.ibm.ive.eccomm.client.xml,com.ibm.ive.eccomm.client.http.common,com.ibm.ive.eccomm.client.http.client", - main.getValue("Import-Package")); - assertEquals( - "Import-Service not correct", - - "org.osgi.service.log.LogReaderServiceorg.osgi.service.log.LogService,com.ibm.ive.eccomm.service.registry.RegistryService", - main.getValue("Import-Service")); - assertEquals( - "Export-Package not correct", - - "com.ibm.ive.eccomm.client.services.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.service.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.common; specification-version=1.0.0,com.ibm.ive.eccomm.client.services.registry.store; specification-version=1.0.0", - main.getValue("Export-Package")); - assertEquals( - "Export-Service not correct", - - "com.ibm.ive.eccomm.service.authentication.AuthenticationService,com.ibm.ive.eccomm.service.session.SessionService", - main.getValue("Export-Service")); - assertEquals("Bundle-Vendor not correct", "IBM", main - .getValue("Bundle-Vendor")); - assertEquals("Bundle-Version not correct", "1.2.0", main - .getValue("Bundle-Version")); - } + Manifest manifest = new Manifest(new URL(Support_Resources + .getURL(MANIFEST_NAME)).openStream()); + checkManifest(manifest); - // helper class - class InputStreamImpl extends InputStream { - public InputStreamImpl() { - super(); - } + // regression test for HARMONY-5424 + String manifestContent = "Manifest-Version: 1.0\nCreated-By: Apache\nPackage: \nBuild-Jdk: 1.4.1_01\n\n" + + "Name: \nSpecification-Title: foo\nSpecification-Version: 1.0\nSpecification-Vendor: \n" + + "Implementation-Title: \nImplementation-Version: 1.0\nImplementation-Vendor: \n\n"; + ByteArrayInputStream bis = new ByteArrayInputStream(manifestContent + .getBytes("ISO-8859-1")); - @Override - public int read() { - return 0; - } + + Manifest mf = new Manifest(bis); + assertEquals("Should be 4 main attributes", 4, mf.getMainAttributes() + .size()); + + Map<String, Attributes> entries = mf.getEntries(); + assertEquals("Should be one named entry", 1, entries.size()); + + Attributes namedEntryAttributes = (Attributes) (entries.get("")); + assertEquals("Should be 6 named entry attributes", 6, + namedEntryAttributes.size()); } /** - * @tests java.util.jar.Manifest#Manifest(Manifest) + * @tests java.util.jar.Manifest#clear() */ @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "Manifest", - args = {java.util.jar.Manifest.class} + level = TestLevel.COMPLETE, + notes = "", + method = "clear", + args = {} ) - public void test_ConstructorLjava_util_jar_Manifest() { - // Test for method java.util.jar.Manifest() - Manifest emptyManifest = new Manifest(); - Manifest emptyClone = new Manifest(emptyManifest); - assertTrue("Should have no entries", emptyClone.getEntries().isEmpty()); - assertTrue("Should have no main attributes", emptyClone - .getMainAttributes().isEmpty()); - assertEquals(emptyClone, emptyManifest); - assertEquals(emptyClone, emptyManifest.clone()); + public void test_clear() { + m2.clear(); + assertTrue("Should have no entries", m2.getEntries().isEmpty()); + assertTrue("Should have no main attributes", m2.getMainAttributes() + .isEmpty()); } @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "clone", - args = {} + level = TestLevel.COMPLETE, + notes = "", + method = "clone", + args = {} ) - public void test_clone() { + public void test_clone() throws IOException { Manifest emptyManifest = new Manifest(); Manifest emptyClone = (Manifest) emptyManifest.clone(); assertTrue("Should have no entries", emptyClone.getEntries().isEmpty()); @@ -354,88 +243,25 @@ public class ManifestTest extends TestCase { assertEquals(emptyManifest.clone().getClass().getName(), "java.util.jar.Manifest"); - Manifest manifest = null; - try { - manifest = new Manifest(new URL(Support_Resources - .getURL("manifest/hyts_MANIFEST.MF")).openStream()); - } catch (MalformedURLException e) { - fail("Malformed URL"); - } catch (IOException e) { - fail("IOException"); - } + Manifest manifest = new Manifest(new URL(Support_Resources + .getURL("manifest/hyts_MANIFEST.MF")).openStream()); Manifest manifestClone = (Manifest) manifest.clone(); - Attributes main = manifestClone.getMainAttributes(); - assertEquals("Bundle-Name not correct", "ClientSupport", main - .getValue("Bundle-Name")); - assertEquals( - "Bundle-Description not correct", - - "Provides SessionService, AuthenticationService. Extends RegistryService.", - main.getValue("Bundle-Description")); - assertEquals("Bundle-Activator not correct", - "com.ibm.ive.eccomm.client.support.ClientSupportActivator", - main.getValue("Bundle-Activator")); - assertEquals( - "Import-Package not correct", - - "com.ibm.ive.eccomm.client.services.log,com.ibm.ive.eccomm.client.services.registry,com.ibm.ive.eccomm.service.registry; specification-version=1.0.0,com.ibm.ive.eccomm.service.session; specification-version=1.0.0,com.ibm.ive.eccomm.service.framework; specification-version=1.2.0,org.osgi.framework; specification-version=1.0.0,org.osgi.service.log; specification-version=1.0.0,com.ibm.ive.eccomm.flash; specification-version=1.2.0,com.ibm.ive.eccomm.client.xml,com.ibm.ive.eccomm.client.http.common,com.ibm.ive.eccomm.client.http.client", - main.getValue("Import-Package")); - assertEquals( - "Import-Service not correct", - - "org.osgi.service.log.LogReaderServiceorg.osgi.service.log.LogService,com.ibm.ive.eccomm.service.registry.RegistryService", - main.getValue("Import-Service")); - assertEquals( - "Export-Package not correct", - - "com.ibm.ive.eccomm.client.services.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.service.authentication; specification-version=1.0.0,com.ibm.ive.eccomm.common; specification-version=1.0.0,com.ibm.ive.eccomm.client.services.registry.store; specification-version=1.0.0", - main.getValue("Export-Package")); - assertEquals( - "Export-Service not correct", - - "com.ibm.ive.eccomm.service.authentication.AuthenticationService,com.ibm.ive.eccomm.service.session.SessionService", - main.getValue("Export-Service")); - assertEquals("Bundle-Vendor not correct", "IBM", main - .getValue("Bundle-Vendor")); - assertEquals("Bundle-Version not correct", "1.2.0", main - .getValue("Bundle-Version")); + manifestClone.getMainAttributes(); + checkManifest(manifestClone); } @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "equals", - args = {java.lang.Object.class} + level = TestLevel.COMPLETE, + notes = "", + method = "equals", + args = {java.lang.Object.class} ) - public void test_equals() { - Manifest manifest1 = null; - Manifest manifest2 = null; + 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(); - InputStream is = null; - try { - is = new URL(Support_Resources.getURL("manifest/hyts_MANIFEST.MF")) - .openStream(); - } catch (MalformedURLException e1) { - fail("Failed to create InputStream object"); - } catch (IOException e1) { - fail("Failed to create InputStream object"); - } - try { - manifest1 = new Manifest(is); - } catch (MalformedURLException e) { - fail("Malformed URL"); - } catch (IOException e) { - fail("IOException"); - } - - try { - manifest2 = new Manifest(new URL(Support_Resources - .getURL("manifest/hyts_MANIFEST.MF")).openStream()); - } catch (MalformedURLException e) { - fail("Malformed URL"); - } catch (IOException e) { - fail("IOException"); - } assertTrue(manifest1.equals(manifest1)); assertTrue(manifest1.equals(manifest2)); @@ -444,27 +270,69 @@ public class ManifestTest extends TestCase { } @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "hashCode", - args = {} + level = TestLevel.COMPLETE, + notes = "", + method = "hashCode", + args = {} ) - public void test_hashCode() { - Manifest manifest1 = null; + public void test_hashCode() throws IOException { + Manifest manifest1 = new Manifest(new URL(Support_Resources + .getURL("manifest/hyts_MANIFEST.MF")).openStream()); Manifest manifest2 = new Manifest(); - InputStream is = null; - try { - manifest1 = new Manifest(new URL(Support_Resources - .getURL("manifest/hyts_MANIFEST.MF")).openStream()); - } catch (MalformedURLException e) { - fail("Malformed URL"); - } catch (IOException e) { - fail("IOException"); - } assertEquals(manifest1.hashCode(), manifest1.hashCode()); assertNotSame(manifest1.hashCode(), manifest2.hashCode()); } + /** + * @tests java.util.jar.Manifest#getAttributes(java.lang.String) + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getAttributes", + args = {String.class} + ) + public void test_getAttributesLjava_lang_String() { + assertNull("Should not exist", + m2.getAttributes("Doesn't Exist")); + assertEquals("Should exist", "OK", m2.getAttributes("HasAttributes.txt").get( + new Attributes.Name("MyAttribute"))); + } + + /** + * @tests java.util.jar.Manifest#getEntries() + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getEntries", + args = {} + ) + public void test_getEntries() { + Map<String, Attributes> myMap = m2.getEntries(); + assertNull("Shouldn't exist", myMap.get("Doesn't exist")); + assertEquals("Should exist", + "OK", myMap.get("HasAttributes.txt").get( + new Attributes.Name("MyAttribute"))); + } + + /** + * @tests java.util.jar.Manifest#getMainAttributes() + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "getMainAttributes", + args = {} + ) + public void test_getMainAttributes() { + // Test for method java.util.jar.Attributes + // java.util.jar.Manifest.getMainAttributes() + Attributes a = m.getMainAttributes(); + assertEquals("Manifest_Version should return 1.0", "1.0", a.get( + Attributes.Name.MANIFEST_VERSION)); + } + @TestTargetNew( level = TestLevel.COMPLETE, notes = "", @@ -510,4 +378,219 @@ public class ManifestTest extends TestCase { assertTrue(manifest1.equals(manifest2)); } + + /** + * Ensures compatibility with manifests produced by gcc. + * + * @see <a + * href="http://issues.apache.org/jira/browse/HARMONY-5662">HARMONY-5662</a> + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "Manifest", + args = {InputStream.class} + ) + public void testNul() throws IOException { + String manifestContent = + "Manifest-Version: 1.0\nCreated-By: nasty gcc tool\n\n\0"; + + byte[] bytes = manifestContent.getBytes("ISO-8859-1"); + new Manifest(new ByteArrayInputStream(bytes)); // the last NUL is ok + + bytes[bytes.length - 1] = 26; + new Manifest(new ByteArrayInputStream(bytes)); // the last EOF is ok + + bytes[bytes.length - 1] = 'A'; // the last line ignored + new Manifest(new ByteArrayInputStream(bytes)); + + bytes[2] = 0; // NUL char in Manifest + try { + new Manifest(new ByteArrayInputStream(bytes)); + fail("IOException expected"); + } catch (IOException e) { + // desired + } + } + + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + method = "Manifest", + args = {InputStream.class} + ) + @KnownFailure("CharsetDecoder fails with an IllegalStateException") + public void testDecoding() throws IOException { + Manifest m = getManifest(attJarName); + final byte[] bVendor = new byte[] { (byte) 0xd0, (byte) 0x9C, + (byte) 0xd0, (byte) 0xb8, (byte) 0xd0, (byte) 0xbb, + (byte) 0xd0, (byte) 0xb0, (byte) 0xd1, (byte) 0x8f, ' ', + (byte) 0xd0, (byte) 0xb4, (byte) 0xd0, (byte) 0xbe, + (byte) 0xd1, (byte) 0x87, (byte) 0xd1, (byte) 0x83, + (byte) 0xd0, (byte) 0xbd, (byte) 0xd1, (byte) 0x8C, + (byte) 0xd0, (byte) 0xba, (byte) 0xd0, (byte) 0xb0, ' ', + (byte) 0xd0, (byte) 0x9C, (byte) 0xd0, (byte) 0xb0, + (byte) 0xd1, (byte) 0x88, (byte) 0xd0, (byte) 0xb0 }; + + final byte[] bSpec = new byte[] { (byte) 0xe1, (byte) 0x88, + (byte) 0xb0, (byte) 0xe1, (byte) 0x88, (byte) 0x8b, + (byte) 0xe1, (byte) 0x88, (byte) 0x9d, ' ', (byte) 0xe1, + (byte) 0x9a, (byte) 0xa0, (byte) 0xe1, (byte) 0x9a, + (byte) 0xb1, (byte) 0xe1, (byte) 0x9b, (byte) 0x81, + (byte) 0xe1, (byte) 0x9a, (byte) 0xa6, ' ', (byte) 0xd8, + (byte) 0xb3, (byte) 0xd9, (byte) 0x84, (byte) 0xd8, + (byte) 0xa7, (byte) 0xd9, (byte) 0x85, ' ', (byte) 0xd8, + (byte) 0xb9, (byte) 0xd8, (byte) 0xb3, (byte) 0xd9, + (byte) 0x84, (byte) 0xd8, (byte) 0xa7, (byte) 0xd9, + (byte) 0x85, (byte) 0xd8, (byte) 0xa9, ' ', (byte) 0xdc, + (byte) 0xab, (byte) 0xdc, (byte) 0xa0, (byte) 0xdc, + (byte) 0xa1, (byte) 0xdc, (byte) 0x90, ' ', (byte) 0xe0, + (byte) 0xa6, (byte) 0xb6, (byte) 0xe0, (byte) 0xa6, + (byte) 0xbe, (byte) 0xe0, (byte) 0xa6, (byte) 0xa8, + (byte) 0xe0, (byte) 0xa7, (byte) 0x8d, (byte) 0xe0, + (byte) 0xa6, (byte) 0xa4, (byte) 0xe0, (byte) 0xa6, + (byte) 0xbf, ' ', (byte) 0xd0, (byte) 0xa0, (byte) 0xd0, + (byte) 0xb5, (byte) 0xd0, (byte) 0xba, (byte) 0xd1, + (byte) 0x8a, (byte) 0xd0, (byte) 0xb5, (byte) 0xd0, + (byte) 0xbb, ' ', (byte) 0xd0, (byte) 0x9c, (byte) 0xd0, + (byte) 0xb8, (byte) 0xd1, (byte) 0x80, ' ', (byte) 0xe0, + (byte) 0xa6, (byte) 0xb6, (byte) 0xe0, (byte) 0xa6, + (byte) 0xbe, (byte) 0xe0, (byte) 0xa6, (byte) 0xa8, + (byte) 0xe0, (byte) 0xa7, (byte) 0x8d, (byte) 0xe0, + (byte) 0xa6, (byte) 0xa4, (byte) 0xe0, (byte) 0xa6, + (byte) 0xbf, ' ', (byte) 0xe0, (byte) 0xbd, (byte) 0x9e, + (byte) 0xe0, (byte) 0xbd, (byte) 0xb2, (byte) 0xe0, + (byte) 0xbc, (byte) 0x8b, (byte) 0xe0, (byte) 0xbd, + (byte) 0x96, (byte) 0xe0, (byte) 0xbd, (byte) 0x91, + (byte) 0xe0, (byte) 0xbd, (byte) 0xba, ' ', (byte) 0xd0, + (byte) 0x9c, (byte) 0xd0, (byte) 0xb0, (byte) 0xd1, + (byte) 0x88, (byte) 0xd0, (byte) 0xb0, (byte) 0xd1, + (byte) 0x80, ' ', (byte) 0xe1, (byte) 0x8f, (byte) 0x99, + (byte) 0xe1, (byte) 0x8e, (byte) 0xaf, (byte) 0xe1, + (byte) 0x8f, (byte) 0xb1, ' ', (byte) 0xcf, (byte) 0xa8, + (byte) 0xce, (byte) 0xb9, (byte) 0xcf, (byte) 0x81, + (byte) 0xce, (byte) 0xb7, (byte) 0xce, (byte) 0xbd, + (byte) 0xce, (byte) 0xb7, ' ', (byte) 0xde, (byte) 0x90, + (byte) 0xde, (byte) 0xaa, (byte) 0xde, (byte) 0x85, + (byte) 0xde, (byte) 0xa6, ' ', (byte) 0xe0, (byte) 0xbd, + (byte) 0x82, (byte) 0xe0, (byte) 0xbd, (byte) 0x9e, + (byte) 0xe0, (byte) 0xbd, (byte) 0xb2, (byte) 0xe0, + (byte) 0xbc, (byte) 0x8b, (byte) 0xe0, (byte) 0xbd, + (byte) 0x96, (byte) 0xe0, (byte) 0xbd, (byte) 0x91, + (byte) 0xe0, (byte) 0xbd, (byte) 0xba, ' ', (byte) 0xce, + (byte) 0x95, (byte) 0xce, (byte) 0xb9, (byte) 0xcf, + (byte) 0x81, (byte) 0xce, (byte) 0xae, (byte) 0xce, + (byte) 0xbd, (byte) 0xce, (byte) 0xb7, ' ', (byte) 0xd8, + (byte) 0xb5, (byte) 0xd9, (byte) 0x84, (byte) 0xd8, + (byte) 0xad, ' ', (byte) 0xe0, (byte) 0xaa, (byte) 0xb6, + (byte) 0xe0, (byte) 0xaa, (byte) 0xbe, (byte) 0xe0, + (byte) 0xaa, (byte) 0x82, (byte) 0xe0, (byte) 0xaa, + (byte) 0xa4, (byte) 0xe0, (byte) 0xaa, (byte) 0xbf, ' ', + (byte) 0xe5, (byte) 0xb9, (byte) 0xb3, (byte) 0xe5, + (byte) 0x92, (byte) 0x8c, ' ', (byte) 0xd7, (byte) 0xa9, + (byte) 0xd7, (byte) 0x9c, (byte) 0xd7, (byte) 0x95, + (byte) 0xd7, (byte) 0x9d, ' ', (byte) 0xd7, (byte) 0xa4, + (byte) 0xd7, (byte) 0xa8, (byte) 0xd7, (byte) 0x99, + (byte) 0xd7, (byte) 0x93, (byte) 0xd7, (byte) 0x9f, ' ', + (byte) 0xe5, (byte) 0x92, (byte) 0x8c, (byte) 0xe5, + (byte) 0xb9, (byte) 0xb3, ' ', (byte) 0xe5, (byte) 0x92, + (byte) 0x8c, (byte) 0xe5, (byte) 0xb9, (byte) 0xb3, ' ', + (byte) 0xd8, (byte) 0xaa, (byte) 0xd9, (byte) 0x89, + (byte) 0xd9, (byte) 0x86, (byte) 0xda, (byte) 0x86, + (byte) 0xd9, (byte) 0x84, (byte) 0xd9, (byte) 0x89, + (byte) 0xd9, (byte) 0x82, ' ', (byte) 0xe0, (byte) 0xae, + (byte) 0x85, (byte) 0xe0, (byte) 0xae, (byte) 0xae, + (byte) 0xe0, (byte) 0xaf, (byte) 0x88, (byte) 0xe0, + (byte) 0xae, (byte) 0xa4, (byte) 0xe0, (byte) 0xae, + (byte) 0xbf, ' ', (byte) 0xe0, (byte) 0xb0, (byte) 0xb6, + (byte) 0xe0, (byte) 0xb0, (byte) 0xbe, (byte) 0xe0, + (byte) 0xb0, (byte) 0x82, (byte) 0xe0, (byte) 0xb0, + (byte) 0xa4, (byte) 0xe0, (byte) 0xb0, (byte) 0xbf, ' ', + (byte) 0xe0, (byte) 0xb8, (byte) 0xaa, (byte) 0xe0, + (byte) 0xb8, (byte) 0xb1, (byte) 0xe0, (byte) 0xb8, + (byte) 0x99, (byte) 0xe0, (byte) 0xb8, (byte) 0x95, + (byte) 0xe0, (byte) 0xb8, (byte) 0xb4, (byte) 0xe0, + (byte) 0xb8, (byte) 0xa0, (byte) 0xe0, (byte) 0xb8, + (byte) 0xb2, (byte) 0xe0, (byte) 0xb8, (byte) 0x9e, ' ', + (byte) 0xe1, (byte) 0x88, (byte) 0xb0, (byte) 0xe1, + (byte) 0x88, (byte) 0x8b, (byte) 0xe1, (byte) 0x88, + (byte) 0x9d, ' ', (byte) 0xe0, (byte) 0xb7, (byte) 0x83, + (byte) 0xe0, (byte) 0xb7, (byte) 0x8f, (byte) 0xe0, + (byte) 0xb6, (byte) 0xb8, (byte) 0xe0, (byte) 0xb6, + (byte) 0xba, ' ', (byte) 0xe0, (byte) 0xa4, (byte) 0xb6, + (byte) 0xe0, (byte) 0xa4, (byte) 0xbe, (byte) 0xe0, + (byte) 0xa4, (byte) 0xa8, (byte) 0xe0, (byte) 0xa5, + (byte) 0x8d, (byte) 0xe0, (byte) 0xa4, (byte) 0xa4, + (byte) 0xe0, (byte) 0xa4, (byte) 0xbf, (byte) 0xe0, + (byte) 0xa4, (byte) 0x83, ' ', (byte) 0xe1, (byte) 0x83, + (byte) 0x9b, (byte) 0xe1, (byte) 0x83, (byte) 0xa8, + (byte) 0xe1, (byte) 0x83, (byte) 0x95, (byte) 0xe1, + (byte) 0x83, (byte) 0x98, (byte) 0xe1, (byte) 0x83, + (byte) 0x93, (byte) 0xe1, (byte) 0x83, (byte) 0x9d, + (byte) 0xe1, (byte) 0x83, (byte) 0x91, (byte) 0xe1, + (byte) 0x83, (byte) 0x90 }; + // TODO Cannot make the following word work, encoder changes needed + // (byte) 0xed, (byte) 0xa0, (byte) 0x80, + // (byte) 0xed, (byte) 0xbc, (byte) 0xb2, (byte) 0xed, + // (byte) 0xa0, (byte) 0x80, (byte) 0xed, (byte) 0xbc, + // (byte) 0xb0, (byte) 0xed, (byte) 0xa0, (byte) 0x80, + // (byte) 0xed, (byte) 0xbd, (byte) 0x85, (byte) 0xed, + // (byte) 0xa0, (byte) 0x80, (byte) 0xed, (byte) 0xbc, + // (byte) 0xb0, (byte) 0xed, (byte) 0xa0, (byte) 0x80, + // (byte) 0xed, (byte) 0xbc, (byte) 0xb9, (byte) 0xed, + // (byte) 0xa0, (byte) 0x80, (byte) 0xed, (byte) 0xbc, + // (byte) 0xb8, (byte) 0xed, (byte) 0xa0, (byte) 0x80, + // (byte) 0xed, (byte) 0xbc, (byte) 0xb9, ' ' + + final String vendor = new String(bVendor, "UTF-8"); + final String spec = new String(bSpec, "UTF-8"); + m.getMainAttributes() + .put(Attributes.Name.IMPLEMENTATION_VENDOR, vendor); + m.getAttributes(ATT_ENTRY_NAME).put( + Attributes.Name.IMPLEMENTATION_VENDOR, vendor); + m.getEntries().get(ATT_ENTRY_NAME).put( + Attributes.Name.SPECIFICATION_TITLE, spec); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + m.write(baos); + m = new Manifest(new ByteArrayInputStream(baos.toByteArray())); + + assertEquals(vendor, m.getMainAttributes().get( + Attributes.Name.IMPLEMENTATION_VENDOR)); + assertEquals(vendor, m.getEntries().get(ATT_ENTRY_NAME).get( + Attributes.Name.IMPLEMENTATION_VENDOR)); + assertEquals(spec, m.getAttributes(ATT_ENTRY_NAME).get( + Attributes.Name.SPECIFICATION_TITLE)); + } + + /** + * @tests {@link java.util.jar.Manifest#read(java.io.InputStream) + */ + @TestTargetNew( + level = TestLevel.PARTIAL, + notes = "", + method = "read", + args = {InputStream.class} + ) + public void testRead() { + // Regression for HARMONY-89 + InputStream is = new InputStreamImpl(); + try { + new Manifest().read(is); + fail("IOException expected"); + } catch (IOException e) { + // desired + } + } + + // helper class + private class InputStreamImpl extends InputStream { + public InputStreamImpl() { + super(); + } + + @Override + public int read() { + return 0; + } + } } diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPInputStreamTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPInputStreamTest.java index 75060bd..1e8ddb4 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPInputStreamTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPInputStreamTest.java @@ -25,6 +25,8 @@ import dalvik.annotation.TestTargetNew; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -310,7 +312,43 @@ public class GZIPInputStreamTest extends junit.framework.TestCase { } } - @Override + /** + * Regression test for HARMONY-3703. + * @tests java.util.zip.GZIPInputStream#read() + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "read", + args = {byte[].class} + ) + public void test_read() throws IOException { + GZIPInputStream gis = null; + int result = 0; + byte[] buffer = new byte[] {1,2,3,4,5,6,7,8,9,10}; + File f = new File(resources.getAbsolutePath() + "test.gz"); + FileOutputStream out = new FileOutputStream(f); + GZIPOutputStream gout = new GZIPOutputStream(out); + + // write 100 bytes to the stream + for(int i = 0; i < 10; i++) { + gout.write(buffer); + } + gout.finish(); + out.write(1); + out.close(); + + gis = new GZIPInputStream(new FileInputStream(f)); + buffer = new byte[100]; + gis.read(buffer); + result = gis.read(); + gis.close(); + f.delete(); + + assertEquals("Incorrect value returned at the end of the file", -1, result); + } + + @Override protected void setUp() { resources = Support_Resources.createTempFolder(); } diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java index 9b23b56..b71ce63 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/GZIPOutputStreamTest.java @@ -201,8 +201,6 @@ public class GZIPOutputStreamTest extends junit.framework.TestCase { int r = 0; try { outGZIP.write(byteArray, 0, 11); - } catch (ArrayIndexOutOfBoundsException e) { - r = 1; } catch (IndexOutOfBoundsException ee) { r = 1; } diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java index 8b89180..6039c5b 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/InflaterTest.java @@ -409,6 +409,22 @@ public class InflaterTest extends junit.framework.TestCase { } /** + * @tests java.util.zip.Inflater#Inflater() + */ + @TestTargetNew( + level = TestLevel.COMPLETE, + notes = "", + method = "Inflater", + args = {} + ) + public void test_Constructor() { + // test method of java.util.zip.inflater.Inflater() + Inflater inflate = new Inflater(); + assertNotNull("failed to create the instance of inflater", + inflate); + } + + /** * @tests java.util.zip.Inflater#inflate(byte[], int, int) */ @TestTargetNew( @@ -504,27 +520,6 @@ public class InflaterTest extends junit.framework.TestCase { } /** - * @tests java.util.zip.Inflater#Inflater() - */ - @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", - method = "Inflater", - args = {} - ) - public void test_Constructor() { - // test method of java.util.zip.inflater.Inflater() - try { - Inflater inflate = new Inflater(); - assertNotNull("failed to create the instance of inflater", inflate); - - } catch (Exception e) { - - assertTrue("Inflate () constructor threw an exception", true); - } - } - - /** * @tests java.util.zip.Inflater#Inflater(boolean) */ @TestTargetNew( diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java index 5530a2e..c9e7bb8 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipFileTest.java @@ -67,7 +67,7 @@ public class ZipFileTest extends junit.framework.TestCase { public void checkPermission(Permission perm) { // only check if it's a FilePermission because Locale checks // for a PropertyPermission with action"read" to get system props. - if (perm instanceof FilePermission + if (perm instanceof FilePermission && perm.getActions().equals(forbidenPermissionAction)) { throw new SecurityException(); } @@ -145,7 +145,7 @@ public class ZipFileTest extends junit.framework.TestCase { public void test_ConstructorLjava_lang_String() throws IOException { String oldUserDir = System.getProperty("user.dir"); System.setProperty("user.dir", System.getProperty("java.io.tmpdir")); - + zfile.close(); // about to reopen the same temp file ZipFile zip = new ZipFile(tempFileName); zip.close(); @@ -260,7 +260,7 @@ public class ZipFileTest extends junit.framework.TestCase { method = "entries", args = {} ) - public void test_entries() { + public void test_entries() throws Exception { // Test for method java.util.Enumeration java.util.zip.ZipFile.entries() Enumeration<? extends ZipEntry> enumer = zfile.entries(); int c = 0; @@ -270,20 +270,16 @@ public class ZipFileTest extends junit.framework.TestCase { } assertTrue("Incorrect number of entries returned: " + c, c == 6); + Enumeration<? extends ZipEntry> enumeration = zfile.entries(); + zfile.close(); + zfile = null; + boolean pass = false; try { - Enumeration<? extends ZipEntry> enumeration = zfile.entries(); - zfile.close(); - zfile = null; - boolean pass = false; - try { - enumeration.hasMoreElements(); - } catch (IllegalStateException e) { - pass = true; - } - assertTrue("did not detect closed jar file", pass); - } catch (Exception e) { - fail("Exception during entries test: " + e.toString()); + enumeration.hasMoreElements(); + } catch (IllegalStateException e) { + pass = true; } + assertTrue("did not detect closed jar file", pass); } /** @@ -454,6 +450,99 @@ public class ZipFileTest extends junit.framework.TestCase { } /** + * @tests java.io.InputStream#reset() + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + method = "getInputStream", + args = {java.util.zip.ZipEntry.class} + ) + @KnownFailure("ZipEntry.getInputStream().reset() fails with an IOException") + public void test_reset() throws IOException { + // read an uncompressed entry + ZipEntry zentry = zfile.getEntry("File1.txt"); + InputStream is = zfile.getInputStream(zentry); + byte[] rbuf1 = new byte[6]; + byte[] rbuf2 = new byte[6]; + int r1, r2; + r1 = is.read(rbuf1); + assertEquals(rbuf1.length, r1); + r2 = is.read(rbuf2); + assertEquals(rbuf2.length, r2); + + is.reset(); + r2 = is.read(rbuf2); + assertEquals(rbuf2.length, r2); + is.close(); + + // read a compressed entry + byte[] rbuf3 = new byte[4185]; + ZipEntry zentry2 = zfile.getEntry("File3.txt"); + is = zfile.getInputStream(zentry2); + r1 = is.read(rbuf3); + assertEquals(4183, r1); + is.reset(); + + r1 = is.read(rbuf3); + assertEquals(4183, r1); + is.close(); + + is = zfile.getInputStream(zentry2); + r1 = is.read(rbuf3, 0, 3000); + assertEquals(3000, r1); + is.reset(); + r1 = is.read(rbuf3, 0, 3000); + assertEquals(3000, r1); + is.close(); + } + + /** + * @tests java.io.InputStream#reset() + */ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + method = "getInputStream", + args = {java.util.zip.ZipEntry.class} + ) + @KnownFailure("ZipEntry.getInputStream().reset() fails with an IOException") + public void test_reset_subtest0() throws IOException { + // read an uncompressed entry + ZipEntry zentry = zfile.getEntry("File1.txt"); + InputStream is = zfile.getInputStream(zentry); + byte[] rbuf1 = new byte[12]; + byte[] rbuf2 = new byte[12]; + int r = is.read(rbuf1, 0, 4); + assertEquals(4, r); + is.mark(0); + r = is.read(rbuf1); + assertEquals(8, r); + assertEquals(-1, is.read()); + + is.reset(); + r = is.read(rbuf2); + assertEquals(8, r); + assertEquals(-1, is.read()); + is.close(); + + // read a compressed entry + byte[] rbuf3 = new byte[4185]; + ZipEntry zentry2 = zfile.getEntry("File3.txt"); + is = zfile.getInputStream(zentry2); + r = is.read(rbuf3, 0, 3000); + assertEquals(3000, r); + is.mark(0); + r = is.read(rbuf3); + assertEquals(1183, r); + assertEquals(-1, is.read()); + + is.reset(); + r = is.read(rbuf3); + assertEquals(1183, r); + assertEquals(-1, is.read()); + is.close(); + } + + /** * Sets up the fixture, for example, open a network connection. This method * is called before a test is executed. */ diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java index 9a5f63a..8ca551d 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/zip/ZipOutputStreamTest.java @@ -170,11 +170,8 @@ public class ZipOutputStreamTest extends junit.framework.TestCase { public void test_setCommentLjava_lang_String() { // There is no way to get the comment back, so no way to determine if // the comment is set correct - try { - zos.setComment("test setComment"); - } catch (Exception e) { - fail("Trying to set comment failed"); - } + zos.setComment("test setComment"); + try { zos.setComment(new String(new byte[0xFFFF + 1])); fail("Comment over 0xFFFF in length should throw exception"); @@ -301,6 +298,17 @@ public class ZipOutputStreamTest extends junit.framework.TestCase { } catch (IndexOutOfBoundsException e) { // expected } + + // Regression for HARMONY-4405 + try { + zip.write(null, 0, -2); + fail("Should throw IndexOutOfBoundsException"); + } catch (IndexOutOfBoundsException e) { + // expected + } + + // Close stream because ZIP is invalid + stream.close(); } /** @@ -337,6 +345,8 @@ public class ZipOutputStreamTest extends junit.framework.TestCase { } catch (IOException e2) { // expected } + + zip1.close(); } @Override |