7
7
import java .io .ByteArrayOutputStream ;
8
8
import java .io .File ;
9
9
import java .io .FileOutputStream ;
10
- import java .io .IOException ;
11
10
import java .nio .file .attribute .PosixFilePermission ;
12
11
import java .text .MessageFormat ;
13
12
import java .util .Set ;
14
13
import java .util .zip .ZipEntry ;
15
14
import java .util .zip .ZipOutputStream ;
16
15
17
- import org .junit .Assert ;
18
- import org .junit .Before ;
19
- import org .junit .Test ;
16
+ import org .junit .jupiter .api .BeforeAll ;
17
+ import org .junit .jupiter .api .Test ;
18
+
19
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
20
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
21
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
20
22
21
23
public class FileUtilsTest {
22
24
private static final String FILE_ERR_FORMAT = "Unexpected {0}: got {1}, expected {2}" ;
@@ -60,15 +62,15 @@ public class FileUtilsTest {
60
62
private static final File UNIT_TEST_TARGET_DIR = new File (WLSDeployZipFileTest .UNIT_TEST_TARGET_DIR , "fileutils" );
61
63
private static final String WALLET_PATH = "wlsdeploy/wallet.zip" ;
62
64
63
- @ Before
64
- public void initialize () throws Exception {
65
+ @ BeforeAll
66
+ static void initialize () throws Exception {
65
67
if (!UNIT_TEST_TARGET_DIR .exists () && !UNIT_TEST_TARGET_DIR .mkdirs ()) {
66
68
throw new Exception ("Unable to create unit test directory: " + UNIT_TEST_TARGET_DIR );
67
69
}
68
70
}
69
71
70
72
@ Test
71
- public void testNormalFile_parseFileName () throws Exception {
73
+ void testNormalFile_parseFileName () {
72
74
File f = new File (FILE1 );
73
75
String [] nameComponents = FileUtils .parseFileName (f );
74
76
assertMatch ("filename" , nameComponents [0 ], FILE1_EXPECTED_NAME );
@@ -111,22 +113,22 @@ public void testNormalFile_parseFileName() throws Exception {
111
113
}
112
114
113
115
@ Test
114
- public void testHashing () throws Exception {
116
+ void testHashing () throws Exception {
115
117
File archiveFile = FileUtils .getCanonicalFile (new File (ARCHIVE_FILE_NAME ));
116
118
WLSDeployArchive archive = new WLSDeployArchive (archiveFile .getAbsolutePath ());
117
119
String archiveHash = archive .getFileHash (APP_PATH );
118
120
119
121
File appFile = FileUtils .getCanonicalFile (new File (APP_FILE_NAME ));
120
122
String appHash = FileUtils .computeHash (appFile .getAbsolutePath ());
121
123
122
- Assert . assertEquals (appHash , archiveHash );
124
+ assertEquals (appHash , archiveHash );
123
125
}
124
126
125
127
@ Test
126
128
/* A wallet zip inside the archive must not contain an entry such as ../info.txt,
127
129
since this creates a file overwrite security vulnerability (zip slip).
128
130
*/
129
- public void testZipVulnerability () throws Exception {
131
+ void testZipVulnerability () throws Exception {
130
132
String extractPath = UNIT_TEST_TARGET_DIR .getPath ();
131
133
132
134
// an entry with a simple name or path works fine
@@ -135,15 +137,12 @@ public void testZipVulnerability() throws Exception {
135
137
FileUtils .extractZipFileContent (deployArchive , WALLET_PATH , extractPath );
136
138
137
139
// an entry with parent directory notation should throw an exception
138
- try {
139
- zipFile = buildWalletArchiveZip ("../info.txt" );
140
- deployArchive = new WLSDeployArchive (zipFile .getPath ());
141
- FileUtils .extractZipFileContent (deployArchive , WALLET_PATH , extractPath );
142
- Assert .fail ("Exception not thrown for zip entry outside extract directory" );
143
-
144
- } catch (IllegalArgumentException e ) {
145
- // expected behavior
146
- }
140
+ zipFile = buildWalletArchiveZip ("../info.txt" );
141
+ final WLSDeployArchive deployArchive2 = new WLSDeployArchive (zipFile .getPath ());
142
+ assertThrows (IllegalArgumentException .class ,
143
+ () -> FileUtils .extractZipFileContent (deployArchive2 , WALLET_PATH , extractPath ),
144
+ "Exception not thrown for zip entry outside extract directory" );
145
+
147
146
}
148
147
149
148
/* Build an archive zip containing a wallet zip.
@@ -174,19 +173,18 @@ private File buildWalletArchiveZip(String contentName) throws Exception {
174
173
}
175
174
176
175
private void assertMatch (String name , String got , String expected ) {
177
- Assert .assertTrue (MessageFormat .format (FILE_ERR_FORMAT , name , got , expected ),
178
- got .equals (expected ));
176
+ assertEquals (expected , got , MessageFormat .format (FILE_ERR_FORMAT , name , got , expected ));
179
177
}
180
178
181
179
@ Test
182
- public void posixPermissions () throws IOException {
180
+ void posixPermissions () {
183
181
Set <PosixFilePermission > perms = FileUtils .getPermissions (0700 );
184
- Assert . assertTrue (perms .contains (PosixFilePermission .OWNER_READ ));
185
- Assert . assertTrue (perms .contains (PosixFilePermission .OWNER_WRITE ));
186
- Assert . assertTrue (perms .contains (PosixFilePermission .OWNER_EXECUTE ));
182
+ assertTrue (perms .contains (PosixFilePermission .OWNER_READ ));
183
+ assertTrue (perms .contains (PosixFilePermission .OWNER_WRITE ));
184
+ assertTrue (perms .contains (PosixFilePermission .OWNER_EXECUTE ));
187
185
188
186
Set <PosixFilePermission > perms2 = FileUtils .getPermissions (0006 );
189
- Assert . assertTrue (perms2 .contains (PosixFilePermission .OTHERS_READ ));
190
- Assert . assertTrue (perms2 .contains (PosixFilePermission .OTHERS_WRITE ));
187
+ assertTrue (perms2 .contains (PosixFilePermission .OTHERS_READ ));
188
+ assertTrue (perms2 .contains (PosixFilePermission .OTHERS_WRITE ));
191
189
}
192
190
}
0 commit comments