Commit 1725697f authored by Dapperstout's avatar Dapperstout

Implement LZ4 compression in message.

parent 35ccc602
......@@ -2,15 +2,15 @@ import Foundation
public class Message
{
let version : UInt8 = 0
let id = getNextMessageId()
let type : UInt8
let contents : [UInt8]
let isCompressed : Bool
private let version : UInt8 = 0
private let id = getNextMessageId()
private let type : UInt8
private let contents : [UInt8]
private let isCompressed : Bool
public init(type : UInt8, contents : [UInt8] = [], compress : Bool = true) {
self.type = type
self.contents = contents
self.contents = compress ? syncthing.compress(contents) : contents
self.isCompressed = compress
}
......
......@@ -46,18 +46,31 @@ class MessageTests : XCTestCase {
}
func testUncompressedLengthIsIndicatedInBytesFiveThroughEight() {
let message = Message(type: 0, contents: someContents)
let message = Message(type: 0, contents: someContents, compress: false)
let bytes = message.serialize()
let length = concatenateBytes(bytes[4], bytes[5], bytes[6], bytes[7])
XCTAssertEqual(length, UInt32(someContents.count))
}
func testUncompressedContentsIsPresentInBytesNineAndForward() {
func testCompressedLengthIsIndicatedInBytesFiveThroughEight() {
let message = Message(type: 0, contents: someContents)
let bytes = message.serialize()
let length = concatenateBytes(bytes[4], bytes[5], bytes[6], bytes[7])
XCTAssertEqual(length, UInt32(compress(someContents).count))
}
func testUncompressedContentsIsPresentInBytesNineAndForward() {
let message = Message(type: 0, contents: someContents, compress: false)
let bytes = message.serialize()
XCTAssertEqual(someContents, Array(bytes[8..<bytes.count]))
}
func testCompressedContentsIsPresentInBytesNineAndForward() {
let message = Message(type: 0, contents: someContents)
let bytes = message.serialize()
XCTAssertEqual(compress(someContents), Array(bytes[8..<bytes.count]))
}
let someContents : [UInt8] = [12, 34, 56, 78]
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment