PaddingTests.swift 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import XCTest
  16. @testable import CryptoSwift
  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 testZeroPadding() {
  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. static let allTests = [
  50. ("testPKCS7_0", testPKCS7_0),
  51. ("testPKCS7_1", testPKCS7_1),
  52. ("testPKCS7_2", testPKCS7_2),
  53. ("testZeroPadding", testZeroPadding),
  54. ]
  55. }