|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#import <XCTest/XCTest.h> |
| 9 | + |
| 10 | +#import <ReactCommon/RCTTurboModule.h> |
| 11 | +#import <hermes/hermes.h> |
| 12 | + |
| 13 | +#import <memory> |
| 14 | +#import <string> |
| 15 | +#import <vector> |
| 16 | + |
| 17 | +using namespace facebook; |
| 18 | +using facebook::react::TurboModuleConvertUtils::convertObjCObjectToJSIValue; |
| 19 | + |
| 20 | +// An NSString subclass that only implements the primitive methods, so |
| 21 | +// CoreFoundation can't hand out a pointer to its characters. |
| 22 | +@interface RCTTurboModuleStringConversionTestsString : NSString |
| 23 | +- (instancetype)initWithCharacters:(std::vector<unichar>)characters; |
| 24 | +@end |
| 25 | + |
| 26 | +@implementation RCTTurboModuleStringConversionTestsString { |
| 27 | + std::vector<unichar> _characters; |
| 28 | +} |
| 29 | + |
| 30 | +- (instancetype)initWithCharacters:(std::vector<unichar>)characters |
| 31 | +{ |
| 32 | + if (self = [super init]) { |
| 33 | + _characters = std::move(characters); |
| 34 | + } |
| 35 | + return self; |
| 36 | +} |
| 37 | + |
| 38 | +- (NSUInteger)length |
| 39 | +{ |
| 40 | + return _characters.size(); |
| 41 | +} |
| 42 | + |
| 43 | +- (unichar)characterAtIndex:(NSUInteger)index |
| 44 | +{ |
| 45 | + return _characters[index]; |
| 46 | +} |
| 47 | + |
| 48 | +@end |
| 49 | + |
| 50 | +namespace { |
| 51 | + |
| 52 | +std::u16string utf16FromNSString(NSString *string) |
| 53 | +{ |
| 54 | + std::u16string utf16(string.length, u'\0'); |
| 55 | + [string getCharacters:reinterpret_cast<unichar *>(utf16.data()) range:NSMakeRange(0, string.length)]; |
| 56 | + return utf16; |
| 57 | +} |
| 58 | + |
| 59 | +// Strings read from data, like the text that `FileReader` returns. |
| 60 | +NSString *stringFromUTF8Data(NSString *string) |
| 61 | +{ |
| 62 | + return [[NSString alloc] initWithData:[string dataUsingEncoding:NSUTF8StringEncoding] encoding:NSUTF8StringEncoding]; |
| 63 | +} |
| 64 | + |
| 65 | +NSString *stringWithCharacters(std::vector<unichar> characters) |
| 66 | +{ |
| 67 | + return [NSString stringWithCharacters:characters.data() length:characters.size()]; |
| 68 | +} |
| 69 | + |
| 70 | +bool hasASCIIPointer(NSString *string) |
| 71 | +{ |
| 72 | + return CFStringGetCStringPtr((__bridge CFStringRef)string, kCFStringEncodingASCII) != nullptr; |
| 73 | +} |
| 74 | + |
| 75 | +bool hasUTF16Pointer(NSString *string) |
| 76 | +{ |
| 77 | + return CFStringGetCharactersPtr((__bridge CFStringRef)string) != nullptr; |
| 78 | +} |
| 79 | + |
| 80 | +} // namespace |
| 81 | + |
| 82 | +@interface RCTTurboModuleStringConversionTests : XCTestCase |
| 83 | +@end |
| 84 | + |
| 85 | +@implementation RCTTurboModuleStringConversionTests { |
| 86 | + std::unique_ptr<jsi::Runtime> _runtime; |
| 87 | +} |
| 88 | + |
| 89 | +- (void)setUp |
| 90 | +{ |
| 91 | + [super setUp]; |
| 92 | + _runtime = facebook::hermes::makeHermesRuntime(); |
| 93 | +} |
| 94 | + |
| 95 | +- (void)tearDown |
| 96 | +{ |
| 97 | + _runtime.reset(); |
| 98 | + [super tearDown]; |
| 99 | +} |
| 100 | + |
| 101 | +- (void)assertConvertsToSameCharacters:(NSString *)string |
| 102 | +{ |
| 103 | + jsi::Value value = convertObjCObjectToJSIValue(*_runtime, string); |
| 104 | + XCTAssertTrue(value.isString(), @"Expected a JS string for \"%@\"", string); |
| 105 | + XCTAssertTrue( |
| 106 | + value.asString(*_runtime).utf16(*_runtime) == utf16FromNSString(string), |
| 107 | + @"Expected the JS string to have the same characters as \"%@\"", |
| 108 | + string); |
| 109 | +} |
| 110 | + |
| 111 | +- (void)testASCIIStringWithASCIIPointer |
| 112 | +{ |
| 113 | + NSString *string = stringFromUTF8Data(@"com.facebook.react.uiapp"); |
| 114 | + XCTAssertTrue(hasASCIIPointer(string), @"Expected the string to expose its ASCII characters"); |
| 115 | + [self assertConvertsToSameCharacters:string]; |
| 116 | + [self assertConvertsToSameCharacters:@"constant"]; |
| 117 | +} |
| 118 | + |
| 119 | +- (void)testTaggedPointerString |
| 120 | +{ |
| 121 | + // Short strings like this one are tagged pointers, which have no character buffer. |
| 122 | + NSString *string = stringWithCharacters({u'i', u'd'}); |
| 123 | + XCTAssertFalse(hasASCIIPointer(string), @"Expected the string to have no ASCII pointer"); |
| 124 | + XCTAssertFalse(hasUTF16Pointer(string), @"Expected the string to have no UTF-16 pointer"); |
| 125 | + [self assertConvertsToSameCharacters:string]; |
| 126 | +} |
| 127 | + |
| 128 | +- (void)testNonASCIIString |
| 129 | +{ |
| 130 | + NSString *string = stringFromUTF8Data(@"Zażółć gęślą jaźń"); |
| 131 | + XCTAssertTrue(hasUTF16Pointer(string), @"Expected the string to expose its UTF-16 characters"); |
| 132 | + [self assertConvertsToSameCharacters:string]; |
| 133 | + [self assertConvertsToSameCharacters:stringFromUTF8Data(@"日本語のテキスト")]; |
| 134 | +} |
| 135 | + |
| 136 | +- (void)testStringWithSurrogatePairs |
| 137 | +{ |
| 138 | + [self assertConvertsToSameCharacters:stringFromUTF8Data(@"Rocket 🚀 and family 👨👩👧")]; |
| 139 | +} |
| 140 | + |
| 141 | +- (void)testEmptyString |
| 142 | +{ |
| 143 | + [self assertConvertsToSameCharacters:@""]; |
| 144 | +} |
| 145 | + |
| 146 | +- (void)testLargeString |
| 147 | +{ |
| 148 | + NSMutableString *string = [NSMutableString new]; |
| 149 | + for (int i = 0; i < 20000; i++) { |
| 150 | + [string appendFormat:@"{\"id\":%d,\"name\":\"Zażółć %d 🚀\"},", i, i]; |
| 151 | + } |
| 152 | + [self assertConvertsToSameCharacters:stringFromUTF8Data(string)]; |
| 153 | +} |
| 154 | + |
| 155 | +- (void)testStringSubclass |
| 156 | +{ |
| 157 | + NSString *string = |
| 158 | + [[RCTTurboModuleStringConversionTestsString alloc] initWithCharacters:{u'a', u'ż', 0xD83D, 0xDE80}]; |
| 159 | + XCTAssertFalse(hasASCIIPointer(string), @"Expected the string to have no ASCII pointer"); |
| 160 | + XCTAssertFalse(hasUTF16Pointer(string), @"Expected the string to have no UTF-16 pointer"); |
| 161 | + [self assertConvertsToSameCharacters:string]; |
| 162 | +} |
| 163 | + |
| 164 | +- (void)testStringWithNullCharacterIsNotTruncated |
| 165 | +{ |
| 166 | + NSString *ascii = [[NSString alloc] initWithBytes:"aaaaaaaaaaaaaaaaaaaa\0b" length:22 encoding:NSUTF8StringEncoding]; |
| 167 | + XCTAssertTrue(hasASCIIPointer(ascii), @"Expected the string to expose its ASCII characters"); |
| 168 | + [self assertConvertsToSameCharacters:ascii]; |
| 169 | + [self assertConvertsToSameCharacters:stringWithCharacters({u'a', 0, u'b'})]; |
| 170 | + [self assertConvertsToSameCharacters:stringWithCharacters({u'ż', 0, u'b'})]; |
| 171 | +} |
| 172 | + |
| 173 | +- (void)testStringWithLoneSurrogateIsKept |
| 174 | +{ |
| 175 | + NSString *string = stringWithCharacters({u'a', 0xD800, u'b'}); |
| 176 | + XCTAssertTrue(hasUTF16Pointer(string), @"Expected the string to expose its UTF-16 characters"); |
| 177 | + [self assertConvertsToSameCharacters:string]; |
| 178 | + [self assertConvertsToSameCharacters:stringWithCharacters({u'a', 0xDC00})]; |
| 179 | +} |
| 180 | + |
| 181 | +- (void)testStringSubclassWithLoneSurrogateIsKept |
| 182 | +{ |
| 183 | + NSString *string = [[RCTTurboModuleStringConversionTestsString alloc] initWithCharacters:{u'a', 0xD800, u'b'}]; |
| 184 | + XCTAssertFalse(hasUTF16Pointer(string), @"Expected the string to have no UTF-16 pointer"); |
| 185 | + [self assertConvertsToSameCharacters:string]; |
| 186 | +} |
| 187 | + |
| 188 | +@end |
0 commit comments