@@ -16,39 +16,65 @@ import System
1616#endif
1717
1818@available ( /*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8 , * )
19- func filePathFromUnterminatedBytes < S: Sequence > ( _ bytes: S ) -> FilePath where S. Element == UInt8 {
19+ func filePathFromInvalidCodePointSequence < S: Sequence > ( _ bytes: S ) -> FilePath where S. Element == UTF16 . CodeUnit {
2020 var array = Array ( bytes)
2121 assert ( array. last != 0 , " already null terminated " )
2222 array += [ 0 ]
2323
2424 return array. withUnsafeBufferPointer {
25- $0. withMemoryRebound ( to: CChar . self) {
25+ $0. withMemoryRebound ( to: CInterop . PlatformChar. self) {
26+ FilePath ( platformString: $0. baseAddress!)
27+ }
28+ }
29+ }
30+
31+ @available ( /*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8 , * )
32+ func filePathFromInvalidCodePointSequence< S: Sequence > ( _ bytes: S ) -> FilePath where S. Element == UTF8 . CodeUnit {
33+ var array = Array ( bytes)
34+ assert ( array. last != 0 , " already null terminated " )
35+ array += [ 0 ]
36+
37+ return array. withUnsafeBufferPointer {
38+ $0. withMemoryRebound ( to: CInterop . PlatformChar. self) {
2639 FilePath ( platformString: $0. baseAddress!)
2740 }
2841 }
2942}
30- let invalidBytes : [ UInt8 ] = [ 0x2F , 0x61 , 0x2F , 0x62 , 0x2F , 0x83 ]
3143
3244@available ( /*System 0.0.2: macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0*/iOS 8 , * )
3345final class FilePathTest : XCTestCase {
3446 struct TestPath {
3547 let filePath : FilePath
3648 let string : String
37- let validUTF8 : Bool
49+ let validString : Bool
3850 }
3951
40- let testPaths : [ TestPath ] = [
52+ #if os(Windows)
53+ static let invalidSequence : [ UTF16 . CodeUnit ] = [ 0xd800 , 0x0020 ]
54+ static let invalidSequenceTest =
55+ TestPath ( filePath: filePathFromInvalidCodePointSequence ( invalidSequence) ,
56+ string: String ( decoding: invalidSequence, as: UTF16 . self) ,
57+ validString: false )
58+ #else
59+ static let invalidSequence : [ UTF8 . CodeUnit ] = [ 0x2F , 0x61 , 0x2F , 0x62 , 0x2F , 0x83 ]
60+ static let invalidSequenceTest =
61+ TestPath ( filePath: filePathFromInvalidCodePointSequence ( invalidSequence) ,
62+ string: String ( decoding: invalidSequence, as: UTF8 . self) ,
63+ validString: false )
64+ #endif
65+
66+ var testPaths : [ TestPath ] = [
4167 // empty
42- TestPath ( filePath: FilePath ( ) , string: String ( ) , validUTF8 : true ) ,
68+ TestPath ( filePath: FilePath ( ) , string: String ( ) , validString : true ) ,
4369
4470 // valid ascii
45- TestPath ( filePath: " /a/b/c " , string: " /a/b/c " , validUTF8 : true ) ,
71+ TestPath ( filePath: " /a/b/c " , string: " /a/b/c " , validString : true ) ,
4672
4773 // valid utf8
48- TestPath ( filePath: " /あ/🧟♀️ " , string: " /あ/🧟♀️ " , validUTF8 : true ) ,
74+ TestPath ( filePath: " /あ/🧟♀️ " , string: " /あ/🧟♀️ " , validString : true ) ,
4975
50- // invalid utf8
51- TestPath ( filePath : filePathFromUnterminatedBytes ( invalidBytes ) , string : String ( decoding : invalidBytes , as : UTF8 . self ) , validUTF8 : false ) ,
76+ // invalid sequence
77+ invalidSequenceTest ,
5278 ]
5379
5480 func testFilePath( ) {
@@ -59,8 +85,8 @@ final class FilePathTest: XCTestCase {
5985
6086 XCTAssertEqual ( testPath. string, String ( decoding: testPath. filePath) )
6187
62- // TODO: test component UTF8 validation
63- if testPath. validUTF8 {
88+ // TODO: test component CodeUnit representation validation
89+ if testPath. validString {
6490 XCTAssertEqual ( testPath. filePath, FilePath ( testPath. string) )
6591 XCTAssertEqual ( testPath. string, String ( validating: testPath. filePath) )
6692 } else {
0 commit comments