SPB Git

Repository Public License

SafeOutputTests.swift 17.6 KB 330 lines swift

1// ORIX Convert — Packages/ORIXConvertCore/Tests/ConvertCoreTests/SafeOutputTests.swift2// Description: Work directories, generic output validation, naming templates and the conflict-aware atomic writer.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Foundation5import Testing6@testable import ConvertCore78@Suite struct WorkDirectoryTests {9    @Test func createsPrivateDirectories() throws {10        let temp = TempDir("work")11        let manager = WorkDirectoryManager(root: temp.file("root"))12        let dir = try manager.create()13        let attributes = try FileManager.default.attributesOfItem(atPath: dir.path)14        #expect((attributes[.posixPermissions] as? NSNumber)?.intValue == 0o700)15        let rootAttributes = try FileManager.default.attributesOfItem(atPath: manager.root.path)16        #expect((rootAttributes[.posixPermissions] as? NSNumber)?.intValue == 0o700)17        #expect(dir.lastPathComponent.hasPrefix("\(ProcessInfo.processInfo.processIdentifier)-"))18        let step = try manager.createSubdirectory("step-1", in: dir)19        #expect(FileManager.default.fileExists(atPath: step.path))20        #expect(try manager.create() != dir)21    }2223    @Test func removesOnlyItsOwnDirectories() throws {24        let temp = TempDir("work")25        let manager = WorkDirectoryManager(root: temp.file("root"))26        let dir = try manager.create()27        let outsider = temp.subdirectory("precious")28        manager.remove(outsider)29        #expect(FileManager.default.fileExists(atPath: outsider.path))30        manager.remove(dir)31        #expect(!FileManager.default.fileExists(atPath: dir.path))32    }3334    @Test func sweepsDirectoriesOfDeadProcessesOnly() throws {35        let temp = TempDir("work")36        let manager = WorkDirectoryManager(root: temp.file("root"))37        let mine = try manager.create()38        // PID 0x7FFFFFF0 cannot exist (above the system's maximum).39        let dead = manager.root.appendingPathComponent("2147483632-\(UUID().uuidString)")40        let oldUnknown = manager.root.appendingPathComponent("leftover")41        let freshUnknown = manager.root.appendingPathComponent("recent")42        for dir in [dead, oldUnknown, freshUnknown] { try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) }43        try FileManager.default.setAttributes([.modificationDate: Date(timeIntervalSinceNow: -3 * 86_400)], ofItemAtPath: oldUnknown.path)44        #expect(manager.sweepStale() == 2)45        #expect(FileManager.default.fileExists(atPath: mine.path), "directories of the running process are kept")46        #expect(FileManager.default.fileExists(atPath: freshUnknown.path))47        #expect(!FileManager.default.fileExists(atPath: dead.path) && !FileManager.default.fileExists(atPath: oldUnknown.path))48    }4950    @Test func sweepOfAMissingRootIsHarmless() {51        #expect(WorkDirectoryManager(root: URL(fileURLWithPath: "/nonexistent/orix-\(UUID().uuidString)")).sweepStale() == 0)52    }53}5455@Suite struct OutputValidatorTests {56    let temp = TempDir("validate")5758    private func validate(_ files: [URL], _ format: FormatID) -> [ValidationCheck] {59        OutputValidator.validate(ConversionOutput(files: files), destinationFormat: format, workDirectory: temp.url)60    }6162    @Test func acceptsAFileOfTheRightFormat() {63        let checks = validate([temp.write("a.png", bytes: Fixtures.png)], .png)64        #expect(checks.allPassed && checks.map(\.name) == ["output.not-empty", "output.format"])65    }6667    @Test func rejectsNoOutputMissingAndEmptyFiles() {68        #expect(validate([], .png).first?.passed == false)69        let missing = validate([temp.file("ghost.png")], .png)70        #expect(missing.map(\.name) == ["output.exists"] && missing[0].passed == false)71        let empty = validate([temp.write("empty.png", bytes: [])], .png)72        #expect(empty.map(\.name) == ["output.not-empty"] && empty[0].passed == false)73    }7475    @Test func rejectsContentOfAnotherFormat() {76        let checks = validate([temp.write("fake.png", "plain text")], .png)77        let format = checks.first { $0.name == "output.format" }78        #expect(format?.passed == false && format?.detail.contains("txt") == true)79        let jpegNamedPNG = validate([temp.write("b.png", bytes: [0xFF, 0xD8, 0xFF, 0xE0] + zeros(16))], .png)80        #expect(jpegNamedPNG.contains { !$0.passed })81    }8283    @Test func acceptsDeclaredEquivalences() {84        #expect(validate([temp.write("anim.png", bytes: Fixtures.apng)], .apng).allPassed)85        #expect(validate([temp.write("anim2.apng", bytes: Fixtures.apng)], .png).allPassed)86        #expect(validate([temp.write("a.tar.gz", bytes: Fixtures.gzip(Fixtures.tar()))], .tgz).allPassed)87        #expect(validate([temp.write("song.m4a", bytes: Fixtures.ftyp("M4A "))], .alac).allPassed)88        #expect(validate([temp.write("doc.md", "no markdown syntax here")], .markdown).allPassed)89    }9091    @Test func formatsWithoutSignaturePassOnExtensionAndSaySo() {92        let checks = validate([temp.write("tex.tga", bytes: [9, 9, 99, 1, 2, 3, 4, 5] + zeros(32))], .tga)93        #expect(checks.allPassed)94        #expect(checks.last?.detail.contains("no content signature") == true)95    }9697    @Test func rejectsOutputsOutsideTheWorkDirectory() {98        let other = TempDir("elsewhere")99        let checks = validate([other.write("a.png", bytes: Fixtures.png)], .png)100        #expect(checks.map(\.name) == ["output.location"] && checks[0].passed == false)101    }102103    @Test func rejectsSymlinkOutputs() throws {104        let real = temp.write("real.png", bytes: Fixtures.png)105        let link = temp.file("link.png")106        try FileManager.default.createSymbolicLink(at: link, withDestinationURL: real)107        #expect(validate([link], .png).contains { !$0.passed })108    }109110    @Test func directoryOutputMustContainFiles() {111        let empty = temp.subdirectory("empty")112        #expect(validate([empty], .folder).contains { $0.name == "output.not-empty" && !$0.passed })113        temp.write("full/a/b.txt", "x")114        #expect(validate([temp.file("full")], .folder).allPassed)115    }116117    @Test func directoryOutputMayNotLinkOutside() throws {118        temp.write("tree/a.txt", "x")119        try FileManager.default.createSymbolicLink(atPath: temp.file("tree/inner").path, withDestinationPath: "a.txt")120        #expect(validate([temp.file("tree")], .folder).allPassed, "a link staying inside is fine")121        try FileManager.default.createSymbolicLink(atPath: temp.file("tree/escape").path, withDestinationPath: "../../etc")122        let escaping = validate([temp.file("tree")], .folder)123        #expect(escaping.contains { $0.name == "output.no-symlink-escape" && !$0.passed && $0.detail.contains("escape") })124        try FileManager.default.removeItem(at: temp.file("tree/escape"))125        try FileManager.default.createSymbolicLink(atPath: temp.file("tree/abs").path, withDestinationPath: "/etc/hosts")126        #expect(validate([temp.file("tree")], .folder).contains { !$0.passed })127    }128129    @Test func multipleOutputsAreAllChecked() {130        let good = temp.write("p-1.png", bytes: Fixtures.png)131        let bad = temp.write("p-2.png", "nope")132        let checks = validate([good, bad], .png)133        #expect(checks.filter { !$0.passed }.count == 1 && checks.count == 4)134    }135}136137@Suite struct OutputNameTemplateTests {138    let context = OutputNameContext(name: "Holiday", ext: "heic", date: Date(timeIntervalSince1970: 1_700_000_000), counter: 7, width: 4032, height: 3024)139140    @Test func rendersEveryToken() {141        #expect(OutputNameTemplate.render("{name}", context) == "Holiday")142        #expect(OutputNameTemplate.render("{name}-{ext}", context) == "Holiday-heic")143        #expect(OutputNameTemplate.render("{name} {width}x{height}", context) == "Holiday 4032x3024")144        #expect(OutputNameTemplate.render("{counter}_{name}", context) == "7_Holiday")145        #expect(OutputNameTemplate.render("{counter:3}_{name}", context) == "007_Holiday")146        #expect(OutputNameTemplate.render("{date} {name}", context).hasSuffix(" Holiday"))147        #expect(OutputNameTemplate.render("{date}", context).count == 10)148        #expect(OutputNameTemplate.render("", context) == "Holiday")149        #expect(OutputNameTemplate.render("{unknown}-{name}", context) == "{unknown}-Holiday")150    }151152    @Test func unknownDimensionsDisappearCleanly() {153        var c = context154        c.width = nil155        c.height = nil156        #expect(OutputNameTemplate.render("{name} {width}x{height}", c) == "Holiday")157        #expect(OutputNameTemplate.render("{name}_{width}", c) == "Holiday")158        #expect(OutputNameTemplate.render("{width}x{height}", c) == "untitled")159    }160161    @Test func sanitisesDangerousCharacters() {162        #expect(OutputNameTemplate.sanitize("a/b:c") == "a-b-c")163        #expect(OutputNameTemplate.sanitize("..hidden") == "hidden")164        #expect(OutputNameTemplate.sanitize("  spaced  ") == "spaced")165        #expect(OutputNameTemplate.sanitize("tab\there\u{0}nul\nline") == "tab-here-nul-line")166        #expect(OutputNameTemplate.sanitize("") == "untitled")167        #expect(OutputNameTemplate.sanitize("...") == "untitled")168        #expect(OutputNameTemplate.sanitize("../../etc/passwd") == "-..-etc-passwd")169        #expect(OutputNameTemplate.render("{name}", OutputNameContext(name: "../../x")) == "-..-x")170    }171172    @Test func limitsNamesTo255Bytes() {173        let long = String(repeating: "é", count: 400)   // 800 bytes174        let name = OutputNameTemplate.fileName(base: long, ext: "jpeg")175        #expect(name.utf8.count <= 255 && name.hasSuffix(".jpeg"))176        #expect(String(name.dropLast(5)).allSatisfy { $0 == "é" }, "cut on a character boundary")177        let emoji = OutputNameTemplate.fileName(base: String(repeating: "👨‍👩‍👧‍👦", count: 40), ext: "png")178        #expect(emoji.utf8.count <= 255 && emoji.hasSuffix(".png"))179        #expect(OutputNameTemplate.fileName(base: "plain", ext: "") == "plain")180    }181182    @Test func contextFromSourceURL() {183        let c = OutputNameContext(source: URL(fileURLWithPath: "/tmp/My Photo.JPG"), counter: 2)184        #expect(c.name == "My Photo" && c.ext == "jpg" && c.counter == 2)185    }186}187188@Suite struct SafeWriterTests {189    let temp = TempDir("writer")190191    private func staged(_ name: String, _ text: String = "new") -> URL { temp.write("work/" + name, text) }192    private var destination: URL { temp.subdirectory("dest") }193194    @Test func movesAFileIntoPlace() throws {195        let item = staged("a.txt")196        let result = try SafeWriter.commit([item], to: destination, policy: .askUser)197        #expect(result.map(\.outcome) == [.written])198        #expect(temp.read("dest/a.txt") == "new")199        #expect(!FileManager.default.fileExists(atPath: item.path), "moved, not copied")200        #expect(temp.list("dest") == ["a.txt"], "no staging leftovers")201    }202203    @Test func createsTheDestinationFolder() throws {204        let deep = temp.file("x/y/z")205        _ = try SafeWriter.commit([staged("a.txt")], to: deep, policy: .rename)206        #expect(FileManager.default.fileExists(atPath: deep.appendingPathComponent("a.txt").path))207    }208209    @Test func askUserReportsTheConflictAndKeepsBothFiles() {210        temp.write("dest/a.txt", "old")211        let item = staged("a.txt")212        #expect(throws: ConversionError.destinationExists(temp.file("dest/a.txt").path)) {213            try SafeWriter.commit([item], to: destination, policy: .askUser)214        }215        #expect(temp.read("dest/a.txt") == "old")216        #expect(FileManager.default.fileExists(atPath: item.path), "the converted file is still available for a later decision")217        #expect(temp.list("dest") == ["a.txt"])218    }219220    @Test func renameNumbersTheOutput() throws {221        temp.write("dest/a.txt", "old")222        let first = try SafeWriter.commit([staged("a.txt", "v2")], to: destination, policy: .rename)223        #expect(first[0].outcome == .renamed && first[0].url.lastPathComponent == "a 2.txt")224        let second = try SafeWriter.commit([staged("a.txt", "v3")], to: destination, policy: .rename)225        #expect(second[0].url.lastPathComponent == "a 3.txt")226        #expect(temp.read("dest/a.txt") == "old" && temp.read("dest/a 2.txt") == "v2" && temp.read("dest/a 3.txt") == "v3")227    }228229    @Test func skipKeepsTheExistingFile() throws {230        temp.write("dest/a.txt", "old")231        let result = try SafeWriter.commit([staged("a.txt")], to: destination, policy: .skip)232        #expect(result[0].outcome == .skipped && temp.read("dest/a.txt") == "old")233        #expect(temp.list("dest") == ["a.txt"])234    }235236    @Test func replaceSwapsAtomically() throws {237        temp.write("dest/a.txt", "old")238        let result = try SafeWriter.commit([staged("a.txt", "fresh")], to: destination, policy: .replace)239        #expect(result[0].outcome == .replaced && temp.read("dest/a.txt") == "fresh")240        #expect(temp.list("dest") == ["a.txt"])241    }242243    @Test func neverOverwritesTheSourceWithoutExplicitPermission() throws {244        let source = temp.write("dest/a.txt", "original")245        #expect(throws: ConversionError.self) {246            try SafeWriter.commit([staged("a.txt")], to: destination, policy: .replace, sources: [source])247        }248        #expect(throws: ConversionError.self) {249            try SafeWriter.commit([staged("a.txt")], to: destination, policy: .askUser, sources: [source])250        }251        #expect(temp.read("dest/a.txt") == "original")252        let renamed = try SafeWriter.commit([staged("a.txt", "converted")], to: destination, policy: .rename, sources: [source])253        #expect(renamed[0].url.lastPathComponent == "a 2.txt" && temp.read("dest/a.txt") == "original")254        let skipped = try SafeWriter.commit([staged("a.txt")], to: destination, policy: .skip, sources: [source])255        #expect(skipped[0].outcome == .skipped && temp.read("dest/a.txt") == "original")256    }257258    @Test func replacesTheSourceOnlyWithBothFlags() throws {259        let source = temp.write("dest/a.txt", "original")260        let result = try SafeWriter.commit([staged("a.txt", "converted")], to: destination, policy: .replace, sources: [source], allowReplacingSource: true)261        #expect(result[0].outcome == .replaced && temp.read("dest/a.txt") == "converted")262    }263264    @Test func sourceIsRecognisedThroughSymlinksAndCase() throws {265        let source = temp.write("dest/a.txt", "original")266        let alias = temp.file("alias")267        try FileManager.default.createSymbolicLink(at: alias, withDestinationURL: destination)268        #expect(throws: ConversionError.self) {269            try SafeWriter.commit([staged("a.txt")], to: alias, policy: .replace, sources: [source])270        }271        #expect(SafeWriter.isSameFile(source, alias.appendingPathComponent("a.txt")))272        #expect(!SafeWriter.isSameFile(source, temp.file("dest/b.txt")))273        #expect(temp.read("dest/a.txt") == "original")274    }275276    @Test func copyLeavesTheItemInPlace() throws {277        let item = staged("a.txt", "kept")278        _ = try SafeWriter.commit([item], to: destination, policy: .rename, copy: true)279        #expect(FileManager.default.fileExists(atPath: item.path) && temp.read("dest/a.txt") == "kept")280    }281282    @Test func commitsDirectories() throws {283        temp.write("work/bundle/one.txt", "1")284        temp.write("work/bundle/sub/two.txt", "2")285        temp.write("dest/bundle/old.txt", "old")286        let renamed = try SafeWriter.commit([temp.file("work/bundle")], to: destination, policy: .rename)287        #expect(renamed[0].url.lastPathComponent == "bundle 2" && temp.read("dest/bundle 2/sub/two.txt") == "2")288        temp.write("work/bundle/one.txt", "again")289        let replaced = try SafeWriter.commit([temp.file("work/bundle")], to: destination, policy: .replace)290        #expect(replaced[0].outcome == .replaced && temp.read("dest/bundle/one.txt") == "again" && !temp.exists("dest/bundle/old.txt"))291    }292293    @Test func explicitNamesAreSanitised() throws {294        let result = try SafeWriter.commit([staged("a.txt")], names: ["../evil:name.txt"], to: destination, policy: .rename)295        #expect(result[0].url.deletingLastPathComponent().standardizedFileURL.path == destination.standardizedFileURL.path)296        #expect(result[0].url.lastPathComponent == "-evil-name.txt")297    }298299    @Test func resolvedDestinationPredictsTheOutcome() {300        temp.write("dest/a.txt", "old")301        #expect(SafeWriter.resolvedDestination(name: "b.txt", in: destination, policy: .skip)?.lastPathComponent == "b.txt")302        #expect(SafeWriter.resolvedDestination(name: "a.txt", in: destination, policy: .skip) == nil)303        #expect(SafeWriter.resolvedDestination(name: "a.txt", in: destination, policy: .rename)?.lastPathComponent == "a 2.txt")304        #expect(SafeWriter.resolvedDestination(name: "a.txt", in: destination, policy: .replace)?.lastPathComponent == "a.txt")305        #expect(SafeWriter.firstFreeName(for: "archive.tar.gz", in: destination).lastPathComponent == "archive.tar 2.gz")306    }307308    @Test func unwritableDestinationIsAWriteError() {309        #expect(throws: ConversionError.self) {310            try SafeWriter.commit([staged("a.txt")], to: URL(fileURLWithPath: "/dev/null/impossible"), policy: .rename)311        }312    }313314    @Test func manyWritersNeverClobberEachOther() async throws {315        let dest = destination316        let items = (0..<24).map { staged("job\($0)/same.txt", "content \($0)") }317        let urls = try await withThrowingTaskGroup(of: URL.self) { group -> [URL] in318            for item in items {319                group.addTask { try SafeWriter.commit([item], to: dest, policy: .rename)[0].url }320            }321            var all: [URL] = []322            for try await url in group { all.append(url) }323            return all324        }325        #expect(Set(urls.map(\.lastPathComponent)).count == 24)326        let contents = Set(temp.list("dest").map { temp.read("dest/" + $0) })327        #expect(contents.count == 24, "every writer's content survived")328    }329}330
Files main
Menu

Theme