diff --git a/GoogleDataTransport.podspec b/GoogleDataTransport.podspec index 22243dbffa5..7c6d5f04459 100644 --- a/GoogleDataTransport.podspec +++ b/GoogleDataTransport.podspec @@ -46,7 +46,6 @@ Shared library for iOS SDK data transport needs. s.test_spec 'Tests-Integration' do |test_spec| test_spec.requires_app_host = false test_spec.source_files = ['GoogleDataTransport/Tests/Integration/**/*.{h,m}'] + common_test_sources - test_spec.compiler_flags = '-DGDT_LOG_TRACE_ENABLED=1' test_spec.dependency 'GCDWebServer' end end diff --git a/GoogleDataTransport/GoogleDataTransport/Classes/GDTClock.m b/GoogleDataTransport/GoogleDataTransport/Classes/GDTClock.m index 208aaede20b..179e4c90b52 100644 --- a/GoogleDataTransport/GoogleDataTransport/Classes/GDTClock.m +++ b/GoogleDataTransport/GoogleDataTransport/Classes/GDTClock.m @@ -74,13 +74,7 @@ static int64_t UptimeInNanoseconds() { } // TODO: Consider adding a 'trustedTime' property that can be populated by the response from a BE. -@implementation GDTClock { - /** The kernel boot time when this clock was created. */ - int64_t _kernelBootTime; - - /** The device uptime when this clock was created. */ - int64_t _uptime; -} +@implementation GDTClock - (instancetype)init { self = [super init]; @@ -118,7 +112,8 @@ - (BOOL)isAfter:(GDTClock *)otherClock { } - (NSUInteger)hash { - return [@(_kernelBootTime) hash] ^ [@(_uptime) hash] ^ [@(_timeMillis) hash]; + return [@(_kernelBootTime) hash] ^ [@(_uptime) hash] ^ [@(_timeMillis) hash] ^ + [@(_timezoneOffsetSeconds) hash]; } - (BOOL)isEqual:(id)object { diff --git a/GoogleDataTransport/GoogleDataTransport/Classes/GDTStoredEvent.m b/GoogleDataTransport/GoogleDataTransport/Classes/GDTStoredEvent.m index 9c662b0f8e7..c1166058a99 100644 --- a/GoogleDataTransport/GoogleDataTransport/Classes/GDTStoredEvent.m +++ b/GoogleDataTransport/GoogleDataTransport/Classes/GDTStoredEvent.m @@ -81,4 +81,13 @@ - (nullable instancetype)initWithCoder:(nonnull NSCoder *)aDecoder { return self; } +- (BOOL)isEqual:(GDTStoredEvent *)other { + return [self hash] == [other hash]; +} + +- (NSUInteger)hash { + return + [_eventFileURL hash] ^ [_mappingID hash] ^ [_target hash] ^ [_clockSnapshot hash] ^ _qosTier; +} + @end diff --git a/GoogleDataTransport/GoogleDataTransport/Classes/Public/GDTClock.h b/GoogleDataTransport/GoogleDataTransport/Classes/Public/GDTClock.h index 9190b5a08f9..4c6666e0659 100644 --- a/GoogleDataTransport/GoogleDataTransport/Classes/Public/GDTClock.h +++ b/GoogleDataTransport/GoogleDataTransport/Classes/Public/GDTClock.h @@ -27,6 +27,12 @@ NS_ASSUME_NONNULL_BEGIN /** The offset from UTC in seconds. */ @property(nonatomic, readonly) int64_t timezoneOffsetSeconds; +/** The kernel boot time when this clock was created. */ +@property(nonatomic, readonly) int64_t kernelBootTime; + +/** The device uptime when this clock was created. */ +@property(nonatomic, readonly) int64_t uptime; + /** Creates a GDTClock object using the current time and offsets. * * @return A new GDTClock object representing the current time state. diff --git a/GoogleDataTransport/Tests/Unit/GDTStoredEventTest.m b/GoogleDataTransport/Tests/Unit/GDTStoredEventTest.m index 026c5234820..103058e983d 100644 --- a/GoogleDataTransport/Tests/Unit/GDTStoredEventTest.m +++ b/GoogleDataTransport/Tests/Unit/GDTStoredEventTest.m @@ -50,4 +50,39 @@ - (void)testNSSecureCoding { XCTAssertNil(storedEvent.customPrioritizationParams); XCTAssertNotNil(storedEvent.eventFileURL); } + +/** Tests equality between GDTStoredEvents. */ +- (void)testIsEqualAndHash { + GDTEvent *event1 = [[GDTEvent alloc] initWithMappingID:@"1018" target:1]; + event1.clockSnapshot = [GDTClock snapshot]; + [event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"]; + [event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"]; + [event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"]; + [event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"]; + event1.qosTier = GDTEventQosDefault; + event1.customPrioritizationParams = @{@"customParam1" : @"aValue1"}; + GDTStoredEvent *storedEvent1 = + [event1 storedEventWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]]; + + GDTEvent *event2 = [[GDTEvent alloc] initWithMappingID:@"1018" target:1]; + event2.clockSnapshot = [GDTClock snapshot]; + [event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"]; + [event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"]; + [event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"]; + [event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"]; + event2.qosTier = GDTEventQosDefault; + event2.customPrioritizationParams = @{@"customParam1" : @"aValue1"}; + GDTStoredEvent *storedEvent2 = + [event2 storedEventWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]]; + + XCTAssertEqual([storedEvent1 hash], [storedEvent2 hash]); + XCTAssertEqualObjects(storedEvent1, storedEvent2); + + // This only really tests that changing the timezoneOffsetSeconds value causes a change in hash. + [storedEvent2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"]; + + XCTAssertNotEqual([storedEvent1 hash], [storedEvent2 hash]); + XCTAssertNotEqualObjects(storedEvent1, storedEvent2); +} + @end