PaddingTests.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2017 Marcin Krzyżanowski <marcin@krzyzanowskim.com>
  5. // This software is provided 'as-is', without any express or implied warranty.
  6. //
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  10. //
  11. // - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
  12. // - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. // - This notice may not be removed or altered from any source or binary distribution.
  14. //
  15. @testable import CryptoSwift
  16. import XCTest
  17. final class PaddingTests: XCTestCase {
  18. func testPKCS7_0() {
  19. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
  20. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16]
  21. let padded = PKCS7.Padding().add(to: input, blockSize: 16)
  22. XCTAssertEqual(padded, expected, "PKCS7 failed")
  23. let clean = PKCS7.Padding().remove(from: padded, blockSize: nil)
  24. XCTAssertEqual(clean, input, "PKCS7 failed")
  25. }
  26. func testPKCS7_1() {
  27. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
  28. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 1]
  29. let padded = PKCS7.Padding().add(to: input, blockSize: 16)
  30. XCTAssertEqual(padded, expected, "PKCS7 failed")
  31. let clean = PKCS7.Padding().remove(from: padded, blockSize: nil)
  32. XCTAssertEqual(clean, input, "PKCS7 failed")
  33. }
  34. func testPKCS7_2() {
  35. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
  36. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 2, 2]
  37. let padded = PKCS7.Padding().add(to: input, blockSize: 16)
  38. XCTAssertEqual(padded, expected, "PKCS7 failed")
  39. let clean = PKCS7.Padding().remove(from: padded, blockSize: nil)
  40. XCTAssertEqual(clean, input, "PKCS7 failed")
  41. }
  42. func testZeroPadding1() {
  43. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  44. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0]
  45. let padding = ZeroPadding()
  46. XCTAssertEqual(padding.add(to: input, blockSize: 16), expected, "ZeroPadding failed")
  47. XCTAssertEqual(padding.remove(from: padding.add(to: input, blockSize: 16), blockSize: 16), input, "ZeroPadding failed")
  48. }
  49. func testZeroPadding2() {
  50. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
  51. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  52. let padding = ZeroPadding()
  53. XCTAssertEqual(padding.add(to: input, blockSize: 16), expected, "ZeroPadding failed")
  54. XCTAssertEqual(padding.remove(from: padding.add(to: input, blockSize: 16), blockSize: 16), input, "ZeroPadding failed")
  55. }
  56. static let allTests = [
  57. ("testPKCS7_0", testPKCS7_0),
  58. ("testPKCS7_1", testPKCS7_1),
  59. ("testPKCS7_2", testPKCS7_2),
  60. ("testZeroPadding1", testZeroPadding1),
  61. ("testZeroPadding2", testZeroPadding2),
  62. ]
  63. }