FIRRecordExceptionModelTests.m 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. FIRMockInstallations *iid = [[FIRMockInstallations alloc] initWithFID:@"test_instance_id"];
  37. FIRCLSInstallIdentifierModel *installIDModel =
  38. [[FIRCLSInstallIdentifierModel alloc] initWithInstallations:iid];
  39. NSString *name = @"exception_model_report";
  40. self.reportPath = [self.fileManager.rootPath stringByAppendingPathComponent:name];
  41. [self.fileManager createDirectoryAtPath:self.reportPath];
  42. FIRCLSInternalReport *report =
  43. [[FIRCLSInternalReport alloc] initWithPath:self.reportPath
  44. executionIdentifier:@"TEST_EXECUTION_IDENTIFIER"];
  45. FIRCLSContextInitialize(report, self.mockSettings, installIDModel, self.fileManager);
  46. }
  47. - (void)tearDown {
  48. [[NSFileManager defaultManager] removeItemAtPath:self.fileManager.rootPath error:nil];
  49. }
  50. - (void)testWrittenCLSRecordFile {
  51. NSArray *stackTrace = @[
  52. [FIRStackFrame stackFrameWithSymbol:@"CrashyFunc" file:@"AppLib.m" line:504],
  53. [FIRStackFrame stackFrameWithSymbol:@"ApplicationMain" file:@"AppleLib" line:1],
  54. [FIRStackFrame stackFrameWithSymbol:@"main()" file:@"main.m" line:201],
  55. ];
  56. NSString *name = @"FIRExceptionModelTestsCrash";
  57. NSString *reason = @"Programmer made an error";
  58. FIRExceptionModel *exceptionModel = [FIRExceptionModel exceptionModelWithName:name reason:reason];
  59. exceptionModel.stackTrace = stackTrace;
  60. FIRCLSExceptionRecordModel(exceptionModel);
  61. NSData *data = [NSData
  62. dataWithContentsOfFile:[self.reportPath
  63. stringByAppendingPathComponent:@"custom_exception_a.clsrecord"]];
  64. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
  65. NSDictionary *exception = json[@"exception"];
  66. NSArray *frames = exception[@"frames"];
  67. XCTAssertEqualObjects(exception[@"name"],
  68. @"464952457863657074696f6e4d6f64656c54657374734372617368");
  69. XCTAssertEqualObjects(exception[@"reason"], @"50726f6772616d6d6572206d61646520616e206572726f72");
  70. XCTAssertEqual(frames.count, 3);
  71. XCTAssertEqualObjects(frames[2][@"file"], @"6d61696e2e6d");
  72. XCTAssertEqual([frames[2][@"line"] intValue], 201);
  73. XCTAssertEqual([frames[2][@"offset"] intValue], 0);
  74. XCTAssertEqual([frames[2][@"pc"] intValue], 0);
  75. XCTAssertEqualObjects(frames[2][@"symbol"], @"6d61696e2829");
  76. }
  77. @end