2
0

ExtensionsTest.swift 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 testBytes() {
  21. let size = MemoryLayout<UInt32>.size // 32 or 64 bit
  22. let i:UInt32 = 1024
  23. var bytes = i.bytes()
  24. XCTAssertTrue(bytes.count == size, "Invalid bytes length = \(bytes.count)")
  25. // test padding
  26. bytes = i.bytes(totalBytes: 16)
  27. XCTAssertTrue(bytes.count == 16, "Invalid return type \(bytes.count)")
  28. XCTAssertTrue(bytes[14] == 4, "Invalid return type \(bytes.count)")
  29. }
  30. func testToUInt32Array() {
  31. let chunk:ArraySlice<UInt8> = [1,1,1,7,2,3,4,5]
  32. let result = chunk.toUInt32Array()
  33. XCTAssert(result.count == 2, "Invalid conversion")
  34. XCTAssert(result[0] == 117506305, "Invalid conversion")
  35. XCTAssert(result[1] == 84148994, "Invalid conversion")
  36. }
  37. func testDataInit() {
  38. let data = Data(bytes: [0x01, 0x02, 0x03])
  39. XCTAssert(data.count == 3, "Invalid data")
  40. }
  41. func testStringEncrypt() {
  42. do {
  43. let encryptedHex = try "my secret string".encrypt(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  44. XCTAssertEqual(encryptedHex, "68f7ff8bdb61f625febdfe3d791ecf624daaed2e719a6de39112de8e0cc7349b")
  45. } catch {
  46. XCTFail(error.localizedDescription)
  47. }
  48. }
  49. func testStringDecryptBase64() {
  50. let encryptedBase64 = "aPf/i9th9iX+vf49eR7PYk2q7S5xmm3jkRLejgzHNJs="
  51. let decrypted = try! encryptedBase64.decryptBase64ToString(cipher: AES(key: "secret0key000000", iv: "0123456789012345"))
  52. XCTAssertEqual(decrypted, "my secret string")
  53. }
  54. func testArrayInitHex() {
  55. let bytes = Array<UInt8>(hex: "0xb1b1b2b2")
  56. XCTAssertEqual(bytes, [177,177,178,178])
  57. let str = "b1b2b3b3b3b3b3b3b1b2b3b3b3b3b3b3"
  58. let array = Array<UInt8>(hex: str)
  59. let hex = array.toHexString()
  60. XCTAssertEqual(str, hex)
  61. }
  62. static let allTests = [
  63. ("testArrayChunksPerformance", testArrayChunksPerformance),
  64. ("testBytes", testBytes),
  65. ("testToUInt32Array", testToUInt32Array),
  66. ("testDataInit", testDataInit),
  67. ("testStringEncrypt", testStringEncrypt),
  68. ("testStringDecryptBase64", testStringDecryptBase64),
  69. ("testArrayInitHex", testArrayInitHex)
  70. ]
  71. }