FIRCLSReportUploaderTests.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. NSString *const TestFIID = @"TestFIID";
  33. @interface FIRCLSReportUploaderTests : XCTestCase
  34. @property(nonatomic, strong) FIRCLSReportUploader *uploader;
  35. @property(nonatomic, strong) FIRCLSTempMockFileManager *fileManager;
  36. @property(nonatomic, strong) NSOperationQueue *queue;
  37. @property(nonatomic, strong) FIRCLSManagerData *managerData;
  38. // Add mock prefix to names as there are naming conflicts with FIRCLSReportUploaderDelegate
  39. @property(nonatomic, strong) FIRMockGDTCORTransport *mockDataTransport;
  40. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  41. @property(nonatomic, strong) FIRMockInstallations *mockInstallations;
  42. @property(nonatomic, strong) FIRCLSDataCollectionArbiter *dataArbiter;
  43. @end
  44. @implementation FIRCLSReportUploaderTests
  45. - (void)setUp {
  46. [super setUp];
  47. FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
  48. self.queue = [NSOperationQueue new];
  49. self.mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  50. appIDModel:appIDModel];
  51. self.mockDataTransport = [[FIRMockGDTCORTransport alloc] initWithMappingID:@"1206"
  52. transformers:nil
  53. target:kGDTCORTargetCSH];
  54. self.mockDataTransport.sendDataEvent_wasWritten = YES;
  55. self.fileManager = [[FIRCLSTempMockFileManager alloc] init];
  56. id fakeApp = [[FIRAppFake alloc] init];
  57. self.dataArbiter = [[FIRCLSDataCollectionArbiter alloc] initWithApp:fakeApp withAppInfo:@{}];
  58. self.mockInstallations = [[FIRMockInstallations alloc] initWithFID:TestFIID];
  59. [self setupUploaderWithInstallations:self.mockInstallations];
  60. }
  61. - (void)tearDown {
  62. self.uploader = nil;
  63. [super tearDown];
  64. }
  65. - (void)setupUploaderWithInstallations:(FIRMockInstallations *)installations {
  66. // Allow nil values only in tests
  67. #pragma clang diagnostic push
  68. #pragma clang diagnostic ignored "-Wnonnull"
  69. self.managerData = [[FIRCLSManagerData alloc] initWithGoogleAppID:@"someGoogleAppId"
  70. googleTransport:self.mockDataTransport
  71. installations:installations
  72. analytics:nil
  73. fileManager:self.fileManager
  74. dataArbiter:self.dataArbiter
  75. settings:self.mockSettings
  76. onDemandModel:nil];
  77. #pragma clang diagnostic pop
  78. self.uploader = [[FIRCLSReportUploader alloc] initWithManagerData:self.managerData];
  79. }
  80. #pragma mark - Tests
  81. - (void)testPrepareReport {
  82. NSString *path = [self.fileManager.activePath stringByAppendingPathComponent:@"pkg_uuid"];
  83. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:path];
  84. self.fileManager.moveItemAtPathResult = [NSNumber numberWithInt:1];
  85. XCTAssertNil(self.uploader.fiid);
  86. [self.uploader prepareAndSubmitReport:report
  87. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  88. asUrgent:YES
  89. withProcessing:YES];
  90. XCTAssertEqual(self.uploader.fiid, TestFIID);
  91. // Verify with the last move operation is from processing -> prepared
  92. XCTAssertTrue(
  93. [self.fileManager.moveItemAtPath_destDir containsString:self.fileManager.preparedPath]);
  94. }
  95. - (void)test_NilFIID_DoesNotCrash {
  96. NSString *path = [self.fileManager.activePath stringByAppendingPathComponent:@"pkg_uuid"];
  97. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:path];
  98. self.fileManager.moveItemAtPathResult = [NSNumber numberWithInt:1];
  99. self.mockInstallations = [[FIRMockInstallations alloc]
  100. initWithError:[NSError errorWithDomain:@"TestDomain" code:-1 userInfo:nil]];
  101. [self setupUploaderWithInstallations:self.mockInstallations];
  102. XCTAssertNil(self.uploader.fiid);
  103. [self.uploader prepareAndSubmitReport:report
  104. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  105. asUrgent:YES
  106. withProcessing:YES];
  107. XCTAssertNil(self.uploader.fiid);
  108. }
  109. - (void)testUploadPackagedReportWithPath {
  110. [self runUploadPackagedReportWithUrgency:NO];
  111. }
  112. - (void)testUrgentUploadPackagedReportWithPath {
  113. [self runUploadPackagedReportWithUrgency:YES];
  114. }
  115. - (void)testUrgentWaitUntillUpload {
  116. self.mockDataTransport.async = YES;
  117. [self runUploadPackagedReportWithUrgency:YES];
  118. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  119. }
  120. - (void)testUrgentWaitUntillUploadWithError {
  121. self.mockDataTransport.async = YES;
  122. self.mockDataTransport.sendDataEvent_error = [[NSError alloc] initWithDomain:@"domain"
  123. code:1
  124. userInfo:nil];
  125. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  126. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  127. asUrgent:YES];
  128. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  129. }
  130. - (void)testUrgentWaitUntillUploadWithWritingError {
  131. self.mockDataTransport.async = YES;
  132. self.mockDataTransport.sendDataEvent_wasWritten = NO;
  133. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  134. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  135. asUrgent:YES];
  136. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  137. }
  138. - (void)testUploadPackagedReportWithoutDataCollectionToken {
  139. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  140. // Ensure we don't hand off an event to GDT
  141. XCTAssertNil(self.mockDataTransport.sendDataEvent_event);
  142. }
  143. - (void)testUploadPackagedReportNotGDTWritten {
  144. self.mockDataTransport.sendDataEvent_wasWritten = NO;
  145. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  146. // Did not delete report
  147. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  148. }
  149. - (void)testUploadPackagedReportGDTError {
  150. self.mockDataTransport.sendDataEvent_error = [[NSError alloc] initWithDomain:@"domain"
  151. code:1
  152. userInfo:nil];
  153. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  154. // Did not delete report
  155. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  156. }
  157. #pragma mark - Helper functions
  158. - (NSString *)packagePath {
  159. return [self.fileManager.preparedPath stringByAppendingPathComponent:@"pkg_uuid"];
  160. }
  161. - (void)runUploadPackagedReportWithUrgency:(BOOL)urgent {
  162. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  163. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  164. asUrgent:urgent];
  165. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  166. XCTAssertEqualObjects(self.fileManager.removedItemAtPath_path, [self packagePath]);
  167. }
  168. @end