Merge d32021049e1b053cabe765374343fc72b161da35 into 9b84b361225facd8e623f25d76f889d3dc54a879

This commit is contained in:
harshjainnn 2026-08-11 09:05:57 +02:00 committed by GitHub
commit 9b1f280821
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 11 deletions

View File

@ -7,15 +7,9 @@ public enum FileTransferLimits {
/// Compressed images after downscaling should comfortably fit under this budget.
public static let maxImageBytes: Int = 512 * 1024 // 512 KiB
/// Worst-case size once TLV metadata and binary packet framing are included for the largest payloads.
public static let maxFramedFileBytes: Int = {
let maxMetadataBytes = Int(UInt16.max) * 2 // fileName + mimeType TLVs
let tlvEnvelopeOverhead = 18 + maxMetadataBytes // TLV tags + lengths + metadata bytes
let binaryEnvelopeOverhead = BinaryProtocol.v2HeaderSize
+ BinaryProtocol.senderIDSize
+ BinaryProtocol.recipientIDSize
+ BinaryProtocol.signatureSize
return maxPayloadBytes + tlvEnvelopeOverhead + binaryEnvelopeOverhead
}()
/// Note: This is now a hard expanded-size ceiling rather than the old tlv+frame formula.
/// MUST stay in sync with AppConstants.Protocol.MAX_PAYLOAD_LENGTH in the Android repo (issue #1618).
public static let maxFramedFileBytes: Int = 10_485_760
public static func isValidPayload(_ size: Int) -> Bool {
size <= maxPayloadBytes

View File

@ -350,6 +350,17 @@ struct BinaryProtocolTests {
#expect(BinaryProtocol.decode(encoded) == nil)
}
@Test("Regression test for issue #1618: decode payload between 1.13 MiB and 10 MiB")
func payloadSizeBetweenOldAndNewLimit() throws {
let payloadSize = 2_000_000
let payload = Data(repeating: 0x41, count: payloadSize)
let packet = TestHelpers.createTestPacket(payload: payload, version: 2)
let encodedData = try #require(BinaryProtocol.encode(packet), "Failed to encode packet")
let decodedPacket = try #require(BinaryProtocol.decode(encodedData), "Failed to decode packet")
#expect(decodedPacket.payload == payload)
}
// MARK: - Message Padding Tests
@Test func messagePadding() throws {

View File

@ -56,7 +56,8 @@ final class TestHelpers {
recipientID: PeerID? = nil,
payload: Data = Data("test payload".utf8),
signature: Data? = nil,
ttl: UInt8 = 3
ttl: UInt8 = 3,
version: UInt8 = 1
) -> BitchatPacket {
return BitchatPacket(
type: type,
@ -65,7 +66,8 @@ final class TestHelpers {
timestamp: UInt64(Date().timeIntervalSince1970 * 1000),
payload: payload,
signature: signature,
ttl: ttl
ttl: ttl,
version: version
)
}