From 674b2238b8ef317716f645265043a21c2d649467 Mon Sep 17 00:00:00 2001 From: Zorg Date: Wed, 17 Feb 2016 00:04:07 -0500 Subject: [PATCH 1/5] Make Sparkle look for the highest compatible version Sparkle's past behavior was sorting appcast items by published dates and picking the first compatible item. However, it makes more sense to pick the compatible item with the highest version (see issue #31). The NSDate instance creation & sorting has also been removed from the code. The methods we were using to parse the string into a NSDate have been deprecated, and are strongly discouraged. Namely, all methods taking an arbitrary/natural string have been nuked signaling they were probably a really bad idea. Furthermore, the date code we had created a NSCalendarDate which has been long deprecated. So we just don't handle date parsing. Even if there is a proper format for the pubDate, It is an extra burden on the developer to insert a conforming date. And we shouldn't want to rely on a potentially error-prone input field. --- Sparkle/SUAppcast.m | 20 ++++++++------------ Sparkle/SUAppcastItem.h | 2 +- Sparkle/SUAppcastItem.m | 6 +++--- Sparkle/SUBasicUpdateDriver.m | 14 +++++++++----- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 42254148..2dcc7a96 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -169,12 +169,12 @@ } else if ([name isEqualToString:SURSSElementPubDate]) { - // pubDate is expected to be an NSDate by SUAppcastItem, but the RSS class was returning an NSString - NSString *string = node.stringValue; - if (string) { - NSDate *date = [NSDate dateWithNaturalLanguageString:string]; - if (date) - dict[name] = date; + // We don't want to parse and create a NSDate instance - + // that's a risk we can avoid. We don't use the date anywhere other + // than it being accessible from SUAppcastItem + NSString *dateString = node.stringValue; + if (dateString) { + dict[name] = dateString; } } else if ([name isEqualToString:SUAppcastElementDeltas]) @@ -221,12 +221,8 @@ [dict removeAllObjects]; } } - - if ([appcastItems count]) { - NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO]; - [appcastItems sortUsingDescriptors:@[sort]]; - self.items = appcastItems; - } + + self.items = appcastItems; if (failed) { [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while parsing the update feed.", nil) }]]; diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index 86843bfb..47b6ee48 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -14,7 +14,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly) NSString *title; -@property (copy, readonly) NSDate *date; +@property (copy, readonly) NSString *dateString; @property (copy, readonly) NSString *itemDescription; @property (strong, readonly) NSURL *releaseNotesURL; @property (copy, readonly) NSString *DSASignature; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 7ee42020..e43f3574 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -16,7 +16,7 @@ @interface SUAppcastItem () @property (copy, readwrite) NSString *title; -@property (copy, readwrite) NSDate *date; +@property (copy, readwrite) NSString *dateString; @property (copy, readwrite) NSString *itemDescription; @property (strong, readwrite) NSURL *releaseNotesURL; @property (copy, readwrite) NSString *DSASignature; @@ -31,7 +31,7 @@ @end @implementation SUAppcastItem -@synthesize date; +@synthesize dateString; @synthesize deltaUpdates; @synthesize displayVersionString; @synthesize DSASignature; @@ -104,7 +104,7 @@ propertiesDictionary = [[NSMutableDictionary alloc] initWithDictionary:dict]; self.title = dict[SURSSElementTitle]; - self.date = dict[SURSSElementPubDate]; + self.dateString = dict[SURSSElementPubDate]; self.itemDescription = dict[SURSSElementDescription]; NSString *theInfoURL = dict[SURSSElementLink]; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 4ab61093..bb92274a 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -136,11 +136,15 @@ } else // If not, we'll take care of it ourselves. { - // Find the first update we can actually use. - NSEnumerator *updateEnumerator = [[ac items] objectEnumerator]; - do { - item = [updateEnumerator nextObject]; - } while (item && ![self hostSupportsItem:item]); + // Find the best supported update + id comparator = [self versionComparator]; + for(SUAppcastItem *candidate in ac.items) { + if ([self hostSupportsItem:candidate]) { + if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { + item = candidate; + } + } + } SUAppcastItem *deltaUpdateItem = [item deltaUpdates][[self.host version]]; if (deltaUpdateItem && [self hostSupportsItem:deltaUpdateItem]) { From 8a04cc726f86dfb8a8aaf7eb54eca91c30e9545c Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 19 Feb 2016 10:19:00 -0500 Subject: [PATCH 2/5] Appcast items are no longer sorted; reflect that in unit tests --- Tests/SUAppcastTest.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 5f39f795..b60478f9 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -21,11 +21,13 @@ class SUAppcastTest: XCTestCase { let items = try appcast.parseAppcastItemsFromXMLFile(testFileUrl) as! [SUAppcastItem]; XCTAssertEqual(2, items.count); - XCTAssertEqual("Version 3.0", items[0].title); - XCTAssertNil(items[0].itemDescription); - XCTAssertEqual("Version 2.0", items[1].title); - XCTAssertEqual("desc", items[1].itemDescription); + XCTAssertEqual("Version 2.0", items[0].title); + XCTAssertEqual("desc", items[0].itemDescription); + + XCTAssertEqual("Version 3.0", items[1].title); + XCTAssertNil(items[1].itemDescription); + } catch let err as NSError { NSLog("%@", err); XCTFail(err.localizedDescription); From 004ac3f9ae88012e06c6cc092f5a8b4635c78374 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 19 Feb 2016 11:26:35 -0500 Subject: [PATCH 3/5] Test the best chosen appcast item --- Sparkle/SUBasicUpdateDriver.h | 2 +- Sparkle/SUBasicUpdateDriver.m | 32 +++++++++++++--------- Sparkle/SUUserInitiatedUpdateDriver.m | 2 +- Tests/Resources/testappcast.xml | 15 ++++++++++ Tests/SUAppcastTest.swift | 12 +++++++- Tests/Sparkle Unit Tests-Bridging-Header.h | 9 ++++++ 6 files changed, 56 insertions(+), 16 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 8c6960d8..301b4bc5 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -24,7 +24,7 @@ - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)host; - (BOOL)isItemNewer:(SUAppcastItem *)ui; -- (BOOL)hostSupportsItem:(SUAppcastItem *)ui; ++ (BOOL)hostSupportsItem:(SUAppcastItem *)ui; - (BOOL)itemContainsSkippedVersion:(SUAppcastItem *)ui; - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui; - (void)appcastDidFinishLoading:(SUAppcast *)ac; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index f6cf84b7..1362a57e 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -82,12 +82,20 @@ return comparator; } -- (BOOL)isItemNewer:(SUAppcastItem *)ui ++ (SUAppcastItem *)bestAppcastItemFromAppcastItems:(NSArray *)appcastItems withComparator:(id)comparator { - return [[self versionComparator] compareVersion:[self.host version] toVersion:[ui versionString]] == NSOrderedAscending; + SUAppcastItem *item = nil; + for(SUAppcastItem *candidate in appcastItems) { + if ([[self class] hostSupportsItem:candidate]) { + if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { + item = candidate; + } + } + } + return item; } -- (BOOL)hostSupportsItem:(SUAppcastItem *)ui ++ (BOOL)hostSupportsItem:(SUAppcastItem *)ui { if (([ui minimumSystemVersion] == nil || [[ui minimumSystemVersion] isEqualToString:@""]) && ([ui maximumSystemVersion] == nil || [[ui maximumSystemVersion] isEqualToString:@""])) { return YES; } @@ -106,6 +114,11 @@ return minimumVersionOK && maximumVersionOK; } +- (BOOL)isItemNewer:(SUAppcastItem *)ui +{ + return [[self versionComparator] compareVersion:[self.host version] toVersion:[ui versionString]] == NSOrderedAscending; +} + - (BOOL)itemContainsSkippedVersion:(SUAppcastItem *)ui { NSString *skippedVersion = [self.host objectForUserDefaultsKey:SUSkippedVersionKey]; @@ -115,7 +128,7 @@ - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { - return ui && [self hostSupportsItem:ui] && [self isItemNewer:ui] && ![self itemContainsSkippedVersion:ui]; + return ui && [[self class] hostSupportsItem:ui] && [self isItemNewer:ui] && ![self itemContainsSkippedVersion:ui]; } - (void)appcastDidFinishLoading:(SUAppcast *)ac @@ -140,18 +153,11 @@ else // If not, we'll take care of it ourselves. { // Find the best supported update - id comparator = [self versionComparator]; - for(SUAppcastItem *candidate in ac.items) { - if ([self hostSupportsItem:candidate]) { - if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { - item = candidate; - } - } - } + item = [[self class] bestAppcastItemFromAppcastItems:ac.items withComparator:[self versionComparator]]; if (item) { SUAppcastItem *deltaUpdateItem = [item deltaUpdates][[self.host version]]; - if (deltaUpdateItem && [self hostSupportsItem:deltaUpdateItem]) { + if (deltaUpdateItem && [[self class] hostSupportsItem:deltaUpdateItem]) { self.nonDeltaUpdateItem = item; item = deltaUpdateItem; } diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index d1e0c9a6..04e96b23 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -81,7 +81,7 @@ - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { // We don't check to see if this update's been skipped, because the user explicitly *asked* if he had the latest version. - return [self hostSupportsItem:ui] && [self isItemNewer:ui]; + return [[self class] hostSupportsItem:ui] && [self isItemNewer:ui]; } @end diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index 0023508e..b88eb16b 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -8,10 +8,25 @@ Sat, 26 Jul 2014 15:20:11 +0000 + Version 3.0 Sat, 26 Jul 2014 15:20:12 +0000 + + + Version 4.0 + Sat, 26 Jul 2014 15:20:13 +0000 + + 17.0.0 + + + + Version 5.0 + Sat, 26 Jul 2014 15:20:14 +0000 + + 2.0.0 + diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index b60478f9..72d55639 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -20,14 +20,24 @@ class SUAppcastTest: XCTestCase { do { let items = try appcast.parseAppcastItemsFromXMLFile(testFileUrl) as! [SUAppcastItem]; - XCTAssertEqual(2, items.count); + XCTAssertEqual(4, items.count); XCTAssertEqual("Version 2.0", items[0].title); XCTAssertEqual("desc", items[0].itemDescription); + // This is the best release matching our system version XCTAssertEqual("Version 3.0", items[1].title); XCTAssertNil(items[1].itemDescription); + XCTAssertEqual("Version 4.0", items[2].title); + XCTAssertNil(items[2].itemDescription); + + XCTAssertEqual("Version 5.0", items[3].title); + XCTAssertNil(items[3].itemDescription); + + let bestAppcastItem = SUBasicUpdateDriver.bestAppcastItemFromAppcastItems(items, withComparator: SUStandardVersionComparator.defaultComparator()) + + XCTAssertEqual(bestAppcastItem, items[1]) } catch let err as NSError { NSLog("%@", err); XCTFail(err.localizedDescription); diff --git a/Tests/Sparkle Unit Tests-Bridging-Header.h b/Tests/Sparkle Unit Tests-Bridging-Header.h index 512f4a58..8794e12b 100644 --- a/Tests/Sparkle Unit Tests-Bridging-Header.h +++ b/Tests/Sparkle Unit Tests-Bridging-Header.h @@ -8,6 +8,9 @@ #import "SUFileManager.h" #import "SUAppcast.h" #import "SUAppcastItem.h" +#import "SUBasicUpdateDriver.h" +#import "SUVersionComparisonProtocol.h" +#import "SUStandardVersionComparator.h" // Duplicated to avoid exporting a private symbol from SUFileManager static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; @@ -23,6 +26,12 @@ static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; @end +@interface SUBasicUpdateDriver (Private) + ++ (SUAppcastItem *)bestAppcastItemFromAppcastItems:(NSArray *)appcastItems withComparator:(id)comparator; + +@end + @interface SUAppcast (Private) - (NSArray *)parseAppcastItemsFromXMLFile:(NSURL *)appcastFile error:(NSError *__autoreleasing*)errorp; From 2a3e88bf96030e8584647055ee113af9fa3c548d Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 19 Feb 2016 11:45:54 -0500 Subject: [PATCH 4/5] Test the dateString field --- Tests/Resources/testappcast.xml | 2 -- Tests/SUAppcastTest.swift | 4 ++++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index b88eb16b..18a256ac 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -11,7 +11,6 @@ Version 3.0 - Sat, 26 Jul 2014 15:20:12 +0000 @@ -24,7 +23,6 @@ Version 5.0 - Sat, 26 Jul 2014 15:20:14 +0000 2.0.0 diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 72d55639..097cf95b 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -24,16 +24,20 @@ class SUAppcastTest: XCTestCase { XCTAssertEqual("Version 2.0", items[0].title); XCTAssertEqual("desc", items[0].itemDescription); + XCTAssertEqual("Sat, 26 Jul 2014 15:20:11 +0000", items[0].dateString) // This is the best release matching our system version XCTAssertEqual("Version 3.0", items[1].title); XCTAssertNil(items[1].itemDescription); + XCTAssertNil(items[1].dateString) XCTAssertEqual("Version 4.0", items[2].title); XCTAssertNil(items[2].itemDescription); + XCTAssertEqual("Sat, 26 Jul 2014 15:20:13 +0000", items[2].dateString) XCTAssertEqual("Version 5.0", items[3].title); XCTAssertNil(items[3].itemDescription); + XCTAssertNil(items[3].dateString) let bestAppcastItem = SUBasicUpdateDriver.bestAppcastItemFromAppcastItems(items, withComparator: SUStandardVersionComparator.defaultComparator()) From b9c664687dd720cf6ec0f9ea3925bbc65e6a3841 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 19 Feb 2016 12:43:41 -0500 Subject: [PATCH 5/5] Take delta updates in account and add tests for them --- Sparkle/SUBasicUpdateDriver.m | 24 ++++++++++++++-------- Tests/Resources/testappcast.xml | 15 ++++++++++++++ Tests/SUAppcastTest.swift | 19 +++++++++++++++-- Tests/Sparkle Unit Tests-Bridging-Header.h | 2 +- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 1362a57e..51ea4896 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -82,7 +82,7 @@ return comparator; } -+ (SUAppcastItem *)bestAppcastItemFromAppcastItems:(NSArray *)appcastItems withComparator:(id)comparator ++ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator { SUAppcastItem *item = nil; for(SUAppcastItem *candidate in appcastItems) { @@ -92,6 +92,14 @@ } } } + + if (item && deltaItem) { + SUAppcastItem *deltaUpdateItem = [item deltaUpdates][hostVersion]; + if (deltaUpdateItem && [[self class] hostSupportsItem:deltaUpdateItem]) { + *deltaItem = deltaUpdateItem; + } + } + return item; } @@ -153,14 +161,12 @@ else // If not, we'll take care of it ourselves. { // Find the best supported update - item = [[self class] bestAppcastItemFromAppcastItems:ac.items withComparator:[self versionComparator]]; - - if (item) { - SUAppcastItem *deltaUpdateItem = [item deltaUpdates][[self.host version]]; - if (deltaUpdateItem && [[self class] hostSupportsItem:deltaUpdateItem]) { - self.nonDeltaUpdateItem = item; - item = deltaUpdateItem; - } + SUAppcastItem *deltaUpdateItem = nil; + item = [[self class] bestItemFromAppcastItems:ac.items getDeltaItem:&deltaUpdateItem withHostVersion:self.host.version comparator:[self versionComparator]]; + + if (item && deltaUpdateItem) { + self.nonDeltaUpdateItem = item; + item = deltaUpdateItem; } } diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index 18a256ac..e3894b41 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -12,6 +12,21 @@ Version 3.0 + + + + + diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 097cf95b..14fa2978 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -39,9 +39,24 @@ class SUAppcastTest: XCTestCase { XCTAssertNil(items[3].itemDescription); XCTAssertNil(items[3].dateString) - let bestAppcastItem = SUBasicUpdateDriver.bestAppcastItemFromAppcastItems(items, withComparator: SUStandardVersionComparator.defaultComparator()) - + // Test best appcast item & a delta update item + var deltaItem: SUAppcastItem? = nil + let bestAppcastItem = SUBasicUpdateDriver.bestItemFromAppcastItems(items, getDeltaItem: &deltaItem, withHostVersion: "1.0", comparator: SUStandardVersionComparator.defaultComparator()) + XCTAssertEqual(bestAppcastItem, items[1]) + XCTAssertEqual(deltaItem!.fileURL.lastPathComponent, "3.0_from_1.0.patch") + + // Test latest delta update item available + var latestDeltaItem: SUAppcastItem? = nil + SUBasicUpdateDriver.bestItemFromAppcastItems(items, getDeltaItem: &latestDeltaItem, withHostVersion: "2.0", comparator: SUStandardVersionComparator.defaultComparator()) + + XCTAssertEqual(latestDeltaItem!.fileURL.lastPathComponent, "3.0_from_2.0.patch") + + // Test a delta item that does not exist + var nonexistantDeltaItem: SUAppcastItem? = nil + SUBasicUpdateDriver.bestItemFromAppcastItems(items, getDeltaItem: &nonexistantDeltaItem, withHostVersion: "2.1", comparator: SUStandardVersionComparator.defaultComparator()) + + XCTAssertNil(nonexistantDeltaItem) } catch let err as NSError { NSLog("%@", err); XCTFail(err.localizedDescription); diff --git a/Tests/Sparkle Unit Tests-Bridging-Header.h b/Tests/Sparkle Unit Tests-Bridging-Header.h index 8794e12b..d08dafb8 100644 --- a/Tests/Sparkle Unit Tests-Bridging-Header.h +++ b/Tests/Sparkle Unit Tests-Bridging-Header.h @@ -28,7 +28,7 @@ static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; @interface SUBasicUpdateDriver (Private) -+ (SUAppcastItem *)bestAppcastItemFromAppcastItems:(NSArray *)appcastItems withComparator:(id)comparator; ++ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator; @end