FIRCLSProcessReportOperationTests.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "Crashlytics/Crashlytics/Operations/Reports/FIRCLSProcessReportOperation.h"
  15. #import <Foundation/Foundation.h>
  16. #import <XCTest/XCTest.h>
  17. #import "Crashlytics/Crashlytics/Helpers/FIRCLSFile.h"
  18. #import "Crashlytics/Crashlytics/Models/FIRCLSFileManager.h"
  19. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  20. #import "Crashlytics/Crashlytics/Private/FIRStackFrame_Private.h"
  21. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockSymbolResolver.h"
  22. @interface FIRCLSProcessReportOperationTests : XCTestCase
  23. @end
  24. @implementation FIRCLSProcessReportOperationTests
  25. - (void)setUp {
  26. [super setUp];
  27. [[NSFileManager defaultManager] removeItemAtPath:self.reportPath error:nil];
  28. }
  29. - (void)tearDown {
  30. [[NSFileManager defaultManager] removeItemAtPath:self.reportPath error:nil];
  31. [super tearDown];
  32. }
  33. - (NSString *)resourcePath {
  34. #if SWIFT_PACKAGE
  35. NSBundle *bundle = Firebase_FirebaseCrashlyticsUnit_SWIFTPM_MODULE_BUNDLE();
  36. return [bundle.resourcePath stringByAppendingPathComponent:@"Data"];
  37. #else
  38. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  39. return bundle.resourcePath;
  40. #endif
  41. }
  42. - (NSString *)reportPath {
  43. return [NSTemporaryDirectory() stringByAppendingPathComponent:@"execution_identifier"];
  44. }
  45. - (NSString *)pathForResource:(NSString *)name {
  46. return [[self resourcePath] stringByAppendingPathComponent:name];
  47. }
  48. - (FIRCLSInternalReport *)createReportAndPath {
  49. FIRCLSInternalReport *report =
  50. [[FIRCLSInternalReport alloc] initWithPath:self.reportPath
  51. executionIdentifier:@"execution_identifier"];
  52. // create the directory path
  53. assert([[NSFileManager defaultManager] createDirectoryAtPath:[report path]
  54. withIntermediateDirectories:YES
  55. attributes:nil
  56. error:nil]);
  57. return report;
  58. }
  59. #if TARGET_OS_IPHONE
  60. #else
  61. - (void)testExceptionSymbolication {
  62. // Setup a resolver that will work for the contents of the file
  63. FIRCLSMockSymbolResolver *resolver = [[FIRCLSMockSymbolResolver alloc] init];
  64. FIRStackFrame *frame = nil;
  65. frame = [FIRStackFrame stackFrameWithSymbol:@"testSymbolA"];
  66. [frame setLibrary:@"libA"];
  67. [frame setOffset:10];
  68. [resolver addMockFrame:frame atAddress:4321599284];
  69. // create a report and symbolicate
  70. FIRCLSInternalReport *report = [self createReportAndPath];
  71. NSFileManager *fileManager = [NSFileManager defaultManager];
  72. // put an exception in place
  73. XCTAssertTrue([fileManager copyItemAtPath:[self pathForResource:FIRCLSReportExceptionFile]
  74. toPath:[report pathForContentFile:FIRCLSReportExceptionFile]
  75. error:nil],
  76. @"");
  77. FIRCLSProcessReportOperation *operation =
  78. [[FIRCLSProcessReportOperation alloc] initWithReport:report resolver:resolver];
  79. [operation start];
  80. // Read the symbolicated output and verify
  81. NSArray *sections = FIRCLSFileReadSections(
  82. [[report pathForContentFile:@"exception.clsrecord.symbolicated"] fileSystemRepresentation],
  83. false, nil);
  84. XCTAssertEqual([sections count], 1, @"");
  85. XCTAssertEqualObjects(sections[0][@"threads"][0][0][@"library"], @"libA", @"");
  86. XCTAssertEqualObjects(sections[0][@"threads"][0][0][@"offset"], @(10), @"");
  87. XCTAssertEqualObjects(sections[0][@"threads"][0][0][@"symbol"], @"testSymbolA", @"");
  88. }
  89. #endif
  90. @end