PaddingTests.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 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. func testISO78164_0() {
  57. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 0x80]
  58. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 0x80, 0x80]
  59. let padded = ISO78164Padding().add(to: input, blockSize: 16)
  60. XCTAssertEqual(padded, expected, "ISO78164 failed")
  61. let clean = ISO78164Padding().remove(from: padded, blockSize: nil)
  62. XCTAssertEqual(clean, input, "ISO78164 failed")
  63. }
  64. func testISO78164_1() {
  65. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 0]
  66. let expected: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 0, 0x80] + [UInt8](repeating: UInt8(0), count: 15)
  67. let padded = ISO78164Padding().add(to: input, blockSize: 16)
  68. XCTAssertEqual(padded, expected, "ISO78164 failed")
  69. let clean = ISO78164Padding().remove(from: padded, blockSize: nil)
  70. XCTAssertEqual(clean, input, "ISO78164 failed")
  71. }
  72. func testISO78164_2() {
  73. let input: Array<UInt8> = []
  74. let expected: Array<UInt8> = [0x80] + [UInt8](repeating: UInt8(0), count: 15)
  75. let padded = ISO78164Padding().add(to: input, blockSize: 16)
  76. XCTAssertEqual(padded, expected, "ISO78164 failed")
  77. let clean = ISO78164Padding().remove(from: padded, blockSize: nil)
  78. XCTAssertEqual(clean, input, "ISO78164 failed")
  79. }
  80. func testISO10126_0() {
  81. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
  82. let padded = ISO10126Padding().add(to: input, blockSize: 16)
  83. XCTAssertTrue(padded.starts(with: input), "ISO10126 failed")
  84. XCTAssertEqual(padded.last, 3, "ISO10126 failed")
  85. XCTAssertEqual(padded.count, 16)
  86. let clean = ISO10126Padding().remove(from: padded, blockSize: nil)
  87. XCTAssertEqual(clean, input, "ISO10126 failed")
  88. }
  89. func testISO10126_1() {
  90. let input: Array<UInt8> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6]
  91. let padded = ISO10126Padding().add(to: input, blockSize: 16)
  92. XCTAssertTrue(padded.starts(with: input), "ISO10126 failed")
  93. XCTAssertEqual(padded.last, 16, "ISO10126 failed")
  94. XCTAssertEqual(padded.count, 32)
  95. let clean = ISO10126Padding().remove(from: padded, blockSize: nil)
  96. XCTAssertEqual(clean, input, "ISO10126 failed")
  97. }
  98. func testISO10126_2() {
  99. let input: Array<UInt8> = []
  100. let padded = ISO10126Padding().add(to: input, blockSize: 16)
  101. XCTAssertTrue(padded.starts(with: input), "ISO10126 failed")
  102. XCTAssertEqual(padded.last, 16, "ISO10126 failed")
  103. XCTAssertEqual(padded.count, 16)
  104. let clean = ISO10126Padding().remove(from: padded, blockSize: nil)
  105. XCTAssertEqual(clean, input, "ISO10126 failed")
  106. }
  107. static let allTests = [
  108. ("testPKCS7_0", testPKCS7_0),
  109. ("testPKCS7_1", testPKCS7_1),
  110. ("testPKCS7_2", testPKCS7_2),
  111. ("testZeroPadding1", testZeroPadding1),
  112. ("testZeroPadding2", testZeroPadding2),
  113. ("testISO78164_0", testISO78164_0),
  114. ("testISO78164_1", testISO78164_1),
  115. ("testISO78164_2", testISO78164_2),
  116. ("testISO10126_0", testISO10126_0),
  117. ("testISO10126_1", testISO10126_1),
  118. ("testISO10126_2", testISO10126_2)
  119. ]
  120. }