FIRCLSUtilityTests.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #include "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
  15. #import <Foundation/Foundation.h>
  16. #import <XCTest/XCTest.h>
  17. #include "Crashlytics/Shared/FIRCLSByteUtility.h"
  18. @interface FIRCLSUtilityTests : XCTestCase
  19. @end
  20. @implementation FIRCLSUtilityTests
  21. - (void)setUp {
  22. [super setUp];
  23. // Put setup code here. This method is called before the invocation of each test method in the
  24. // class.
  25. }
  26. - (void)tearDown {
  27. // Put teardown code here. This method is called after the invocation of each test method in the
  28. // class.
  29. [super tearDown];
  30. }
  31. - (void)testHexFromByte {
  32. char output[2] = {0, 0};
  33. FIRCLSHexFromByte('A', output);
  34. XCTAssertEqual(output[0], '4', @"");
  35. XCTAssertEqual(output[1], '1', @"");
  36. }
  37. - (void)testHexFromByteForNotPrintableCharacter {
  38. char output[2] = {0, 0};
  39. FIRCLSHexFromByte(0xd0, output);
  40. XCTAssertEqual(output[0], 'd', @"");
  41. XCTAssertEqual(output[1], '0', @"");
  42. }
  43. - (void)testHexToString {
  44. const uint8_t bytes[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
  45. char string[(sizeof(bytes) * 2) + 1];
  46. FIRCLSSafeHexToString(bytes, 8, string);
  47. string[(sizeof(bytes) * 2)] = 0;
  48. XCTAssertEqualObjects([NSString stringWithUTF8String:string], @"6162636465666768", @"");
  49. }
  50. - (void)testHexToStringWithNonPrintableCharacters {
  51. const uint8_t bytes[4] = {0x52, 0xd0, 0x4e, 0x1f};
  52. char string[(sizeof(bytes) * 2) + 1];
  53. FIRCLSSafeHexToString(bytes, 4, string);
  54. XCTAssertEqualObjects([NSString stringWithUTF8String:string], @"52d04e1f", @"");
  55. }
  56. - (void)testRedactUUIDWithExpectedPattern {
  57. const char* readonly = "CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  58. "(45D62CC2-CFB5-4E33-AB61-B0684627F1B6) - Runtime: iOS 13.4 (17E8260) - "
  59. "DeviceType: iPhone SE (2nd generation)";
  60. size_t len = strlen(readonly);
  61. char message[len];
  62. strcpy(message, readonly);
  63. FIRCLSRedactUUID(message);
  64. NSString* actual = [NSString stringWithUTF8String:message];
  65. NSString* expected = @"CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  66. @"(********-****-****-****-************) - Runtime: iOS 13.4 (17E8260) - "
  67. @"DeviceType: iPhone SE (2nd generation)";
  68. XCTAssertEqualObjects(actual, expected);
  69. }
  70. - (void)testRedactUUIDWithMalformedPattern {
  71. const char* readonly = "CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  72. "(45D62CC2-CFB5-4E33-AB61-B0684627F1B6";
  73. size_t len = strlen(readonly);
  74. char message[len];
  75. strcpy(message, readonly);
  76. FIRCLSRedactUUID(message);
  77. NSString* actual = [NSString stringWithUTF8String:message];
  78. NSString* expected = @"CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  79. @"(45D62CC2-CFB5-4E33-AB61-B0684627F1B6";
  80. XCTAssertEqualObjects(actual, expected);
  81. }
  82. - (void)testRedactUUIDWithoutUUID {
  83. const char* readonly = "Fatal error: file /Users/test/src/foo/bar/ViewController.swift, line 25";
  84. size_t len = strlen(readonly);
  85. char message[len];
  86. strcpy(message, readonly);
  87. FIRCLSRedactUUID(message);
  88. NSString* actual = [NSString stringWithUTF8String:message];
  89. NSString* expected = @"Fatal error: file /Users/test/src/foo/bar/ViewController.swift, line 25";
  90. XCTAssertEqualObjects(actual, expected);
  91. }
  92. - (void)testRedactUUIDWithNull {
  93. char* message = NULL;
  94. XCTAssertNoThrow(FIRCLSRedactUUID(message));
  95. }
  96. @end