Search in sources :

Example 51 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_JarFile_Modified_Class.

/*
     * 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
    }
}
Also used : InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 52 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method testJarVerificationModifiedEntry.

/**
     * 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
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 53 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_ConstructorLjava_lang_String.

/**
     * java.util.jar.JarFile#JarFile(java.lang.String)
     */
public void test_ConstructorLjava_lang_String() {
    try {
        JarFile jarFile = new JarFile("Wrong.file");
        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);
    } catch (IOException e) {
        fail("Should not throw IOException");
    }
}
Also used : IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 54 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_JarFile_Modified_Manifest_MainAttributes.

/*
     * 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.
     */
public void test_JarFile_Modified_Manifest_MainAttributes() throws IOException {
    String modifiedJarName = "Modified_Manifest_MainAttributes.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("SecurityException expected");
        } catch (SecurityException e) {
        // desired
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 55 with JarFile

use of java.util.jar.JarFile in project robovm by robovm.

the class JarFileTest method test_close.

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
}
Also used : JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) Support_PlatformFile(tests.support.Support_PlatformFile) JarFile(java.util.jar.JarFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

JarFile (java.util.jar.JarFile)509 File (java.io.File)289 JarEntry (java.util.jar.JarEntry)230 IOException (java.io.IOException)212 URL (java.net.URL)92 ZipEntry (java.util.zip.ZipEntry)92 InputStream (java.io.InputStream)90 Manifest (java.util.jar.Manifest)81 ZipFile (java.util.zip.ZipFile)74 FileOutputStream (java.io.FileOutputStream)71 Test (org.junit.Test)66 ArrayList (java.util.ArrayList)54 JarURLConnection (java.net.JarURLConnection)48 Attributes (java.util.jar.Attributes)43 JarOutputStream (java.util.jar.JarOutputStream)41 MalformedURLException (java.net.MalformedURLException)31 FileInputStream (java.io.FileInputStream)30 Support_PlatformFile (tests.support.Support_PlatformFile)26 Enumeration (java.util.Enumeration)24 HashSet (java.util.HashSet)20