GDTCOREventTest.m 6.0 KB

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