39 lines
2.1 KiB
Swift
39 lines
2.1 KiB
Swift
import Foundation
|
|
import Testing
|
|
@testable import SamsungProtocol
|
|
|
|
private let parser = SamsungEventParser(maximumMessageBytes: 256)
|
|
|
|
@Test func parsesAllowlistedPairingEvents() throws {
|
|
#expect(try parser.parse(Data(#"{"event":"ms.channel.connect","data":{"token":"synthetic-token"}}"#.utf8)) == .connected(token: "synthetic-token"))
|
|
#expect(try parser.parse(Data(#"{"event":"ms.channel.connect","data":{}}"#.utf8)) == .connected(token: nil))
|
|
#expect(try parser.parse(Data(#"{"event":"ms.channel.unauthorized"}"#.utf8)) == .unauthorized)
|
|
#expect(try parser.parse(Data(#"{"event":"ms.channel.timeOut"}"#.utf8)) == .timedOut)
|
|
#expect(try parser.parse(Data(#"{"event":"ms.error","data":{"message":"redacted"}}"#.utf8)) == .error)
|
|
}
|
|
|
|
@Test func unknownEventsAreInert() throws {
|
|
let fixture = Data(#"{"event":"synthetic.unknown","data":{"token":"not-consumed"}}"#.utf8)
|
|
#expect(try parser.parse(fixture) == .unknown)
|
|
}
|
|
|
|
@Test func rejectsMalformedEmptyAndOversizedMessages() {
|
|
#expect(throws: SamsungEventParserError.empty) { try parser.parse(Data()) }
|
|
#expect(throws: SamsungEventParserError.malformed) { try parser.parse(Data(#"{"event":42}"#.utf8)) }
|
|
#expect(throws: SamsungEventParserError.oversized) { try parser.parse(Data(repeating: 0x41, count: 257)) }
|
|
}
|
|
|
|
@Test func boundsEventNamesAndTokens() {
|
|
let longEvent = String(repeating: "e", count: SamsungEventParser.maximumEventNameBytes + 1)
|
|
let eventFixture = Data(#"{"event":"\#(longEvent)"}"#.utf8)
|
|
#expect(throws: SamsungEventParserError.invalidEventName) { try parser.parse(eventFixture) }
|
|
|
|
let tokenParser = SamsungEventParser(maximumMessageBytes: 8 * 1024)
|
|
let longToken = String(repeating: "t", count: SamsungEventParser.maximumTokenBytes + 1)
|
|
let tokenFixture = Data(#"{"event":"ms.channel.connect","data":{"token":"\#(longToken)"}}"#.utf8)
|
|
#expect(throws: SamsungEventParserError.invalidToken) { try tokenParser.parse(tokenFixture) }
|
|
#expect(throws: SamsungEventParserError.invalidToken) {
|
|
try tokenParser.parse(Data(#"{"event":"ms.channel.connect","data":{"token":""}}"#.utf8))
|
|
}
|
|
}
|