@@ -37,6 +37,8 @@ class TestNSData: XCTestCase {
37
37
( " test_base64Data_medium " , test_base64Data_medium) ,
38
38
( " test_base64Data_small " , test_base64Data_small) ,
39
39
( " test_openingNonExistentFile " , test_openingNonExistentFile) ,
40
+ ( " test_contentsOfFile " , test_contentsOfFile) ,
41
+ ( " test_contentsOfZeroFile " , test_contentsOfZeroFile) ,
40
42
( " test_basicReadWrite " , test_basicReadWrite) ,
41
43
( " test_bufferSizeCalculation " , test_bufferSizeCalculation) ,
42
44
// ("test_dataHash", test_dataHash), Disabled due to lack of brdiging in swift runtime -- infinite loops
@@ -908,6 +910,47 @@ extension TestNSData {
908
910
XCTAssertTrue ( didCatchError)
909
911
}
910
912
913
+ func test_contentsOfFile( ) {
914
+ let testDir = testBundle ( ) . resourcePath
915
+ let filename = testDir!. appending ( " /NSStringTestData.txt " )
916
+
917
+ let contents = NSData ( contentsOfFile: filename)
918
+ XCTAssertNotNil ( contents)
919
+ if let contents = contents {
920
+ let ptr = UnsafeMutableRawPointer ( mutating: contents. bytes)
921
+ let str = String ( bytesNoCopy: ptr, length: contents. length,
922
+ encoding: . ascii, freeWhenDone: false )
923
+ XCTAssertEqual ( str, " swift-corelibs-foundation " )
924
+ }
925
+ }
926
+
927
+ func test_contentsOfZeroFile( ) {
928
+ #if os(Linux)
929
+ guard FileManager . default. fileExists ( atPath: " /proc/self " ) else {
930
+ return
931
+ }
932
+ let contents = NSData ( contentsOfFile: " /proc/self/cmdline " )
933
+ XCTAssertNotNil ( contents)
934
+ if let contents = contents {
935
+ XCTAssertTrue ( contents. length > 0 )
936
+ let ptr = UnsafeMutableRawPointer ( mutating: contents. bytes)
937
+ let str = String ( bytesNoCopy: ptr, length: contents. length,
938
+ encoding: . ascii, freeWhenDone: false )
939
+ XCTAssertNotNil ( str)
940
+ if let str = str {
941
+ XCTAssertTrue ( str. hasSuffix ( " TestFoundation " ) )
942
+ }
943
+ }
944
+
945
+ do {
946
+ let maps = try String ( contentsOfFile: " /proc/self/maps " , encoding: . utf8)
947
+ XCTAssertTrue ( maps. count > 0 )
948
+ } catch {
949
+ XCTFail ( " Cannot read /proc/self/maps: \( String ( describing: error) ) " )
950
+ }
951
+ #endif
952
+ }
953
+
911
954
func test_basicReadWrite( ) {
912
955
let url = URL ( fileURLWithPath: NSTemporaryDirectory ( ) , isDirectory: true ) . appendingPathComponent ( " testfile " )
913
956
let count = 1 << 24
0 commit comments