ExtensionsTest.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // CryptoSwift
  3. //
  4. // Copyright (C) 2014-2025 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 Foundation
  16. import XCTest
  17. @testable import CryptoSwift
  18. final class ExtensionsTest: XCTestCase {
  19. func testBytes() {
  20. let size = MemoryLayout<UInt32>.size // 32 or 64 bit
  21. let i: UInt32 = 1024
  22. var bytes = i.bytes()
  23. XCTAssertTrue(bytes.count == size, "Invalid bytes length = \(bytes.count)")
  24. // test padding
  25. bytes = i.bytes(totalBytes: 16)
  26. XCTAssertTrue(bytes.count == 16, "Invalid return type \(bytes.count)")
  27. XCTAssertTrue(bytes[14] == 4, "Invalid return type \(bytes.count)")
  28. }
  29. func testToUInt32Array() {
  30. let chunk: ArraySlice<UInt8> = [0x8, 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1]
  31. let result = chunk.toUInt32Array()
  32. XCTAssert(result.count == 2, "Invalid conversion")
  33. XCTAssertEqual(result[0], 0x5060708)
  34. XCTAssertEqual(result[1], 0x1020304)
  35. }
  36. func testToUInt32Performance() {
  37. let len = 1_000_000
  38. let a = [UInt8](unsafeUninitializedCapacity: len) { buf, count in
  39. for i in 0..<len {
  40. buf[i] = UInt8.random(in: 0...UInt8.max)
  41. }
  42. count = len
  43. }
  44. self.measure {
  45. _ = a.toUInt32Array()
  46. }
  47. }
  48. func testToUInt64Performance() {
  49. let len = 1_000_000
  50. let a = [UInt8](unsafeUninitializedCapacity: len) { buf, count in
  51. for i in 0..<len {
  52. buf[i] = UInt8.random(in: 0...UInt8.max)
  53. }
  54. count = len
  55. }
  56. self.measure {
  57. _ = a.toUInt64Array()
  58. }
  59. }
  60. func testDataInit() {
  61. let data = Data( [0x01, 0x02, 0x03])
  62. XCTAssert(data.count == 3, "Invalid data")
  63. }
  64. func testStringEncrypt() {
  65. do {
  66. let encryptedHex = try "my secret string".encrypt(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  67. XCTAssertEqual(encryptedHex, "68f7ff8bdb61f625febdfe3d791ecf624daaed2e719a6de39112de8e0cc7349b")
  68. } catch {
  69. XCTFail(error.localizedDescription)
  70. }
  71. }
  72. func testEmptyStringEncrypt() {
  73. do {
  74. let cipher = try AES(key: "secret0key000000".bytes.md5(), blockMode: ECB())
  75. let encrypted = try "".encryptToBase64(cipher: cipher)
  76. let decrypted = try encrypted.decryptBase64ToString(cipher: cipher)
  77. XCTAssertEqual("", decrypted)
  78. XCTAssertThrowsError(try "".decryptBase64(cipher: cipher))
  79. } catch {
  80. XCTFail(error.localizedDescription)
  81. }
  82. }
  83. func testStringDecryptBase64() {
  84. let encryptedBase64 = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  85. let decrypted = try! encryptedBase64.decryptBase64ToString(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  86. XCTAssertEqual(decrypted, "my secret string")
  87. }
  88. func testArrayInitHex() {
  89. let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
  90. XCTAssertEqual(bytes, [177, 177, 178, 178])
  91. let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
  92. let array = Array<UInt8>(hex: str)
  93. let hex = array.toHexString()
  94. XCTAssertEqual(str, hex)
  95. }
  96. func testToHexStringPerformance() {
  97. let len = 100000
  98. let a = [UInt8](unsafeUninitializedCapacity: len) { buf, count in
  99. for i in 0..<len {
  100. buf[i] = UInt8.random(in: 0...UInt8.max)
  101. }
  102. count = len
  103. }
  104. self.measure {
  105. _ = a.toHexString()
  106. }
  107. }
  108. }
  109. extension ExtensionsTest {
  110. static func allTests() -> [(String, (ExtensionsTest) -> () -> Void)] {
  111. let tests = [
  112. ("testBytes", testBytes),
  113. ("testToUInt32Array", testToUInt32Array),
  114. ("testDataInit", testDataInit),
  115. ("testStringEncrypt", testStringEncrypt),
  116. ("testStringDecryptBase64", testStringDecryptBase64),
  117. ("testEmptyStringEncrypt", testEmptyStringEncrypt),
  118. ("testArrayInitHex", testArrayInitHex)
  119. ]
  120. return tests
  121. }
  122. }