Buckets:
localhost1978/localhost / LOCALHOST1978 /Modules /SSK_Providers /Tests /SSK_ProvidersTests /DocGenHostConfigurationStoreTests.swift
| #if os(macOS) | |
| import Darwin | |
| import Foundation | |
| import XCTest | |
| @testable import SSK_Providers | |
| final class DocGenHostConfigurationStoreTests: XCTestCase { | |
| func testRoundTripUsesPrivateOwnerModes() throws { | |
| let fixture = try Fixture() | |
| defer { fixture.remove() } | |
| let workspace = fixture.root.appendingPathComponent("workspace", isDirectory: true) | |
| let vault = fixture.root.appendingPathComponent("vault", isDirectory: true) | |
| try FileManager.default.createDirectory(at: workspace, withIntermediateDirectories: true) | |
| try FileManager.default.createDirectory(at: vault, withIntermediateDirectories: true) | |
| try fixture.store.save(workspaceURL: workspace, vaultURL: vault) | |
| XCTAssertEqual( | |
| try fixture.store.load(), | |
| DocGenHostConfiguration(workspaceURL: workspace, vaultURL: vault) | |
| ) | |
| XCTAssertEqual(try permissions(of: fixture.directory), 0o700) | |
| XCTAssertEqual(try permissions(of: fixture.configurationFile), 0o600) | |
| } | |
| func testMissingStoreLoadsAsNoConfiguration() throws { | |
| let fixture = try Fixture(createDirectory: false) | |
| defer { fixture.remove() } | |
| XCTAssertNil(try fixture.store.load()) | |
| } | |
| func testRelativePersistedPathsAreRejected() throws { | |
| let fixture = try Fixture() | |
| defer { fixture.remove() } | |
| try Data( | |
| #"{"version":1,"workspacePath":"relative/workspace","vaultPath":"relative/vault"}"#.utf8 | |
| ).write(to: fixture.configurationFile) | |
| try FileManager.default.setAttributes( | |
| [.posixPermissions: NSNumber(value: 0o600)], | |
| ofItemAtPath: fixture.configurationFile.path | |
| ) | |
| XCTAssertThrowsError(try fixture.store.load()) { error in | |
| XCTAssertEqual( | |
| error as? DocGenHostConfigurationStoreError, | |
| .malformedConfiguration | |
| ) | |
| } | |
| } | |
| func testMalformedDataIsRejected() throws { | |
| let fixture = try Fixture() | |
| defer { fixture.remove() } | |
| try Data("not-json".utf8).write(to: fixture.configurationFile) | |
| try FileManager.default.setAttributes( | |
| [.posixPermissions: NSNumber(value: 0o600)], | |
| ofItemAtPath: fixture.configurationFile.path | |
| ) | |
| XCTAssertThrowsError(try fixture.store.load()) { error in | |
| XCTAssertEqual( | |
| error as? DocGenHostConfigurationStoreError, | |
| .malformedConfiguration | |
| ) | |
| } | |
| } | |
| func testUnsafeFilePermissionsAreRejected() throws { | |
| let fixture = try Fixture() | |
| defer { fixture.remove() } | |
| try Data( | |
| #"{"version":1,"workspacePath":"/private/tmp","vaultPath":"/private/tmp"}"#.utf8 | |
| ).write(to: fixture.configurationFile) | |
| try FileManager.default.setAttributes( | |
| [.posixPermissions: NSNumber(value: 0o644)], | |
| ofItemAtPath: fixture.configurationFile.path | |
| ) | |
| XCTAssertThrowsError(try fixture.store.load()) { error in | |
| XCTAssertEqual(error as? DocGenHostConfigurationStoreError, .insecureStorage) | |
| } | |
| } | |
| func testUnsafeDirectoryPermissionsAreRejected() throws { | |
| let fixture = try Fixture() | |
| defer { fixture.remove() } | |
| try FileManager.default.setAttributes( | |
| [.posixPermissions: NSNumber(value: 0o755)], | |
| ofItemAtPath: fixture.directory.path | |
| ) | |
| XCTAssertThrowsError(try fixture.store.load()) { error in | |
| XCTAssertEqual(error as? DocGenHostConfigurationStoreError, .insecureStorage) | |
| } | |
| } | |
| func testConfigurationFileSymlinkIsRejected() throws { | |
| let fixture = try Fixture() | |
| defer { fixture.remove() } | |
| let outside = fixture.root.appendingPathComponent("outside.json") | |
| try Data( | |
| #"{"version":1,"workspacePath":"/private/tmp","vaultPath":"/private/tmp"}"#.utf8 | |
| ).write(to: outside) | |
| try FileManager.default.createSymbolicLink( | |
| at: fixture.configurationFile, | |
| withDestinationURL: outside | |
| ) | |
| XCTAssertThrowsError(try fixture.store.load()) { error in | |
| XCTAssertEqual(error as? DocGenHostConfigurationStoreError, .insecureStorage) | |
| } | |
| } | |
| func testStoreDirectorySymlinkIsRejected() throws { | |
| let fixture = try Fixture(createDirectory: false) | |
| defer { fixture.remove() } | |
| let outside = fixture.root.appendingPathComponent("outside", isDirectory: true) | |
| try FileManager.default.createDirectory(at: outside, withIntermediateDirectories: true) | |
| try FileManager.default.createSymbolicLink( | |
| at: fixture.directory, | |
| withDestinationURL: outside | |
| ) | |
| XCTAssertThrowsError(try fixture.store.load()) { error in | |
| XCTAssertEqual(error as? DocGenHostConfigurationStoreError, .insecureStorage) | |
| } | |
| } | |
| func testWrongOwnerMetadataIsRejected() { | |
| var metadata = stat() | |
| metadata.st_mode = mode_t(S_IFREG) | mode_t(0o600) | |
| metadata.st_uid = geteuid() &+ 1 | |
| XCTAssertThrowsError( | |
| try DocGenHostConfigurationStore.validate( | |
| metadata: metadata, | |
| kind: .regularFile | |
| ) | |
| ) { error in | |
| XCTAssertEqual(error as? DocGenHostConfigurationStoreError, .insecureStorage) | |
| } | |
| } | |
| private func permissions(of url: URL) throws -> Int { | |
| let attributes = try FileManager.default.attributesOfItem(atPath: url.path) | |
| return try XCTUnwrap((attributes[.posixPermissions] as? NSNumber)?.intValue) | |
| } | |
| } | |
| private struct Fixture { | |
| let root: URL | |
| let directory: URL | |
| let configurationFile: URL | |
| let store: DocGenHostConfigurationStore | |
| init(createDirectory: Bool = true) throws { | |
| root = FileManager.default.temporaryDirectory | |
| .appendingPathComponent("SSKDocGenHostStore-\(UUID().uuidString)", isDirectory: true) | |
| directory = root.appendingPathComponent("store", isDirectory: true) | |
| configurationFile = directory.appendingPathComponent("configuration.json") | |
| store = DocGenHostConfigurationStore(directoryURL: directory) | |
| try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) | |
| if createDirectory { | |
| try FileManager.default.createDirectory( | |
| at: directory, | |
| withIntermediateDirectories: true, | |
| attributes: [.posixPermissions: NSNumber(value: 0o700)] | |
| ) | |
| try FileManager.default.setAttributes( | |
| [.posixPermissions: NSNumber(value: 0o700)], | |
| ofItemAtPath: directory.path | |
| ) | |
| } | |
| } | |
| func remove() { | |
| try? FileManager.default.removeItem(at: root) | |
| } | |
| } | |
| #endif | |
Xet Storage Details
- Size:
- 6.82 kB
- Xet hash:
- 0f20ce8b838a7633e53130f098bea60a80c1808e8a3333206a21f2ae2842206e
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.