FIRCLSOnDemandModelTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2022 Google LLC
  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. #include "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
  16. #import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
  17. #import "Crashlytics/Crashlytics/Models/FIRCLSExecutionIdentifierModel.h"
  18. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  19. #import "Crashlytics/Crashlytics/Private/FIRCLSOnDemandModel_Private.h"
  20. #import "Crashlytics/UnitTests/Mocks/FIRAppFake.h"
  21. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockExistingReportManager.h"
  22. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h"
  23. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockOnDemandModel.h"
  24. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockReportUploader.h"
  25. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockSettings.h"
  26. #import "Crashlytics/UnitTests/Mocks/FIRMockGDTCoreTransport.h"
  27. #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
  28. #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
  29. #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
  30. #import "Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.h"
  31. #define TEST_GOOGLE_APP_ID (@"1:632950151350:ios:d5b0d08d4f00f4b1")
  32. @interface FIRCLSOnDemandModelTests : XCTestCase
  33. @property(nonatomic, retain) FIRCLSMockOnDemandModel *onDemandModel;
  34. @property(nonatomic, strong) FIRCLSExistingReportManager *existingReportManager;
  35. @property(nonatomic, strong) FIRCLSManagerData *managerData;
  36. @property(nonatomic, strong) FIRCLSDataCollectionArbiter *dataArbiter;
  37. @property(nonatomic, strong) FIRCLSMockFileManager *fileManager;
  38. @property(nonatomic, strong) FIRCLSMockReportUploader *mockReportUploader;
  39. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  40. @end
  41. @implementation FIRCLSOnDemandModelTests
  42. - (void)setUp {
  43. [super setUp];
  44. FIRSetLoggerLevel(FIRLoggerLevelMax);
  45. FIRCLSContextBaseInit();
  46. id fakeApp = [[FIRAppFake alloc] init];
  47. self.dataArbiter = [[FIRCLSDataCollectionArbiter alloc] initWithApp:fakeApp withAppInfo:@{}];
  48. self.fileManager = [[FIRCLSMockFileManager alloc] init];
  49. FIRCLSApplicationIdentifierModel *appIDModel = [[FIRCLSApplicationIdentifierModel alloc] init];
  50. _mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  51. appIDModel:appIDModel];
  52. _onDemandModel = [[FIRCLSMockOnDemandModel alloc] initWithFIRCLSSettings:_mockSettings
  53. fileManager:_fileManager
  54. sleepBlock:^(int delay){
  55. }];
  56. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_token"];
  57. FIRMockGDTCORTransport *mockGoogleTransport =
  58. [[FIRMockGDTCORTransport alloc] initWithMappingID:@"id" transformers:nil target:0];
  59. _managerData = [[FIRCLSManagerData alloc] initWithGoogleAppID:TEST_GOOGLE_APP_ID
  60. googleTransport:mockGoogleTransport
  61. installations:iid
  62. analytics:nil
  63. fileManager:self.fileManager
  64. dataArbiter:self.dataArbiter
  65. settings:self.mockSettings
  66. onDemandModel:_onDemandModel];
  67. _mockReportUploader = [[FIRCLSMockReportUploader alloc] initWithManagerData:self.managerData];
  68. _existingReportManager =
  69. [[FIRCLSExistingReportManager alloc] initWithManagerData:self.managerData
  70. reportUploader:self.mockReportUploader];
  71. [self.fileManager createReportDirectories];
  72. [self.fileManager
  73. setupNewPathForExecutionIdentifier:self.managerData.executionIDModel.executionID];
  74. NSString *name = @"exception_model_report";
  75. NSString *reportPath = [self.fileManager.rootPath stringByAppendingPathComponent:name];
  76. [self.fileManager createDirectoryAtPath:reportPath];
  77. FIRCLSInternalReport *report =
  78. [[FIRCLSInternalReport alloc] initWithPath:reportPath
  79. executionIdentifier:@"TEST_EXECUTION_IDENTIFIER"];
  80. FIRCLSContextInitialize(report, self.mockSettings, self.fileManager);
  81. }
  82. - (void)tearDown {
  83. self.onDemandModel = nil;
  84. [[NSFileManager defaultManager] removeItemAtPath:self.fileManager.rootPath error:nil];
  85. [super tearDown];
  86. }
  87. - (void)setSleepBlock:(void (^)(int))sleepBlock {
  88. ((FIRCLSMockOnDemandModel *)self.managerData.onDemandModel).sleepBlock = sleepBlock;
  89. }
  90. - (void)testIncrementsQueueWhenEventRecorded {
  91. FIRExceptionModel *exceptionModel = [self getTestExceptionModel];
  92. XCTestExpectation *testComplete =
  93. [[XCTestExpectation alloc] initWithDescription:@"complete test"];
  94. // Put an expectation in the sleep block so we can test the state of the queue.
  95. __weak FIRCLSOnDemandModelTests *weakSelf = self;
  96. [self setSleepBlock:^(int delay) {
  97. XCTAssertEqual(delay, 60 / self.mockSettings.onDemandUploadRate);
  98. [weakSelf waitForExpectations:@[ testComplete ] timeout:1.0];
  99. }];
  100. BOOL success = [self.onDemandModel recordOnDemandExceptionIfQuota:exceptionModel
  101. withDataCollectionEnabled:YES
  102. usingExistingReportManager:self.existingReportManager];
  103. // Should record but not submit a report.
  104. XCTAssertTrue(success);
  105. XCTAssertEqual([self.onDemandModel recordedOnDemandExceptionCount], 1);
  106. XCTAssertEqual(self.onDemandModel.getQueuedOperationsCount, 1);
  107. // Fulfill the expectation so the sleep block completes.
  108. [testComplete fulfill];
  109. }
  110. - (void)testCompliesWithDataCollectionOff {
  111. FIRExceptionModel *exceptionModel = [self getTestExceptionModel];
  112. XCTestExpectation *sleepComplete =
  113. [[XCTestExpectation alloc] initWithDescription:@"complete test"];
  114. // Put an expectation in the sleep block so we can test the state of the queue.
  115. __weak FIRCLSOnDemandModelTests *weakSelf = self;
  116. [self setSleepBlock:^(int delay) {
  117. XCTAssertEqual(delay, 60 / self.mockSettings.onDemandUploadRate);
  118. [weakSelf waitForExpectations:@[ sleepComplete ] timeout:1.0];
  119. }];
  120. BOOL success = [self.onDemandModel recordOnDemandExceptionIfQuota:exceptionModel
  121. withDataCollectionEnabled:NO
  122. usingExistingReportManager:self.existingReportManager];
  123. // Should record but not submit a report.
  124. XCTAssertTrue(success);
  125. // We still count this as a recorded event if it was recorded but not submitted.
  126. XCTAssertEqual([self.onDemandModel recordedOnDemandExceptionCount], 1);
  127. XCTAssertEqual(self.onDemandModel.getQueuedOperationsCount, 1);
  128. // Fulfill the expectation so the sleep block completes.
  129. [sleepComplete fulfill];
  130. [self.managerData.onDemandModel.operationQueue waitUntilAllOperationsAreFinished];
  131. XCTAssertEqual(self.onDemandModel.getQueuedOperationsCount, 0);
  132. XCTAssertEqual([self contentsOfActivePath].count, 1);
  133. XCTAssertEqual([self.onDemandModel.storedActiveReportPaths count], 1);
  134. }
  135. - (void)testQuotaWithDataCollectionOff {
  136. FIRExceptionModel *exceptionModel = [self getTestExceptionModel];
  137. for (int i = 0; i < 10; i++) {
  138. BOOL success =
  139. [self.managerData.onDemandModel recordOnDemandExceptionIfQuota:exceptionModel
  140. withDataCollectionEnabled:NO
  141. usingExistingReportManager:self.existingReportManager];
  142. XCTAssertTrue(success);
  143. }
  144. // Once we've finished processing, there should be only FIRCLSMaxUnsentReports recorded with the
  145. // rest considered dropped. The recorded events should be stored in storedActiveReportPaths which
  146. // is kept in sync with the contents of the active path.
  147. [self.managerData.onDemandModel.operationQueue waitUntilAllOperationsAreFinished];
  148. XCTAssertEqual([self.managerData.onDemandModel.operationQueue operationCount], 0);
  149. XCTAssertEqual([self.managerData.onDemandModel recordedOnDemandExceptionCount],
  150. FIRCLSMaxUnsentReports);
  151. XCTAssertEqual([self contentsOfActivePath].count, FIRCLSMaxUnsentReports);
  152. XCTAssertEqual([self.managerData.onDemandModel.storedActiveReportPaths count],
  153. FIRCLSMaxUnsentReports);
  154. // Once we call sendUnsentReports, stored reports should be sent immediately.
  155. [self.existingReportManager sendUnsentReportsWithToken:[FIRCLSDataCollectionToken validToken]
  156. asUrgent:YES];
  157. XCTAssertEqual([self.managerData.onDemandModel recordedOnDemandExceptionCount],
  158. FIRCLSMaxUnsentReports);
  159. [self.existingReportManager.operationQueue waitUntilAllOperationsAreFinished];
  160. XCTAssertEqual([self contentsOfActivePath].count, 0);
  161. XCTAssertEqual([self.managerData.onDemandModel.storedActiveReportPaths count], 0);
  162. }
  163. - (void)testDropsEventIfNoQuota {
  164. [self.onDemandModel setQueueToFull];
  165. FIRExceptionModel *exceptionModel = [self getTestExceptionModel];
  166. BOOL success = [self.onDemandModel recordOnDemandExceptionIfQuota:exceptionModel
  167. withDataCollectionEnabled:NO
  168. usingExistingReportManager:self.existingReportManager];
  169. // Should return false when attempting to record an event and increment the count of dropped
  170. // events.
  171. XCTAssertFalse(success);
  172. XCTAssertEqual(self.onDemandModel.getQueuedOperationsCount, [self.onDemandModel getQueueMax]);
  173. XCTAssertEqual([self.onDemandModel droppedOnDemandExceptionCount], 1);
  174. }
  175. - (void)testDroppedEventCountResets {
  176. [self.onDemandModel setQueueToFull];
  177. FIRExceptionModel *exceptionModel = [self getTestExceptionModel];
  178. BOOL success = [self.onDemandModel recordOnDemandExceptionIfQuota:exceptionModel
  179. withDataCollectionEnabled:NO
  180. usingExistingReportManager:self.existingReportManager];
  181. // Should return false when attempting to record an event and increment the count of dropped
  182. // events.
  183. XCTAssertFalse(success);
  184. XCTAssertEqual(self.onDemandModel.getQueuedOperationsCount, [self.onDemandModel getQueueMax]);
  185. XCTAssertEqual([self.onDemandModel droppedOnDemandExceptionCount], 1);
  186. // Reset the queue to empty
  187. [self.onDemandModel setQueuedOperationsCount:0];
  188. success = [self.onDemandModel recordOnDemandExceptionIfQuota:exceptionModel
  189. withDataCollectionEnabled:NO
  190. usingExistingReportManager:self.existingReportManager];
  191. // Now have room in the queue to record the event
  192. XCTAssertTrue(success);
  193. // droppedOnDemandExceptionCount should be reset once we record the event
  194. XCTAssertEqual([self.onDemandModel droppedOnDemandExceptionCount], 0);
  195. }
  196. #pragma mark - Helpers
  197. - (NSArray *)contentsOfActivePath {
  198. return [self.fileManager activePathContents];
  199. }
  200. - (FIRExceptionModel *)getTestExceptionModel {
  201. NSArray *stackTrace = @[
  202. [FIRStackFrame stackFrameWithSymbol:@"CrashyFunc" file:@"AppLib.m" line:504],
  203. [FIRStackFrame stackFrameWithSymbol:@"ApplicationMain" file:@"AppleLib" line:1],
  204. [FIRStackFrame stackFrameWithSymbol:@"main()" file:@"main.m" line:201],
  205. ];
  206. NSString *name = @"FIRCLSOnDemandModelTestCrash";
  207. NSString *reason = @"Programmer made an error";
  208. FIRExceptionModel *exceptionModel = [FIRExceptionModel exceptionModelWithName:name reason:reason];
  209. exceptionModel.stackTrace = stackTrace;
  210. exceptionModel.isFatal = YES;
  211. exceptionModel.onDemand = YES;
  212. return exceptionModel;
  213. }
  214. @end