SecEncodeTransformTests.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.dataUsingUTF8StringEncoding, $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. var results = Array<String>(count: countElements(convertedVectors), repeatedValue: "")
  35. let vectorsAndIndices = Zip2(convertedVectors, indices(results))
  36. self.measureBlock{
  37. for _ in 0...100 {
  38. for ((test, expect, _), index) in vectorsAndIndices {
  39. results[index] = TTTBase32EncodedStringFromData(test)
  40. }
  41. }
  42. }
  43. for ((test, expect, _), result) in Zip2(convertedVectors, results) {
  44. XCTAssertEqual(result, expect, "TTTBase32EncodedStringFromData for \(test)")
  45. }
  46. }
  47. func test_RFC4648_Decode_UsingSecEncodeTransform() {
  48. var results = Array<NSData>(count: countElements(convertedVectors), repeatedValue: NSData())
  49. let vectorsAndIndices = Zip2(convertedVectors, indices(results))
  50. self.measureBlock{
  51. for _ in 0...100 {
  52. for ((expect, test, _), index) in vectorsAndIndices {
  53. results[index] = TTTDataFromBase32EncodedString(test)
  54. }
  55. }
  56. }
  57. for ((expect, test, _), result) in Zip2(convertedVectors, results) {
  58. XCTAssertEqual(result, expect, "TTTDataFromBase32EncodedString for \(test)")
  59. }
  60. }
  61. // MARK: Using Base32
  62. func test_RFC4648_Encode_UsingBase32() {
  63. var results = Array<String>(count: countElements(convertedVectors), repeatedValue: "")
  64. let vectorsAndIndices = Zip2(convertedVectors, indices(results))
  65. self.measureBlock{
  66. for _ in 0...100 {
  67. for ((test, expect, _), index) in vectorsAndIndices {
  68. results[index] = base32Encode(test)
  69. }
  70. }
  71. }
  72. for ((test, expect, _), result) in Zip2(convertedVectors, results) {
  73. XCTAssertEqual(result, expect, "base32Encode for \(test)")
  74. }
  75. }
  76. func test_RFC4648_Decode_UsingBase32() {
  77. var results = Array<NSData>(count: countElements(convertedVectors), repeatedValue: NSData())
  78. let vectorsAndIndices = Zip2(convertedVectors, indices(results))
  79. self.measureBlock{
  80. for _ in 0...100 {
  81. for ((expect, test, _), index) in vectorsAndIndices {
  82. results[index] = base32DecodeToData(test)!
  83. }
  84. }
  85. }
  86. for ((expect, test, _), result) in Zip2(convertedVectors, results) {
  87. XCTAssertEqual(result, expect, "base32Decode for \(test)")
  88. }
  89. }
  90. }