diff options
Diffstat (limited to 'archive/src')
5 files changed, 365 insertions, 38 deletions
diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/DalvikExecTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/DalvikExecTest.java new file mode 100644 index 0000000..77fbb15 --- /dev/null +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/DalvikExecTest.java @@ -0,0 +1,315 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * 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.TestLevel; +import dalvik.annotation.TestTargetClass; +import dalvik.annotation.TestTargetNew; +import dalvik.annotation.TestTargets; + +import junit.framework.TestCase; + +import tests.support.Support_Exec; +import tests.support.resource.Support_Resources; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.jar.Attributes; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; +import java.util.jar.JarFile; + + +@TestTargetClass(JarOutputStream.class) +@AndroidOnly("dalvik vm specific") +public class DalvikExecTest extends TestCase { + + String execDalvik1 (String classpath, String mainClass, String arg1) + throws IOException, InterruptedException { + + ArrayList<String> cmdLine = new ArrayList<String>(10); + + String base = System.getenv("OUT"); + cmdLine.add(base + "/system/bin/dalvikvm"); + + cmdLine.add("-Djava.io.tmpdir=/tmp/mc"); + cmdLine.add("-Duser.language=en"); + cmdLine.add("-Duser.region=US"); + + if ("true".equals(System.getenv("TARGET_SIMULATOR"))) { + // Test against SIMULATOR: +// cmdLine.add("-Xmx512M"); +// cmdLine.add("-Xcheck:jni"); + cmdLine.add("-Xbootclasspath:" + System.getProperty("java.boot.class.path")); + } else { + // Test against EMULATOR: + } + + cmdLine.add("-classpath"); + cmdLine.add(classpath); + cmdLine.add(mainClass); + + if (arg1 != null) cmdLine.add(arg1); + + Object[] res = Support_Exec.execAndDigestOutput( + cmdLine.toArray(new String[cmdLine.size()]), + null ); + return Support_Exec.getProcessOutput(res, true); + } + + String execDalvik (String classpath, String mainClass) + throws IOException, InterruptedException { + return execDalvik1(classpath, mainClass, null); + } + + @TestTargets ({ + @TestTargetNew( + level = TestLevel.ADDITIONAL, + notes = "Execute an existing JAR on dalvikvm using -classpath option.", + clazz = Runtime.class, + method = "exec", + args = {java.lang.String[].class} + ) + }) + public void test_execExistingJar () throws IOException, InterruptedException { + String res; + File jarFile; + if (System.getProperty("java.vendor").contains("Android")) { + // + // Test against Android: + // + File tempDir = Support_Resources.createTempFolder(); + jarFile = Support_Resources.copyFile( + tempDir, null, "cts_dalvikExecTest.jar" ); + res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld"); + assertEquals("Hello Android World!", "Hello Android World!\n", res); + + res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper"); + assertTrue("Android Resource Dumper started", + res.contains("Android Resource Dumper started")); + assertTrue("This Resource contains some text.", + res.contains("This Resource contains some text.")); + } else { + // + // Test against RI: + // + // Do nothing! + } + } + + + @TestTargets ({ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + method = "putNextEntry", + args = {java.util.zip.ZipEntry.class} + ), + @TestTargetNew( + level = TestLevel.ADDITIONAL, + method = "JarOutputStream", + args = {java.io.OutputStream.class} + ), + @TestTargetNew( + level = TestLevel.ADDITIONAL, + notes = "Create a temp file, fill it with contents according to Dalvik JAR format, and execute it on dalvikvm using -classpath option.", + clazz = Runtime.class, + method = "exec", + args = {java.lang.String[].class} + ) + }) + public void test_execCreatedJar () throws IOException, InterruptedException { + File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar"); + jarFile.deleteOnExit(); + + // Create a JAR output stream on the temp file: + JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile)); + + // Define the entry for the classes.dex: + jarOut.putNextEntry(new JarEntry("classes.dex")); + + // Fill in the classes.dex contents, i.e. the Dalvik executable code: + // (See below for the detailed source code contents.) + Support_Resources.writeResourceToStream("cts_dalvikExecTest_classes.dex", jarOut); + + // Now add a resource file: + // + jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource")); + jarOut.write("This Resource contains some text.".getBytes()); + + // Close the stream to the completed JAR file. + jarOut.close(); + + // The resulting JAR file contains the classes listed at the end of this text, + // like the 'cts_dalvikExecTest.jar' as part of the resources, too. + + String res; + + res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld"); + assertEquals("Hello Android World!", "Hello Android World!\n", res); + + res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper"); + assertTrue("Android Resource Dumper started", + res.contains("Android Resource Dumper started")); + assertTrue("This Resource contains some text.", + res.contains("This Resource contains some text.")); + } + + + @TestTargets ({ + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + method = "putNextEntry", + args = {java.util.zip.ZipEntry.class} + ), + @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + method = "JarOutputStream", + args = {java.io.OutputStream.class, java.util.jar.Manifest.class} + ), + @TestTargetNew( + level = TestLevel.ADDITIONAL, + clazz = Runtime.class, + method = "exec", + args = {java.lang.String[].class} + ) + }) + /** + * This test does quite the same as test_execCreatedJar, but includes a manifest. + * Note however that the Dalvik JAR format does not require this manifest. + * We just test whether the manifest is placed correctly within the JAR by + * dumping its contents read as a simple text resource. + * No! We can't do that so easily either, as there are other (parent) JARs + * with a manifest inside, taken with precedence. + * So we will reopen the JAR as a JarFile and check the manifest + * with a top level end-to-end approach. + */ + public void test_execCreatedJarWithManifest () throws IOException, InterruptedException { + File jarFile = File.createTempFile("cts_dalvikExecTest_", ".jar"); + jarFile.deleteOnExit(); + + // Create the manifest: + Manifest manifest = new Manifest(); + Attributes attrs = manifest.getMainAttributes(); + attrs.put(Attributes.Name.MANIFEST_VERSION, "3.1415962"); + attrs.put(Attributes.Name.MAIN_CLASS, "dalvikExecTest.HelloWorld"); + attrs.put(Attributes.Name.CLASS_PATH, jarFile.getName()); + + // Create a JAR output stream on the temp file using the manifest: + JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(jarFile), manifest); + + // Define the entry for the classes.dex: + jarOut.putNextEntry(new JarEntry("classes.dex")); + + // Fill in the classes.dex contents, i.e. the Dalvik executable code: + // (See below for the detailed source code contents.) + Support_Resources.writeResourceToStream("cts_dalvikExecTest_classes.dex", jarOut); + + // Now add a resource file: + // + jarOut.putNextEntry(new JarEntry("dalvikExecTest/myResource")); + jarOut.write("This Resource contains some text.".getBytes()); + + // Close the stream to the completed JAR file. + jarOut.close(); + + // The resulting JAR file contains the classes listed at the end of this text, + // like the 'cts_dalvikExecTest.jar' as part of the resources, too. + + String res; + + res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.HelloWorld"); + assertEquals("Hello Android World!", "Hello Android World!\n", res); + + res = execDalvik(jarFile.getAbsolutePath(), "dalvikExecTest.ResourceDumper"); + assertTrue("Android Resource Dumper started", + res.contains("Android Resource Dumper started")); + assertTrue("This Resource contains some text.", + res.contains("This Resource contains some text.")); + + // And now reread the manifest: + // + JarFile jarIn = new JarFile(jarFile); + manifest = jarIn.getManifest(); + attrs = manifest.getMainAttributes(); + assertEquals("MANIFEST_VERSION must match!", "3.1415962", + attrs.get(Attributes.Name.MANIFEST_VERSION)); + assertEquals("MAIN_CLASS must match!", "dalvikExecTest.HelloWorld", + attrs.get(Attributes.Name.MAIN_CLASS)); + assertEquals("CLASS_PATH must match!", jarFile.getName(), + attrs.get(Attributes.Name.CLASS_PATH)); + } + + + /* + * The following two classes are added, here, only for completeness. + * They form the contents of the dalvikExecTest package contained + * in the 'cts_dalvikExecTest_classes.dex' resource file. + */ + /** + * @hide + */ + public static class HelloWorld { + + public static void main(String[] args) { + System.out.println("Hello Android World!"); + } + + } + + public static class ResourceDumper { + + static ByteArrayOutputStream outputFrom (InputStream input) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[512]; + int total = 0; + int count; + count = input.read(buffer); + while (count != -1) { + out.write(buffer, 0, count); + total = total + count; + count = input.read(buffer); + } + return out; + } + + public static void main(String[] args) throws IOException { + System.out.print("Android Resource Dumper started "); + String fileName; + if (args.length >= 1) { + fileName = args[0]; + System.out.format("for argument '%s'.\n", fileName); + } else { + System.out.print("standard "); + fileName = "myResource"; + System.out.println("for standard 'myResource'."); + } + InputStream is = ResourceDumper.class.getResourceAsStream(fileName); + if (is != null) { + System.out.println("Resource obtained and being dumped:"); + System.out.println(outputFrom(is).toString()); + } + } + + } + +} diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java index 4204ed6..4c209d1 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/JarExecTest.java @@ -17,8 +17,7 @@ package org.apache.harmony.archive.tests.java.util.jar; -import dalvik.annotation.AndroidOnly; -import dalvik.annotation.BrokenTest; +import dalvik.annotation.KnownFailure; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; @@ -47,13 +46,12 @@ public class JarExecTest extends junit.framework.TestCase { * */ @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Regression functional test. Exception checking missed.", method = "putNextEntry", args = {java.util.zip.ZipEntry.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_1562() throws Exception { // create the manifest Manifest man = new Manifest(); @@ -92,13 +90,12 @@ public class JarExecTest extends junit.framework.TestCase { * @throws Exception in case of troubles */ @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "JarOutputStream", args = {java.io.OutputStream.class, java.util.jar.Manifest.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_jar_class_path() throws Exception { File fooJar = File.createTempFile("hyts_", ".jar"); File barJar = File.createTempFile("hyts_", ".jar"); @@ -169,13 +166,12 @@ public class JarExecTest extends junit.framework.TestCase { * @throws Exception in case of troubles */ @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "JarOutputStream", args = {java.io.OutputStream.class, java.util.jar.Manifest.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_main_class_in_another_jar() throws Exception { File fooJar = File.createTempFile("hyts_", ".jar"); File barJar = File.createTempFile("hyts_", ".jar"); @@ -213,13 +209,12 @@ public class JarExecTest extends junit.framework.TestCase { } @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "JarOutputStream", args = {java.io.OutputStream.class, java.util.jar.Manifest.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_classpath() throws Exception { File resources = Support_Resources.createTempFolder(); 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 e5edfe0..497b897 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 @@ -18,6 +18,7 @@ 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; @@ -72,6 +73,8 @@ 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 entryName = "foo/bar/A.class"; @@ -623,6 +626,10 @@ public class JarFileTest extends TestCase { method = "getInputStream", args = {java.util.zip.ZipEntry.class} ) + @AndroidOnly("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 incorreclty.") public void test_getInputStreamLjava_util_jar_JarEntry_subtest0() { File signedFile = null; try { @@ -652,8 +659,7 @@ public class JarFileTest extends TestCase { } catch (Exception e) { fail("Exception during test 4: " + e); } - - boolean exception = false; + try { JarFile jar = new JarFile(signedFile); JarEntry entry = new JarEntry(entryName3); @@ -662,12 +668,30 @@ public class JarFileTest extends TestCase { // BEGIN android-added byte[] dummy = getAllBytesFromStream(in); // END android-added + fail("SecurityException should be thrown."); } catch (SecurityException e) { - exception = true; + // 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); } - assertTrue("Failed to throw SecurityException", exception); } /* 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 a416847..e652137 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 @@ -44,8 +44,7 @@ public class JarOutputStreamTest extends junit.framework.TestCase { * @tests java.util.jar.JarOutputStream#putNextEntry(java.util.zip.ZipEntry) */ @TestTargetNew( - level = TestLevel.COMPLETE, - notes = "", + level = TestLevel.PARTIAL_COMPLETE, method = "putNextEntry", args = {java.util.zip.ZipEntry.class} ) @@ -112,7 +111,7 @@ public class JarOutputStreamTest extends junit.framework.TestCase { args[0] = "-jar"; args[1] = outputJar.getAbsolutePath(); -// It's not that simple to execute a JAR agains Dalvik VM (see JarExecTest): +// It's not that simple to execute a JAR against Dalvik VM (see DalvikExecTest): // // try { // // execute the JAR and read the result diff --git a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java index 109ac02..c6f07de 100644 --- a/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java +++ b/archive/src/test/java/org/apache/harmony/archive/tests/java/util/jar/ZipExecTest.java @@ -17,8 +17,7 @@ package org.apache.harmony.archive.tests.java.util.jar; -import dalvik.annotation.AndroidOnly; -import dalvik.annotation.BrokenTest; +import dalvik.annotation.KnownFailure; import dalvik.annotation.TestLevel; import dalvik.annotation.TestTargetClass; import dalvik.annotation.TestTargetNew; @@ -45,13 +44,12 @@ import java.util.zip.ZipOutputStream; @TestTargetClass(ZipOutputStream.class) public class ZipExecTest extends junit.framework.TestCase { @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Regression functional test. Exception checking missed.", method = "putNextEntry", args = {java.util.zip.ZipEntry.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_1562() throws Exception { Manifest man = new Manifest(); Attributes att = man.getMainAttributes(); @@ -91,13 +89,12 @@ public class ZipExecTest extends junit.framework.TestCase { * @throws Exception in case of troubles */ @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "ZipOutputStream", args = {java.io.OutputStream.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_zip_class_path() throws Exception { File fooZip = File.createTempFile("hyts_", ".zip"); File barZip = File.createTempFile("hyts_", ".zip"); @@ -169,13 +166,12 @@ public class ZipExecTest extends junit.framework.TestCase { @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "ZipOutputStream", args = {java.io.OutputStream.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_zip_jar_mix() throws Exception { File fooJar = File.createTempFile("hyts_", ".jar"); File barZip = File.createTempFile("hyts_", ".zip"); @@ -213,13 +209,12 @@ public class ZipExecTest extends junit.framework.TestCase { } @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "ZipOutputStream", args = {java.io.OutputStream.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_zip_jar_mix_1() throws Exception { File fooZip = File.createTempFile("hyts_", ".zip"); File barJar = File.createTempFile("hyts_", ".jar"); @@ -265,13 +260,12 @@ public class ZipExecTest extends junit.framework.TestCase { * @throws Exception in case of troubles */ @TestTargetNew( - level = TestLevel.PARTIAL_COMPLETE, + level = TestLevel.ADDITIONAL, notes = "Functional test.", method = "ZipOutputStream", args = {java.io.OutputStream.class} ) - @BrokenTest("Support_Exec.execJava is not so simple to use: Harmony Test cannot be easily adapted.") - @AndroidOnly("dalvik vm specific") + @KnownFailure("Maybe not a failure, but dalvikvm -jar is not supported (, as yet).") public void test_main_class_in_another_zip() throws Exception { File fooZip = File.createTempFile("hyts_", ".zip"); File barZip = File.createTempFile("hyts_", ".zip"); |