ExtensionsTest.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 testToUInt32Array() {
  41. let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
  42. let result = chunk.toUInt32Array()
  43. XCTAssert(result.count == 2, "Invalid conversion")
  44. XCTAssert(result[0] == 117506305, "Invalid conversion")
  45. XCTAssert(result[1] == 84148994, "Invalid conversion")
  46. }
  47. func testDataInit() {
  48. let data = Data(bytes: [0x01, 0x02, 0x03])
  49. XCTAssert(data.count == 3, "Invalid data")
  50. }
  51. func testStringEncrypt() {
  52. do {
  53. let encryptedHex = try "my secret string".encrypt(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  54. XCTAssertEqual(encryptedHex, "68f7ff8bdb61f625febdfe3d791ecf624daaed2e719a6de39112de8e0cc7349b")
  55. } catch {
  56. XCTFail(error.localizedDescription)
  57. }
  58. }
  59. func testStringDecryptBase64() {
  60. let encryptedBase64 = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  61. let decrypted = try! encryptedBase64.decryptBase64ToString(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  62. XCTAssertEqual(decrypted, "my secret string")
  63. }
  64. func testArrayInitHex() {
  65. let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
  66. XCTAssertEqual(bytes, [177,177,178,178])
  67. let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
  68. let array = Array<UInt8>(hex: str)
  69. let hex = array.toHexString()
  70. XCTAssertEqual(str, hex)
  71. }
  72. static let allTests = [
  73. ("testArrayChunksPerformance", testArrayChunksPerformance),
  74. ("testIntExtension", testIntExtension),
  75. ("testBytes", testBytes),
  76. ("testToUInt32Array", testToUInt32Array),
  77. ("testDataInit", testDataInit),
  78. ("testStringEncrypt", testStringEncrypt),
  79. ("testStringDecryptBase64", testStringDecryptBase64),
  80. ("testArrayInitHex", testArrayInitHex)
  81. ]
  82. }