FIRCLSReportUploaderTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. [FIRApp resetApps];
  64. [super tearDown];
  65. }
  66. - (void)setupUploaderWithInstallations:(FIRInstallations *)installations {
  67. // Allow nil values only in tests
  68. #pragma clang diagnostic push
  69. #pragma clang diagnostic ignored "-Wnonnull"
  70. self.managerData = [[FIRCLSManagerData alloc] initWithGoogleAppID:@"someGoogleAppId"
  71. googleTransport:self.mockDataTransport
  72. installations:installations
  73. analytics:nil
  74. fileManager:self.fileManager
  75. dataArbiter:self.dataArbiter
  76. settings:self.mockSettings
  77. onDemandModel:nil];
  78. #pragma clang diagnostic pop
  79. self.uploader = [[FIRCLSReportUploader alloc] initWithManagerData:self.managerData];
  80. }
  81. - (NSString *)resourcePath {
  82. #if SWIFT_PACKAGE
  83. NSBundle *bundle = SWIFTPM_MODULE_BUNDLE;
  84. return [bundle.resourcePath stringByAppendingPathComponent:@"Data"];
  85. #else
  86. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  87. return bundle.resourcePath;
  88. #endif
  89. }
  90. - (FIRCLSInternalReport *)createReport {
  91. NSString *path = [self.fileManager.activePath stringByAppendingPathComponent:@"pkg_uuid"];
  92. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:path];
  93. self.fileManager.moveItemAtPathResult = [NSNumber numberWithInt:1];
  94. return report;
  95. }
  96. #pragma mark - Tests
  97. - (void)testPrepareReport {
  98. FIRCLSInternalReport *report = [self createReport];
  99. XCTAssertNil(self.uploader.fiid);
  100. [self.uploader prepareAndSubmitReport:report
  101. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  102. asUrgent:NO
  103. withProcessing:YES];
  104. XCTAssertEqual(self.uploader.fiid, TestFIID);
  105. // Verify with the last move operation is from processing -> prepared
  106. XCTAssertTrue(
  107. [self.fileManager.moveItemAtPath_destDir containsString:self.fileManager.preparedPath]);
  108. }
  109. - (void)testPrepareReportOnMainThread {
  110. NSString *pathToPlist =
  111. [[self resourcePath] stringByAppendingPathComponent:@"GoogleService-Info.plist"];
  112. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:pathToPlist];
  113. [FIRApp configureWithName:@"__FIRAPP_DEFAULT" options:options];
  114. XCTAssertNotNil([FIRApp defaultApp], @"configureWithName must have been initialized");
  115. FIRInstallations *installations = [FIRInstallations installationsWithApp:[FIRApp defaultApp]];
  116. FIRCLSInternalReport *report = [self createReport];
  117. [self setupUploaderWithInstallations:installations];
  118. /*
  119. if a report is urgent report will be processed on the Main Thread
  120. otherwise, it will be dispatched to a NSOperationQueue (see `FIRCLSExistingReportManager.m:230`)
  121. This test checks if `prepareAndSubmitReport` finishes in a reasonable time.
  122. */
  123. NSOperationQueue *queue = [NSOperationQueue new];
  124. // target call will block the main thread, so we need a background thread
  125. // that will wait on a semaphore for a timeout
  126. dispatch_semaphore_t backgroundWaiter = dispatch_semaphore_create(0);
  127. [queue addOperationWithBlock:^{
  128. intptr_t result = dispatch_semaphore_wait(backgroundWaiter,
  129. dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC));
  130. BOOL exitBecauseOfTimeout = result != 0;
  131. XCTAssertFalse(exitBecauseOfTimeout, @"Main Thread was blocked for more than 1 second");
  132. }];
  133. // Urgent (on the Main thread)
  134. [self.uploader prepareAndSubmitReport:report
  135. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  136. asUrgent:YES
  137. withProcessing:YES];
  138. dispatch_semaphore_signal(backgroundWaiter);
  139. // Not urgent (on a background thread)
  140. XCTestExpectation *expectation =
  141. [self expectationWithDescription:@"wait for a preparation to complete"];
  142. [queue addOperationWithBlock:^{
  143. [self.uploader prepareAndSubmitReport:report
  144. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  145. asUrgent:YES
  146. withProcessing:YES];
  147. [expectation fulfill];
  148. }];
  149. [self waitForExpectationsWithTimeout:1
  150. handler:^(NSError *_Nullable error) {
  151. XCTAssertNil(error, @"expectations failed: %@", error);
  152. }];
  153. }
  154. - (void)test_NilFIID_DoesNotCrash {
  155. FIRCLSInternalReport *report = [self createReport];
  156. self.mockInstallations = [[FIRMockInstallations alloc]
  157. initWithError:[NSError errorWithDomain:@"TestDomain" code:-1 userInfo:nil]];
  158. [self setupUploaderWithInstallations:self.mockInstallations];
  159. XCTAssertNil(self.uploader.fiid);
  160. [self.uploader prepareAndSubmitReport:report
  161. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  162. asUrgent:YES
  163. withProcessing:YES];
  164. XCTAssertNil(self.uploader.fiid);
  165. }
  166. - (void)testUploadPackagedReportWithPath {
  167. [self runUploadPackagedReportWithUrgency:NO];
  168. }
  169. - (void)testUrgentUploadPackagedReportWithPath {
  170. [self runUploadPackagedReportWithUrgency:YES];
  171. }
  172. - (void)testUrgentWaitUntillUpload {
  173. self.mockDataTransport.async = YES;
  174. [self runUploadPackagedReportWithUrgency:YES];
  175. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  176. }
  177. - (void)testUrgentWaitUntillUploadWithError {
  178. self.mockDataTransport.async = YES;
  179. self.mockDataTransport.sendDataEvent_error = [[NSError alloc] initWithDomain:@"domain"
  180. code:1
  181. userInfo:nil];
  182. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  183. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  184. asUrgent:YES];
  185. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  186. }
  187. - (void)testUrgentWaitUntillUploadWithWritingError {
  188. self.mockDataTransport.async = YES;
  189. self.mockDataTransport.sendDataEvent_wasWritten = NO;
  190. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  191. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  192. asUrgent:YES];
  193. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  194. }
  195. - (void)testUploadPackagedReportWithoutDataCollectionToken {
  196. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  197. // Ensure we don't hand off an event to GDT
  198. XCTAssertNil(self.mockDataTransport.sendDataEvent_event);
  199. }
  200. - (void)testUploadPackagedReportNotGDTWritten {
  201. self.mockDataTransport.sendDataEvent_wasWritten = NO;
  202. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  203. // Did not delete report
  204. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  205. }
  206. - (void)testUploadPackagedReportGDTError {
  207. self.mockDataTransport.sendDataEvent_error = [[NSError alloc] initWithDomain:@"domain"
  208. code:1
  209. userInfo:nil];
  210. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  211. // Did not delete report
  212. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  213. }
  214. #pragma mark - Helper functions
  215. - (NSString *)packagePath {
  216. return [self.fileManager.preparedPath stringByAppendingPathComponent:@"pkg_uuid"];
  217. }
  218. - (void)runUploadPackagedReportWithUrgency:(BOOL)urgent {
  219. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  220. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  221. asUrgent:urgent];
  222. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  223. XCTAssertEqualObjects(self.fileManager.removedItemAtPath_path, [self packagePath]);
  224. }
  225. @end