Skip to content

Commit

Permalink
test dateWithMSDOSFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
Coeur committed Nov 11, 2024
1 parent a0e8e08 commit f29c80a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
61 changes: 59 additions & 2 deletions Example/ObjectiveCExampleTests/SSZipArchiveTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ @interface NSString (SSZipArchive)
- (NSString *)_sanitizedPath;
@end

@interface SSZipArchive (XCTest)
+ (NSDate *)_dateWithMSDOSFormat:(UInt32)msdosDateTime;
@end

@implementation SSZipArchiveTests

int twentyMB = 20 * 1024 * 1024;
Expand Down Expand Up @@ -285,7 +289,6 @@ - (void)testUnzippingWithInvalidPassword {
}

- (void)testUnzippingWithInvalidPassword2 {
XCTSkip("https://github.com/ZipArchive/ZipArchive/issues/633");
NSString *zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestPasswordArchive2" ofType:@"zip"];
NSString *outputPath = [self _cachesPath:@"Password"];

Expand Down Expand Up @@ -315,7 +318,6 @@ - (void)testIsPasswordInvalidForArchiveAtPath {
}

- (void)testIsPasswordInvalidForArchiveAtPath2 {
XCTSkip("https://github.com/ZipArchive/ZipArchive/issues/633");
NSString *zipPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestPasswordArchive2" ofType:@"zip"];

NSError *error = nil;
Expand Down Expand Up @@ -746,6 +748,61 @@ - (void)testPathSanitation {
}
}

- (void)testDateSanitation {
NSCalendar *gregorianGMT = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
gregorianGMT.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDateComponents *components = [[NSDateComponents alloc] init];
struct tm ptm;

UInt32 dos_date_min = 1;
NSDate *minDate = [SSZipArchive _dateWithMSDOSFormat:dos_date_min];

// non-deprecated equivalent of:
// XCTAssertEqual(minDate, [NSDate dateWithString:@"1979-11-29 23:00:02 +0000"]);
components.year = 1979;
components.month = 11;
components.day = 29;
components.hour = 23;
components.minute = 0;
components.second = 2;
XCTAssertEqual(minDate, [gregorianGMT dateFromComponents:components]);

// fixed mz_zip_dosdate_to_time_t implementation (https://github.com/zlib-ng/minizip-ng/pull/820)
uint64_t date_min = (uint64_t)(dos_date_min >> 16);
ptm.tm_mday = (int16_t)(date_min & 0x1f);
ptm.tm_mon = (int16_t)(((date_min & 0x1E0) / 0x20) - 1);
ptm.tm_year = (int16_t)(((date_min & 0x0FE00) / 0x0200) + 80);
ptm.tm_hour = (int16_t)((dos_date_min & 0xF800) / 0x800);
ptm.tm_min = (int16_t)((dos_date_min & 0x7E0) / 0x20);
ptm.tm_sec = (int16_t)(2 * (dos_date_min & 0x1f));
ptm.tm_isdst = -1;
XCTAssertEqual(minDate, [NSDate dateWithTimeIntervalSince1970:mktime(&ptm)]);

UInt32 dos_date_max = -1;
NSDate *maxDate = [SSZipArchive _dateWithMSDOSFormat:dos_date_max];

// non-deprecated equivalent of:
// XCTAssertEqual(maxDate, [NSDate dateWithString:@"2108-04-01 06:04:02 +0000"]);
components.year = 2108;
components.month = 4;
components.day = 1;
components.hour = 6;
components.minute = 4;
components.second = 2;
XCTAssertEqual(maxDate, [gregorianGMT dateFromComponents:components]);

// fixed mz_zip_dosdate_to_time_t implementation (https://github.com/zlib-ng/minizip-ng/pull/820)
uint64_t date_max = (uint64_t)(dos_date_max >> 16);
ptm.tm_mday = (int16_t)(date_max & 0x1f);
ptm.tm_mon = (int16_t)(((date_max & 0x1E0) / 0x20) - 1);
ptm.tm_year = (int16_t)(((date_max & 0x0FE00) / 0x0200) + 80);
ptm.tm_hour = (int16_t)((dos_date_max & 0xF800) / 0x800);
ptm.tm_min = (int16_t)((dos_date_max & 0x7E0) / 0x20);
ptm.tm_sec = (int16_t)(2 * (dos_date_max & 0x1f));
ptm.tm_isdst = -1;
XCTAssertEqual(maxDate, [NSDate dateWithTimeIntervalSince1970:mktime(&ptm)]);
}

// This tests whether the payload size of the zip file containing 4.8Gb of files with compression 0 is correct,
// and creates a zip file containing 240 20Mb sized files with multiple compression levels. It then unpacks the file and checks whether
// the same number of files is present in the unpacked folder.
Expand Down
2 changes: 1 addition & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: f72aa12c216b9028a1e05f38d3c62c3f6f602c06

COCOAPODS: 1.15.2
COCOAPODS: 1.16.1
3 changes: 2 additions & 1 deletion SSZipArchive/SSZipArchive.m
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ + (NSDate *)_dateWithMSDOSFormat:(UInt32)msdosDateTime
{
// the whole `_dateWithMSDOSFormat:` method is equivalent but faster than this one line,
// essentially because `mktime` is slow:
//NSDate *date = [NSDate dateWithTimeIntervalSince1970:dosdate_to_time_t(msdosDateTime)];
//NSDate *date = [NSDate dateWithTimeIntervalSince1970:mz_zip_dosdate_to_time_t(msdosDateTime)];
static const UInt32 kYearMask = 0xFE000000;
static const UInt32 kMonthMask = 0x1E00000;
static const UInt32 kDayMask = 0x1F0000;
Expand All @@ -1360,6 +1360,7 @@ + (NSDate *)_dateWithMSDOSFormat:(UInt32)msdosDateTime
components.second = (msdosDateTime & kSecondMask) * 2;

NSDate *date = [self._gregorian dateFromComponents:components];
NSAssert(date != nil, @"Failed to convert %u to date", msdosDateTime);
return date;
}

Expand Down

0 comments on commit f29c80a

Please sign in to comment.