FIRCLSReportUploaderTests.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <XCTest/XCTest.h>
  15. #import "Crashlytics/Crashlytics/Controllers/FIRCLSReportUploader_Private.h"
  16. #import "Crashlytics/Crashlytics/Components/FIRCLSApplication.h"
  17. #include "Crashlytics/Crashlytics/DataCollection/FIRCLSDataCollectionToken.h"
  18. #include "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
  19. #include "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
  20. #include "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  21. #include "Crashlytics/Crashlytics/Models/FIRCLSSettings.h"
  22. #include "Crashlytics/Shared/FIRCLSConstants.h"
  23. #import "Crashlytics/UnitTests/Mocks/FABMockApplicationIdentifierModel.h"
  24. #include "Crashlytics/UnitTests/Mocks/FIRCLSMockSettings.h"
  25. #include "Crashlytics/UnitTests/Mocks/FIRCLSTempMockFileManager.h"
  26. #include "Crashlytics/UnitTests/Mocks/FIRMockGDTCoreTransport.h"
  27. NSString *const TestEndpoint = @"https://reports.crashlytics.com";
  28. @interface FIRCLSReportUploaderTests : XCTestCase <FIRCLSReportUploaderDataSource>
  29. @property(nonatomic, strong) FIRCLSReportUploader *uploader;
  30. @property(nonatomic, strong) FIRCLSTempMockFileManager *fileManager;
  31. @property(nonatomic, strong) NSOperationQueue *queue;
  32. // Add mock prefix to names as there are naming conflicts with FIRCLSReportUploaderDelegate
  33. @property(nonatomic, strong) FIRMockGDTCORTransport *mockDataTransport;
  34. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  35. @end
  36. @implementation FIRCLSReportUploaderTests
  37. - (void)setUp {
  38. [super setUp];
  39. FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
  40. self.queue = [NSOperationQueue new];
  41. self.mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  42. appIDModel:appIDModel];
  43. self.fileManager = [[FIRCLSTempMockFileManager alloc] init];
  44. self.uploader = [[FIRCLSReportUploader alloc] initWithQueue:self.queue
  45. dataSource:self
  46. fileManager:self.fileManager
  47. analytics:nil];
  48. self.mockDataTransport = [[FIRMockGDTCORTransport alloc] initWithMappingID:@"1206"
  49. transformers:nil
  50. target:kGDTCORTargetCSH];
  51. }
  52. - (void)tearDown {
  53. self.uploader = nil;
  54. [super tearDown];
  55. }
  56. #pragma mark - Tests
  57. - (void)testPrepareReport {
  58. NSString *path = [self.fileManager.activePath stringByAppendingPathComponent:@"pkg_uuid"];
  59. FIRCLSInternalReport *report = [[FIRCLSInternalReport alloc] initWithPath:path];
  60. self.fileManager.moveItemAtPathResult = [NSNumber numberWithInt:1];
  61. [self.uploader prepareAndSubmitReport:report
  62. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  63. asUrgent:YES
  64. withProcessing:YES];
  65. // Verify with the last move operation is from processing -> prepared
  66. XCTAssertTrue(
  67. [self.fileManager.moveItemAtPath_destDir containsString:self.fileManager.preparedPath]);
  68. }
  69. - (void)testUploadPackagedReportWithPath {
  70. [self runUploadPackagedReportWithUrgency:NO];
  71. }
  72. - (void)testUrgentUploadPackagedReportWithPath {
  73. [self runUploadPackagedReportWithUrgency:YES];
  74. }
  75. - (void)testUploadPackagedReportWithoutDataCollectionToken {
  76. [self setUpForUpload];
  77. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  78. // Ensure we don't hand off an event to GDT
  79. XCTAssertNil(self.mockDataTransport.sendDataEvent_event);
  80. }
  81. - (void)testUploadPackagedReportNotGDTWritten {
  82. [self setUpForUpload];
  83. self.mockDataTransport.sendDataEvent_wasWritten = NO;
  84. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  85. // Did not delete report
  86. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  87. }
  88. - (void)testUploadPackagedReportGDTError {
  89. [self setUpForUpload];
  90. self.mockDataTransport.sendDataEvent_error = [[NSError alloc] initWithDomain:@"domain"
  91. code:1
  92. userInfo:nil];
  93. [self.uploader uploadPackagedReportAtPath:[self packagePath] dataCollectionToken:nil asUrgent:NO];
  94. // Did not delete report
  95. XCTAssertNil(self.fileManager.removedItemAtPath_path);
  96. }
  97. #pragma mark - Helper functions
  98. - (NSString *)packagePath {
  99. return [self.fileManager.preparedPath stringByAppendingPathComponent:@"pkg_uuid"];
  100. }
  101. - (void)runUploadPackagedReportWithUrgency:(BOOL)urgent {
  102. [self setUpForUpload];
  103. [self.uploader uploadPackagedReportAtPath:[self packagePath]
  104. dataCollectionToken:FIRCLSDataCollectionToken.validToken
  105. asUrgent:urgent];
  106. XCTAssertNotNil(self.mockDataTransport.sendDataEvent_event);
  107. XCTAssertEqualObjects(self.fileManager.removedItemAtPath_path, [self packagePath]);
  108. }
  109. - (void)setUpForUpload {
  110. self.mockDataTransport.sendDataEvent_wasWritten = YES;
  111. }
  112. #pragma mark - FIRCLSReportUploaderDataSource
  113. - (NSString *)bundleIdentifier {
  114. return @"com.test.TestApp";
  115. }
  116. - (NSString *)googleAppID {
  117. return @"someGoogleAppId";
  118. }
  119. - (GDTCORTransport *)googleTransport {
  120. return self.mockDataTransport;
  121. }
  122. - (FIRCLSSettings *)settings {
  123. return self.mockSettings;
  124. }
  125. - (void)didCompleteAllSubmissions {
  126. }
  127. @end