FIRCLSReportManagerTests.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. #if SWIFT_PACKAGE
  106. NSBundle *bundle = SWIFTPM_MODULE_BUNDLE;
  107. return [bundle.resourcePath stringByAppendingPathComponent:@"Data"];
  108. #else
  109. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  110. return bundle.resourcePath;
  111. #endif
  112. }
  113. - (NSArray *)contentsOfActivePath {
  114. return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.fileManager.activePath
  115. error:nil];
  116. }
  117. - (NSArray *)contentsOfPreparedPath {
  118. return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.fileManager.preparedPath
  119. error:nil];
  120. }
  121. - (NSArray *)contentsOfProcessingPath {
  122. return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.fileManager.processingPath
  123. error:nil];
  124. }
  125. #pragma mark - Report Helpers
  126. - (FIRCLSInternalReport *)createActiveReport {
  127. NSString *reportPath =
  128. [self.fileManager.activePath stringByAppendingPathComponent:@"my_session_id"];
  129. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:reportPath
  130. executionIdentifier:@"my_session_id"];
  131. if (![self.fileManager createDirectoryAtPath:report.path]) {
  132. return nil;
  133. }
  134. if (![self createMetadata:
  135. @"{\"identity\":{\"api_key\":\"my_key\",\"session_id\":\"my_session_id\"}}\n"
  136. forReport:report]) {
  137. return nil;
  138. }
  139. return report;
  140. }
  141. - (BOOL)createFileWithContents:(NSString *)contents atPath:(NSString *)path {
  142. return [self.fileManager.underlyingFileManager
  143. createFileAtPath:path
  144. contents:[contents dataUsingEncoding:NSUTF8StringEncoding]
  145. attributes:nil];
  146. }
  147. - (BOOL)createMetadata:(NSString *)value forReport:(FIRCLSInternalReport *)report {
  148. return [self createFileWithContents:value atPath:[report metadataPath]];
  149. }
  150. #pragma mark - Property Helpers
  151. - (NSArray *)prepareAndSubmitReportArray {
  152. return self.mockReportUploader.prepareAndSubmitReportArray;
  153. }
  154. - (NSArray *)uploadReportArray {
  155. return self.mockReportUploader.uploadReportArray;
  156. }
  157. #pragma mark - File/Directory Handling
  158. - (void)testCreatesNewReportOnStart {
  159. FBLPromise<NSNumber *> *promise = [self->_reportManager startWithProfilingMark:0];
  160. XCTestExpectation *expectation =
  161. [[XCTestExpectation alloc] initWithDescription:@"waiting on promise"];
  162. [promise then:^id _Nullable(NSNumber *_Nullable value) {
  163. XCTAssertTrue([value boolValue]);
  164. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  165. [expectation fulfill];
  166. return value;
  167. }];
  168. [self waitForExpectations:@[ expectation ] timeout:1.0];
  169. }
  170. - (void)waitForPromise:(FBLPromise<NSNumber *> *)promise {
  171. [self waitForPromise:promise withTimeout:1.0];
  172. }
  173. - (void)waitForPromise:(FBLPromise<NSNumber *> *)promise withTimeout:(double)timeout {
  174. __block NSNumber *value = nil;
  175. __block NSError *error = nil;
  176. XCTestExpectation *expectation =
  177. [[XCTestExpectation alloc] initWithDescription:@"waiting on promise"];
  178. [[promise then:^id _Nullable(NSNumber *_Nullable innerValue) {
  179. value = innerValue;
  180. [expectation fulfill];
  181. return nil;
  182. }] catch:^(NSError *_Nonnull innerError) {
  183. error = innerError;
  184. [expectation fulfill];
  185. }];
  186. [self waitForExpectations:@[ expectation ] timeout:timeout];
  187. XCTAssertNil(error);
  188. XCTAssertTrue([value boolValue]);
  189. }
  190. - (void)startReportManager {
  191. [self waitForPromise:[self startReportManagerWithDataCollectionEnabled:YES]];
  192. }
  193. - (FBLPromise<NSNumber *> *)startReportManagerWithDataCollectionEnabled:(BOOL)enabled {
  194. [self.dataArbiter setCrashlyticsCollectionEnabled:enabled];
  195. return [self.reportManager startWithProfilingMark:0];
  196. }
  197. - (void)processReports:(BOOL)send andExpectReports:(BOOL)reportsExpected {
  198. XCTestExpectation *processReportsComplete =
  199. [[XCTestExpectation alloc] initWithDescription:@"processReports: complete"];
  200. __block BOOL reportsAvailable = NO;
  201. [[[self.reportManager checkForUnsentReports]
  202. then:^id _Nullable(FIRCrashlyticsReport *_Nullable report) {
  203. reportsAvailable = report ? true : false;
  204. if (send) {
  205. return [self->_reportManager sendUnsentReports];
  206. } else {
  207. return [self->_reportManager deleteUnsentReports];
  208. }
  209. }] then:^id _Nullable(id _Nullable ignored) {
  210. [processReportsComplete fulfill];
  211. return nil;
  212. }];
  213. [self waitForExpectations:@[ processReportsComplete ] timeout:1.0];
  214. if (reportsExpected) {
  215. XCTAssertTrue(reportsAvailable, "should have unsent reports");
  216. } else {
  217. XCTAssertFalse(reportsAvailable, "should not have unsent reports");
  218. }
  219. }
  220. - (void)processReports:(BOOL)send {
  221. [self processReports:send andExpectReports:YES];
  222. }
  223. - (void)testExistingUnimportantReportOnStart {
  224. // Create a report representing the last run and put it in place
  225. [self createActiveReport];
  226. // Report from the last run should get deleted, and a new
  227. // one should be created for this run.
  228. [self startReportManager];
  229. // If this is > 1 it means we're not cleaning up reports from previous runs.
  230. // If this == 0, it means we're not creating new reports.
  231. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  232. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  233. XCTAssertEqual([self.uploadReportArray count], 0);
  234. }
  235. - (void)testMetricKitResolvesPromiseIfNoDiagnostics {
  236. // Create a report representing the last run and put it in place, then create a crashed file
  237. // marker and MetricKit diagnostic file so that MetricKit manager doesn't resolve the promise
  238. // immediately.
  239. [self createActiveReport];
  240. [self.existingReportManager setShouldHaveExistingReport];
  241. NSString *metricKitPath =
  242. [self.fileManager.cachesPath stringByAppendingString:@"/MetricKit/Diagnostics/"];
  243. [self.fileManager createFileAtPath:[[self.fileManager rootPath]
  244. stringByAppendingPathComponent:@"previously-crashed"]
  245. contents:nil
  246. attributes:nil];
  247. [self.fileManager createDirectoryAtPath:metricKitPath];
  248. [self.fileManager createFileAtPath:[metricKitPath stringByAppendingString:@"Diagnostics.txt"]
  249. contents:nil
  250. attributes:nil];
  251. // MetricKit manager should resolve its promise after 3 seconds.
  252. [self waitForPromise:[self startReportManagerWithDataCollectionEnabled:YES] withTimeout:4];
  253. }
  254. - (void)testExistingUnimportantReportOnStartWithDataCollectionDisabled {
  255. // create a report and put it in place
  256. [self createActiveReport];
  257. // Starting with data collection disabled should report in nothing changing
  258. [self startReportManagerWithDataCollectionEnabled:NO];
  259. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  260. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  261. XCTAssertEqual([self.uploadReportArray count], 0);
  262. }
  263. - (void)testExistingReportOnStart {
  264. // create a report and put it in place
  265. FIRCLSInternalReport *report = [self createActiveReport];
  266. // create a signal file so it is considering worth reporting
  267. XCTAssertTrue([self createFileWithContents:@"signal"
  268. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  269. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  270. [self startReportManager];
  271. // verify that processReports won't get called.
  272. [self processReports:YES andExpectReports:NO];
  273. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  274. // should call report manager once for that report
  275. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  276. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  277. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  278. }
  279. - (void)testExistingReportOnStartWithDataCollectionDisabledThenEnabled {
  280. // create a report and put it in place
  281. FIRCLSInternalReport *report = [self createActiveReport];
  282. // create a signal file so it is considering worth reporting
  283. XCTAssertTrue([self createFileWithContents:@"signal"
  284. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  285. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  286. FBLPromise<NSNumber *> *promise = [self startReportManagerWithDataCollectionEnabled:NO];
  287. XCTAssertEqual([[self contentsOfActivePath] count], 2,
  288. @"should contain the current and old reports");
  289. // should call report manager once for that report
  290. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  291. // We can turn data collection on instead of calling processReports.
  292. [self.dataArbiter setCrashlyticsCollectionEnabled:YES];
  293. [self waitForPromise:promise];
  294. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  295. // should call report manager once for that report
  296. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  297. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  298. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  299. }
  300. - (void)testExistingReportOnStartWithDataCollectionDisabledAndSend {
  301. // create a report and put it in place
  302. FIRCLSInternalReport *report = [self createActiveReport];
  303. // create a signal file so it is considering worth reporting
  304. XCTAssertTrue([self createFileWithContents:@"signal"
  305. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  306. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  307. [self startReportManagerWithDataCollectionEnabled:NO];
  308. XCTAssertEqual([[self contentsOfActivePath] count], 2,
  309. @"should contain the current and old reports");
  310. // should call report manager once for that report
  311. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  312. [self processReports:YES];
  313. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  314. // should call report manager once for that report
  315. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  316. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  317. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  318. // Calling processReports again should not call the callback.
  319. // Technically, the behavior is unspecified.
  320. [self processReports:YES andExpectReports:NO];
  321. }
  322. - (void)testExistingReportOnStartWithDataCollectionDisabledAndDelete {
  323. // create a report and put it in place
  324. FIRCLSInternalReport *report = [self createActiveReport];
  325. // create a signal file so it is considering worth reporting
  326. XCTAssertTrue([self createFileWithContents:@"signal"
  327. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  328. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  329. [self startReportManagerWithDataCollectionEnabled:NO];
  330. XCTAssertEqual([[self contentsOfActivePath] count], 2,
  331. @"should contain the current and old reports");
  332. // should call report manager once for that report
  333. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  334. [self processReports:NO];
  335. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  336. // Should not call report manager for that report.
  337. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  338. }
  339. - (void)testExistingUrgentReportOnStart {
  340. // create a report and put it in place
  341. FIRCLSInternalReport *report = [self createActiveReport];
  342. // create a signal file so it is considering worth reporting
  343. XCTAssertTrue([self createFileWithContents:@"signal"
  344. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  345. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  346. // Put the launch marker in place
  347. [self.reportManager.launchMarker createLaunchFailureMarker];
  348. // should call back to the delegate on start
  349. [self startReportManager];
  350. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only the current report");
  351. // should call report manager once for that report
  352. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  353. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  354. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(YES));
  355. }
  356. - (void)testExistingUrgentReportOnStartWithDataCollectionDisabled {
  357. // create a report and put it in place
  358. FIRCLSInternalReport *report = [self createActiveReport];
  359. // create a signal file so it is considering worth reporting
  360. XCTAssertTrue([self createFileWithContents:@"signal"
  361. atPath:[report pathForContentFile:FIRCLSReportSignalFile]]);
  362. XCTAssertEqual([[self contentsOfActivePath] count], 1);
  363. // Put the launch marker in place
  364. [self.reportManager.launchMarker createLaunchFailureMarker];
  365. // Should wait for processReports: to be called.
  366. [self startReportManagerWithDataCollectionEnabled:NO];
  367. XCTAssertEqual([[self contentsOfActivePath] count], 2, @"the report hasn't been sent");
  368. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  369. [self processReports:YES];
  370. XCTAssertEqual([[self contentsOfActivePath] count], 1, @"should contain only current report");
  371. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  372. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(YES));
  373. // If data collection is disabled, you can never send the report urgently / blocking
  374. // startup because you need to call a method after startup to send the report
  375. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  376. }
  377. - (void)testFilesLeftInProcessing {
  378. // put report in processing
  379. FIRCLSInternalReport *report = [self createActiveReport];
  380. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.processingPath]);
  381. XCTAssert([_fileManager moveItemAtPath:[report path] toDirectory:_fileManager.processingPath]);
  382. [self startReportManager];
  383. // we should not process reports left over in processing
  384. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  385. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  386. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(NO));
  387. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  388. }
  389. /*
  390. * This tests an edge case where there is a report in processing. For the purposes of unsent
  391. * reports these are not shown to the developer, but they are uploaded / deleted upon
  392. * calling send / delete.
  393. */
  394. - (void)testFilesLeftInProcessingWithDataCollectionDisabled {
  395. // Put report in processing.
  396. FIRCLSInternalReport *report = [self createActiveReport];
  397. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.processingPath]);
  398. XCTAssert([_fileManager moveItemAtPath:[report path] toDirectory:_fileManager.processingPath]);
  399. [self startReportManagerWithDataCollectionEnabled:NO];
  400. // Nothing should have happened yet.
  401. XCTAssertEqual([[self contentsOfProcessingPath] count], 1,
  402. @"Processing should still have the report");
  403. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  404. // We don't expect reports here because we don't consider processing or prepared
  405. // reports as unsent as they need to be marked for sending before being placed
  406. // in those directories.
  407. [self processReports:YES andExpectReports:NO];
  408. // We should not process reports left over in processing.
  409. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  410. XCTAssertEqual([[self contentsOfPreparedPath] count], 0, @"Prepared should be cleared");
  411. XCTAssertEqual([self.prepareAndSubmitReportArray count], 1);
  412. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"process"], @(NO));
  413. XCTAssertEqualObjects(self.prepareAndSubmitReportArray[0][@"urgent"], @(NO));
  414. }
  415. - (void)testFilesLeftInPrepared {
  416. // Drop a phony multipart-mime file in here, with non-zero contents.
  417. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.preparedPath]);
  418. NSString *path = [_fileManager.preparedPath stringByAppendingPathComponent:@"phony-report"];
  419. path = [path stringByAppendingPathExtension:@"multipart-mime"];
  420. XCTAssertTrue([[_fileManager underlyingFileManager]
  421. createFileAtPath:path
  422. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  423. attributes:nil]);
  424. [self startReportManager];
  425. // Reports should be moved out of prepared
  426. XCTAssertEqual([[self contentsOfPreparedPath] count], 0, @"Prepared should be cleared");
  427. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  428. XCTAssertEqual([self.uploadReportArray count], 1);
  429. XCTAssertEqualObjects(self.uploadReportArray[0][@"path"], path);
  430. }
  431. /*
  432. * This tests an edge case where there is a report in prepared. For the purposes of unsent
  433. * reports these are not shown to the developer, but they are uploaded / deleted upon
  434. * calling send / delete.
  435. */
  436. - (void)testFilesLeftInPreparedWithDataCollectionDisabled {
  437. // drop a phony multipart-mime file in here, with non-zero contents
  438. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.preparedPath]);
  439. NSString *path = [_fileManager.preparedPath stringByAppendingPathComponent:@"phony-report"];
  440. path = [path stringByAppendingPathExtension:@"multipart-mime"];
  441. XCTAssertTrue([[_fileManager underlyingFileManager]
  442. createFileAtPath:path
  443. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  444. attributes:nil]);
  445. [self startReportManagerWithDataCollectionEnabled:NO];
  446. // Nothing should have happened yet.
  447. XCTAssertEqual([[self contentsOfPreparedPath] count], 1,
  448. @"Prepared should still have the report");
  449. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  450. // We don't expect reports here because we don't consider processing or prepared
  451. // reports as unsent as they need to be marked for sending before being placed
  452. // in those directories.
  453. [self processReports:YES andExpectReports:NO];
  454. // Reports should be moved out of prepared
  455. XCTAssertEqual([[self contentsOfPreparedPath] count], 0, @"Prepared should be cleared");
  456. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  457. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  458. XCTAssertEqual([self.uploadReportArray count], 1);
  459. XCTAssertEqualObjects(self.uploadReportArray[0][@"path"], path);
  460. }
  461. - (void)testSuccessfulSubmission {
  462. // drop a phony multipart-mime file in here, with non-zero contents
  463. XCTAssert([_fileManager createDirectoryAtPath:_fileManager.preparedPath]);
  464. NSString *path = [_fileManager.preparedPath stringByAppendingPathComponent:@"phony-report"];
  465. path = [path stringByAppendingPathExtension:@"multipart-mime"];
  466. XCTAssertTrue([[_fileManager underlyingFileManager]
  467. createFileAtPath:path
  468. contents:[@"contents" dataUsingEncoding:NSUTF8StringEncoding]
  469. attributes:nil]);
  470. [self startReportManager];
  471. // we should not process reports left over in processing
  472. XCTAssertEqual([[self contentsOfProcessingPath] count], 0, @"Processing should be cleared");
  473. XCTAssertEqual([self.prepareAndSubmitReportArray count], 0);
  474. XCTAssertEqual([self.uploadReportArray count], 1);
  475. XCTAssertEqualObjects(self.uploadReportArray[0][@"path"], path);
  476. [self.reportManager.operationQueue waitUntilAllOperationsAreFinished];
  477. // not 100% sure what to verify here
  478. // lol
  479. }
  480. - (void)testLogInvalidJSONAnalyticsEvents {
  481. NSDictionary *eventAsDict = @{
  482. @"price" : @(NAN),
  483. @"count" : @(INFINITY),
  484. };
  485. NSString *json = FIRCLSFIRAEventDictionaryToJSON(eventAsDict);
  486. XCTAssertEqualObjects(json, nil);
  487. }
  488. @end