2024-09-23 13:02:26 +00:00
|
|
|
//
|
|
|
|
// ProfileImporterTests.swift
|
|
|
|
// Passepartout
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/12/24.
|
|
|
|
// Copyright (c) 2024 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of Passepartout.
|
|
|
|
//
|
|
|
|
// Passepartout is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Passepartout is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Passepartout. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
2024-10-29 13:30:41 +00:00
|
|
|
@testable import AppUIMain
|
2024-09-23 13:02:26 +00:00
|
|
|
import Combine
|
2024-11-03 12:16:13 +00:00
|
|
|
import CommonLibrary
|
2024-09-23 13:02:26 +00:00
|
|
|
import Foundation
|
|
|
|
import PassepartoutKit
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
final class ProfileImporterTests: XCTestCase {
|
|
|
|
private let registry = Registry(
|
|
|
|
allHandlers: [SomeModule.moduleHandler],
|
|
|
|
allImplementations: [SomeModule.Implementation()]
|
|
|
|
)
|
|
|
|
|
|
|
|
private var subscriptions: Set<AnyCancellable> = []
|
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
extension ProfileImporterTests {
|
|
|
|
func test_givenNoURLs_whenImport_thenNothingIsImported() async throws {
|
|
|
|
let sut = ProfileImporter()
|
|
|
|
let profileManager = ProfileManager(profiles: [])
|
|
|
|
|
|
|
|
try await sut.tryImport(urls: [], profileManager: profileManager, registry: registry)
|
|
|
|
XCTAssertEqual(sut.nextURL, nil)
|
2024-11-22 11:52:51 +00:00
|
|
|
XCTAssertTrue(profileManager.previews.isEmpty)
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenURL_whenImport_thenOneProfileIsImported() async throws {
|
|
|
|
let sut = ProfileImporter()
|
|
|
|
let profileManager = ProfileManager(profiles: [])
|
|
|
|
let url = URL(string: "file:///filename.txt")!
|
|
|
|
|
|
|
|
let exp = expectation(description: "Save")
|
|
|
|
profileManager
|
2024-09-30 12:56:20 +00:00
|
|
|
.didChange
|
|
|
|
.sink {
|
|
|
|
switch $0 {
|
|
|
|
case .save(let profile):
|
2024-09-30 15:40:24 +00:00
|
|
|
XCTAssertEqual(profile.modules.count, 2)
|
2024-09-30 12:56:20 +00:00
|
|
|
XCTAssertTrue(profile.modules.first is SomeModule)
|
2024-09-30 15:40:24 +00:00
|
|
|
XCTAssertTrue(profile.modules.last is OnDemandModule)
|
2024-09-30 12:56:20 +00:00
|
|
|
exp.fulfill()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
try await sut.tryImport(
|
|
|
|
urls: [url],
|
|
|
|
profileManager: profileManager,
|
|
|
|
registry: registry
|
|
|
|
)
|
|
|
|
XCTAssertEqual(sut.nextURL, nil)
|
|
|
|
|
|
|
|
await fulfillment(of: [exp])
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenURLRequiringPassphrase_whenImportWithPassphrase_thenProfileIsImported() async throws {
|
|
|
|
let sut = ProfileImporter()
|
|
|
|
let profileManager = ProfileManager(profiles: [])
|
|
|
|
let url = URL(string: "file:///filename.encrypted")!
|
|
|
|
|
|
|
|
let exp = expectation(description: "Save")
|
|
|
|
profileManager
|
2024-09-30 12:56:20 +00:00
|
|
|
.didChange
|
|
|
|
.sink {
|
|
|
|
switch $0 {
|
|
|
|
case .save(let profile):
|
2024-09-30 15:40:24 +00:00
|
|
|
XCTAssertEqual(profile.modules.count, 2)
|
2024-09-30 12:56:20 +00:00
|
|
|
XCTAssertTrue(profile.modules.first is SomeModule)
|
2024-09-30 15:40:24 +00:00
|
|
|
XCTAssertTrue(profile.modules.last is OnDemandModule)
|
2024-09-30 12:56:20 +00:00
|
|
|
exp.fulfill()
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2024-09-23 13:02:26 +00:00
|
|
|
}
|
|
|
|
.store(in: &subscriptions)
|
|
|
|
|
|
|
|
try await sut.tryImport(
|
|
|
|
urls: [url],
|
|
|
|
profileManager: profileManager,
|
|
|
|
registry: registry
|
|
|
|
)
|
|
|
|
XCTAssertEqual(sut.nextURL, url)
|
|
|
|
|
|
|
|
sut.currentPassphrase = "passphrase"
|
|
|
|
try await sut.reImport(url: url, profileManager: profileManager, registry: registry)
|
|
|
|
XCTAssertEqual(sut.nextURL, nil)
|
|
|
|
|
|
|
|
await fulfillment(of: [exp])
|
|
|
|
}
|
|
|
|
|
|
|
|
func test_givenURLsRequiringPassphrase_whenImport_thenURLsArePending() async throws {
|
|
|
|
let sut = ProfileImporter()
|
|
|
|
let profileManager = ProfileManager(profiles: [])
|
|
|
|
let url = URL(string: "file:///filename.encrypted")!
|
|
|
|
|
|
|
|
try await sut.tryImport(
|
|
|
|
urls: [url, url, url],
|
|
|
|
profileManager: profileManager,
|
|
|
|
registry: registry
|
|
|
|
)
|
|
|
|
XCTAssertEqual(sut.nextURL, url)
|
|
|
|
XCTAssertEqual(sut.urlsRequiringPassphrase.count, 3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private struct SomeModule: Module {
|
|
|
|
struct Implementation: ModuleImplementation, ModuleImporter {
|
|
|
|
var moduleHandlerId: ModuleHandler.ID {
|
|
|
|
moduleHandler.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func module(fromURL url: URL, object: Any?) throws -> Module {
|
|
|
|
if url.absoluteString.hasSuffix(".encrypted") {
|
|
|
|
guard let passphrase = object as? String else {
|
|
|
|
throw PassepartoutError(.OpenVPN.passphraseRequired)
|
|
|
|
}
|
|
|
|
guard passphrase == "passphrase" else {
|
|
|
|
throw PassepartoutError(.crypto)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SomeModule()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|