恢复 USB 相机连接、边拍边传、三品牌驱动与 SwiftUI 历史导入页,通过适配层对接现有本地上传与 OSS 管道。 Co-authored-by: Cursor <cursoragent@cursor.com>
77 lines
2.6 KiB
Swift
77 lines
2.6 KiB
Swift
//
|
|
// PTPUtilitiesTests.swift
|
|
// suixinkanTests
|
|
//
|
|
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// PTP Container 构建与解析测试。
|
|
final class PTPUtilitiesTests: XCTestCase {
|
|
/// 测试 buildCommand 与 parseContainer 往返一致。
|
|
func testBuildAndParseCommandRoundTrip() throws {
|
|
let command = PTPHelper.buildCommand(
|
|
code: PTPStandardCommand.getObject,
|
|
transactionID: 42,
|
|
parameters: [1, 2, 3]
|
|
)
|
|
|
|
let parsed = try PTPHelper.parseContainer(command)
|
|
XCTAssertEqual(parsed.length, UInt32(12 + 4 * 3))
|
|
XCTAssertEqual(parsed.type, PTPContainerType.command.rawValue)
|
|
XCTAssertEqual(parsed.code, PTPStandardCommand.getObject)
|
|
XCTAssertEqual(parsed.transactionID, 42)
|
|
XCTAssertEqual(parsed.params, [1, 2, 3])
|
|
}
|
|
|
|
/// 测试数据过短时抛出 tooShort。
|
|
func testParseContainerTooShortThrows() {
|
|
XCTAssertThrowsError(try PTPHelper.parseContainer(Data([0x01, 0x02]))) { error in
|
|
XCTAssertEqual(error as? PTPParseError, .tooShort)
|
|
}
|
|
}
|
|
|
|
/// 测试 length 与 buffer 不一致时抛出 invalidLength。
|
|
func testParseContainerInvalidLengthThrows() {
|
|
var data = Data(count: 12)
|
|
data[0] = 32
|
|
XCTAssertThrowsError(try PTPHelper.parseContainer(data)) { error in
|
|
XCTAssertEqual(error as? PTPParseError, .invalidLength)
|
|
}
|
|
}
|
|
|
|
/// 测试 objectSignature 拼接规则。
|
|
func testObjectSignature() {
|
|
XCTAssertEqual(PTPHelper.objectSignature(filename: "DSC_0001.JPG", size: 2048), "DSC_0001.JPG_2048")
|
|
}
|
|
|
|
/// 测试 sanitizeFilename 去除非法路径字符。
|
|
func testSanitizeFilename() {
|
|
XCTAssertEqual(PTPHelper.sanitizeFilename("DSC/0001.JPG"), "DSC_0001.JPG")
|
|
XCTAssertEqual(PTPHelper.sanitizeFilename(" "), "photo.jpg")
|
|
}
|
|
|
|
/// 测试从 ObjectInfo 固定偏移解析压缩大小。
|
|
func testParseObjectCompressedSize() {
|
|
var data = Data(count: 12)
|
|
data[8] = 0x00
|
|
data[9] = 0x10
|
|
data[10] = 0x00
|
|
data[11] = 0x00
|
|
XCTAssertEqual(PTPHelper.parseObjectCompressedSize(data), 4096)
|
|
}
|
|
|
|
/// 测试 ResponseOK 判断。
|
|
func testIsResponseOK() throws {
|
|
let okResponse = PTPHelper.buildCommand(
|
|
code: PTPStandardCommand.responseOK,
|
|
transactionID: 1,
|
|
parameters: []
|
|
)
|
|
var responseData = okResponse
|
|
responseData[4] = UInt8(PTPContainerType.response.rawValue & 0xFF)
|
|
responseData[5] = UInt8((PTPContainerType.response.rawValue >> 8) & 0xFF)
|
|
XCTAssertTrue(PTPHelper.isResponseOK(responseData))
|
|
}
|
|
}
|