| 1 | import AppKit |
| 2 | import Darwin |
| 3 | |
| 4 | final class TestDocument: NSDocument { |
| 5 | var contents = Data() |
| 6 | override func data(ofType typeName: String) throws -> Data { contents } |
| 7 | } |
| 8 | |
| 9 | guard CommandLine.arguments.count == 2 else { |
| 10 | print("Usage: swift tools/samba-macos-test.swift <mounted-share>") |
| 11 | exit(2) |
| 12 | } |
| 13 | |
| 14 | let fm = FileManager.default |
| 15 | let directory = URL(fileURLWithPath: CommandLine.arguments[1], isDirectory: true) |
| 16 | .appendingPathComponent(".snowglobe-smb-test-\(UUID().uuidString)", isDirectory: true) |
| 17 | var failures = 0 |
| 18 | |
| 19 | func check(_ condition: Bool) throws { |
| 20 | if !condition { throw CocoaError(.fileReadCorruptFile) } |
| 21 | } |
| 22 | |
| 23 | func test(_ name: String, _ operation: () throws -> Void) { |
| 24 | do { |
| 25 | try operation() |
| 26 | print("PASS \(name)") |
| 27 | } catch { |
| 28 | failures += 1 |
| 29 | print("FAIL \(name): \(error as NSError)") |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | do { |
| 34 | try fm.createDirectory(at: directory, withIntermediateDirectories: false) |
| 35 | let file = directory.appendingPathComponent("document.rtf") |
| 36 | let first = Data("{\\rtf1 first save}".utf8) |
| 37 | let second = Data("{\\rtf1 updated save}".utf8) |
| 38 | |
| 39 | test("atomic save") { |
| 40 | try first.write(to: file) |
| 41 | try second.write(to: file, options: .atomic) |
| 42 | try check(Data(contentsOf: file) == second) |
| 43 | } |
| 44 | test("Cocoa replacement preserving permissions") { |
| 45 | let replacement = directory.appendingPathComponent("replacement.rtf") |
| 46 | try first.write(to: replacement) |
| 47 | _ = try fm.replaceItemAt(file, withItemAt: replacement) |
| 48 | try check(Data(contentsOf: file) == first) |
| 49 | } |
| 50 | test("NSDocument Save As, save and autosave") { |
| 51 | let document = TestDocument() |
| 52 | let url = directory.appendingPathComponent("café 🍀.rtf") |
| 53 | document.contents = first |
| 54 | try document.writeSafely(to: url, ofType: "public.rtf", for: .saveAsOperation) |
| 55 | document.fileURL = url |
| 56 | document.fileType = "public.rtf" |
| 57 | for operation in [NSDocument.SaveOperationType.saveOperation, .autosaveInPlaceOperation] { |
| 58 | document.contents = second |
| 59 | try document.writeSafely(to: url, ofType: "public.rtf", for: operation) |
| 60 | try check(Data(contentsOf: url) == second) |
| 61 | } |
| 62 | } |
| 63 | test("document package") { |
| 64 | let package = directory.appendingPathComponent("document.rtfd", isDirectory: true) |
| 65 | let wrapper = FileWrapper(directoryWithFileWrappers: ["TXT.rtf": FileWrapper(regularFileWithContents: first)]) |
| 66 | try wrapper.write(to: package, options: .atomic, originalContentsURL: nil) |
| 67 | try wrapper.write(to: package, options: .atomic, originalContentsURL: package) |
| 68 | try check(Data(contentsOf: package.appendingPathComponent("TXT.rtf")) == first) |
| 69 | } |
| 70 | test("metadata and large resource fork copy") { |
| 71 | let attributes = ["com.apple.snowglobe-smb-test": first, "com.apple.ResourceFork": Data(repeating: 0x5a, count: 128 * 1024)] |
| 72 | for (name, value) in attributes { |
| 73 | let result = value.withUnsafeBytes { setxattr(file.path, name, $0.baseAddress, $0.count, 0, 0) } |
| 74 | guard result == 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } |
| 75 | } |
| 76 | let copied = directory.appendingPathComponent("copied.rtf") |
| 77 | try fm.copyItem(at: file, to: copied) |
| 78 | for (name, value) in attributes { |
| 79 | var bytes = Data(count: value.count) |
| 80 | let result = bytes.withUnsafeMutableBytes { getxattr(copied.path, name, $0.baseAddress, $0.count, 0, 0) } |
| 81 | guard result >= 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } |
| 82 | try check(result == value.count && bytes == value) |
| 83 | } |
| 84 | } |
| 85 | test("Mac archive and temporary filenames") { |
| 86 | let url = directory.appendingPathComponent("._client-temp") |
| 87 | try first.write(to: url) |
| 88 | try check(Data(contentsOf: url) == first) |
| 89 | } |
| 90 | test("permission update") { |
| 91 | guard chmod(file.path, 0o660) == 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } |
| 92 | } |
| 93 | test("replace while a reader holds the file open") { |
| 94 | let reader = try FileHandle(forReadingFrom: file) |
| 95 | defer { try? reader.close() } |
| 96 | let replacement = directory.appendingPathComponent("open-replacement.rtf") |
| 97 | try second.write(to: replacement) |
| 98 | guard rename(replacement.path, file.path) == 0 else { throw NSError(domain: NSPOSIXErrorDomain, code: Int(errno)) } |
| 99 | try check(Data(contentsOf: file) == second) |
| 100 | } |
| 101 | } catch { |
| 102 | failures += 1 |
| 103 | print("FAIL fixture: \(error as NSError)") |
| 104 | } |
| 105 | test("remove disposable fixture") { try fm.removeItem(at: directory) } |
| 106 | exit(failures == 0 ? 0 : 1) |