2121import static org .junit .Assert .assertEquals ;
2222import static org .junit .Assert .assertFalse ;
2323import static org .junit .Assert .assertTrue ;
24+ import static org .junit .Assert .fail ;
2425import static org .mockito .Matchers .any ;
2526import static org .mockito .Mockito .spy ;
2627import static org .mockito .Mockito .times ;
3233import java .io .FileOutputStream ;
3334import java .io .IOException ;
3435import java .io .InputStream ;
36+ import java .nio .charset .StandardCharsets ;
3537import java .util .Random ;
3638import java .util .jar .JarEntry ;
3739import java .util .jar .JarOutputStream ;
@@ -255,4 +257,44 @@ public void testClientClassLoaderSkipUnjar() throws Throwable {
255257 // it should not throw an exception
256258 verify (runJar , times (0 )).unJar (any (File .class ), any (File .class ));
257259 }
260+
261+ @ Test
262+ public void testUnJar2 () throws IOException {
263+ // make a simple zip
264+ File jarFile = new File (TEST_ROOT_DIR , TEST_JAR_NAME );
265+ JarOutputStream jstream =
266+ new JarOutputStream (new FileOutputStream (jarFile ));
267+ JarEntry je = new JarEntry ("META-INF/MANIFEST.MF" );
268+ byte [] data = "Manifest-Version: 1.0\n Created-By: 1.8.0_1 (Manual)"
269+ .getBytes (StandardCharsets .UTF_8 );
270+ je .setSize (data .length );
271+ jstream .putNextEntry (je );
272+ jstream .write (data );
273+ jstream .closeEntry ();
274+ je = new JarEntry ("../outside.path" );
275+ data = "any data here" .getBytes (StandardCharsets .UTF_8 );
276+ je .setSize (data .length );
277+ jstream .putNextEntry (je );
278+ jstream .write (data );
279+ jstream .closeEntry ();
280+ jstream .close ();
281+
282+ File unjarDir = getUnjarDir ("unjar-path" );
283+
284+ // Unjar everything
285+ try {
286+ RunJar .unJar (jarFile , unjarDir , MATCH_ANY );
287+ fail ("unJar should throw IOException." );
288+ } catch (IOException e ) {
289+ GenericTestUtils .assertExceptionContains (
290+ "would create file outside of" , e );
291+ }
292+ try {
293+ RunJar .unJar (new FileInputStream (jarFile ), unjarDir , MATCH_ANY );
294+ fail ("unJar should throw IOException." );
295+ } catch (IOException e ) {
296+ GenericTestUtils .assertExceptionContains (
297+ "would create file outside of" , e );
298+ }
299+ }
258300}
0 commit comments