FIRCLSReportUploaderTests.m 6.5 KB

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