ExtensionsTest.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // ExtensionsTest.swift
  3. // CryptoSwift
  4. //
  5. // Created by Marcin Krzyzanowski on 15/08/14.
  6. // Copyright (c) 2014 Marcin Krzyzanowski. All rights reserved.
  7. //
  8. import XCTest
  9. import Foundation
  10. @testable import CryptoSwift
  11. final class ExtensionsTest: XCTestCase {
  12. func testArrayChunksPerformance() {
  13. measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: false, for: { () -> Void in
  14. let message = Array<UInt8>(repeating: 7, count: 1024 * 1024)
  15. self.startMeasuring()
  16. _ = message.chunks(size: AES.blockSize)
  17. self.stopMeasuring()
  18. })
  19. }
  20. func testIntExtension() {
  21. let i1:Int = 1024
  22. let i1Array = i1.bytes(totalBytes: 32 / 8) // 32 bit
  23. let i1recovered = Int(bytes: i1Array)
  24. XCTAssertEqual(i1, i1recovered, "Bytes conversion failed")
  25. let i2:Int = 1024
  26. let i2Array = i2.bytes(totalBytes: 160 / 8) // 160 bit
  27. let i2recovered = Int(bytes: i2Array)
  28. XCTAssertEqual(i2, i2recovered, "Bytes conversion failed")
  29. }
  30. func testBytes() {
  31. let size = MemoryLayout<UInt32>.size // 32 or 64 bit
  32. let i:UInt32 = 1024
  33. var bytes = i.bytes()
  34. XCTAssertTrue(bytes.count == size, "Invalid bytes length = \(bytes.count)")
  35. // test padding
  36. bytes = i.bytes(totalBytes: 16)
  37. XCTAssertTrue(bytes.count == 16, "Invalid return type \(bytes.count)")
  38. XCTAssertTrue(bytes[14] == 4, "Invalid return type \(bytes.count)")
  39. }
  40. func testShiftLeft() {
  41. // Unsigned
  42. let i:UInt32 = 1
  43. XCTAssert(i &<< 1 == 2, "shift left failed")
  44. XCTAssert(i &<< 8 == 256, "shift left failed")
  45. XCTAssert(i &<< 31 == i << 31, "shift left failed")
  46. XCTAssert(i &<< 32 == 0, "shift left failed")
  47. // Signed
  48. let ii:Int = 21
  49. XCTAssert(ii &<< 1 == ii << 1, "shift left failed")
  50. XCTAssert(ii &<< 8 == ii << 8, "shift left failed")
  51. XCTAssert(ii &<< ((MemoryLayout<Int>.size * 8) - 1) == ii << ((MemoryLayout<Int>.size * 8) - 1), "shift left failed")
  52. XCTAssert(ii &<< ((MemoryLayout<Int>.size * 8)) == 0, "shift left failed")
  53. let iii:UInt32 = 21
  54. XCTAssert(iii &<< 1 == iii << 1, "shift left failed")
  55. XCTAssert(iii &<< 8 == iii << 8, "shift left failed")
  56. XCTAssert((iii &<< 32) == 0, "shift left failed")
  57. }
  58. func testToUInt32Array() {
  59. let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
  60. let result = chunk.toUInt32Array()
  61. XCTAssert(result.count == 2, "Invalid conversion")
  62. XCTAssert(result[0] == 117506305, "Invalid conversion")
  63. XCTAssert(result[1] == 84148994, "Invalid conversion")
  64. }
  65. func testDataInit() {
  66. let data = Data(bytes: [0x01, 0x02, 0x03])
  67. XCTAssert(data.count == 3, "Invalid data")
  68. }
  69. func testStringEncrypt() {
  70. do {
  71. let encryptedHex = try "my secret string".encrypt(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  72. XCTAssertEqual(encryptedHex, "68f7ff8bdb61f625febdfe3d791ecf624daaed2e719a6de39112de8e0cc7349b")
  73. } catch {
  74. XCTFail(error.localizedDescription)
  75. }
  76. }
  77. func testStringDecryptBase64() {
  78. let encryptedBase64 = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  79. let decrypted = try! encryptedBase64.decryptBase64ToString(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  80. XCTAssertEqual(decrypted, "my secret string")
  81. }
  82. func testArrayInitHex() {
  83. let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
  84. XCTAssertEqual(bytes, [177,177,178,178])
  85. let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
  86. let array = Array<UInt8>(hex: str)
  87. let hex = array.toHexString()
  88. XCTAssertEqual(str, hex)
  89. }
  90. static let allTests = [
  91. ("testArrayChunksPerformance", testArrayChunksPerformance),
  92. ("testIntExtension", testIntExtension),
  93. ("testBytes", testBytes),
  94. ("testShiftLeft", testShiftLeft),
  95. ("testToUInt32Array", testToUInt32Array),
  96. ("testDataInit", testDataInit),
  97. ("testStringEncrypt", testStringEncrypt),
  98. ("testStringDecryptBase64", testStringDecryptBase64),
  99. ("testArrayInitHex", testArrayInitHex)
  100. ]
  101. }