View Javadoc
1   package org.codehaus.plexus.archiver.zip;
2   
3   import java.io.File;
4   import java.lang.reflect.Method;
5   import org.codehaus.plexus.PlexusTestCase;
6   import org.codehaus.plexus.archiver.Archiver;
7   import org.codehaus.plexus.archiver.UnArchiver;
8   import org.codehaus.plexus.components.io.fileselectors.FileSelector;
9   import org.codehaus.plexus.components.io.fileselectors.IncludeExcludeFileSelector;
10  import org.codehaus.plexus.util.FileUtils;
11  
12  /**
13   * @author Jason van Zyl
14   */
15  public class ZipUnArchiverTest
16      extends PlexusTestCase
17  {
18  
19      public void testExtractingZipPreservesExecutableFlag()
20          throws Exception
21      {
22  
23          String s = "target/zip-unarchiver-tests";
24          File testZip = new File( getBasedir(), "src/test/jars/test.zip" );
25          File outputDirectory = new File( getBasedir(), s );
26  
27          FileUtils.deleteDirectory( outputDirectory );
28  
29          ZipUnArchiver zu = getZipUnArchiver( testZip );
30          zu.extract( "", outputDirectory );
31          File testScript = new File( outputDirectory, "test.sh" );
32  
33          final Method canExecute;
34          try
35          {
36              canExecute = File.class.getMethod( "canExecute" );
37              canExecute.invoke( testScript );
38              assertTrue( (Boolean) canExecute.invoke( testScript ) );
39          }
40          catch ( NoSuchMethodException ignore )
41          {
42          }
43      }
44  
45      public void testZeroFileModeInZip()
46          throws Exception
47      {
48  
49          String s = "target/zip-unarchiver-filemode-tests";
50          File testZip = new File( getBasedir(), "src/test/resources/zeroFileMode/foobar.zip" );
51          File outputDirectory = new File( getBasedir(), s );
52  
53          FileUtils.deleteDirectory( outputDirectory );
54  
55          ZipUnArchiver zu = getZipUnArchiver( testZip );
56          zu.setIgnorePermissions( false );
57          zu.extract( "", outputDirectory );
58  
59          File testScript = new File( outputDirectory, "foo.txt" );
60  
61          final Method canRead;
62          try
63          {
64              canRead = File.class.getMethod( "canRead" );
65              canRead.invoke( testScript );
66              assertTrue( (Boolean) canRead.invoke( testScript ) );
67          }
68          catch ( NoSuchMethodException ignore )
69          {
70          }
71      }
72  
73      public void testUnarchiveUtf8()
74          throws Exception
75      {
76          File dest = new File( "target/output/unzip/utf8" );
77          dest.mkdirs();
78  
79          final File zipFile = new File( "target/output/unzip/utf8-default.zip" );
80          final ZipArchiver zipArchiver = getZipArchiver( zipFile );
81          zipArchiver.addDirectory( new File( "src/test/resources/miscUtf8" ) );
82          zipArchiver.createArchive();
83          final ZipUnArchiver unarchiver = getZipUnArchiver( zipFile );
84          unarchiver.setDestFile( dest );
85          unarchiver.extract();
86          assertTrue( new File( dest, "aPi\u00F1ata.txt" ).exists() );
87          assertTrue( new File( dest, "an\u00FCmlaut.txt" ).exists() );
88          assertTrue( new File( dest, "\u20acuro.txt" ).exists() );
89      }
90  
91      private void runUnarchiver( String path, FileSelector[] selectors, boolean[] results )
92          throws Exception
93      {
94          String s = "target/zip-unarchiver-tests";
95  
96          File testJar = new File( getBasedir(), "src/test/jars/test.jar" );
97  
98          File outputDirectory = new File( getBasedir(), s );
99  
100         ZipUnArchiver zu = getZipUnArchiver( testJar );
101         zu.setFileSelectors( selectors );
102 
103         FileUtils.deleteDirectory( outputDirectory );
104 
105         zu.extract( path, outputDirectory );
106 
107         File f0 = new File( getBasedir(), s + "/resources/artifactId/test.properties" );
108 
109         assertEquals( results[0], f0.exists() );
110 
111         File f1 = new File( getBasedir(), s + "/resources/artifactId/directory/test.properties" );
112 
113         assertEquals( results[1], f1.exists() );
114 
115         File f2 = new File( getBasedir(), s + "/META-INF/MANIFEST.MF" );
116 
117         assertEquals( results[2], f2.exists() );
118     }
119 
120     private ZipUnArchiver getZipUnArchiver( File testJar ) throws Exception
121     {
122         ZipUnArchiver zu = (ZipUnArchiver) lookup( UnArchiver.ROLE, "zip" );
123         zu.setSourceFile( testJar );
124         return zu;
125     }
126 
127     public void testExtractingADirectoryFromAJarFile()
128         throws Exception
129     {
130         runUnarchiver( "resources/artifactId", null,
131                        new boolean[]
132                        {
133                            true, true, false
134                        } );
135         runUnarchiver( "", null,
136                        new boolean[]
137                        {
138                            true, true, true
139                        } );
140     }
141 
142     public void testSelectors()
143         throws Exception
144     {
145         IncludeExcludeFileSelector fileSelector = new IncludeExcludeFileSelector();
146         runUnarchiver( "", new FileSelector[]
147                    {
148                        fileSelector
149         },
150                        new boolean[]
151                        {
152                            true, true, true
153                        } );
154         fileSelector.setExcludes( new String[]
155         {
156             "**/test.properties"
157         } );
158         runUnarchiver( "", new FileSelector[]
159                    {
160                        fileSelector
161         },
162                        new boolean[]
163                        {
164                            false, false, true
165                        } );
166         fileSelector.setIncludes( new String[]
167         {
168             "**/test.properties"
169         } );
170         fileSelector.setExcludes( null );
171         runUnarchiver( "", new FileSelector[]
172                    {
173                        fileSelector
174         },
175                        new boolean[]
176                        {
177                            true, true, false
178                        } );
179         fileSelector.setExcludes( new String[]
180         {
181             "resources/artifactId/directory/test.properties"
182         } );
183         runUnarchiver( "", new FileSelector[]
184                    {
185                        fileSelector
186         },
187                        new boolean[]
188                        {
189                            true, false, false
190                        } );
191     }
192 
193     public void testExtractingZipWithEntryOutsideDestDirThrowsException()
194             throws Exception
195     {
196         Exception ex = null;
197         String s = "target/zip-unarchiver-slip-tests";
198         File testZip = new File( getBasedir(), "src/test/zips/zip-slip.zip" );
199         File outputDirectory = new File( getBasedir(), s );
200 
201         FileUtils.deleteDirectory( outputDirectory );
202 
203         try
204         {
205             ZipUnArchiver zu = getZipUnArchiver( testZip );
206             zu.extract( "", outputDirectory );
207         }
208         catch ( Exception e )
209         {
210             ex = e;
211         }
212 
213         assertNotNull( ex );
214         assertTrue( ex.getMessage().startsWith( "Entry is outside of the target directory" ) );
215     }
216 
217     private ZipArchiver getZipArchiver()
218     {
219         try
220         {
221             return (ZipArchiver) lookup( Archiver.ROLE, "zip" );
222         }
223         catch ( Exception e )
224         {
225             throw new RuntimeException( e );
226         }
227     }
228 
229     private ZipArchiver getZipArchiver( File destFile )
230     {
231         final ZipArchiver zipArchiver = getZipArchiver();
232         zipArchiver.setDestFile( destFile );
233         return zipArchiver;
234     }
235 
236 }