Skip to content

Commit bb07f2e

Browse files
committed
Add unit tests.
1 parent 82244a7 commit bb07f2e

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Tests/Foundation/Tests/TestNSData.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
import CoreFoundation
11+
12+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
13+
#if canImport(SwiftFoundation) && !DEPLOYMENT_RUNTIME_OBJC
14+
@testable import SwiftFoundation
15+
#else
16+
@testable import Foundation
17+
#endif
18+
#endif
19+
1020
class TestNSData: LoopbackServerTest {
1121

1222
class AllOnesImmutableData : NSData {
@@ -223,6 +233,8 @@ class TestNSData: LoopbackServerTest {
223233
("test_limitDebugDescription", test_limitDebugDescription),
224234
("test_edgeDebugDescription", test_edgeDebugDescription),
225235
("test_writeToURLOptions", test_writeToURLOptions),
236+
("test_writeToURLPermissions", test_writeToURLPermissions),
237+
("test_writeToURLPermissionsWithAtomic", test_writeToURLPermissionsWithAtomic),
226238
("test_edgeNoCopyDescription", test_edgeNoCopyDescription),
227239
("test_initializeWithBase64EncodedDataGetsDecodedData", test_initializeWithBase64EncodedDataGetsDecodedData),
228240
("test_initializeWithBase64EncodedDataWithNonBase64CharacterIsNil", test_initializeWithBase64EncodedDataWithNonBase64CharacterIsNil),
@@ -551,6 +563,51 @@ class TestNSData: LoopbackServerTest {
551563
}
552564
}
553565

566+
#if !os(Windows)
567+
// NOTE: `umask(3)` is process global. Therefore, the behavior is unknown if `withUmask(_:_:)` is used simultaniously.
568+
private func withUmask(_ mode: mode_t, _ block: () -> Void) {
569+
let original = umask(mode)
570+
block()
571+
umask(original)
572+
}
573+
#endif
574+
575+
func test_writeToURLPermissions() {
576+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows)
577+
withUmask(0) {
578+
do {
579+
let data = Data()
580+
let url = URL(fileURLWithPath: NSTemporaryDirectory() + "meow")
581+
try data.write(to: url)
582+
let fileManager = FileManager.default
583+
let permission = try fileManager._permissionsOfItem(atPath: url.path)
584+
XCTAssertEqual(0o666, permission)
585+
try! fileManager.removeItem(atPath: url.path)
586+
} catch {
587+
XCTFail()
588+
}
589+
}
590+
#endif
591+
}
592+
593+
func test_writeToURLPermissionsWithAtomic() {
594+
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT && !os(Windows)
595+
withUmask(0) {
596+
do {
597+
let data = Data()
598+
let url = URL(fileURLWithPath: NSTemporaryDirectory() + "meow")
599+
try data.write(to: url, options: .atomic)
600+
let fileManager = FileManager.default
601+
let permission = try fileManager._permissionsOfItem(atPath: url.path)
602+
XCTAssertEqual(0o666, permission)
603+
try! fileManager.removeItem(atPath: url.path)
604+
} catch {
605+
XCTFail()
606+
}
607+
}
608+
#endif
609+
}
610+
554611
func test_emptyDescription() {
555612
let expected = "<>"
556613

0 commit comments

Comments
 (0)