FIRCLSReportManagerTests.m 22 KB

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