Files
swift-nio-imap/Tests/NIOIMAPCoreTests/ModifiedUTF7+Tests.swift
T
David Evans b5b9d3c910 Support UTF7-IMAP (#282)
* Add encoding with tests

* Fix encoding

* Test decodes

* Finish decoding

* Comments

* Fix sanity

* Cory PR comments

* Optimise adding padding

* Optimise writing string

* Docs

* Fix sanity

* Rename UTF7 and reserve string capacity

* Vendor Base64

* Fix sanity

* More tests and fix a few things

* Decoding error checks

* Fix sanity

* Add a notice file

* Add validation function

* PR comments

* Add validation tests
2020-08-19 16:50:23 +01:00

82 lines
2.9 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 ModifiedUTF7_Tests: XCTestCase {}
extension ModifiedUTF7_Tests {
func testEncode() {
let inputs: [(String, String, UInt)] = [
("", "", #line),
("abc", "abc", #line),
("&", "&-", #line),
("ab&12", "ab&-12", #line),
("mail/€/£", "mail/&IKw-/&AKM-", #line),
("Répertoire", "R&AOk-pertoire", #line),
("パリ", "&MNEw6g-", #line),
("雑件", "&ltFO9g-", #line),
("🏣🏣", "&2Dzf49g83+M-", #line),
("🧑🏽‍🦳", "&2D7d0dg83,0gDdg+3bM-", #line),
("a/b/c", "a/b/c", #line),
(#"a\b\c"#, #"a\b\c"#, #line),
("a+b", "a+b", #line),
("~ab", "~ab", #line),
]
for (input, expected, line) in inputs {
let actual = ModifiedUTF7.encode(input)
XCTAssertEqual(expected, String(buffer: actual), line: line)
}
}
func testDecode() {
let inputs: [(String, String, UInt)] = [
("", "", #line),
("abc", "abc", #line),
("&-", "&", #line),
("ab&-12", "ab&12", #line),
("mail/&IKw-/&AKM-", "mail/€/£", #line),
("R&AOk-pertoire", "Répertoire", #line),
("&MNEw6g-", "パリ", #line),
("&ltFO9g-", "雑件", #line),
("&2Dzf49g83+M-", "🏣🏣", #line),
("&2D7d0dg83,0gDdg+3bM-", "🧑🏽‍🦳", #line),
("a/b/c", "a/b/c", #line),
(#"a\b\c"#, #"a\b\c"#, #line),
("a+b", "a+b", #line),
("~ab", "~ab", #line),
]
for (input, expected, line) in inputs {
XCTAssertNoThrow(XCTAssertEqual(expected, try ModifiedUTF7.decode(ByteBuffer(string: input)), line: line), line: line)
}
}
func testDecode_error() {
XCTAssertThrowsError(try ModifiedUTF7.decode(ByteBuffer(string: "&aa==-"))) { e in
XCTAssertTrue(e is ModifiedUTF7.DecodingError)
}
}
func testValidate() {
XCTAssertNoThrow(try ModifiedUTF7.validate("a"))
XCTAssertNoThrow(try ModifiedUTF7.validate("a/b/c"))
XCTAssertNoThrow(try ModifiedUTF7.validate("&2Dzf49g83+M-"))
XCTAssertNoThrow(try ModifiedUTF7.validate("&ltFO9g-"))
XCTAssertThrowsError(try ModifiedUTF7.validate("&Jjo!"))
XCTAssertThrowsError(try ModifiedUTF7.validate("&U,BTFw-&ZeVnLIqe-"))
}
}