GDTCOREventTest.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
  17. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORTargets.h"
  18. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORClock.h"
  19. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORPlatform.h"
  20. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORRegistrar.h"
  21. #import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORStorageProtocol.h"
  22. #import "GoogleDataTransport/GDTCORTests/Unit/GDTCORTestCase.h"
  23. #import "GoogleDataTransport/GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
  24. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCOREvent_Private.h"
  25. #import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORFlatFileStorage.h"
  26. @interface GDTCOREventTest : GDTCORTestCase
  27. @end
  28. @implementation GDTCOREventTest
  29. /** Tests the designated initializer. */
  30. - (void)testInit {
  31. XCTAssertNotNil([[GDTCOREvent alloc] initWithMappingID:@"1" target:kGDTCORTargetTest].eventID);
  32. XCTAssertNotNil([[GDTCOREvent alloc] initWithMappingID:@"1" target:kGDTCORTargetTest]);
  33. XCTAssertNil([[GDTCOREvent alloc] initWithMappingID:@"" target:kGDTCORTargetTest]);
  34. }
  35. /** Tests NSKeyedArchiver encoding and decoding. */
  36. - (void)testArchiving {
  37. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  38. GDTCORClock *clockSnapshot = [GDTCORClock snapshot];
  39. int64_t timeMillis = clockSnapshot.timeMillis;
  40. int64_t timezoneOffsetSeconds = clockSnapshot.timezoneOffsetSeconds;
  41. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testID" target:kGDTCORTargetTest];
  42. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  43. event.qosTier = GDTCOREventQoSTelemetry;
  44. event.clockSnapshot = clockSnapshot;
  45. NSError *error;
  46. NSData *archiveData = GDTCOREncodeArchive(event, nil, &error);
  47. XCTAssertNil(error);
  48. XCTAssertNotNil(archiveData);
  49. // To ensure that all the objects being retained by the original event are dealloc'd.
  50. event = nil;
  51. error = nil;
  52. GDTCOREvent *decodedEvent =
  53. (GDTCOREvent *)GDTCORDecodeArchive([GDTCOREvent class], nil, archiveData, &error);
  54. XCTAssertNil(error);
  55. XCTAssertNotNil(decodedEvent);
  56. XCTAssertEqualObjects(decodedEvent.mappingID, @"testID");
  57. XCTAssertEqual(decodedEvent.target, kGDTCORTargetTest);
  58. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  59. XCTAssertEqual(decodedEvent.qosTier, GDTCOREventQoSTelemetry);
  60. XCTAssertEqual(decodedEvent.clockSnapshot.timeMillis, timeMillis);
  61. XCTAssertEqual(decodedEvent.clockSnapshot.timezoneOffsetSeconds, timezoneOffsetSeconds);
  62. }
  63. /** Tests setting variables on a GDTCOREvent instance.*/
  64. - (void)testSettingVariables {
  65. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  66. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testing" target:kGDTCORTargetTest];
  67. event.clockSnapshot = [GDTCORClock snapshot];
  68. event.qosTier = GDTCOREventQoSTelemetry;
  69. XCTAssertNotNil(event);
  70. XCTAssertNotNil(event.mappingID);
  71. XCTAssertNotNil(@(event.target));
  72. XCTAssertEqual(event.qosTier, GDTCOREventQoSTelemetry);
  73. XCTAssertNotNil(event.clockSnapshot);
  74. XCTAssertNil(event.customBytes);
  75. }
  76. /** Tests equality between GDTCOREvents. */
  77. - (void)testIsEqualAndHash {
  78. GDTCOREvent *event1 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:kGDTCORTargetTest];
  79. event1.eventID = @"123";
  80. event1.clockSnapshot = [GDTCORClock snapshot];
  81. [event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  82. [event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  83. [event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTimeNanoseconds"];
  84. [event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptimeNanoseconds"];
  85. event1.qosTier = GDTCOREventQosDefault;
  86. event1.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  87. NSError *error1;
  88. event1.customBytes = [NSJSONSerialization dataWithJSONObject:@{@"customParam1" : @"aValue1"}
  89. options:0
  90. error:&error1];
  91. XCTAssertNil(error1);
  92. GDTCOREvent *event2 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:kGDTCORTargetTest];
  93. event2.eventID = @"123";
  94. event2.clockSnapshot = [GDTCORClock snapshot];
  95. [event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  96. [event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  97. [event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTimeNanoseconds"];
  98. [event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptimeNanoseconds"];
  99. event2.qosTier = GDTCOREventQosDefault;
  100. event2.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  101. NSError *error2;
  102. event2.customBytes = [NSJSONSerialization dataWithJSONObject:@{@"customParam1" : @"aValue1"}
  103. options:0
  104. error:&error2];
  105. XCTAssertNil(error2);
  106. XCTAssertEqual([event1 hash], [event2 hash]);
  107. XCTAssertEqualObjects(event1, event2);
  108. // This only really tests that changing the timezoneOffsetSeconds value causes a change in hash.
  109. [event2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"];
  110. XCTAssertNotEqual([event1 hash], [event2 hash]);
  111. XCTAssertNotEqualObjects(event1, event2);
  112. }
  113. /** Tests generating event IDs. */
  114. - (void)testGenerateEventIDs {
  115. BOOL originalContinueAfterFailureValue = self.continueAfterFailure;
  116. self.continueAfterFailure = NO;
  117. NSMutableSet *generatedValues = [[NSMutableSet alloc] init];
  118. for (int i = 0; i < 100000; i++) {
  119. NSString *eventID = [GDTCOREvent nextEventID];
  120. XCTAssertNotNil(eventID);
  121. XCTAssertFalse([generatedValues containsObject:eventID]);
  122. [generatedValues addObject:eventID];
  123. }
  124. self.continueAfterFailure = originalContinueAfterFailureValue;
  125. }
  126. @end