FIRCLSUtilityTests.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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)testRedactUUID {
  57. // Define a local struct to hold the test data
  58. struct TestCase {
  59. const char *name; // Name of the test case
  60. const char *readonly; // Input string
  61. const char *expected; // Expected output string
  62. };
  63. // Initialize an array of test cases
  64. struct TestCase tests[] = {
  65. {.name = "Test with valid UUID",
  66. .readonly = "CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  67. "(45D62CC2-CFB5-4E33-AB61-B0684627F1B6) - Runtime: iOS 13.4 (17E8260) - "
  68. "DeviceType: iPhone SE (2nd generation)",
  69. .expected = "CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  70. "(********-****-****-****-************) - Runtime: iOS 13.4 (17E8260) - "
  71. "DeviceType: iPhone SE (2nd generation)"},
  72. {.name = "Test with no UUID",
  73. .readonly = "Example with no UUID",
  74. .expected = "Example with no UUID"},
  75. {.name = "Test with only UUID",
  76. .readonly = "(12345678-1234-1234-1234-123456789012)",
  77. .expected = "(********-****-****-****-************)"},
  78. {.name = "Test with malformed UUID pattern",
  79. .readonly = "CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  80. "(45D62CC2-CFB5-4E33-AB61-B0684627F1B6",
  81. .expected = "CoreSimulator 704.12.1 - Device: iPhone SE (2nd generation) "
  82. "(45D62CC2-CFB5-4E33-AB61-B0684627F1B6"},
  83. {.name = "Test with other error string",
  84. .readonly = "Fatal error: file "
  85. "/Users/test/src/foo/bar/ViewController.swift, line 25",
  86. .expected = "Fatal error: file "
  87. "/Users/test/src/foo/bar/ViewController.swift, line 25"},
  88. {.name = "Test with NULL input", .readonly = NULL, .expected = NULL}};
  89. // Loop over the array of test cases
  90. for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
  91. if (tests[i].readonly == NULL) {
  92. XCTAssertNoThrow(FIRCLSRedactUUID(NULL));
  93. continue;
  94. }
  95. // copy the message because the function modifies the input
  96. // and the input is const
  97. char message[256];
  98. snprintf(message, sizeof(message), "%s", tests[i].readonly);
  99. // Redact the UUID
  100. FIRCLSRedactUUID(message);
  101. // Compare the result with the expected output
  102. NSString *actual = [NSString stringWithUTF8String:message];
  103. NSString *expected = [NSString stringWithUTF8String:tests[i].expected];
  104. XCTAssertEqualObjects(actual, expected, @"Test named: '%s' failed", tests[i].name);
  105. }
  106. }
  107. @end