FIRCLSReportUploaderTests.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
  16. #import "Crashlytics/Crashlytics/Controllers/FIRCLSReportUploader_Private.h"
  17. #import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
  18. #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
  19. #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
  20. #import "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
  21. #import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
  22. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  23. #import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
  24. #import "Crashlytics/Shared/FIRCLSConstants.h"
  25. #import "Crashlytics/UnitTests/Mocks/FABMockApplicationIdentifierModel.h"
  26. #import "Crashlytics/UnitTests/Mocks/FIRAppFake.h"
  27. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockSettings.h"
  28. #import "Crashlytics/UnitTests/Mocks/FIRCLSTempMockFileManager.h"
  29. #import "Crashlytics/UnitTests/Mocks/FIRMockGDTCoreTransport.h"
  30. #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
  31. NSString *const TestEndpoint = @"https://reports.crashlytics.com";
  32. @interface FIRCLSReportUploaderTests : XCTestCase
  33. @property(nonatomic, strong) FIRCLSReportUploader *uploader;
  34. @property(nonatomic, strong) FIRCLSTempMockFileManager *fileManager;
  35. @property(nonatomic, strong) NSOperationQueue *queue;
  36. @property(nonatomic, strong) FIRCLSManagerData *managerData;
  37. // Add mock prefix to names as there are naming conflicts with FIRCLSReportUploaderDelegate
  38. @property(nonatomic, strong) FIRMockGDTCORTransport *mockDataTransport;
  39. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  40. @end
  41. @implementation FIRCLSReportUploaderTests
  42. - (void)setUp {
  43. [super setUp];
  44. FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
  45. self.queue = [NSOperationQueue new];
  46. self.mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  47. appIDModel:appIDModel];
  48. self.mockDataTransport = [[FIRMockGDTCORTransport alloc] initWithMappingID:@"1206"
  49. transformers:nil
  50. target:kGDTCORTargetCSH];
  51. self.fileManager = [[FIRCLSTempMockFileManager alloc] init];
  52. id fakeApp = [[FIRAppFake alloc] init];
  53. FIRCLSDataCollectionArbiter *dataArbiter =
  54. [[FIRCLSDataCollectionArbiter alloc] initWithApp:fakeApp withAppInfo:@{}];
  55. FIRMockInstallations *mockInstallations =
  56. [[FIRMockInstallations alloc] initWithFID:@"test_token"];
  57. // Allow nil values only in tests
  58. #pragma clang diagnostic push
  59. #pragma clang diagnostic ignored "-Wnonnull"
  60. self.managerData = [[FIRCLSManagerData alloc] initWithGoogleAppID:@"someGoogleAppId"
  61. googleTransport:self.mockDataTransport
  62. installations:mockInstallations
  63. analytics:nil
  64. fileManager:self.fileManager
  65. dataArbiter:dataArbiter
  66. settings:self.mockSettings
  67. onDemandModel:nil];
  68. #pragma clang diagnostic pop
  69. self.uploader = [[FIRCLSReportUploader alloc] initWithManagerData:_managerData];
  70. }
  71. - (void)tearDown {
  72. self.uploader = nil;
  73. [super tearDown];
  74. }
  75. #pragma mark - Tests
  76. - (void)testPrepareReport {
  77. NSString *path = [self.fileManager.activePath stringByAppendingPathComponent:@"pkg_uuid"];
  78. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:path];
  79. self.fileManager.moveItemAtPathResult = [NSNumber numberWithInt:1];
  80. [self.uploader prepareAndSubmitReport:report
  81. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  82. asUrgent:YES
  83. withProcessing:YES];
  84. // Verify with the last move operation is from processing -> prepared
  85. XCTAssertTrue(
  86. [self.fileManager.moveItemAtPath_destDir containsString:self.fileManager.preparedPath]);
  87. }
  88. - (void)testUploadPackagedReportWithPath {
  89. [self runUploadPackagedReportWithUrgency:NO];
  90. }
  91. - (void)testUrgentUploadPackagedReportWithPath {
  92. [self runUploadPackagedReportWithUrgency:YES];
  93. }
  94. - (void)testUploadPackagedReportWithoutDataCollectionToken {
  95. [self setUpForUpload];
  96. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  97. // Ensure we don't hand off an event to GDT
  98. XCTAssertNil(self.mockDataTransport.sendDataEvent_event);
  99. }
  100. - (void)testUploadPackagedReportNotGDTWritten {
  101. [self setUpForUpload];
  102. self.mockDataTransport.sendDataEvent_wasWritten = NO;
  103. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  104. // Did not delete report
  105. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  106. }
  107. - (void)testUploadPackagedReportGDTError {
  108. [self setUpForUpload];
  109. self.mockDataTransport.sendDataEvent_error = [[NSError alloc] initWithDomain:@"domain"
  110. code:1
  111. userInfo:nil];
  112. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  113. // Did not delete report
  114. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  115. }
  116. #pragma mark - Helper functions
  117. - (NSString *)packagePath {
  118. return [self.fileManager.preparedPath stringByAppendingPathComponent:@"pkg_uuid"];
  119. }
  120. - (void)runUploadPackagedReportWithUrgency:(BOOL)urgent {
  121. [self setUpForUpload];
  122. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  123. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  124. asUrgent:urgent];
  125. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  126. XCTAssertEqualObjects(self.fileManager.removedItemAtPath_path, [self packagePath]);
  127. }
  128. - (void)setUpForUpload {
  129. self.mockDataTransport.sendDataEvent_wasWritten = YES;
  130. }
  131. @end