FIRCLSReportManagerTests.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 <Foundation/Foundation.h>
  15. #import <XCTest/XCTest.h>
  16. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #include "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
  23. #include "Crashlytics/Crashlytics/Components/FIRCLSCrashedMarkerFile.h"
  24. #import "Crashlytics/Crashlytics/Controllers/FIRCLSAnalyticsManager.h"
  25. #import "Crashlytics/Crashlytics/Controllers/FIRCLSManagerData.h"
  26. #import "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionArbiter.h"
  27. #include "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
  28. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  29. #import "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
  30. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockExistingReportManager.h"
  31. #import "Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.h"
  32. #import "Crashlytics/UnitTests/Mocks/FABMockApplicationIdentifierModel.h"
  33. #import "Crashlytics/UnitTests/Mocks/FIRAppFake.h"
  34. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockReportManager.h"
  35. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockReportUploader.h"
  36. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockSettings.h"
  37. #import "Crashlytics/UnitTests/Mocks/FIRCLSTempMockFileManager.h"
  38. #import "Crashlytics/UnitTests/Mocks/FIRMockGDTCoreTransport.h"
  39. #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
  40. #define TEST_API_KEY (@"DB5C8FA65C0D43419120FB96CFDBDE0C")
  41. #define TEST_GOOGLE_APP_ID (@"1:632950151350:ios:d5b0d08d4f00f4b1")
  42. #define TEST_INSTALL_ID (@"DC352568-33A7-4830-A9D8-20EA708F1905")
  43. #define TEST_API_ENDPOINT (@"http://test.com")
  44. #define TEST_BUNDLE_ID (@"com.crashlytics.test")
  45. #define TEST_ANALYTICS_JSON \
  46. (@"{\"name\":\"some_name\",\"nested\":{\"object\":\"with_stuff\"},\"price\":100}")
  47. @interface FIRCLSReportManagerTests : XCTestCase
  48. @property(nonatomic, strong) FIRCLSMockReportManager *reportManager;
  49. @property(nonatomic, strong) FIRCLSMockExistingReportManager *existingReportManager;
  50. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  51. @property(nonatomic, strong) FIRCLSMockReportUploader *mockReportUploader;
  52. @property(nonatomic, strong) FIRCLSTempMockFileManager *fileManager;
  53. @property(nonatomic, strong) FIRCLSDataCollectionArbiter *dataArbiter;
  54. @property(nonatomic, strong) FIRCLSApplicationIdentifierModel *appIDModel;
  55. @end
  56. @implementation FIRCLSReportManagerTests
  57. - (void)setUp {
  58. [super setUp];
  59. FIRSetLoggerLevel(FIRLoggerLevelMax);
  60. FIRCLSContextBaseInit();
  61. id fakeApp = [[FIRAppFake alloc] init];
  62. self.dataArbiter = [[FIRCLSDataCollectionArbiter alloc] initWithApp:fakeApp withAppInfo:@{}];
  63. self.fileManager = [[FIRCLSTempMockFileManager alloc] init];
  64. // Delete cached settings
  65. [self.fileManager removeItemAtPath:_fileManager.settingsFilePath];
  66. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_token"];
  67. FIRMockGDTCORTransport *mockGoogleTransport =
  68. [[FIRMockGDTCORTransport alloc] initWithMappingID:@"id" transformers:nil target:0];
  69. self.appIDModel = [[FIRCLSApplicationIdentifierModel alloc] init];
  70. self.mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  71. appIDModel:self.appIDModel];
  72. // Allow nil values only in tests
  73. #pragma clang diagnostic push
  74. #pragma clang diagnostic ignored "-Wnonnull"
  75. FIRCLSManagerData *managerData =
  76. [[FIRCLSManagerData alloc] initWithGoogleAppID:TEST_GOOGLE_APP_ID
  77. googleTransport:mockGoogleTransport
  78. installations:iid
  79. analytics:nil
  80. fileManager:self.fileManager
  81. dataArbiter:self.dataArbiter
  82. settings:self.mockSettings
  83. onDemandModel:nil];
  84. #pragma clang diagnostic pop
  85. self.mockReportUploader = [[FIRCLSMockReportUploader alloc] initWithManagerData:managerData];
  86. self.existingReportManager =
  87. [[FIRCLSMockExistingReportManager alloc] initWithManagerData:managerData
  88. reportUploader:self.mockReportUploader];
  89. FIRCLSAnalyticsManager *analyticsManager = [[FIRCLSAnalyticsManager alloc] initWithAnalytics:nil];
  90. self.reportManager =
  91. [[FIRCLSMockReportManager alloc] initWithManagerData:managerData
  92. existingReportManager:self.existingReportManager
  93. analyticsManager:analyticsManager];
  94. }
  95. - (void)tearDown {
  96. self.reportManager = nil;
  97. if ([[NSFileManager defaultManager] fileExistsAtPath:[self.fileManager rootPath]]) {
  98. assert([self.fileManager removeItemAtPath:[self.fileManager rootPath]]);
  99. }
  100. FIRCLSContextBaseDeinit();
  101. [super tearDown];
  102. }
  103. #pragma mark - Path Helpers
  104. - (NSString *)resourcePath {
  105. return [[NSBundle bundleForClass:[self class]] resourcePath];
  106. }
  107. - (NSArray *)contentsOfActivePath {
  108. return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.fileManager.activePath
  109. error:nil];
  110. }
  111. - (NSArray *)contentsOfPreparedPath {
  112. return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.fileManager.preparedPath
  113. error:nil];
  114. }
  115. - (NSArray *)contentsOfProcessingPath {
  116. return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.fileManager.processingPath
  117. error:nil];
  118. }
  119. #pragma mark - Report Helpers
  120. - (FIRCLSInternalReport *)createActiveReport {
  121. NSString *reportPath =
  122. [self.fileManager.activePath stringByAppendingPathComponent:@"my_session_id"];
  123. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:reportPath
  124. executionIdentifier:@"my_session_id"];
  125. if (![self.fileManager createDirectoryAtPath:report.path]) {
  126. return nil;
  127. }
  128. if (![self createMetadata:
  129. @"{\"identity\":{\"api_key\":\"my_key\",\"session_id\":\"my_session_id\"}}\n"
  130. forReport:report]) {
  131. return nil;
  132. }
  133. return report;
  134. }
  135. - (BOOL)createFileWithContents:(NSString *)contents atPath:(NSString *)path {
  136. return [self.fileManager.underlyingFileManager
  137. createFileAtPath:path
  138. contents:[contents dataUsingEncoding:NSUTF8StringEncoding]
  139. attributes:nil];
  140. }
  141. - (BOOL)createMetadata:(NSString *)value forReport:(FIRCLSInternalReport *)report {
  142. return [self createFileWithContents:value atPath:[report metadataPath]];
  143. }
  144. #pragma mark - Property Helpers
  145. - (NSArray *)prepareAndSubmitReportArray {
  146. return self.mockReportUploader.prepareAndSubmitReportArray;
  147. }
  148. - (NSArray *)uploadReportArray {
  149. return self.mockReportUploader.uploadReportArray;
  150. }
  151. #pragma mark - File/Directory Handling
  152. - (void)testCreatesNewReportOnStart {
  153. FBLPromise<NSNumber *> *promise = [self->_reportManager startWithProfilingMark:0];
  154. XCTestExpectation *expectation =
  155. [[XCTestExpectation alloc] initWithDescription:@"waiting on promise"];
  156. [promise then:^id _Nullable(NSNumber *_Nullable value) {
  157. XCTAssertTrue([value boolValue]);
  158. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  159. [expectation fulfill];
  160. return value;
  161. }];
  162. [self waitForExpectations:@[ expectation ] timeout:1.0];
  163. }
  164. - (void)waitForPromise:(FBLPromise<NSNumber *> *)promise {
  165. [self waitForPromise:promise withTimeout:1.0];
  166. }
  167. - (void)waitForPromise:(FBLPromise<NSNumber *> *)promise withTimeout:(double)timeout {
  168. __block NSNumber *value = nil;
  169. __block NSError *error = nil;
  170. XCTestExpectation *expectation =
  171. [[XCTestExpectation alloc] initWithDescription:@"waiting on promise"];
  172. [[promise then:^id _Nullable(NSNumber *_Nullable innerValue) {
  173. value = innerValue;
  174. [expectation fulfill];
  175. return nil;
  176. }] catch:^(NSError *_Nonnull innerError) {
  177. error = innerError;
  178. [expectation fulfill];
  179. }];
  180. [self waitForExpectations:@[ expectation ] timeout:timeout];
  181. XCTAssertNil(error);
  182. XCTAssertTrue([value boolValue]);
  183. }
  184. - (void)startReportManager {
  185. [self waitForPromise:[self startReportManagerWithDataCollectionEnabled:YES]];
  186. }
  187. - (FBLPromise<NSNumber *> *)startReportManagerWithDataCollectionEnabled:(BOOL)enabled {
  188. [self.dataArbiter setCrashlyticsCollectionEnabled:enabled];
  189. return [self.reportManager startWithProfilingMark:0];
  190. }
  191. - (void)processReports:(BOOL)send andExpectReports:(BOOL)reportsExpected {
  192. XCTestExpectation *processReportsComplete =
  193. [[XCTestExpectation alloc] initWithDescription:@"processReports: complete"];
  194. __block BOOL reportsAvailable = NO;
  195. [[[self.reportManager checkForUnsentReports]
  196. then:^id _Nullable(FIRCrashlyticsReport *_Nullable report) {
  197. reportsAvailable = report ? true : false;
  198. if (send) {
  199. return [self->_reportManager sendUnsentReports];
  200. } else {
  201. return [self->_reportManager deleteUnsentReports];
  202. }
  203. }] then:^id _Nullable(id _Nullable ignored) {
  204. [processReportsComplete fulfill];
  205. return nil;
  206. }];
  207. [self waitForExpectations:@[ processReportsComplete ] timeout:1.0];
  208. if (reportsExpected) {
  209. XCTAssertTrue(reportsAvailable, "should have unsent reports");
  210. } else {
  211. XCTAssertFalse(reportsAvailable, "should not have unsent reports");
  212. }
  213. }
  214. - (void)processReports:(BOOL)send {
  215. [self processReports:send andExpectReports:YES];
  216. }
  217. - (void)testExistingUnimportantReportOnStart {
  218. // Create a report representing the last run and put it in place
  219. [self createActiveReport];
  220. // Report from the last run should get deleted, and a new
  221. // one should be created for this run.
  222. [self startReportManager];
  223. // If this is > 1 it means we're not cleaning up reports from previous runs.
  224. // If this == 0, it means we're not creating new reports.
  225. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  226. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  227. XCTAssertEqual([self.uploadReportArray count], 0);
  228. }
  229. - (void)testMetricKitResolvesPromiseIfNoDiagnostics {
  230. // Create a report representing the last run and put it in place, then create a crashed file
  231. // marker and MetricKit diagnostic file so that MetricKit manager doesn't resolve the promise
  232. // immediately.
  233. [self createActiveReport];
  234. [self.existingReportManager setShouldHaveExistingReport];
  235. NSString *metricKitPath =
  236. [self.fileManager.cachesPath stringByAppendingString:@"/MetricKit/Diagnostics/"];
  237. [self.fileManager createFileAtPath:[[self.fileManager rootPath]
  238. stringByAppendingPathComponent:@"previously-crashed"]
  239. contents:nil
  240. attributes:nil];
  241. [self.fileManager createDirectoryAtPath:metricKitPath];
  242. [self.fileManager createFileAtPath:[metricKitPath stringByAppendingString:@"Diagnostics.txt"]
  243. contents:nil
  244. attributes:nil];
  245. // MetricKit manager should resolve its promise after 3 seconds.
  246. [self waitForPromise:[self startReportManagerWithDataCollectionEnabled:YES] withTimeout:4];
  247. }
  248. - (void)testExistingUnimportantReportOnStartWithDataCollectionDisabled {
  249. // create a report and put it in place
  250. [self createActiveReport];
  251. // Starting with data collection disabled should report in nothing changing
  252. [self startReportManagerWithDataCollectionEnabled:NO];
  253. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  254. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  255. XCTAssertEqual([self.uploadReportArray count], 0);
  256. }
  257. - (void)testExistingReportOnStart {
  258. // create a report and put it in place
  259. FIRCLSInternalReport *report = [self createActiveReport];
  260. // create a signal file so it is considering worth reporting
  261. XCTAssertTrue([self createFileWithContents:@"signal"
  262. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  263. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  264. [self startReportManager];
  265. // verify that processReports won't get called.
  266. [self processReports:YES andExpectReports:NO];
  267. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  268. // should call report manager once for that report
  269. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  270. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  271. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  272. }
  273. - (void)testExistingReportOnStartWithDataCollectionDisabledThenEnabled {
  274. // create a report and put it in place
  275. FIRCLSInternalReport *report = [self createActiveReport];
  276. // create a signal file so it is considering worth reporting
  277. XCTAssertTrue([self createFileWithContents:@"signal"
  278. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  279. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  280. FBLPromise<NSNumber *> *promise = [self startReportManagerWithDataCollectionEnabled:NO];
  281. XCTAssertEqual([[self contentsOfActivePath] count], 2,
  282. @"should contain the current and old reports");
  283. // should call report manager once for that report
  284. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  285. // We can turn data collection on instead of calling processReports.
  286. [self.dataArbiter setCrashlyticsCollectionEnabled:YES];
  287. [self waitForPromise:promise];
  288. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  289. // should call report manager once for that report
  290. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  291. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  292. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  293. }
  294. - (void)testExistingReportOnStartWithDataCollectionDisabledAndSend {
  295. // create a report and put it in place
  296. FIRCLSInternalReport *report = [self createActiveReport];
  297. // create a signal file so it is considering worth reporting
  298. XCTAssertTrue([self createFileWithContents:@"signal"
  299. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  300. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  301. [self startReportManagerWithDataCollectionEnabled:NO];
  302. XCTAssertEqual([[self contentsOfActivePath] count], 2,
  303. @"should contain the current and old reports");
  304. // should call report manager once for that report
  305. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  306. [self processReports:YES];
  307. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  308. // should call report manager once for that report
  309. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  310. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  311. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  312. // Calling processReports again should not call the callback.
  313. // Technically, the behavior is unspecified.
  314. [self processReports:YES andExpectReports:NO];
  315. }
  316. - (void)testExistingReportOnStartWithDataCollectionDisabledAndDelete {
  317. // create a report and put it in place
  318. FIRCLSInternalReport *report = [self createActiveReport];
  319. // create a signal file so it is considering worth reporting
  320. XCTAssertTrue([self createFileWithContents:@"signal"
  321. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  322. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  323. [self startReportManagerWithDataCollectionEnabled:NO];
  324. XCTAssertEqual([[self contentsOfActivePath] count], 2,
  325. @"should contain the current and old reports");
  326. // should call report manager once for that report
  327. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  328. [self processReports:NO];
  329. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  330. // Should not call report manager for that report.
  331. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  332. }
  333. - (void)testExistingUrgentReportOnStart {
  334. // create a report and put it in place
  335. FIRCLSInternalReport *report = [self createActiveReport];
  336. // create a signal file so it is considering worth reporting
  337. XCTAssertTrue([self createFileWithContents:@"signal"
  338. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  339. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  340. // Put the launch marker in place
  341. [self.reportManager.launchMarker createLaunchFailureMarker];
  342. // should call back to the delegate on start
  343. [self startReportManager];
  344. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  345. // should call report manager once for that report
  346. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  347. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  348. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(YES));
  349. }
  350. - (void)testExistingUrgentReportOnStartWithDataCollectionDisabled {
  351. // create a report and put it in place
  352. FIRCLSInternalReport *report = [self createActiveReport];
  353. // create a signal file so it is considering worth reporting
  354. XCTAssertTrue([self createFileWithContents:@"signal"
  355. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  356. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  357. // Put the launch marker in place
  358. [self.reportManager.launchMarker createLaunchFailureMarker];
  359. // Should wait for processReports: to be called.
  360. [self startReportManagerWithDataCollectionEnabled:NO];
  361. XCTAssertEqual([[self contentsOfActivePath] count], 2, @"the report hasn't been sent");
  362. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  363. [self processReports:YES];
  364. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only current report");
  365. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  366. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  367. // If data collection is disabled, you can never send the report urgently / blocking
  368. // startup because you need to call a method after startup to send the report
  369. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  370. }
  371. - (void)testFilesLeftInProcessing {
  372. // put report in processing
  373. FIRCLSInternalReport *report = [self createActiveReport];
  374. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.processingPath]);
  375. XCTAssert([_fileManager moveItemAtPath:[report path] toDirectory:_fileManager.processingPath]);
  376. [self startReportManager];
  377. // we should not process reports left over in processing
  378. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  379. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  380. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(NO));
  381. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  382. }
  383. /*
  384. * This tests an edge case where there is a report in processing. For the purposes of unsent
  385. * reports these are not shown to the developer, but they are uploaded / deleted upon
  386. * calling send / delete.
  387. */
  388. - (void)testFilesLeftInProcessingWithDataCollectionDisabled {
  389. // Put report in processing.
  390. FIRCLSInternalReport *report = [self createActiveReport];
  391. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.processingPath]);
  392. XCTAssert([_fileManager moveItemAtPath:[report path] toDirectory:_fileManager.processingPath]);
  393. [self startReportManagerWithDataCollectionEnabled:NO];
  394. // Nothing should have happened yet.
  395. XCTAssertEqual([[self contentsOfProcessingPath] count], 1,
  396. @"Processing should still have the report");
  397. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  398. // We don't expect reports here because we don't consider processing or prepared
  399. // reports as unsent as they need to be marked for sending before being placed
  400. // in those directories.
  401. [self processReports:YES andExpectReports:NO];
  402. // We should not process reports left over in processing.
  403. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  404. XCTAssertEqual([[self contentsOfPreparedPath] count], 0, @"Prepared should be cleared");
  405. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  406. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(NO));
  407. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  408. }
  409. - (void)testFilesLeftInPrepared {
  410. // Drop a phony multipart-mime file in here, with non-zero contents.
  411. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.preparedPath]);
  412. NSString *path = [_fileManager.preparedPath stringByAppendingPathComponent:@"phony-report"];
  413. path = [path stringByAppendingPathExtension:@"multipart-mime"];
  414. XCTAssertTrue([[_fileManager underlyingFileManager]
  415. createFileAtPath:path
  416. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  417. attributes:nil]);
  418. [self startReportManager];
  419. // Reports should be moved out of prepared
  420. XCTAssertEqual([[self contentsOfPreparedPath] count], 0, @"Prepared should be cleared");
  421. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  422. XCTAssertEqual([self.uploadReportArray count], 1);
  423. XCTAssertEqualObjects(self.uploadReportArray[0][@"path"], path);
  424. }
  425. /*
  426. * This tests an edge case where there is a report in prepared. For the purposes of unsent
  427. * reports these are not shown to the developer, but they are uploaded / deleted upon
  428. * calling send / delete.
  429. */
  430. - (void)testFilesLeftInPreparedWithDataCollectionDisabled {
  431. // drop a phony multipart-mime file in here, with non-zero contents
  432. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.preparedPath]);
  433. NSString *path = [_fileManager.preparedPath stringByAppendingPathComponent:@"phony-report"];
  434. path = [path stringByAppendingPathExtension:@"multipart-mime"];
  435. XCTAssertTrue([[_fileManager underlyingFileManager]
  436. createFileAtPath:path
  437. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  438. attributes:nil]);
  439. [self startReportManagerWithDataCollectionEnabled:NO];
  440. // Nothing should have happened yet.
  441. XCTAssertEqual([[self contentsOfPreparedPath] count], 1,
  442. @"Prepared should still have the report");
  443. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  444. // We don't expect reports here because we don't consider processing or prepared
  445. // reports as unsent as they need to be marked for sending before being placed
  446. // in those directories.
  447. [self processReports:YES andExpectReports:NO];
  448. // Reports should be moved out of prepared
  449. XCTAssertEqual([[self contentsOfPreparedPath] count], 0, @"Prepared should be cleared");
  450. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  451. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  452. XCTAssertEqual([self.uploadReportArray count], 1);
  453. XCTAssertEqualObjects(self.uploadReportArray[0][@"path"], path);
  454. }
  455. - (void)testSuccessfulSubmission {
  456. // drop a phony multipart-mime file in here, with non-zero contents
  457. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.preparedPath]);
  458. NSString *path = [_fileManager.preparedPath stringByAppendingPathComponent:@"phony-report"];
  459. path = [path stringByAppendingPathExtension:@"multipart-mime"];
  460. XCTAssertTrue([[_fileManager underlyingFileManager]
  461. createFileAtPath:path
  462. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  463. attributes:nil]);
  464. [self startReportManager];
  465. // we should not process reports left over in processing
  466. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  467. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  468. XCTAssertEqual([self.uploadReportArray count], 1);
  469. XCTAssertEqualObjects(self.uploadReportArray[0][@"path"], path);
  470. [self.reportManager.operationQueue waitUntilAllOperationsAreFinished];
  471. // not 100% sure what to verify here
  472. // lol
  473. }
  474. - (void)testLogInvalidJSONAnalyticsEvents {
  475. NSDictionary *eventAsDict = @{
  476. @"price" : @(NAN),
  477. @"count" : @(INFINITY),
  478. };
  479. NSString *json = FIRCLSFIRAEventDictionaryToJSON(eventAsDict);
  480. XCTAssertEqualObjects(json, nil);
  481. }
  482. @end