FIRRecordExceptionModelTests.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2020 Google LLC
  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/Public/FirebaseCrashlytics/FIRExceptionModel.h"
  16. #import "Crashlytics/Crashlytics/Public/FirebaseCrashlytics/FIRStackFrame.h"
  17. #import "Crashlytics/Crashlytics/Components/FIRCLSContext.h"
  18. #import "Crashlytics/Crashlytics/Models/FIRCLSInstallIdentifierModel.h"
  19. #import "Crashlytics/Crashlytics/Models/FIRCLSInternalReport.h"
  20. #import "Crashlytics/UnitTests/Mocks/FABMockApplicationIdentifierModel.h"
  21. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockFileManager.h"
  22. #import "Crashlytics/UnitTests/Mocks/FIRCLSMockSettings.h"
  23. #import "Crashlytics/UnitTests/Mocks/FIRMockInstallations.h"
  24. #define TEST_BUNDLE_ID (@"com.crashlytics.test")
  25. @interface FIRRecordExceptionModelTests : XCTestCase
  26. @property(nonatomic, strong) FIRCLSMockFileManager *fileManager;
  27. @property(nonatomic, strong) FIRCLSMockSettings *mockSettings;
  28. @property(nonatomic, strong) NSString *reportPath;
  29. @end
  30. @implementation FIRRecordExceptionModelTests
  31. - (void)setUp {
  32. self.fileManager = [[FIRCLSMockFileManager alloc] init];
  33. FABMockApplicationIdentifierModel *appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
  34. self.mockSettings = [[FIRCLSMockSettings alloc] initWithFileManager:self.fileManager
  35. appIDModel:appIDModel];
  36. NSString *name = @"exception_model_report";
  37. self.reportPath = [self.fileManager.rootPath stringByAppendingPathComponent:name];
  38. [self.fileManager createDirectoryAtPath:self.reportPath];
  39. FIRCLSInternalReport *report =
  40. [[FIRCLSInternalReport alloc] initWithPath:self.reportPath
  41. executionIdentifier:@"TEST_EXECUTION_IDENTIFIER"];
  42. FIRCLSContextInitialize(report, self.mockSettings, self.fileManager);
  43. }
  44. - (void)tearDown {
  45. [[NSFileManager defaultManager] removeItemAtPath:self.fileManager.rootPath error:nil];
  46. }
  47. - (void)testWrittenCLSRecordFile {
  48. NSArray *stackTrace = @[
  49. [FIRStackFrame stackFrameWithSymbol:@"CrashyFunc" file:@"AppLib.m" line:504],
  50. [FIRStackFrame stackFrameWithSymbol:@"ApplicationMain" file:@"AppleLib" line:1],
  51. [FIRStackFrame stackFrameWithSymbol:@"main()" file:@"main.m" line:201],
  52. ];
  53. NSString *name = @"FIRExceptionModelTestsCrash";
  54. NSString *reason = @"Programmer made an error";
  55. FIRExceptionModel *exceptionModel = [FIRExceptionModel exceptionModelWithName:name reason:reason];
  56. exceptionModel.stackTrace = stackTrace;
  57. FIRCLSExceptionRecordModel(exceptionModel);
  58. NSData *data = [NSData
  59. dataWithContentsOfFile:[self.reportPath
  60. stringByAppendingPathComponent:@"custom_exception_a.clsrecord"]];
  61. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  62. NSDictionary *exception = json[@"exception"];
  63. NSArray *frames = exception[@"frames"];
  64. XCTAssertEqualObjects(exception[@"name"],
  65. @"464952457863657074696f6e4d6f64656c54657374734372617368");
  66. XCTAssertEqualObjects(exception[@"reason"], @"50726f6772616d6d6572206d61646520616e206572726f72");
  67. XCTAssertEqual(frames.count, 3);
  68. XCTAssertEqualObjects(frames[2][@"file"], @"6d61696e2e6d");
  69. XCTAssertEqual([frames[2][@"line"] intValue], 201);
  70. XCTAssertEqual([frames[2][@"offset"] intValue], 0);
  71. XCTAssertEqual([frames[2][@"pc"] intValue], 0);
  72. XCTAssertEqualObjects(frames[2][@"symbol"], @"6d61696e2829");
  73. }
  74. @end