1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import LanguageServerProtocol
@_spi(Testing) import LanguageServerProtocolJSONRPC
import XCTest
final class MessageParsingTests: XCTestCase {
func testSplitMessage() throws {
func check(
_ string: String,
contentLen: Int? = nil,
restLen: Int?,
file: StaticString = #filePath,
line: UInt = #line
) throws {
let bytes: [UInt8] = [UInt8](string.utf8)
guard let (header, content, rest) = try bytes.jsonrpcSplitMessage() else {
XCTAssert(restLen == nil, "expected non-empty field", file: file, line: line)
return
}
XCTAssertEqual(rest.count, restLen, "rest", file: file, line: line)
XCTAssertEqual(content.count, contentLen, "content", file: file, line: line)
XCTAssertEqual(header.contentLength, contentLen, file: file, line: line)
}
func checkError(
_ string: String,
_ expected: MessageDecodingError,
file: StaticString = #filePath,
line: UInt = #line
) {
do {
_ = try [UInt8](string.utf8).jsonrpcSplitMessage()
XCTFail("missing expected error", file: file, line: line)
} catch let error as MessageDecodingError {
XCTAssertEqual(error, expected, file: file, line: line)
} catch {
XCTFail("error \(error) does not match expected \(expected)", file: file, line: line)
}
}
try check("Content-Length: 2\r\n", restLen: nil)
try check("Content-Length: 1\r\n\r\n", restLen: nil)
try check("Content-Length: 2\r\n\r\n{", restLen: nil)
try check("Content-Length: 0\r\n\r\n", contentLen: 0, restLen: 0)
try check("Content-Length: 0\r\n\r\n{}", contentLen: 0, restLen: 2)
try check("Content-Length: 1\r\n\r\n{}", contentLen: 1, restLen: 1)
try check("Content-Length: 2\r\n\r\n{}", contentLen: 2, restLen: 0)
try check("Content-Length: 2\r\n\r\n{}Co", contentLen: 2, restLen: 2)
checkError("\r\n\r\n{}", MessageDecodingError.parseError("missing Content-Length header"))
}
func testParseHeader() throws {
func check(
_ string: String,
header expected: JSONRPCMessageHeader? = nil,
restLen: Int?,
file: StaticString = #filePath,
line: UInt = #line
) throws {
let bytes: [UInt8] = [UInt8](string.utf8)
guard let (header, rest) = try bytes.jsonrcpParseHeader() else {
XCTAssert(restLen == nil, "expected non-empty field", file: file, line: line)
return
}
XCTAssertEqual(rest.count, restLen, "rest", file: file, line: line)
XCTAssertEqual(header, expected, file: file, line: line)
}
func checkErrorBytes(
_ bytes: [UInt8],
_ expected: MessageDecodingError,
file: StaticString = #filePath,
line: UInt = #line
) {
do {
_ = try bytes.jsonrcpParseHeader()
XCTFail("missing expected error", file: file, line: line)
} catch let error as MessageDecodingError {
XCTAssertEqual(error, expected, file: file, line: line)
} catch {
XCTFail("error \(error) does not match expected \(expected)", file: file, line: line)
}
}
func checkError(
_ string: String,
_ expected: MessageDecodingError,
file: StaticString = #filePath,
line: UInt = #line
) {
checkErrorBytes([UInt8](string.utf8), expected, file: file, line: line)
}
try check("", restLen: nil)
try check("C", restLen: nil)
try check("Content-Length: 1", restLen: nil)
try check("Content-Length: 1\r", restLen: nil)
try check("Content-Length: 1\r\n", restLen: nil)
try check("Content-Length: 1\r\n\r\n", header: JSONRPCMessageHeader(contentLength: 1), restLen: 0)
try check("Content-Length: 1\r\n\r\n{}", header: JSONRPCMessageHeader(contentLength: 1), restLen: 2)
try check("A:B\r\nContent-Length: 1\r\nC:D\r\n\r\n", header: JSONRPCMessageHeader(contentLength: 1), restLen: 0)
try check("Content-Length:123 \r\n\r\n", header: JSONRPCMessageHeader(contentLength: 123), restLen: 0)
checkError("Content-Length:0x1\r\n\r\n", MessageDecodingError.parseError("expected integer value in 0x1"))
checkError("Content-Length:a123\r\n\r\n", MessageDecodingError.parseError("expected integer value in a123"))
checkErrorBytes(
[UInt8]("Content-Length: ".utf8) + [0xFF] + [UInt8]("\r\n".utf8),
MessageDecodingError.parseError("expected integer value in <invalid>")
)
}
func testParseHeaderField() throws {
func check(
_ string: String,
keyLen: Int? = nil,
valueLen: Int? = nil,
restLen: Int?,
file: StaticString = #filePath,
line: UInt = #line
) throws {
let bytes: [UInt8] = [UInt8](string.utf8)
guard let (kv, rest) = try bytes.jsonrpcParseHeaderField() else {
XCTAssert(restLen == nil, "expected non-empty field", file: file, line: line)
return
}
XCTAssertEqual(rest.count, restLen, "rest", file: file, line: line)
XCTAssertEqual(kv?.key.count, keyLen, "key", file: file, line: line)
if let key = kv?.key {
XCTAssertEqual(key, bytes.prefix(key.count), file: file, line: line)
}
XCTAssertEqual(kv?.value.count, valueLen, "value", file: file, line: line)
if let value = kv?.value {
XCTAssertEqual(value, bytes.dropFirst(kv!.key.count + 1).prefix(value.count), file: file, line: line)
}
}
func checkError(
_ string: String,
_ expected: MessageDecodingError,
file: StaticString = #filePath,
line: UInt = #line
) {
do {
_ = try [UInt8](string.utf8).jsonrpcParseHeaderField()
XCTFail("missing expected error", file: file, line: line)
} catch let error as MessageDecodingError {
XCTAssertEqual(error, expected, file: file, line: line)
} catch {
XCTFail("error \(error) does not match expected \(expected)", file: file, line: line)
}
}
try check("", restLen: nil)
try check("C", restLen: nil)
try check("Content-Length", restLen: nil)
try check("Content-Length:", restLen: nil)
try check("Content-Length:a", restLen: nil)
try check("Content-Length: 1", restLen: nil)
try check("Content-Length: 1\r", restLen: nil)
try check("Content-Length: 1\r\n", keyLen: "Content-Length".count, valueLen: 2, restLen: 0)
try check("Content-Length:1\r\n", keyLen: "Content-Length".count, valueLen: 1, restLen: 0)
try check("Content-Length: 1\r\n ", keyLen: "Content-Length".count, valueLen: 2, restLen: 1)
try check("Content-Length: 1\r\n\r\n", keyLen: "Content-Length".count, valueLen: 2, restLen: 2)
try check("Unknown:asdf\r", restLen: nil)
try check("Unknown:asdf\r\n", keyLen: "Unknown".count, valueLen: 4, restLen: 0)
try check("\r", restLen: nil)
try check("\r\n", restLen: 0)
try check("\r\nC", restLen: 1)
try check("\r\nContent-Length:1\r\n", restLen: "Content-Length:1\r\n".utf8.count)
try check(":", restLen: nil)
try check(":\r\n", keyLen: 0, valueLen: 0, restLen: 0)
checkError("C\r\n", MessageDecodingError.parseError("expected ':' in message header"))
}
func testFindSubsequence() {
XCTAssertNil([0, 1, 2].firstIndex(of: [3]))
XCTAssertNil([].firstIndex(of: [3]))
XCTAssertNil([0, 1, 2].firstIndex(of: [1, 3]))
XCTAssertNil([0, 1, 2].firstIndex(of: [0, 2]))
XCTAssertNil([0, 1, 2].firstIndex(of: [2, 3]))
XCTAssertNil([0, 1].firstIndex(of: [1, 0]))
XCTAssertNil([0].firstIndex(of: [0, 1]))
XCTAssertEqual([Int]().firstIndex(of: []), 0)
XCTAssertEqual([0].firstIndex(of: []), 0)
XCTAssertEqual([0].firstIndex(of: [0]), 0)
XCTAssertEqual([0, 1].firstIndex(of: [0]), 0)
XCTAssertEqual([0, 1].firstIndex(of: [1]), 1)
XCTAssertEqual([0, 1].firstIndex(of: [0, 1]), 0)
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [0, 1]), 0)
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [0, 1, 2]), 0)
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [1, 2]), 1)
XCTAssertEqual([0, 1, 2, 3].firstIndex(of: [3]), 3)
}
func testIntFromAscii() {
XCTAssertNil(Int(ascii: ""))
XCTAssertNil(Int(ascii: "a"))
XCTAssertNil(Int(ascii: "0x1"))
XCTAssertNil(Int(ascii: " "))
XCTAssertNil(Int(ascii: "+"))
XCTAssertNil(Int(ascii: "-"))
XCTAssertNil(Int(ascii: "+ "))
XCTAssertNil(Int(ascii: "- "))
XCTAssertNil(Int(ascii: "1 1"))
XCTAssertNil(Int(ascii: "1a1"))
XCTAssertNil(Int(ascii: "1a"))
XCTAssertNil(Int(ascii: "1+"))
XCTAssertNil(Int(ascii: "+ 1"))
XCTAssertNil(Int(ascii: "- 1"))
XCTAssertNil(Int(ascii: "1-1"))
XCTAssertEqual(Int(ascii: "0"), 0)
XCTAssertEqual(Int(ascii: "1"), 1)
XCTAssertEqual(Int(ascii: "45"), 45)
XCTAssertEqual(Int(ascii: " 45 "), 45)
XCTAssertEqual(Int(ascii: "\(Int.max)"), Int.max)
XCTAssertEqual(Int(ascii: "\(Int.max-1)"), Int.max - 1)
XCTAssertEqual(Int(ascii: "\(Int.min)"), Int.min)
XCTAssertEqual(Int(ascii: "\(Int.min+1)"), Int.min + 1)
XCTAssertEqual(Int(ascii: "+0"), 0)
XCTAssertEqual(Int(ascii: "+1"), 1)
XCTAssertEqual(Int(ascii: "+45"), 45)
XCTAssertEqual(Int(ascii: " +45 "), 45)
XCTAssertEqual(Int(ascii: "-0"), 0)
XCTAssertEqual(Int(ascii: "-1"), -1)
XCTAssertEqual(Int(ascii: "-45"), -45)
XCTAssertEqual(Int(ascii: " -45 "), -45)
XCTAssertEqual(Int(ascii: "+\(Int.max)"), Int.max)
XCTAssertEqual(Int(ascii: "+\(Int.max-1)"), Int.max - 1)
XCTAssertEqual(Int(ascii: "\(Int.min)"), Int.min)
XCTAssertEqual(Int(ascii: "\(Int.min+1)"), Int.min + 1)
XCTAssertEqual(Int(ascii: "1234567890"), 1234567890)
XCTAssertEqual(Int(ascii: "\n\r \u{b}\u{d}\t45\n\t\r\u{c}"), 45)
}
}
|