Skip to content

Commit b533b3a

Browse files
committed
update peer test
1 parent 1d0befa commit b533b3a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Networking/Sources/Networking/Connection.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public protocol ConnectionInfoProtocol {
1212
var remoteAddress: NetAddr { get }
1313
}
1414

15+
enum ConnectionError: Error {
16+
case receiveFailed
17+
}
18+
1519
public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoProtocol {
1620
let connection: QuicConnection
1721
let impl: PeerImpl<Handler>
@@ -49,7 +53,15 @@ public final class Connection<Handler: StreamHandler>: Sendable, ConnectionInfoP
4953
response.append(nextData)
5054
break
5155
}
52-
return response
56+
guard response.count >= 4 else {
57+
stream.close(abort: true)
58+
throw ConnectionError.receiveFailed
59+
}
60+
let lengthData = response.prefix(4)
61+
let length = UInt32(
62+
littleEndian: lengthData.withUnsafeBytes { $0.loadUnaligned(as: UInt32.self) }
63+
)
64+
return response.dropFirst(4).prefix(Int(length))
5365
}
5466

5567
@discardableResult

Networking/Tests/NetworkingTests/PeerTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ struct PeerTests {
211211
let data1 = try await connection1.request(
212212
MockRequest(kind: .typeA, data: Data("hello world".utf8))
213213
)
214-
#expect(data1.suffix(from: 4) == Data("hello world".utf8))
214+
#expect(data1 == Data("hello world".utf8))
215215

216216
let connection2 = try peer2.connect(
217217
to: NetAddr(ipAddress: "127.0.0.1", port: 8083)!, role: .validator
@@ -221,6 +221,6 @@ struct PeerTests {
221221
let data2 = try await connection2.request(
222222
MockRequest(kind: .typeB, data: Data("I am jam".utf8))
223223
)
224-
#expect(data2.suffix(from: 4) == Data("I am jam".utf8))
224+
#expect(data2 == Data("I am jam".utf8))
225225
}
226226
}

0 commit comments

Comments
 (0)