GDTCORStoredEventTest.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 2019 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 "GDTCORTests/Unit/GDTCORTestCase.h"
  17. #import <GoogleDataTransport/GDTCORClock.h>
  18. #import <GoogleDataTransport/GDTCORStoredEvent.h>
  19. @interface GDTCORStoredEventTest : GDTCORTestCase
  20. @end
  21. @implementation GDTCORStoredEventTest
  22. /** Tests the default initializer. */
  23. - (void)testInit {
  24. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testing" target:1];
  25. event.clockSnapshot = [GDTCORClock snapshot];
  26. GDTCORDataFuture *dataFuture =
  27. [[GDTCORDataFuture alloc] initWithFileURL:[NSURL URLWithString:@"1"]];
  28. GDTCORStoredEvent *storedEvent = [[GDTCORStoredEvent alloc] initWithEvent:event
  29. dataFuture:dataFuture];
  30. XCTAssertNotNil(storedEvent);
  31. }
  32. /** Tests encoding and decoding. */
  33. - (void)testNSSecureCoding {
  34. XCTAssertTrue([GDTCORStoredEvent supportsSecureCoding]);
  35. GDTCOREvent *event = [[GDTCOREvent alloc] initWithMappingID:@"testing" target:1];
  36. event.clockSnapshot = [GDTCORClock snapshot];
  37. event.qosTier = GDTCOREventQoSTelemetry;
  38. GDTCORDataFuture *dataFuture =
  39. [[GDTCORDataFuture alloc] initWithFileURL:[NSURL URLWithString:@"1"]];
  40. GDTCORStoredEvent *storedEvent = [[GDTCORStoredEvent alloc] initWithEvent:event
  41. dataFuture:dataFuture];
  42. XCTAssertNotNil(storedEvent);
  43. XCTAssertNotNil(storedEvent.mappingID);
  44. XCTAssertNotNil(storedEvent.target);
  45. XCTAssertEqual(storedEvent.qosTier, GDTCOREventQoSTelemetry);
  46. XCTAssertNotNil(storedEvent.clockSnapshot);
  47. XCTAssertNil(storedEvent.customPrioritizationParams);
  48. XCTAssertNotNil(storedEvent.dataFuture.fileURL);
  49. }
  50. /** Tests equality between GDTCORStoredEvents. */
  51. - (void)testIsEqualAndHash {
  52. GDTCOREvent *event1 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:1];
  53. event1.clockSnapshot = [GDTCORClock snapshot];
  54. [event1.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  55. [event1.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  56. [event1.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
  57. [event1.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
  58. event1.qosTier = GDTCOREventQosDefault;
  59. event1.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
  60. GDTCORDataFuture *dataFuture1 =
  61. [[GDTCORDataFuture alloc] initWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]];
  62. GDTCORStoredEvent *storedEvent1 = [[GDTCORStoredEvent alloc] initWithEvent:event1
  63. dataFuture:dataFuture1];
  64. GDTCOREvent *event2 = [[GDTCOREvent alloc] initWithMappingID:@"1018" target:1];
  65. event2.clockSnapshot = [GDTCORClock snapshot];
  66. [event2.clockSnapshot setValue:@(1553534573010) forKeyPath:@"timeMillis"];
  67. [event2.clockSnapshot setValue:@(-25200) forKeyPath:@"timezoneOffsetSeconds"];
  68. [event2.clockSnapshot setValue:@(1552576634359451) forKeyPath:@"kernelBootTime"];
  69. [event2.clockSnapshot setValue:@(961141365197) forKeyPath:@"uptime"];
  70. event2.qosTier = GDTCOREventQosDefault;
  71. event2.customPrioritizationParams = @{@"customParam1" : @"aValue1"};
  72. GDTCORDataFuture *dataFuture2 =
  73. [[GDTCORDataFuture alloc] initWithFileURL:[NSURL fileURLWithPath:@"/tmp/fake.txt"]];
  74. GDTCORStoredEvent *storedEvent2 = [[GDTCORStoredEvent alloc] initWithEvent:event2
  75. dataFuture:dataFuture2];
  76. XCTAssertEqual([storedEvent1 hash], [storedEvent2 hash]);
  77. XCTAssertEqualObjects(storedEvent1, storedEvent2);
  78. // This only really tests that changing the timezoneOffsetSeconds value causes a change in hash.
  79. [storedEvent2.clockSnapshot setValue:@(-25201) forKeyPath:@"timezoneOffsetSeconds"];
  80. XCTAssertNotEqual([storedEvent1 hash], [storedEvent2 hash]);
  81. XCTAssertNotEqualObjects(storedEvent1, storedEvent2);
  82. }
  83. @end