|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +@testable import AWSLambdaEvents |
| 16 | +import XCTest |
| 17 | + |
| 18 | +class Base64Tests: XCTestCase { |
| 19 | + // MARK: - Encoding - |
| 20 | + |
| 21 | + func testEncodeEmptyData() throws { |
| 22 | + let data = [UInt8]() |
| 23 | + let encodedData = String(base64Encoding: data) |
| 24 | + XCTAssertEqual(encodedData.count, 0) |
| 25 | + } |
| 26 | + |
| 27 | + func testBase64EncodingArrayOfNulls() throws { |
| 28 | + let data = Array(repeating: UInt8(0), count: 10) |
| 29 | + let encodedData = String(base64Encoding: data) |
| 30 | + XCTAssertEqual(encodedData, "AAAAAAAAAAAAAA==") |
| 31 | + } |
| 32 | + |
| 33 | + func testBase64EncodingAllTheBytesSequentially() throws { |
| 34 | + let data = Array(UInt8(0) ... UInt8(255)) |
| 35 | + let encodedData = String(base64Encoding: data) |
| 36 | + XCTAssertEqual(encodedData, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==") |
| 37 | + } |
| 38 | + |
| 39 | + func testBase64UrlEncodingAllTheBytesSequentially() throws { |
| 40 | + let data = Array(UInt8(0) ... UInt8(255)) |
| 41 | + let encodedData = String(base64Encoding: data, options: .base64UrlAlphabet) |
| 42 | + XCTAssertEqual(encodedData, "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w==") |
| 43 | + } |
| 44 | + |
| 45 | + // MARK: - Decoding - |
| 46 | + |
| 47 | + func testDecodeEmptyString() throws { |
| 48 | + var decoded: [UInt8]? |
| 49 | + XCTAssertNoThrow(decoded = try "".base64decoded()) |
| 50 | + XCTAssertEqual(decoded?.count, 0) |
| 51 | + } |
| 52 | + |
| 53 | + func testBase64DecodingArrayOfNulls() throws { |
| 54 | + let expected = Array(repeating: UInt8(0), count: 10) |
| 55 | + var decoded: [UInt8]? |
| 56 | + XCTAssertNoThrow(decoded = try "AAAAAAAAAAAAAA==".base64decoded()) |
| 57 | + XCTAssertEqual(decoded, expected) |
| 58 | + } |
| 59 | + |
| 60 | + func testBase64DecodingAllTheBytesSequentially() { |
| 61 | + let base64 = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==" |
| 62 | + |
| 63 | + let expected = Array(UInt8(0) ... UInt8(255)) |
| 64 | + var decoded: [UInt8]? |
| 65 | + XCTAssertNoThrow(decoded = try base64.base64decoded()) |
| 66 | + |
| 67 | + XCTAssertEqual(decoded, expected) |
| 68 | + } |
| 69 | + |
| 70 | + func testBase64UrlDecodingAllTheBytesSequentially() { |
| 71 | + let base64 = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0-P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn-AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq-wsbKztLW2t7i5uru8vb6_wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t_g4eLj5OXm5-jp6uvs7e7v8PHy8_T19vf4-fr7_P3-_w==" |
| 72 | + |
| 73 | + let expected = Array(UInt8(0) ... UInt8(255)) |
| 74 | + var decoded: [UInt8]? |
| 75 | + XCTAssertNoThrow(decoded = try base64.base64decoded(options: .base64UrlAlphabet)) |
| 76 | + |
| 77 | + XCTAssertEqual(decoded, expected) |
| 78 | + } |
| 79 | + |
| 80 | + func testBase64DecodingWithPoop() { |
| 81 | + XCTAssertThrowsError(_ = try "💩".base64decoded()) { error in |
| 82 | + XCTAssertEqual(error as? Base64.DecodingError, .invalidCharacter(240)) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + func testBase64DecodingWithInvalidLength() { |
| 87 | + XCTAssertThrowsError(_ = try "AAAAA".base64decoded()) { error in |
| 88 | + XCTAssertEqual(error as? Base64.DecodingError, .invalidLength) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + func testNSStringToDecode() { |
| 93 | + let test = "1234567" |
| 94 | + let nsstring = test.data(using: .utf8)!.base64EncodedString() |
| 95 | + |
| 96 | + XCTAssertNoThrow(try nsstring.base64decoded()) |
| 97 | + } |
| 98 | +} |
0 commit comments