GDTCOREventTest.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <GoogleDataTransport/GDTCORClock.h>
  17. #import <GoogleDataTransport/GDTCOREvent.h>
  18. #import "GDTCORTests/Unit/GDTCORTestCase.h"
  19. #import "GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
  20. #import "GDTCORLibrary/Private/GDTCOREvent_Private.h"
  21. @interface GDTCOREventTest : GDTCORTestCase
  22. @end
  23. @implementation GDTCOREventTest
  24. /** Tests the designated initializer. */
  25. - (void)testInit {
  26. XCTAssertNotNil([[GDTCOREvent alloc] initWithMappingID:@"1" target:1]);
  27. XCTAssertNil([[GDTCOREvent alloc] initWithMappingID:@"" target:1]);
  28. }
  29. /** Tests NSKeyedArchiver encoding and decoding. */
  30. - (void)testArchiving {
  31. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  32. GDTCORClock *clockSnapshot = [GDTCORClock snapshot];
  33. int64_t timeMillis = clockSnapshot.timeMillis;
  34. int64_t timezoneOffsetSeconds = clockSnapshot.timezoneOffsetSeconds;
  35. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testID" target:42];
  36. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  37. event.qosTier = GDTCOREventQoSTelemetry;
  38. event.clockSnapshot = clockSnapshot;
  39. NSData *archiveData;
  40. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  41. archiveData = [NSKeyedArchiver archivedDataWithRootObject:event
  42. requiringSecureCoding:YES
  43. error:nil];
  44. } else {
  45. #if !TARGET_OS_MACCATALYST
  46. archiveData = [NSKeyedArchiver archivedDataWithRootObject:event];
  47. #endif
  48. }
  49. // To ensure that all the objects being retained by the original event are dealloc'd.
  50. event = nil;
  51. GDTCOREvent *decodedEvent;
  52. if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
  53. decodedEvent = [NSKeyedUnarchiver unarchivedObjectOfClass:[GDTCOREvent class]
  54. fromData:archiveData
  55. error:nil];
  56. } else {
  57. #if !TARGET_OS_MACCATALYST
  58. decodedEvent = [NSKeyedUnarchiver unarchiveObjectWithData:archiveData];
  59. #endif
  60. }
  61. XCTAssertEqualObjects(decodedEvent.mappingID, @"testID");
  62. XCTAssertEqual(decodedEvent.target, 42);
  63. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  64. XCTAssertEqual(decodedEvent.qosTier, GDTCOREventQoSTelemetry);
  65. XCTAssertEqual(decodedEvent.clockSnapshot.timeMillis, timeMillis);
  66. XCTAssertEqual(decodedEvent.clockSnapshot.timezoneOffsetSeconds, timezoneOffsetSeconds);
  67. }
  68. /** Tests setting variables on a GDTCOREvent instance.*/
  69. - (void)testSettingVariables {
  70. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  71. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testing" target:1];
  72. event.clockSnapshot = [GDTCORClock snapshot];
  73. event.qosTier = GDTCOREventQoSTelemetry;
  74. XCTAssertNotNil(event);
  75. XCTAssertNotNil(event.mappingID);
  76. XCTAssertNotNil(@(event.target));
  77. XCTAssertEqual(event.qosTier, GDTCOREventQoSTelemetry);
  78. XCTAssertNotNil(event.clockSnapshot);
  79. XCTAssertNil(event.customPrioritizationParams);
  80. }
  81. /** Tests equality between GDTCOREvents. */
  82. - (void)testIsEqualAndHash {
  83. GDTCOREvent *event1 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:1];
  84. event1.clockSnapshot = [GDTCORClock snapshot];
  85. [event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  86. [event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  87. [event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
  88. [event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
  89. event1.qosTier = GDTCOREventQosDefault;
  90. event1.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
  91. NSError *error1;
  92. [event1 writeToURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"] error:&error1];
  93. XCTAssertNil(error1);
  94. GDTCOREvent *event2 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:1];
  95. event2.clockSnapshot = [GDTCORClock snapshot];
  96. [event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  97. [event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  98. [event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
  99. [event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
  100. event2.qosTier = GDTCOREventQosDefault;
  101. event2.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
  102. NSError *error2;
  103. [event2 writeToURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"] error:&error2];
  104. XCTAssertNil(error2);
  105. XCTAssertEqual([event1 hash], [event2 hash]);
  106. XCTAssertEqualObjects(event1, event2);
  107. // This only really tests that changing the timezoneOffsetSeconds value causes a change in hash.
  108. [event2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"];
  109. XCTAssertNotEqual([event1 hash], [event2 hash]);
  110. XCTAssertNotEqualObjects(event1, event2);
  111. }
  112. @end