FIRCLSOnDemandModelTests.m 12 KB

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