FIRCLSReportManagerTests.m 25 KB

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