SecEncodeTransformTests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // SecEncodeTransformTests.swift
  3. // Base32
  4. //
  5. // Created by 野村 憲男 on 1/25/15.
  6. // Copyright (c) 2015 Norio Nomura. All rights reserved.
  7. //
  8. import Foundation
  9. import XCTest
  10. import Security
  11. import Base32
  12. let vectors: [(String, String, String)] = [
  13. ("", "", ""),
  14. ("f", "MY======", "CO======"),
  15. ("fo", "MZXQ====", "CPNG===="),
  16. ("foo", "MZXW6===", "CPNMU==="),
  17. ("foob", "MZXW6YQ=", "CPNMUOG="),
  18. ("fooba", "MZXW6YTB", "CPNMUOJ1"),
  19. ("foobar", "MZXW6YTBOI======", "CPNMUOJ1E8======"),
  20. ]
  21. let convertedVectors = vectors.map {($0.data(using:.utf8)!, $1, $2)}
  22. class SecEncodeTransformTests: XCTestCase {
  23. override func setUp() {
  24. super.setUp()
  25. // Put setup code here. This method is called before the invocation of each test method in the class.
  26. }
  27. override func tearDown() {
  28. // Put teardown code here. This method is called after the invocation of each test method in the class.
  29. super.tearDown()
  30. }
  31. // MARK: https://tools.ietf.org/html/rfc4648
  32. // MARK: Using SecEncodeTransform
  33. func test_RFC4648_Encode_UsingSecEncodeTransform() {
  34. #if os(macOS)
  35. var results = Array<String>(repeating: "", count: convertedVectors.count)
  36. let vectorsAndIndices = zip(convertedVectors, results.indices)
  37. self.measure{
  38. for _ in 0...100 {
  39. for ((test, _, _), index) in vectorsAndIndices {
  40. results[index] = TTTBase32EncodedString(from: test)!
  41. }
  42. }
  43. }
  44. for ((test, expect, _), result) in zip(convertedVectors, results) {
  45. XCTAssertEqual(result, expect, "TTTBase32EncodedStringFromData for \(test)")
  46. }
  47. #else
  48. print("\(#function) is available on macOS")
  49. #endif
  50. }
  51. func test_RFC4648_Decode_UsingSecEncodeTransform() {
  52. #if os(macOS)
  53. var results = Array<Data>(repeating: Data(), count: convertedVectors.count)
  54. let vectorsAndIndices = zip(convertedVectors, results.indices)
  55. self.measure{
  56. for _ in 0...100 {
  57. for ((_, test, _), index) in vectorsAndIndices {
  58. results[index] = TTTData(fromBase32EncodedString: test)!
  59. }
  60. }
  61. }
  62. for ((expect, test, _), result) in zip(convertedVectors, results) {
  63. XCTAssertEqual(result, expect, "TTTDataFromBase32EncodedString for \(test)")
  64. }
  65. #else
  66. print("\(#function) is available on macOS")
  67. #endif
  68. }
  69. // MARK: Using Base32
  70. func test_RFC4648_Encode_UsingBase32() {
  71. var results = Array<String>(repeating: "", count: convertedVectors.count)
  72. let vectorsAndIndices = zip(convertedVectors, results.indices)
  73. self.measure{
  74. for _ in 0...100 {
  75. for ((test, _, _), index) in vectorsAndIndices {
  76. results[index] = base32Encode(test)
  77. }
  78. }
  79. }
  80. for ((test, expect, _), result) in zip(convertedVectors, results) {
  81. XCTAssertEqual(result, expect, "base32Encode for \(test)")
  82. }
  83. }
  84. func test_RFC4648_Decode_UsingBase32() {
  85. var results = Array<Data>(repeating: Data(), count: convertedVectors.count)
  86. let vectorsAndIndices = zip(convertedVectors, results.indices)
  87. self.measure{
  88. for _ in 0...100 {
  89. for ((_, test, _), index) in vectorsAndIndices {
  90. results[index] = base32DecodeToData(test)!
  91. }
  92. }
  93. }
  94. for ((expect, test, _), result) in zip(convertedVectors, results) {
  95. XCTAssertEqual(result, expect, "base32Decode for \(test)")
  96. }
  97. }
  98. }