@@ -1165,21 +1165,52 @@ void testParseHeaderLevel2FileWithMsdosAttributes() throws IOException {
11651165 }
11661166 }
11671167
1168+ @ Test
1169+ void testParseExtendedHeaderTooShort () throws IOException {
1170+ try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1171+ final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
1172+ try {
1173+ archive .parseExtendedHeader (toByteBuffer (0x00 , 0x00 ), entryBuilder );
1174+ fail ("Expected ArchiveException for invalid extended header length" );
1175+ } catch (ArchiveException e ) {
1176+ assertEquals ("Invalid extended header length" , e .getMessage ());
1177+ }
1178+ }
1179+ }
1180+
11681181 @ Test
11691182 void testParseExtendedHeaderCommon () throws IOException {
11701183 try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1184+ // Valid
11711185 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
11721186 archive .parseExtendedHeader (toByteBuffer (0x00 , 0x22 , 0x33 , 0x00 , 0x00 ), entryBuilder );
11731187 assertEquals (0x3322 , entryBuilder .get ().getHeaderCrc ());
1188+
1189+ // Invalid length
1190+ try {
1191+ archive .parseExtendedHeader (toByteBuffer (0x00 , 0x22 , 0x00 , 0x00 ), entryBuilder );
1192+ fail ("Expected ArchiveException for invalid extended header length" );
1193+ } catch (ArchiveException e ) {
1194+ assertEquals ("Invalid extended header length" , e .getMessage ());
1195+ }
11741196 }
11751197 }
11761198
11771199 @ Test
11781200 void testParseExtendedHeaderFilename () throws IOException {
11791201 try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1202+ // Valid
11801203 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
11811204 archive .parseExtendedHeader (toByteBuffer (0x01 , 't' , 'e' , 's' , 't' , '.' , 't' , 'x' , 't' , 0x00 , 0x00 ), entryBuilder );
11821205 assertEquals ("test.txt" , entryBuilder .get ().getName ());
1206+
1207+ // Invalid length
1208+ try {
1209+ archive .parseExtendedHeader (toByteBuffer (0x01 , 0x00 ), entryBuilder );
1210+ fail ("Expected ArchiveException for invalid extended header length" );
1211+ } catch (ArchiveException e ) {
1212+ assertEquals ("Invalid extended header length" , e .getMessage ());
1213+ }
11831214 }
11841215 }
11851216
@@ -1190,9 +1221,18 @@ void testParseExtendedHeaderDirectoryName() throws IOException {
11901221 .setFileSeparatorChar ('/' )
11911222 .get ()) {
11921223
1224+ // Valid
11931225 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
11941226 archive .parseExtendedHeader (toByteBuffer (0x02 , 'd' , 'i' , 'r' , '1' , 0xff , 0x00 , 0x00 ), entryBuilder );
11951227 assertEquals ("dir1/" , entryBuilder .get ().getName ());
1228+
1229+ // Invalid length
1230+ try {
1231+ archive .parseExtendedHeader (toByteBuffer (0x02 , 0x00 ), entryBuilder );
1232+ fail ("Expected ArchiveException for invalid extended header length" );
1233+ } catch (ArchiveException e ) {
1234+ assertEquals ("Invalid extended header length" , e .getMessage ());
1235+ }
11961236 }
11971237 }
11981238
@@ -1228,44 +1268,86 @@ void testParseExtendedHeaderFilenameAndDirectoryName() throws IOException {
12281268 archive .parseExtendedHeader (toByteBuffer (0x02 , 'd' , 'i' , 'r' , '1' , 0x00 , 0x00 ), entryBuilder );
12291269 archive .parseExtendedHeader (toByteBuffer (0x01 , 't' , 'e' , 's' , 't' , '.' , 't' , 'x' , 't' , 0x00 , 0x00 ), entryBuilder );
12301270 assertEquals ("dir1/test.txt" , entryBuilder .get ().getName ());
1271+
1272+ // Test empty directory name, no trailing slash
1273+ entryBuilder = LhaArchiveEntry .builder ();
1274+ archive .parseExtendedHeader (toByteBuffer (0x02 , 0x00 , 0x00 ), entryBuilder );
1275+ archive .parseExtendedHeader (toByteBuffer (0x01 , 't' , 'e' , 's' , 't' , '.' , 't' , 'x' , 't' , 0x00 , 0x00 ), entryBuilder );
1276+ assertEquals ("test.txt" , entryBuilder .get ().getName ());
12311277 }
12321278 }
12331279
12341280 @ Test
12351281 void testParseExtendedHeaderUnixPermission () throws IOException {
12361282 try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1283+ // Valid
12371284 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
12381285 archive .parseExtendedHeader (toByteBuffer (0x50 , 0xa4 , 0x81 , 0x00 , 0x00 ), entryBuilder );
12391286 assertEquals (0x81a4 , entryBuilder .get ().getUnixPermissionMode ());
12401287 assertEquals (0100644 , entryBuilder .get ().getUnixPermissionMode ());
1288+
1289+ // Invalid length
1290+ try {
1291+ archive .parseExtendedHeader (toByteBuffer (0x50 , 0xa4 , 0x00 , 0x00 ), entryBuilder );
1292+ fail ("Expected ArchiveException for invalid extended header length" );
1293+ } catch (ArchiveException e ) {
1294+ assertEquals ("Invalid extended header length" , e .getMessage ());
1295+ }
12411296 }
12421297 }
12431298
12441299 @ Test
12451300 void testParseExtendedHeaderUnixUidGid () throws IOException {
12461301 try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1302+ // Valid
12471303 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
12481304 archive .parseExtendedHeader (toByteBuffer (0x51 , 0x14 , 0x00 , 0xf5 , 0x01 , 0x00 , 0x00 ), entryBuilder );
12491305 assertEquals (0x0014 , entryBuilder .get ().getUnixGroupId ());
12501306 assertEquals (0x01f5 , entryBuilder .get ().getUnixUserId ());
1307+
1308+ // Invalid length
1309+ try {
1310+ archive .parseExtendedHeader (toByteBuffer (0x51 , 0x14 , 0x00 , 0xf5 , 0x00 , 0x00 ), entryBuilder );
1311+ fail ("Expected ArchiveException for invalid extended header length" );
1312+ } catch (ArchiveException e ) {
1313+ assertEquals ("Invalid extended header length" , e .getMessage ());
1314+ }
12511315 }
12521316 }
12531317
12541318 @ Test
12551319 void testParseExtendedHeaderUnixTimestamp () throws IOException {
12561320 try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1321+ // Valid
12571322 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
12581323 archive .parseExtendedHeader (toByteBuffer (0x54 , 0x5c , 0x73 , 0x9c , 0x68 , 0x00 , 0x00 ), entryBuilder );
12591324 assertEquals (0x689c735cL , entryBuilder .get ().getLastModifiedDate ().getTime () / 1000 );
1325+
1326+ // Invalid length
1327+ try {
1328+ archive .parseExtendedHeader (toByteBuffer (0x54 , 0x5c , 0x73 , 0x9c , 0x00 , 0x00 ), entryBuilder );
1329+ fail ("Expected ArchiveException for invalid extended header length" );
1330+ } catch (ArchiveException e ) {
1331+ assertEquals ("Invalid extended header length" , e .getMessage ());
1332+ }
12601333 }
12611334 }
12621335
12631336 @ Test
12641337 void testParseExtendedHeaderMSdosFileAttributes () throws IOException {
12651338 try (LhaArchiveInputStream archive = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1339+ // Valid
12661340 final LhaArchiveEntry .Builder entryBuilder = LhaArchiveEntry .builder ();
1267- archive .parseExtendedHeader (toByteBuffer (0x40 , 0x10 , 0x00 , 0x00 ), entryBuilder );
1341+ archive .parseExtendedHeader (toByteBuffer (0x40 , 0x10 , 0x00 , 0x00 , 0x00 ), entryBuilder );
12681342 assertEquals (0x10 , entryBuilder .get ().getMsdosFileAttributes ());
1343+
1344+ // Invalid length
1345+ try {
1346+ archive .parseExtendedHeader (toByteBuffer (0x40 , 0x10 , 0x00 , 0x00 ), entryBuilder );
1347+ fail ("Expected ArchiveException for invalid extended header length" );
1348+ } catch (ArchiveException e ) {
1349+ assertEquals ("Invalid extended header length" , e .getMessage ());
1350+ }
12691351 }
12701352 }
12711353
@@ -1373,6 +1455,9 @@ void testGetCompressionMethod() throws IOException {
13731455 @ Test
13741456 void testGetPathnameUnixFileSeparatorCharDefaultEncoding () throws IOException , UnsupportedEncodingException {
13751457 try (LhaArchiveInputStream is = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).setFileSeparatorChar ('/' ).get ()) {
1458+ assertEquals ("" , getPathname (is ));
1459+ assertEquals ("" , getPathname (is , 0xff ));
1460+ assertEquals ("a" , getPathname (is , 'a' ));
13761461 assertEquals ("folder/" , getPathname (is , 'f' , 'o' , 'l' , 'd' , 'e' , 'r' , 0xff ));
13771462 assertEquals ("folder/file.txt" , getPathname (is , 'f' , 'o' , 'l' , 'd' , 'e' , 'r' , 0xff , 'f' , 'i' , 'l' , 'e' , '.' , 't' , 'x' , 't' ));
13781463 assertEquals ("folder/file.txt" , getPathname (is , 0xff , 'f' , 'o' , 'l' , 'd' , 'e' , 'r' , 0xff , 'f' , 'i' , 'l' , 'e' , '.' , 't' , 'x' , 't' ));
@@ -1411,6 +1496,18 @@ void testGetPathnameWindowsFileSeparatorCharDefaultEncoding() throws IOException
14111496 }
14121497 }
14131498
1499+ @ Test
1500+ void testGetPathnameNegativeLength () throws IOException , UnsupportedEncodingException {
1501+ try (LhaArchiveInputStream is = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1502+ try {
1503+ is .getPathname (ByteBuffer .wrap (new byte [0 ]), -1 );
1504+ fail ("Expected ArchiveException when pathname length is negative" );
1505+ } catch (ArchiveException e ) {
1506+ assertEquals ("Pathname length is negative" , e .getMessage ());
1507+ }
1508+ }
1509+ }
1510+
14141511 @ Test
14151512 void testGetPathnameTooLong () throws IOException , UnsupportedEncodingException {
14161513 try (LhaArchiveInputStream is = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
@@ -1424,6 +1521,19 @@ void testGetPathnameTooLong() throws IOException, UnsupportedEncodingException {
14241521 }
14251522 }
14261523
1524+ @ Test
1525+ void testGetPathnameInvalidLength () throws IOException , UnsupportedEncodingException {
1526+ try (LhaArchiveInputStream is = LhaArchiveInputStream .builder ().setInputStream (newEmptyInputStream ()).get ()) {
1527+ try {
1528+ final byte [] pathname = new byte [] { 'a' , 'b' , 'c' };
1529+ is .getPathname (ByteBuffer .wrap (pathname ), pathname .length + 1 );
1530+ fail ("Expected ArchiveException for invalid pathname length" );
1531+ } catch (ArchiveException e ) {
1532+ assertEquals ("Invalid pathname length" , e .getMessage ());
1533+ }
1534+ }
1535+ }
1536+
14271537 @ Test
14281538 void testGetPathnameWindowsFileSeparatorCharIso88591 () throws IOException , UnsupportedEncodingException {
14291539 try (LhaArchiveInputStream is = LhaArchiveInputStream .builder ()
0 commit comments