From cfdd25e29cf18cfcb7bbc243f181bd5eabe942ac Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Wed, 7 Aug 2019 16:05:50 -0700 Subject: [PATCH 1/4] Build GDT for Catalyst --- GoogleDataTransport/GDTLibrary/GDTStorage.m | 26 +++++++++++++++++++ .../GDTTests/Unit/GDTClockTest.m | 9 +++++++ .../GDTTests/Unit/GDTEventTest.m | 14 ++++++++-- .../GDTTests/Unit/GDTStorageTest.m | 26 +++++++++++++++++-- .../GDTTests/Unit/GDTUploadPackageTest.m | 10 +++++++ 5 files changed, 81 insertions(+), 4 deletions(-) diff --git a/GoogleDataTransport/GDTLibrary/GDTStorage.m b/GoogleDataTransport/GDTLibrary/GDTStorage.m index ea73f1aebb4..8404c7382c5 100644 --- a/GoogleDataTransport/GDTLibrary/GDTStorage.m +++ b/GoogleDataTransport/GDTLibrary/GDTStorage.m @@ -111,7 +111,14 @@ - (void)storeEvent:(GDTEvent *)event { // If running in the background, save state to disk and end the associated background task. if (bgID != GDTBackgroundIdentifierInvalid) { +#if TARGET_OS_MACCATALYST + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:NO + error:nil]; + [data writeToFile:[GDTStorage archivePath] atomically:YES]; +#else [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; +#endif [[GDTApplication sharedApplication] endBackgroundTask:bgID]; } }); @@ -193,13 +200,25 @@ - (void)addEventToTrackingCollections:(GDTStoredEvent *)event { #pragma mark - GDTLifecycleProtocol - (void)appWillForeground:(GDTApplication *)app { +#ifdef TARGET_OS_MACCATALYST + NSData *data = [NSData dataWithContentsOfFile:[GDTStorage archivePath]]; + [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] fromData:data error:nil]; +#else [NSKeyedUnarchiver unarchiveObjectWithFile:[GDTStorage archivePath]]; +#endif self->_runningInBackground = NO; } - (void)appWillBackground:(GDTApplication *)app { self->_runningInBackground = YES; +#if TARGET_OS_MACCATALYST + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:NO + error:nil]; + [data writeToFile:[GDTStorage archivePath] atomically:YES]; +#else [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; +#endif // Create an immediate background task to run until the end of the current queue of work. __block GDTBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgID]; @@ -210,7 +229,14 @@ - (void)appWillBackground:(GDTApplication *)app { } - (void)appWillTerminate:(GDTApplication *)application { +#if TARGET_OS_MACCATALYST + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:NO + error:nil]; + [data writeToFile:[GDTStorage archivePath] atomically:YES]; +#else [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; +#endif } #pragma mark - NSSecureCoding diff --git a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m index 8f72775291c..4e5ea62bec7 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m @@ -51,8 +51,17 @@ - (void)testSupportsSecureEncoding { /** Tests encoding and decoding a clock using a keyed archiver. */ - (void)testEncoding { GDTClock *clock = [GDTClock snapshot]; +#ifdef TARGET_OS_MACCATALYST + NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock + requiringSecureCoding:NO + error:nil]; + GDTClock *unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTClock class] + fromData:clockData + error:nil]; +#else NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock]; GDTClock *unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData]; +#endif XCTAssertEqual([clock hash], [unarchivedClock hash]); XCTAssertEqualObjects(clock, unarchivedClock); } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m b/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m index 890ffd009a3..c924f8be4d9 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m @@ -43,12 +43,22 @@ - (void)testArchiving { event.qosTier = GDTEventQoSTelemetry; event.clockSnapshot = clockSnapshot; +#ifdef TARGET_OS_MACCATALYST + NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:event + requiringSecureCoding:NO + error:nil]; +#else NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:event]; - +#endif // To ensure that all the objects being retained by the original event are dealloc'd. event = nil; - +#ifdef TARGET_OS_MACCATALYST + GDTEvent *decodedEvent = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTEvent class] + fromData:archiveData + error:nil]; +#else GDTEvent *decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData]; +#endif XCTAssertEqualObjects(decodedEvent.mappingID, @"testID"); XCTAssertEqual(decodedEvent.target, 42); XCTAssertEqualObjects(decodedEvent.dataObjectTransportBytes, diff --git a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m index fd05bffc9aa..ce2b19ec05a 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m @@ -259,7 +259,13 @@ - (void)testNSSecureCoding { event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); event = nil; +#ifdef TARGET_OS_MACCATALYST + NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] + requiringSecureCoding:NO + error:nil]; +#else NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; +#endif dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNotNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); @@ -267,8 +273,13 @@ - (void)testNSSecureCoding { dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); - +#ifdef TARGET_OS_MACCATALYST + GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] + fromData:storageData + error:nil]; +#else GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; +#endif XCTAssertNotNil([unarchivedStorage.storedEvents lastObject]); } @@ -278,7 +289,13 @@ - (void)testNSSecureCodingWithSharedInstance { event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); event = nil; +#ifdef TARGET_OS_MACCATALYST + NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] + requiringSecureCoding:NO + error:nil]; +#else NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; +#endif dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNotNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); @@ -286,8 +303,13 @@ - (void)testNSSecureCodingWithSharedInstance { dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); - +#ifdef TARGET_OS_MACCATALYST + GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] + fromData:storageData + error:nil]; +#else GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; +#endif XCTAssertNotNil([unarchivedStorage.storedEvents lastObject]); } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m index 45b425a1de0..a0a69027346 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m @@ -111,8 +111,18 @@ - (void)testEncoding { uploadPackage.events = set; uploadPackage.handler = self; +#ifdef TARGET_OS_MACCATALYST + NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:NO + error:nil]; + GDTUploadPackage *recreatedPackage = + [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTUploadPackage class] + fromData:packageData + error:nil]; +#else NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:uploadPackage]; GDTUploadPackage *recreatedPackage = [NSKeyedUnarchiver unarchiveObjectWithData:packageData]; +#endif XCTAssertEqualObjects(uploadPackage, recreatedPackage); } From e7895e0a4e1002565b6f5e4e8efa343977ecba19 Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Thu, 8 Aug 2019 09:01:27 -0700 Subject: [PATCH 2/4] wip --- GoogleDataTransport/GDTLibrary/GDTStorage.m | 12 ++--- .../GDTTests/Unit/GDTClockTest.m | 17 +++---- .../GDTTests/Unit/GDTEventTest.m | 30 +++++++------ .../GDTTests/Unit/GDTStorageTest.m | 44 ++++++++++--------- .../GDTTests/Unit/GDTUploadPackageTest.m | 26 +++++------ 5 files changed, 69 insertions(+), 60 deletions(-) diff --git a/GoogleDataTransport/GDTLibrary/GDTStorage.m b/GoogleDataTransport/GDTLibrary/GDTStorage.m index 8404c7382c5..d27975eea22 100644 --- a/GoogleDataTransport/GDTLibrary/GDTStorage.m +++ b/GoogleDataTransport/GDTLibrary/GDTStorage.m @@ -200,12 +200,14 @@ - (void)addEventToTrackingCollections:(GDTStoredEvent *)event { #pragma mark - GDTLifecycleProtocol - (void)appWillForeground:(GDTApplication *)app { -#ifdef TARGET_OS_MACCATALYST - NSData *data = [NSData dataWithContentsOfFile:[GDTStorage archivePath]]; - [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] fromData:data error:nil]; -#else - [NSKeyedUnarchiver unarchiveObjectWithFile:[GDTStorage archivePath]]; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + NSData *data = [NSData dataWithContentsOfFile:[GDTStorage archivePath]]; + [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] fromData:data error:nil]; + } else { +#if !defined(TARGET_OS_MACCATALYST) + [NSKeyedUnarchiver unarchiveObjectWithFile:[GDTStorage archivePath]]; #endif + } self->_runningInBackground = NO; } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m index 4e5ea62bec7..88403290389 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m @@ -51,17 +51,18 @@ - (void)testSupportsSecureEncoding { /** Tests encoding and decoding a clock using a keyed archiver. */ - (void)testEncoding { GDTClock *clock = [GDTClock snapshot]; -#ifdef TARGET_OS_MACCATALYST - NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock - requiringSecureCoding:NO + GDTClock *unarchivedClock; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock + requiringSecureCoding:NO error:nil]; - GDTClock *unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTClock class] + unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTClock class] fromData:clockData error:nil]; -#else - NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock]; - GDTClock *unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData]; -#endif + } else { + NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock]; + unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData]; + } XCTAssertEqual([clock hash], [unarchivedClock hash]); XCTAssertEqualObjects(clock, unarchivedClock); } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m b/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m index c924f8be4d9..d892e109eb3 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m @@ -43,22 +43,24 @@ - (void)testArchiving { event.qosTier = GDTEventQoSTelemetry; event.clockSnapshot = clockSnapshot; -#ifdef TARGET_OS_MACCATALYST - NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:event - requiringSecureCoding:NO - error:nil]; -#else - NSData *archiveData = [NSKeyedArchiver archivedDataWithRootObject:event]; -#endif + NSData *archiveData; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + archiveData = [NSKeyedArchiver archivedDataWithRootObject:event + requiringSecureCoding:NO + error:nil]; + } else { + archiveData = [NSKeyedArchiver archivedDataWithRootObject:event]; + } // To ensure that all the objects being retained by the original event are dealloc'd. event = nil; -#ifdef TARGET_OS_MACCATALYST - GDTEvent *decodedEvent = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTEvent class] - fromData:archiveData - error:nil]; -#else - GDTEvent *decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData]; -#endif + GDTEvent *decodedEvent; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + decodedEvent = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTEvent class] + fromData:archiveData + error:nil]; + } else { + decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData]; + } XCTAssertEqualObjects(decodedEvent.mappingID, @"testID"); XCTAssertEqual(decodedEvent.target, 42); XCTAssertEqualObjects(decodedEvent.dataObjectTransportBytes, diff --git a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m index ce2b19ec05a..71d3aa04e45 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m @@ -259,13 +259,14 @@ - (void)testNSSecureCoding { event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); event = nil; -#ifdef TARGET_OS_MACCATALYST - NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] + NSData *storageData; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] requiringSecureCoding:NO error:nil]; -#else - NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; -#endif + } else { + storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; + } dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNotNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); @@ -273,13 +274,14 @@ - (void)testNSSecureCoding { dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); -#ifdef TARGET_OS_MACCATALYST - GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] + GDTStorage *unarchivedStorage; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] fromData:storageData error:nil]; -#else - GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; -#endif + } else { + unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; + } XCTAssertNotNil([unarchivedStorage.storedEvents lastObject]); } @@ -289,13 +291,14 @@ - (void)testNSSecureCodingWithSharedInstance { event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); event = nil; -#ifdef TARGET_OS_MACCATALYST - NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] + NSData *storageData; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] requiringSecureCoding:NO error:nil]; -#else - NSData *storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; -#endif + } else { + storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; + } dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNotNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); @@ -303,13 +306,14 @@ - (void)testNSSecureCodingWithSharedInstance { dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); -#ifdef TARGET_OS_MACCATALYST - GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] + GDTStorage *unarchivedStorage; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] fromData:storageData error:nil]; -#else - GDTStorage *unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; -#endif + } else { + unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; + } XCTAssertNotNil([unarchivedStorage.storedEvents lastObject]); } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m index a0a69027346..8670741d42d 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m @@ -110,19 +110,19 @@ - (void)testEncoding { NSMutableSet *set = [GDTEventGenerator generate3StoredEvents]; uploadPackage.events = set; uploadPackage.handler = self; - -#ifdef TARGET_OS_MACCATALYST - NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:NO - error:nil]; - GDTUploadPackage *recreatedPackage = - [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTUploadPackage class] - fromData:packageData - error:nil]; -#else - NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:uploadPackage]; - GDTUploadPackage *recreatedPackage = [NSKeyedUnarchiver unarchiveObjectWithData:packageData]; -#endif + GDTUploadPackage *recreatedPackage; + + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:NO + error:nil]; + recreatedPackage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTUploadPackage class] + fromData:packageData + error:nil]; + } else { + NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:uploadPackage]; + recreatedPackage = [NSKeyedUnarchiver unarchiveObjectWithData:packageData]; + } XCTAssertEqualObjects(uploadPackage, recreatedPackage); } From efb01a7013f4aa67abb62c4247c696779fca3148 Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Sat, 10 Aug 2019 08:33:07 -0700 Subject: [PATCH 3/4] Fix GDT tests for Catalyst --- GoogleDataTransport/GDTLibrary/GDTStorage.m | 12 +++++----- .../GDTLibrary/GDTUploadPackage.m | 8 ++++--- .../GDTTests/Unit/GDTClockTest.m | 4 +++- .../GDTTests/Unit/GDTEventTest.m | 6 ++++- .../GDTTests/Unit/GDTStorageTest.m | 22 +++++++++++++++---- .../GDTTests/Unit/GDTUploadPackageTest.m | 14 +++++++----- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/GoogleDataTransport/GDTLibrary/GDTStorage.m b/GoogleDataTransport/GDTLibrary/GDTStorage.m index d27975eea22..51a6add29d6 100644 --- a/GoogleDataTransport/GDTLibrary/GDTStorage.m +++ b/GoogleDataTransport/GDTLibrary/GDTStorage.m @@ -113,7 +113,7 @@ - (void)storeEvent:(GDTEvent *)event { if (bgID != GDTBackgroundIdentifierInvalid) { #if TARGET_OS_MACCATALYST NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; [data writeToFile:[GDTStorage archivePath] atomically:YES]; #else @@ -215,7 +215,7 @@ - (void)appWillBackground:(GDTApplication *)app { self->_runningInBackground = YES; #if TARGET_OS_MACCATALYST NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; [data writeToFile:[GDTStorage archivePath] atomically:YES]; #else @@ -233,7 +233,7 @@ - (void)appWillBackground:(GDTApplication *)app { - (void)appWillTerminate:(GDTApplication *)application { #if TARGET_OS_MACCATALYST NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; [data writeToFile:[GDTStorage archivePath] atomically:YES]; #else @@ -260,10 +260,12 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { // Create the singleton and populate its ivars. GDTStorage *sharedInstance = [self.class sharedInstance]; dispatch_sync(sharedInstance.storageQueue, ^{ - sharedInstance->_storedEvents = [aDecoder decodeObjectOfClass:[NSMutableOrderedSet class] + NSSet *classes = [NSSet setWithObjects:[NSMutableOrderedSet class], [GDTStoredEvent class], nil]; + sharedInstance->_storedEvents = [aDecoder decodeObjectOfClasses:classes forKey:kGDTStorageStoredEventsKey]; + classes = [NSSet setWithObjects:[NSMutableDictionary class], [NSMutableSet class], [GDTStoredEvent class], nil]; sharedInstance->_targetToEventSet = - [aDecoder decodeObjectOfClass:[NSMutableDictionary class] + [aDecoder decodeObjectOfClasses:classes forKey:kGDTStorageTargetToEventSetKey]; sharedInstance->_uploadCoordinator = [aDecoder decodeObjectOfClass:[GDTUploadCoordinator class] diff --git a/GoogleDataTransport/GDTLibrary/GDTUploadPackage.m b/GoogleDataTransport/GDTLibrary/GDTUploadPackage.m index 36164acdb8d..24d3b6e5441 100644 --- a/GoogleDataTransport/GDTLibrary/GDTUploadPackage.m +++ b/GoogleDataTransport/GDTLibrary/GDTUploadPackage.m @@ -18,6 +18,7 @@ #import #import +#import #import "GDTLibrary/Private/GDTStorage_Private.h" #import "GDTLibrary/Private/GDTUploadCoordinator.h" @@ -140,11 +141,12 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder { GDTTarget target = [aDecoder decodeIntegerForKey:kTargetKey]; self = [self initWithTarget:target]; if (self) { - _events = [aDecoder decodeObjectOfClass:[NSSet class] forKey:kEventsKey]; + NSSet *classes = [NSSet setWithObjects:[NSSet class], [GDTStoredEvent class], nil]; + _events = [aDecoder decodeObjectOfClasses:classes forKey:kEventsKey]; _deliverByTime = [aDecoder decodeObjectOfClass:[GDTClock class] forKey:kDeliverByTimeKey]; _isHandled = [aDecoder decodeBoolForKey:kIsHandledKey]; - // Isn't technically NSSecureCoding, because we don't know the class of this object. - _handler = [aDecoder decodeObjectForKey:kHandlerKey]; + // _handler isn't technically NSSecureCoding, because we don't know the class of this object. + // but it gets decoded anyway. } return self; } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m index 88403290389..13e008d7af0 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m @@ -54,14 +54,16 @@ - (void)testEncoding { GDTClock *unarchivedClock; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTClock class] fromData:clockData error:nil]; } else { +#if !defined(TARGET_OS_MACCATALYST) NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock]; unarchivedClock = [NSKeyedUnarchiver unarchiveObjectWithData:clockData]; +#endif } XCTAssertEqual([clock hash], [unarchivedClock hash]); XCTAssertEqualObjects(clock, unarchivedClock); diff --git a/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m b/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m index d892e109eb3..3609e3aa50d 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTEventTest.m @@ -46,10 +46,12 @@ - (void)testArchiving { NSData *archiveData; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { archiveData = [NSKeyedArchiver archivedDataWithRootObject:event - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; } else { +#if !defined(TARGET_OS_MACCATALYST) archiveData = [NSKeyedArchiver archivedDataWithRootObject:event]; +#endif } // To ensure that all the objects being retained by the original event are dealloc'd. event = nil; @@ -59,7 +61,9 @@ - (void)testArchiving { fromData:archiveData error:nil]; } else { +#if !defined(TARGET_OS_MACCATALYST) decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData]; +#endif } XCTAssertEqualObjects(decodedEvent.mappingID, @"testID"); XCTAssertEqual(decodedEvent.target, 42); diff --git a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m index 71d3aa04e45..b6d44230c11 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m @@ -83,6 +83,7 @@ - (void)testStoreEvent { @autoreleasepool { GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"404" target:target]; event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; + event.clockSnapshot = [GDTClock snapshot]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); } dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ @@ -103,6 +104,7 @@ - (void)testRemoveEvent { @autoreleasepool { GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"404" target:target]; event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; + event.clockSnapshot = [GDTClock snapshot]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); } __block NSURL *eventFile; @@ -223,7 +225,7 @@ - (void)testEventDeallocationIsEnforced { GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"404" target:target]; weakEvent = event; event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; - + event.clockSnapshot = [GDTClock snapshot]; // Store the event and wait for the expectation. [[GDTStorage sharedInstance] storeEvent:event]; GDTDataFuture *dataFuture = @@ -256,16 +258,19 @@ - (void)testEventDeallocationIsEnforced { - (void)testNSSecureCoding { XCTAssertTrue([GDTStorage supportsSecureCoding]); GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"404" target:target]; + event.clockSnapshot = [GDTClock snapshot]; event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); event = nil; NSData *storageData; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; } else { +#if !defined(TARGET_OS_MACCATALYST) storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; +#endif } dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNotNil([[GDTStorage sharedInstance].storedEvents lastObject]); @@ -275,12 +280,15 @@ - (void)testNSSecureCoding { XCTAssertNil([[GDTStorage sharedInstance].storedEvents lastObject]); }); GDTStorage *unarchivedStorage; + NSError *error; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] fromData:storageData - error:nil]; + error:&error]; } else { +#if !defined(TARGET_OS_MACCATALYST) unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; +#endif } XCTAssertNotNil([unarchivedStorage.storedEvents lastObject]); } @@ -289,15 +297,18 @@ - (void)testNSSecureCoding { - (void)testNSSecureCodingWithSharedInstance { GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"404" target:target]; event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; + event.clockSnapshot = [GDTClock snapshot]; XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); event = nil; NSData *storageData; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] - requiringSecureCoding:NO + requiringSecureCoding:YES error:nil]; } else { +#if !defined(TARGET_OS_MACCATALYST) storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; +#endif } dispatch_sync([GDTStorage sharedInstance].storageQueue, ^{ XCTAssertNotNil([[GDTStorage sharedInstance].storedEvents lastObject]); @@ -312,7 +323,9 @@ - (void)testNSSecureCodingWithSharedInstance { fromData:storageData error:nil]; } else { +#if !defined(TARGET_OS_MACCATALYST) unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; +#endif } XCTAssertNotNil([unarchivedStorage.storedEvents lastObject]); } @@ -324,6 +337,7 @@ - (void)testQoSTierFast { GDTEvent *event = [[GDTEvent alloc] initWithMappingID:@"404" target:target]; event.dataObjectTransportBytes = [@"testString" dataUsingEncoding:NSUTF8StringEncoding]; event.qosTier = GDTEventQoSFast; + event.clockSnapshot = [GDTClock snapshot]; XCTAssertFalse(self.uploaderFake.forceUploadCalled); XCTAssertNoThrow([[GDTStorage sharedInstance] storeEvent:event]); } diff --git a/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m index 8670741d42d..cdaeb0dc118 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTUploadPackageTest.m @@ -111,17 +111,21 @@ - (void)testEncoding { uploadPackage.events = set; uploadPackage.handler = self; GDTUploadPackage *recreatedPackage; - + NSError *error; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { - NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:NO - error:nil]; + NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:uploadPackage + requiringSecureCoding:YES + error:&error]; recreatedPackage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTUploadPackage class] fromData:packageData - error:nil]; + error:&error]; + XCTAssertNil(error); } else { +#if !defined(TARGET_OS_MACCATALYST) NSData *packageData = [NSKeyedArchiver archivedDataWithRootObject:uploadPackage]; recreatedPackage = [NSKeyedUnarchiver unarchiveObjectWithData:packageData]; +#endif } XCTAssertEqualObjects(uploadPackage, recreatedPackage); } From 8b78c59f71a3fc4a0214d2a2ace8d28976a0ac46 Mon Sep 17 00:00:00 2001 From: Paul Beusterien Date: Sat, 10 Aug 2019 09:18:31 -0700 Subject: [PATCH 4/4] style --- GoogleDataTransport/GDTLibrary/GDTStorage.m | 59 +++++++++++-------- .../GDTTests/Unit/GDTClockTest.m | 8 +-- .../GDTTests/Unit/GDTStorageTest.m | 16 ++--- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/GoogleDataTransport/GDTLibrary/GDTStorage.m b/GoogleDataTransport/GDTLibrary/GDTStorage.m index 51a6add29d6..623630c0df8 100644 --- a/GoogleDataTransport/GDTLibrary/GDTStorage.m +++ b/GoogleDataTransport/GDTLibrary/GDTStorage.m @@ -111,14 +111,16 @@ - (void)storeEvent:(GDTEvent *)event { // If running in the background, save state to disk and end the associated background task. if (bgID != GDTBackgroundIdentifierInvalid) { -#if TARGET_OS_MACCATALYST - NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:YES - error:nil]; - [data writeToFile:[GDTStorage archivePath] atomically:YES]; -#else - [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:YES + error:nil]; + [data writeToFile:[GDTStorage archivePath] atomically:YES]; + } else { +#if !defined(TARGET_OS_MACCATALYST) + [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; #endif + } [[GDTApplication sharedApplication] endBackgroundTask:bgID]; } }); @@ -213,14 +215,16 @@ - (void)appWillForeground:(GDTApplication *)app { - (void)appWillBackground:(GDTApplication *)app { self->_runningInBackground = YES; -#if TARGET_OS_MACCATALYST - NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:YES - error:nil]; - [data writeToFile:[GDTStorage archivePath] atomically:YES]; -#else - [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:YES + error:nil]; + [data writeToFile:[GDTStorage archivePath] atomically:YES]; + } else { +#if !defined(TARGET_OS_MACCATALYST) + [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; #endif + } // Create an immediate background task to run until the end of the current queue of work. __block GDTBackgroundIdentifier bgID = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgID]; @@ -231,14 +235,16 @@ - (void)appWillBackground:(GDTApplication *)app { } - (void)appWillTerminate:(GDTApplication *)application { -#if TARGET_OS_MACCATALYST - NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self - requiringSecureCoding:YES - error:nil]; - [data writeToFile:[GDTStorage archivePath] atomically:YES]; -#else - [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; + if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { + NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self + requiringSecureCoding:YES + error:nil]; + [data writeToFile:[GDTStorage archivePath] atomically:YES]; + } else { +#if !defined(TARGET_OS_MACCATALYST) + [NSKeyedArchiver archiveRootObject:self toFile:[GDTStorage archivePath]]; #endif + } } #pragma mark - NSSecureCoding @@ -260,13 +266,14 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder { // Create the singleton and populate its ivars. GDTStorage *sharedInstance = [self.class sharedInstance]; dispatch_sync(sharedInstance.storageQueue, ^{ - NSSet *classes = [NSSet setWithObjects:[NSMutableOrderedSet class], [GDTStoredEvent class], nil]; + NSSet *classes = + [NSSet setWithObjects:[NSMutableOrderedSet class], [GDTStoredEvent class], nil]; sharedInstance->_storedEvents = [aDecoder decodeObjectOfClasses:classes - forKey:kGDTStorageStoredEventsKey]; - classes = [NSSet setWithObjects:[NSMutableDictionary class], [NSMutableSet class], [GDTStoredEvent class], nil]; + forKey:kGDTStorageStoredEventsKey]; + classes = [NSSet setWithObjects:[NSMutableDictionary class], [NSMutableSet class], + [GDTStoredEvent class], nil]; sharedInstance->_targetToEventSet = - [aDecoder decodeObjectOfClasses:classes - forKey:kGDTStorageTargetToEventSetKey]; + [aDecoder decodeObjectOfClasses:classes forKey:kGDTStorageTargetToEventSetKey]; sharedInstance->_uploadCoordinator = [aDecoder decodeObjectOfClass:[GDTUploadCoordinator class] forKey:kGDTStorageUploadCoordinatorKey]; diff --git a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m index 13e008d7af0..a6127267732 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTClockTest.m @@ -54,11 +54,11 @@ - (void)testEncoding { GDTClock *unarchivedClock; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock - requiringSecureCoding:YES - error:nil]; + requiringSecureCoding:YES + error:nil]; unarchivedClock = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTClock class] - fromData:clockData - error:nil]; + fromData:clockData + error:nil]; } else { #if !defined(TARGET_OS_MACCATALYST) NSData *clockData = [NSKeyedArchiver archivedDataWithRootObject:clock]; diff --git a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m index b6d44230c11..32828e89fca 100644 --- a/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m +++ b/GoogleDataTransport/GDTTests/Unit/GDTStorageTest.m @@ -265,8 +265,8 @@ - (void)testNSSecureCoding { NSData *storageData; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] - requiringSecureCoding:YES - error:nil]; + requiringSecureCoding:YES + error:nil]; } else { #if !defined(TARGET_OS_MACCATALYST) storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; @@ -283,8 +283,8 @@ - (void)testNSSecureCoding { NSError *error; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] - fromData:storageData - error:&error]; + fromData:storageData + error:&error]; } else { #if !defined(TARGET_OS_MACCATALYST) unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData]; @@ -303,8 +303,8 @@ - (void)testNSSecureCodingWithSharedInstance { NSData *storageData; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance] - requiringSecureCoding:YES - error:nil]; + requiringSecureCoding:YES + error:nil]; } else { #if !defined(TARGET_OS_MACCATALYST) storageData = [NSKeyedArchiver archivedDataWithRootObject:[GDTStorage sharedInstance]]; @@ -320,8 +320,8 @@ - (void)testNSSecureCodingWithSharedInstance { GDTStorage *unarchivedStorage; if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { unarchivedStorage = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTStorage class] - fromData:storageData - error:nil]; + fromData:storageData + error:nil]; } else { #if !defined(TARGET_OS_MACCATALYST) unarchivedStorage = [NSKeyedUnarchiver unarchiveObjectWithData:storageData];