Files
swift-torrent/Tests/SwiftTorrentTests/InfoHashTests.swift
T
Chad Paulson 6c8c581517 Implement SwiftTorrent: pure Swift BitTorrent library
Full BEP-3 peer wire protocol, BEP-5 DHT, BEP-15 UDP trackers,
magnet link support, bencode serialization, rarest-first piece
selection, and async session management using SwiftNIO and
swift-crypto. Includes 64 passing unit tests.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-29 04:17:43 -06:00

44 lines
1.4 KiB
Swift

import XCTest
@testable import SwiftTorrent
final class InfoHashTests: XCTestCase {
func testFromHex() {
let hash = InfoHash(hex: "0123456789abcdef0123456789abcdef01234567")
XCTAssertNotNil(hash)
XCTAssertEqual(hash?.bytes.count, 20)
XCTAssertEqual(hash?.version, .v1)
}
func testInvalidHex() {
XCTAssertNil(InfoHash(hex: "short"))
XCTAssertNil(InfoHash(hex: "xyz"))
}
func testV1Hash() {
let data = Data("test info dictionary".utf8)
let hash = InfoHash.v1(from: data)
XCTAssertEqual(hash.bytes.count, 20)
XCTAssertEqual(hash.version, .v1)
}
func testV2Hash() {
let data = Data("test info dictionary".utf8)
let hash = InfoHash.v2(from: data)
XCTAssertEqual(hash.bytes.count, 32)
XCTAssertEqual(hash.version, .v2)
}
func testDescription() {
let hash = InfoHash(hex: "0123456789abcdef0123456789abcdef01234567")!
XCTAssertEqual(hash.description, "0123456789abcdef0123456789abcdef01234567")
}
func testEquatable() {
let h1 = InfoHash(hex: "0123456789abcdef0123456789abcdef01234567")!
let h2 = InfoHash(hex: "0123456789abcdef0123456789abcdef01234567")!
let h3 = InfoHash(hex: "abcdef0123456789abcdef0123456789abcdef01")!
XCTAssertEqual(h1, h2)
XCTAssertNotEqual(h1, h3)
}
}