GDTCOREventTest.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/GDTCORClock.h>
  18. #import <GoogleDataTransport/GDTCORPlatform.h>
  19. #import "GDTCORTests/Unit/GDTCORTestCase.h"
  20. #import "GDTCORTests/Unit/Helpers/GDTCORDataObjectTesterClasses.h"
  21. #import "GDTCORLibrary/Private/GDTCOREvent_Private.h"
  22. @interface GDTCOREventTest : GDTCORTestCase
  23. @end
  24. @implementation GDTCOREventTest
  25. /** Tests the designated initializer. */
  26. - (void)testInit {
  27. XCTAssertNotNil([[GDTCOREvent alloc] initWithMappingID:@"1" target:1]);
  28. XCTAssertNil([[GDTCOREvent alloc] initWithMappingID:@"" target:1]);
  29. }
  30. /** Tests NSKeyedArchiver encoding and decoding. */
  31. - (void)testArchiving {
  32. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  33. GDTCORClock *clockSnapshot = [GDTCORClock snapshot];
  34. int64_t timeMillis = clockSnapshot.timeMillis;
  35. int64_t timezoneOffsetSeconds = clockSnapshot.timezoneOffsetSeconds;
  36. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testID" target:42];
  37. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  38. event.qosTier = GDTCOREventQoSTelemetry;
  39. event.clockSnapshot = clockSnapshot;
  40. NSError *error;
  41. NSData *archiveData = GDTCOREncodeArchive(event, nil, &error);
  42. XCTAssertNil(error);
  43. XCTAssertNotNil(archiveData);
  44. // To ensure that all the objects being retained by the original event are dealloc'd.
  45. event = nil;
  46. error = nil;
  47. GDTCOREvent *decodedEvent =
  48. (GDTCOREvent *)GDTCORDecodeArchive([GDTCOREvent class], nil, archiveData, &error);
  49. XCTAssertNil(error);
  50. XCTAssertNotNil(decodedEvent);
  51. XCTAssertEqualObjects(decodedEvent.mappingID, @"testID");
  52. XCTAssertEqual(decodedEvent.target, 42);
  53. event.dataObject = [[GDTCORDataObjectTesterSimple alloc] initWithString:@"someData"];
  54. XCTAssertEqual(decodedEvent.qosTier, GDTCOREventQoSTelemetry);
  55. XCTAssertEqual(decodedEvent.clockSnapshot.timeMillis, timeMillis);
  56. XCTAssertEqual(decodedEvent.clockSnapshot.timezoneOffsetSeconds, timezoneOffsetSeconds);
  57. }
  58. /** Tests setting variables on a GDTCOREvent instance.*/
  59. - (void)testSettingVariables {
  60. XCTAssertTrue([GDTCOREvent supportsSecureCoding]);
  61. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testing" target:1];
  62. event.clockSnapshot = [GDTCORClock snapshot];
  63. event.qosTier = GDTCOREventQoSTelemetry;
  64. XCTAssertNotNil(event);
  65. XCTAssertNotNil(event.mappingID);
  66. XCTAssertNotNil(@(event.target));
  67. XCTAssertEqual(event.qosTier, GDTCOREventQoSTelemetry);
  68. XCTAssertNotNil(event.clockSnapshot);
  69. XCTAssertNil(event.customPrioritizationParams);
  70. }
  71. /** Tests equality between GDTCOREvents. */
  72. - (void)testIsEqualAndHash {
  73. GDTCOREvent *event1 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:1];
  74. event1.clockSnapshot = [GDTCORClock snapshot];
  75. [event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  76. [event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  77. [event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
  78. [event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
  79. event1.qosTier = GDTCOREventQosDefault;
  80. event1.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
  81. NSError *error1;
  82. [event1 writeToGDTPath:@"/tmp/fake.txt" error:&error1];
  83. XCTAssertNil(error1);
  84. GDTCOREvent *event2 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:1];
  85. event2.clockSnapshot = [GDTCORClock snapshot];
  86. [event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  87. [event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  88. [event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
  89. [event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
  90. event2.qosTier = GDTCOREventQosDefault;
  91. event2.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
  92. NSError *error2;
  93. [event2 writeToGDTPath:@"/tmp/fake.txt" error:&error2];
  94. XCTAssertNil(error2);
  95. XCTAssertEqual([event1 hash], [event2 hash]);
  96. XCTAssertEqualObjects(event1, event2);
  97. // This only really tests that changing the timezoneOffsetSeconds value causes a change in hash.
  98. [event2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"];
  99. XCTAssertNotEqual([event1 hash], [event2 hash]);
  100. XCTAssertNotEqualObjects(event1, event2);
  101. }
  102. @end