mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-29 05:24:23 +00:00
222 lines
7.1 KiB
Swift
222 lines
7.1 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 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 NIO
|
|
@testable import NIOIMAPCore
|
|
import XCTest
|
|
|
|
class CommandParser_Tests: XCTestCase, _ParserTestHelpers {}
|
|
|
|
// MARK: - init
|
|
|
|
extension CommandParser_Tests {
|
|
func testInit_defaultBufferSize() {
|
|
let parser = CommandParser()
|
|
XCTAssertEqual(parser.bufferLimit, 8_192)
|
|
}
|
|
|
|
func testInit_customBufferSize() {
|
|
let parser = CommandParser(bufferLimit: 80_000)
|
|
XCTAssertEqual(parser.bufferLimit, 80_000)
|
|
}
|
|
}
|
|
|
|
// MARK: - Test normal usage
|
|
|
|
extension CommandParser_Tests {
|
|
// test that we don't just get returned an empty byte case if
|
|
// we haven't yet recieved any literal data from the network
|
|
func testParseEmptyByteBufferAppend() {
|
|
var input = ByteBuffer("1 APPEND INBOX {5}\r\n") // everything except the literal data
|
|
var parser = CommandParser()
|
|
XCTAssertNoThrow(XCTAssertNotNil(try parser.parseCommandStream(buffer: &input)))
|
|
XCTAssertNoThrow(XCTAssertNotNil(try parser.parseCommandStream(buffer: &input)))
|
|
|
|
// At this point we should have parse off all the metadata
|
|
// so should be ready for the literal
|
|
var literalBuffer = ByteBuffer(string: "")
|
|
XCTAssertNoThrow(XCTAssertNil(try parser.parseCommandStream(buffer: &literalBuffer)))
|
|
}
|
|
|
|
func testParseWithLFMissingFromNewline() {
|
|
// The FramingParser might split the CRLF lineline into CF only (LF in the “next frame”).
|
|
// We thus need to be able to parse this:
|
|
var inputA = ByteBuffer("A26 UID FETCH 1:10002 (UID FLAGS MODSEQ)\r")
|
|
var parser = CommandParser()
|
|
|
|
XCTAssertEqual(
|
|
try parser.parseCommandStream(buffer: &inputA),
|
|
.init(.tagged(.init(tag: "A26", command: .uidFetch(messages: UIDSet([1 ... 10002]), attributes: [.uid, .flags, .modificationSequence], modifiers: [])!)), numberOfSynchronisingLiterals: 0)
|
|
)
|
|
// Send in another line:
|
|
var inputB = ByteBuffer("A27 UID FETCH 2:22 (UID FLAGS)\r")
|
|
XCTAssertEqual(
|
|
try parser.parseCommandStream(buffer: &inputB),
|
|
.init(.tagged(.init(tag: "A27", command: .uidFetch(messages: UIDSet([2 ... 22]), attributes: [.uid, .flags], modifiers: [])!)), numberOfSynchronisingLiterals: 0)
|
|
)
|
|
}
|
|
|
|
func testNormalUsage() {
|
|
var input = ByteBuffer("")
|
|
var parser = CommandParser()
|
|
|
|
XCTAssertNoThrow(XCTAssertNil(try parser.parseCommandStream(buffer: &input)))
|
|
|
|
input = "1 NOOP\r\n"
|
|
XCTAssertNoThrow(
|
|
XCTAssertEqual(
|
|
try parser.parseCommandStream(buffer: &input),
|
|
.init(.tagged(.init(tag: "1", command: .noop)), numberOfSynchronisingLiterals: 0)
|
|
)
|
|
)
|
|
XCTAssertEqual(input, "")
|
|
|
|
input = "2 LOGIN {0}\r\n {0}\r\n\r\n"
|
|
XCTAssertNoThrow(
|
|
XCTAssertEqual(
|
|
try parser.parseCommandStream(buffer: &input),
|
|
.init(.tagged(.init(tag: "2", command: .login(username: "", password: ""))), numberOfSynchronisingLiterals: 2)
|
|
)
|
|
)
|
|
XCTAssertEqual(input, "")
|
|
|
|
input = "3 APPEND INBOX {3+}\r\n123 {3+}\r\n456 {3+}\r\n789\r\n"
|
|
XCTAssertEqual(try! parser.parseCommandStream(buffer: &input), .init(.append(.start(tag: "3", appendingTo: .inbox)), numberOfSynchronisingLiterals: 0))
|
|
XCTAssertEqual(input, " {3+}\r\n123 {3+}\r\n456 {3+}\r\n789\r\n")
|
|
}
|
|
|
|
func testRandomDataDoesntCrash() {
|
|
let inputs: [[UInt8]] = [
|
|
Array("+000000000000000000000000000000000000000000000000000000000}\n".utf8),
|
|
Array("eSequence468117eY SEARCH 4:1 000,0\n000059?000000600=)O".utf8),
|
|
[0x41, 0x5D, 0x20, 0x55, 0x49, 0x44, 0x20, 0x43, 0x4F, 0x50, 0x59, 0x20, 0x35, 0x2C, 0x35, 0x3A, 0x34, 0x00, 0x3D, 0x0C, 0x0A, 0x43, 0x20, 0x22, 0xE8],
|
|
]
|
|
|
|
for input in inputs {
|
|
var parser = CommandParser()
|
|
do {
|
|
var buffer = ByteBuffer(bytes: input)
|
|
var lastReadableBytes = buffer.readableBytes
|
|
var newReadableBytes = 0
|
|
while newReadableBytes < lastReadableBytes {
|
|
lastReadableBytes = buffer.readableBytes
|
|
_ = try parser.parseCommandStream(buffer: &buffer)
|
|
newReadableBytes = buffer.readableBytes
|
|
}
|
|
} catch {
|
|
// do nothing, we don't care
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: -
|
|
|
|
extension CommandParser_Tests {
|
|
func testParseString() {
|
|
let inputs: [(String, String, ByteBuffer, UInt)] = [
|
|
(
|
|
#""foo""#,
|
|
" ",
|
|
ByteBuffer(string: "foo"),
|
|
#line
|
|
),
|
|
(
|
|
#""f\"oo""#,
|
|
" ",
|
|
ByteBuffer(string: #"f"oo"#),
|
|
#line
|
|
),
|
|
(
|
|
#""f\\oo""#,
|
|
" ",
|
|
ByteBuffer(string: #"f\oo"#),
|
|
#line
|
|
),
|
|
(
|
|
"{3}\r\nfoo",
|
|
" ",
|
|
ByteBuffer(string: "foo"),
|
|
#line
|
|
),
|
|
]
|
|
let invalidInputs: [(String, String, UInt)] = [
|
|
(
|
|
#""aäb""#,
|
|
" ",
|
|
#line
|
|
),
|
|
(
|
|
#"foo"#,
|
|
" ",
|
|
#line
|
|
),
|
|
(
|
|
#" "foo""#,
|
|
" ",
|
|
#line
|
|
),
|
|
]
|
|
|
|
self.iterateTests(
|
|
testFunction: GrammarParser().parseString,
|
|
validInputs: inputs,
|
|
parserErrorInputs: invalidInputs,
|
|
incompleteMessageInputs: []
|
|
)
|
|
}
|
|
|
|
func testParseStringAllowingNonASCII() {
|
|
let inputs: [(String, String, ByteBuffer, UInt)] = [
|
|
(
|
|
#""foo""#,
|
|
" ",
|
|
ByteBuffer(string: "foo"),
|
|
#line
|
|
),
|
|
(
|
|
#""äø""#,
|
|
" ",
|
|
ByteBuffer(string: "äø"),
|
|
#line
|
|
),
|
|
(
|
|
#""ä\"ø""#,
|
|
" ",
|
|
ByteBuffer(string: #"ä"ø"#),
|
|
#line
|
|
),
|
|
(
|
|
#""ä\\ø""#,
|
|
" ",
|
|
ByteBuffer(string: #"ä\ø"#),
|
|
#line
|
|
),
|
|
(
|
|
"{3}\r\nfoo",
|
|
" ",
|
|
ByteBuffer(string: "foo"),
|
|
#line
|
|
),
|
|
]
|
|
|
|
self.iterateTests(
|
|
testFunction: GrammarParser().parseStringAllowingNonASCII,
|
|
validInputs: inputs,
|
|
parserErrorInputs: [],
|
|
incompleteMessageInputs: []
|
|
)
|
|
}
|
|
}
|