tests/java/src/stx/libjava/tests/lib/java/util/jar/JarFileTests.java
changeset 4011 1e5cf64a3779
parent 4010 19843598d34b
child 4012 117835eb9839
equal deleted inserted replaced
4010:19843598d34b 4011:1e5cf64a3779
     1 package stx.libjava.tests.lib.java.util.jar;
       
     2 
       
     3 import static org.junit.Assert.*;
       
     4 
       
     5 import java.io.IOException;
       
     6 import java.io.InputStream;
       
     7 import java.security.CryptoPrimitive;
       
     8 import java.security.cert.Certificate;
       
     9 import java.util.Collections;
       
    10 import java.util.EnumSet;
       
    11 import java.util.Enumeration;
       
    12 import java.util.Set;
       
    13 import java.util.jar.JarEntry;
       
    14 import java.util.jar.JarFile;
       
    15 
       
    16 import org.junit.Test;
       
    17 
       
    18 import sun.security.util.DisabledAlgorithmConstraints;
       
    19 
       
    20 /**
       
    21  * Regression tests for JarFile 
       
    22  * 
       
    23  * @author Jan Vrany <jan.vrany [at] fit.cvut.cz>
       
    24  *
       
    25  */
       
    26 @stx.libjava.annotation.Package("stx:libjava/tests")
       
    27 public class JarFileTests {
       
    28     
       
    29     /**
       
    30      * This test tests signed JAR files. As of 2013-09-06, it works
       
    31      * in interpreted mode but fails when JIT is on. 
       
    32      */     
       
    33     @Test    
       
    34     public void signed_jar_01() {
       
    35     	/* 
       
    36     	 * Test file is signed using MD5withRSA algorithm that has been disabled
       
    37     	 * in some versions of OpenJDK (and possibly elsewhere too - namely 
       
    38     	 * OpenJDK 7 update 111, Debian 2.7.2 has it disabled. In that case this 
       
    39     	 * test fail. 
       
    40     	 * 
       
    41     	 * So first check if MD5withRSA enabled and if not, skip the test. See
       
    42     	 * sun.security.pkcs.SignerInfo, line ~ 389. However, we need to be careful
       
    43     	 * here - some old JDKs have no CryptoPrimitive class and so on (older JDK 6)
       
    44     	 * So, catch ClassNotFoundException and eventually proceed, assuming old JDKs
       
    45     	 * have "MD5withRSA" enabled. Sigh, so hacky.
       
    46     	 */
       
    47     	try {
       
    48     		Set<CryptoPrimitive> sigset = Collections.unmodifiableSet(EnumSet.of(CryptoPrimitive.SIGNATURE));
       
    49     		DisabledAlgorithmConstraints constraints = new DisabledAlgorithmConstraints("jdk.jar.disabledAlgorithms");
       
    50     		org.junit.Assume.assumeTrue(constraints.permits(sigset, "MD5withRSA", null));    	
       
    51     	} catch (Exception e) {
       
    52     		if (!(e instanceof ClassNotFoundException)) {
       
    53     			/* 
       
    54     			 * Some class has not been found when executing the code above.
       
    55     			 * This could mean we're running on an oldish JDK, in that case
       
    56     			 * proceed. Otherwise, re-throw. 
       
    57     			 */
       
    58     			throw e;
       
    59     		}
       
    60     	}
       
    61 
       
    62         String jarfile = System.getProperty("libjava.tests.dir", "..") + "/mauve/java/src/gnu/testlet/java/util/jar/JarFile/jfaceSmall.jar";        
       
    63         try {
       
    64         	JarFile jar = new JarFile(jarfile);
       
    65         	try {
       
    66 	            for (Enumeration<JarEntry> entries = jar.entries(); entries.hasMoreElements();) {
       
    67 	                JarEntry entry = entries.nextElement();
       
    68 	                if (entry.isDirectory()) {
       
    69 	                    continue;
       
    70 	                }
       
    71 	                InputStream stream = null;
       
    72 	                try {
       
    73 	                    stream = jar.getInputStream(entry);
       
    74 	                    byte[] ba = new byte[8192];
       
    75 	                    int n;
       
    76 	                    while ((n = stream.read(ba)) >= 0)
       
    77 	                  /* keep reading */;
       
    78 	                } finally {
       
    79 	                    if (stream != null) {
       
    80 	                        try {
       
    81 	                            stream.close();
       
    82 	                        } catch (IOException ignored) {
       
    83 	                        }
       
    84 	                    }
       
    85 	                }
       
    86 	                Certificate[] certs = entry.getCertificates();
       
    87 	                if (certs == null || certs.length == 0) // No certificate
       
    88 	                {
       
    89 	                    if (!entry.getName().startsWith("META-INF")) {
       
    90 	                        assertTrue("Entry " + entry.getName() + " in jar file "
       
    91 	                              + jarfile + " does not have a certificate", false);                        
       
    92 	                    }
       
    93 	                }
       
    94 	            }
       
    95         	} finally {
       
    96         		jar.close();
       
    97         	}                       
       
    98         } catch (IOException e) {            
       
    99             e.printStackTrace();
       
   100             assertTrue(e.getMessage(), false);
       
   101         }
       
   102 
       
   103     }
       
   104 
       
   105 }