From b6640fd2ab869528e42322ab3268ddcec132a6e0 Mon Sep 17 00:00:00 2001 From: Ehren Kret Date: Sat, 27 Apr 2024 13:54:35 -0500 Subject: [PATCH] move signal core kit tests from ios app repo to signal core kit --- SignalCoreKitTests/src/NSObjectTest.swift | 21 +++++++++++++ SignalCoreKitTests/src/SCKStringTests.m | 36 +++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 SignalCoreKitTests/src/NSObjectTest.swift diff --git a/SignalCoreKitTests/src/NSObjectTest.swift b/SignalCoreKitTests/src/NSObjectTest.swift new file mode 100644 index 0000000..345f0e9 --- /dev/null +++ b/SignalCoreKitTests/src/NSObjectTest.swift @@ -0,0 +1,21 @@ +// +// Copyright 2024 Signal Messenger, LLC +// SPDX-License-Identifier: AGPL-3.0-only +// + +import Foundation +import XCTest + +@testable import SignalCoreKit + +class NSObjectTest: XCTestCase { + func testObjectComparison() { + let yes: NSNumber = true + let no: NSNumber = false + XCTAssertTrue(NSObject.isNullableObject(nil, equalTo: nil)) + XCTAssertFalse(NSObject.isNullableObject(yes, equalTo: nil)) + XCTAssertFalse(NSObject.isNullableObject(nil, equalTo: yes)) + XCTAssertFalse(NSObject.isNullableObject(yes, equalTo: no)) + XCTAssertTrue(NSObject.isNullableObject(yes, equalTo: yes)) + } +} diff --git a/SignalCoreKitTests/src/SCKStringTests.m b/SignalCoreKitTests/src/SCKStringTests.m index c7fb85f..852d252 100644 --- a/SignalCoreKitTests/src/SCKStringTests.m +++ b/SignalCoreKitTests/src/SCKStringTests.m @@ -7,6 +7,15 @@ NS_ASSUME_NONNULL_BEGIN +#pragma mark - private interface in NSString+OWS.h + +@interface NSString (OWS_Test) +- (NSString *)removeAllCharactersIn:(NSCharacterSet *)characterSet; +- (NSString *)filterUnsafeFilenameCharacters; +@end + +#pragma mark - + @interface SCKStringTests : XCTestCase @end @@ -106,6 +115,33 @@ - (void)testBalancedBidiControlCharacters stringByPrependingCharacter:bidiLeftToRightEmbedding]); } +- (void)testRemoveAllCharactersIn +{ + XCTAssert([[@"" removeAllCharactersIn:NSCharacterSet.letterCharacterSet] isEqual:@""]); + XCTAssert([[@"1" removeAllCharactersIn:NSCharacterSet.letterCharacterSet] isEqual:@"1"]); + XCTAssert([[@"a" removeAllCharactersIn:NSCharacterSet.letterCharacterSet] isEqual:@""]); + XCTAssert([[@"A" removeAllCharactersIn:NSCharacterSet.letterCharacterSet] isEqual:@""]); + XCTAssert([[@"abc123%^&" removeAllCharactersIn:NSCharacterSet.letterCharacterSet] isEqual:@"123%^&"]); + + XCTAssert([[@"" removeAllCharactersIn:NSCharacterSet.decimalDigitCharacterSet] isEqual:@""]); + XCTAssert([[@"1" removeAllCharactersIn:NSCharacterSet.decimalDigitCharacterSet] isEqual:@""]); + XCTAssert([[@"a" removeAllCharactersIn:NSCharacterSet.decimalDigitCharacterSet] isEqual:@"a"]); + XCTAssert([[@"A" removeAllCharactersIn:NSCharacterSet.decimalDigitCharacterSet] isEqual:@"A"]); + XCTAssert([[@"abc123%^&" removeAllCharactersIn:NSCharacterSet.decimalDigitCharacterSet] isEqual:@"abc%^&"]); +} + +- (void)testfilterUnsafeFilenameCharacters +{ + XCTAssertEqualObjects(@"1".filterUnsafeFilenameCharacters, @"1"); + XCTAssertEqualObjects(@"alice\u202Dbob".filterUnsafeFilenameCharacters, @"alice\uFFFDbob"); + XCTAssertEqualObjects(@"\u202Dalicebob".filterUnsafeFilenameCharacters, @"\uFFFDalicebob"); + XCTAssertEqualObjects(@"alicebob\u202D".filterUnsafeFilenameCharacters, @"alicebob\uFFFD"); + XCTAssertEqualObjects(@"alice\u202Ebob".filterUnsafeFilenameCharacters, @"alice\uFFFDbob"); + XCTAssertEqualObjects(@"\u202Ealicebob".filterUnsafeFilenameCharacters, @"\uFFFDalicebob"); + XCTAssertEqualObjects(@"alicebob\u202E".filterUnsafeFilenameCharacters, @"alicebob\uFFFD"); + XCTAssertEqualObjects(@"alice\u202Dbobalice\u202Ebob".filterUnsafeFilenameCharacters, @"alice\uFFFDbobalice\uFFFDbob"); +} + @end NS_ASSUME_NONNULL_END