Track whether server pushed a compression option

This commit is contained in:
Davide De Rosa 2019-02-23 19:54:38 +01:00
parent 8c1b95eaa7
commit 367e8b7e08
2 changed files with 43 additions and 2 deletions

View File

@ -150,6 +150,9 @@ public protocol SessionReply {
/// The optional compression framing. /// The optional compression framing.
var compressionFraming: SessionProxy.CompressionFraming? { get } var compressionFraming: SessionProxy.CompressionFraming? { get }
/// True if uses compression.
var usesCompression: Bool { get }
/// The optional keep-alive interval. /// The optional keep-alive interval.
var ping: Int? { get } var ping: Int? { get }
@ -193,7 +196,7 @@ extension SessionProxy {
static let dns = NSRegularExpression("dhcp-option DNS6? [\\d\\.a-fA-F:]+") static let dns = NSRegularExpression("dhcp-option DNS6? [\\d\\.a-fA-F:]+")
static let comp = NSRegularExpression("comp(ress|-lzo)") static let comp = NSRegularExpression("comp(ress|-lzo)[ \\w]*")
static let ping = NSRegularExpression("ping \\d+") static let ping = NSRegularExpression("ping \\d+")
@ -214,6 +217,8 @@ extension SessionProxy {
let compressionFraming: SessionProxy.CompressionFraming? let compressionFraming: SessionProxy.CompressionFraming?
let usesCompression: Bool
let ping: Int? let ping: Int?
let authToken: String? let authToken: String?
@ -241,6 +246,7 @@ extension SessionProxy {
var dnsServers: [String] = [] var dnsServers: [String] = []
var compressionFraming: SessionProxy.CompressionFraming? var compressionFraming: SessionProxy.CompressionFraming?
var usesCompression = false
var ping: Int? var ping: Int?
var authToken: String? var authToken: String?
var peerId: UInt32? var peerId: UInt32?
@ -389,10 +395,12 @@ extension SessionProxy {
switch $0[0] { switch $0[0] {
case "comp-lzo": case "comp-lzo":
compressionFraming = .compLZO compressionFraming = .compLZO
usesCompression = !(($0.count == 2) && ($0[1] == "no"))
case "compress": case "compress":
compressionFraming = .compress compressionFraming = .compress
usesCompression = ($0.count > 1)
default: default:
break break
} }
@ -422,6 +430,7 @@ extension SessionProxy {
self.dnsServers = dnsServers self.dnsServers = dnsServers
self.compressionFraming = compressionFraming self.compressionFraming = compressionFraming
self.usesCompression = usesCompression
self.ping = ping self.ping = ping
self.authToken = authToken self.authToken = authToken
self.peerId = peerId self.peerId = peerId

View File

@ -28,6 +28,8 @@ import XCTest
private extension SessionReply { private extension SessionReply {
func debug() { func debug() {
print("Compression framing: \(dnsServers)")
print("Compression: \(usesCompression)")
print("IPv4: \(ipv4?.description ?? "none")") print("IPv4: \(ipv4?.description ?? "none")")
print("IPv6: \(ipv6?.description ?? "none")") print("IPv6: \(ipv6?.description ?? "none")")
print("DNS: \(dnsServers)") print("DNS: \(dnsServers)")
@ -100,6 +102,36 @@ class PushTests: XCTestCase {
XCTAssertEqual(reply.compressionFraming, .compLZO) XCTAssertEqual(reply.compressionFraming, .compLZO)
} }
func testCompression() {
let msg = "PUSH_REPLY,dhcp-option DNS 8.8.8.8,dhcp-option DNS 4.4.4.4,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-CBC"
var reply: SessionReply
reply = try! SessionProxy.PushReply(message: msg.appending(",comp-lzo no"))!
reply.debug()
XCTAssertEqual(reply.compressionFraming, .compLZO)
XCTAssertFalse(reply.usesCompression)
reply = try! SessionProxy.PushReply(message: msg.appending(",comp-lzo"))!
reply.debug()
XCTAssertEqual(reply.compressionFraming, .compLZO)
XCTAssertTrue(reply.usesCompression)
reply = try! SessionProxy.PushReply(message: msg.appending(",comp-lzo yes"))!
reply.debug()
XCTAssertEqual(reply.compressionFraming, .compLZO)
XCTAssertTrue(reply.usesCompression)
reply = try! SessionProxy.PushReply(message: msg.appending(",compress"))!
reply.debug()
XCTAssertEqual(reply.compressionFraming, .compress)
XCTAssertFalse(reply.usesCompression)
reply = try! SessionProxy.PushReply(message: msg.appending(",compress lz4"))!
reply.debug()
XCTAssertEqual(reply.compressionFraming, .compress)
XCTAssertTrue(reply.usesCompression)
}
func testNCP() { func testNCP() {
let msg = "PUSH_REPLY,dhcp-option DNS 8.8.8.8,dhcp-option DNS 4.4.4.4,comp-lzo no,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM" let msg = "PUSH_REPLY,dhcp-option DNS 8.8.8.8,dhcp-option DNS 4.4.4.4,comp-lzo no,route 10.8.0.1,topology net30,ping 10,ping-restart 120,ifconfig 10.8.0.6 10.8.0.5,peer-id 0,cipher AES-256-GCM"
let reply = try! SessionProxy.PushReply(message: msg)! let reply = try! SessionProxy.PushReply(message: msg)!