Files
swift-nio-http2/Tests/NIOHPACKTests/HeaderTableTests.swift
T
Jim Dovey 6f8f523684 Implementation of HPACK encoding and header tables (#10)
Motivation:

This is the beginning of removing the dependency on `nghttp2`.

Modifications:

I've added a new library target, `NIOHPACK`, which contains all the necessary pieces to implement HPACK encoding as specified in RFC 7541. There are unit tests exercising all the code and some additional edge cases around integer encoding, along with some performance tests for the Huffman encode/decode process. The latter currently takes longer to decode than to encode, which is worrying, but this is only really noticeable when encoding > 16KB strings as a rule; therefore I've decided not to pull out the optimization hammer just yet. Included in the unit tests are the inputs and outputs from all the examples in RFC 7541 and RFC 7540.

I've tried to match the coding style from the repo, but some of my own idioms may have crept in here & there. Please let me know if you see anything wrong in that regard.

Currently the static header table and Huffman encoding tables are implemented as Swift `let`s. For the static header table that's ok (only 62 entries), but the Huffman table is huge. I'm debating whether it's worth having that in a plain C file somewhere, but I'm not sure what the tradeoffs are vs. performance. Right now it seems to only affect compilation time, but I may be wrong.

Result:

In general, nothing will change: nothing in NIOHTTP2 is using this library yet. That part is what I'm working on now.
2018-08-16 09:51:13 +01:00

150 lines
8.0 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import XCTest
import NIO
@testable import NIOHPACK
func XCTAssertEqualTuple<T1: Equatable, T2: Equatable>(_ expression1: @autoclosure () throws -> (T1, T2), _ expression2: @autoclosure () throws -> (T1, T2), _ message: @autoclosure () -> String = "", file: StaticString = #file, line: UInt = #line) {
let ex1: (T1, T2)
let ex2: (T1, T2)
do {
ex1 = try expression1()
ex2 = try expression2()
}
catch {
XCTFail("Unexpected exception: \(error) \(message())", file: file, line: line)
return
}
XCTAssertEqual(ex1.0, ex2.0, message(), file: file, line: line)
XCTAssertEqual(ex1.1, ex2.1, message(), file: file, line: line)
}
class HeaderTableTests: XCTestCase {
func testStaticHeaderTable() {
let table = IndexedHeaderTable(allocator: ByteBufferAllocator())
// headers with matching values
XCTAssertEqualTuple((2, true), table.firstHeaderMatch(for: ":method", value: "GET")!)
XCTAssertEqualTuple((3, true), table.firstHeaderMatch(for: ":method", value: "POST")!)
XCTAssertEqualTuple((4, true), table.firstHeaderMatch(for: ":path", value: "/")!)
XCTAssertEqualTuple((5, true), table.firstHeaderMatch(for: ":path", value: "/index.html")!)
XCTAssertEqualTuple((8, true), table.firstHeaderMatch(for: ":status", value: "200")!)
XCTAssertEqualTuple((13, true), table.firstHeaderMatch(for: ":status", value: "404")!)
XCTAssertEqualTuple((14, true), table.firstHeaderMatch(for: ":status", value: "500")!)
XCTAssertEqualTuple((16, true), table.firstHeaderMatch(for: "accept-encoding", value: "gzip, deflate")!)
// headers with no values in the table
XCTAssertEqualTuple((15, false), table.firstHeaderMatch(for: "accept-charset", value: "any")!)
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "private")!)
// header-only matches for table entries with a different value
XCTAssertEqualTuple((8, false), table.firstHeaderMatch(for: ":status", value: "501")!)
XCTAssertEqualTuple((4, false), table.firstHeaderMatch(for: ":path", value: "/test/path.html")!)
XCTAssertEqualTuple((2, false), table.firstHeaderMatch(for: ":method", value: "CONNECT")!)
// things that aren't in the table at all
XCTAssertNil(table.firstHeaderMatch(for: "non-existent-key", value: "non-existent-value"))
}
func testDynamicTableInsertion() {
// NB: I'm using the overall table class to verify the expected indices of dynamic table items.
var table = IndexedHeaderTable(allocator: ByteBufferAllocator(), maxDynamicTableSize: 1024)
XCTAssertEqual(table.dynamicTableLength, 0)
XCTAssertNoThrow(try table.add(headerNamed: ":authority", value: "www.example.com"))
XCTAssertEqual(table.dynamicTableLength, 57)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.something-else.com")!)
XCTAssertNoThrow(try table.add(headerNamed: "cache-control", value: "no-cache"))
XCTAssertEqual(table.dynamicTableLength, 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
// custom key not yet in the table, should return nil
XCTAssertNil(table.firstHeaderMatch(for: "custom-key", value: "custom-value"))
XCTAssertNoThrow(try table.add(headerNamed: "custom-key", value: "custom-value"))
XCTAssertEqual(table.dynamicTableLength, 164)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((64, true), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!)
// should evict the first-inserted value (:authority = www.example.com)
table.dynamicTableAllowedLength = 128
XCTAssertEqual(table.dynamicTableLength, 164 - 57)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((63, true), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!)
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!) // will find the header name in static table
table.dynamicTableAllowedLength = 64
XCTAssertEqual(table.dynamicTableLength, 164 - 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
XCTAssertEqualTuple((62, false), table.firstHeaderMatch(for: "custom-key", value: "other-value")!)
XCTAssertEqualTuple((24, false), table.firstHeaderMatch(for: "cache-control", value: "no-cache")!) // will find the header name in static table
XCTAssertEqualTuple((1, false), table.firstHeaderMatch(for: ":authority", value: "www.example.com")!) // will find the header name in static table
table.dynamicTableAllowedLength = 164 - 110 // should cause no evictions
XCTAssertEqual(table.dynamicTableLength, 164 - 110)
XCTAssertEqualTuple((62, true), table.firstHeaderMatch(for: "custom-key", value: "custom-value")!)
// get code coverage for dynamic table search with no value to match
// Note that the resulting index is an index into the dynamic table only, so we
// have to modify it to check that it's what we expect.
if let found = table.dynamicTable.findExistingHeader(named: "custom-key".utf8, value: Optional<[UInt8]>.none) {
XCTAssertEqualTuple((62 - StaticHeaderTable.count, false), found)
}
// evict final entry
table.dynamicTableAllowedLength = table.dynamicTableLength - 1
XCTAssertEqual(table.dynamicTableLength, 0)
XCTAssertNil(table.firstHeaderMatch(for: "custom-key", value: "custom-value"))
}
func testHeaderDump() throws {
var table = IndexedHeaderTable(allocator: ByteBufferAllocator())
let description = table.dumpHeaders()
// construct what we expect
var expected = StaticHeaderTable.enumerated().reduce("") {
$0 + "\($1.0) - \($1.1.0) : \($1.1.1)\n"
}
// there will be a newline at the end, followed by no data for the dynamic table
expected += "\n"
XCTAssertEqual(description, expected)
// add an item to the dynamic table
try table.add(headerNamed: "custom-key", value: "custom-value")
expected += "\(StaticHeaderTable.count) - custom-key : custom-value\n"
let description2 = table.dumpHeaders()
XCTAssertEqual(description2, expected)
}
func testHeaderDescription() throws {
let table = IndexedHeaderTable(allocator: ByteBufferAllocator())
let staticDescription = table.staticTable.description
let staticExpected = StaticHeaderTable.description
XCTAssertEqual(staticDescription, staticExpected)
}
}