Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdottori-stark committed Mar 16, 2022
1 parent 8cf2f8d commit 4dfc6a9
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Tests/starkbank-ecdsaTests/EcdsaTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// EcdsaTests.swift
// MyAppTests
//
// Created by Felipe Sueto on 28/10/21.
// Copyright © 2021 Stark Bank. All rights reserved.
//

import XCTest

@testable import starkbank_ecdsa

class EcdsaTests: XCTestCase {

func testVerifyRightMessage() throws {
let privateKey = try PrivateKey()
let publicKey = privateKey.publicKey()
let message = "This is a text message"

let signature = try Ecdsa.sign(message: message, privateKey: privateKey)

XCTAssertTrue(Ecdsa.verify(message: message, signature: signature, publicKey: publicKey))
}

func testVerifyWrongMessage() throws {
let privateKey = try PrivateKey()
let publicKey = privateKey.publicKey()

let message1 = "This is the right message"
let message2 = "This is the wrong message"

let signature = try Ecdsa.sign(message: message1, privateKey: privateKey)

XCTAssertFalse(Ecdsa.verify(message: message2, signature: signature, publicKey: publicKey))
}

func testSignatureZero() throws {
let privateKey = try PrivateKey()
let publicKey = privateKey.publicKey()
let message = "This is a text message"

XCTAssertFalse(Ecdsa.verify(message: message, signature: Signature(0, 0), publicKey: publicKey))
}
}
47 changes: 47 additions & 0 deletions Tests/starkbank-ecdsaTests/OpenSSLTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// OpenSSLTests.swift
// MyAppTests
//
// Created by Felipe Sueto on 03/11/21.
// Copyright © 2021 Stark Bank. All rights reserved.
//

import XCTest

@testable import starkbank_ecdsa

class OpenSSLTests: XCTestCase {

func testSign() throws {
let privateKeyPem = """
-----BEGIN EC PARAMETERS-----
BgUrgQQACg==
-----END EC PARAMETERS-----
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIODvZuS34wFbt0X53+P5EnSj6tMjfVK01dD1dgDH02RzoAcGBSuBBAAK
oUQDQgAE/nvHu/SQQaos9TUljQsUuKI15Zr5SabPrbwtbfT/408rkVVzq8vAisbB
RmpeRREXj5aog/Mq8RrdYy75W9q/Ig==
-----END EC PRIVATE KEY-----
"""
let privateKey = try PrivateKey.fromPem(privateKeyPem)
let message = "This is a text message"
let signature = try Ecdsa.sign(message: message, privateKey: privateKey)
let publicKey = privateKey.publicKey()

XCTAssertTrue(Ecdsa.verify(message: message, signature: signature, publicKey: publicKey))
}

func testVerifySignature() throws {
let publicKeyPem = """
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEPyoJVvN9AX5tREfx0RswXJrCD7OIyRyL
r486ERyOjzaz0cdnJt6IfVvgRvxgZc9aidSx1FcA8qTBvEhji3f9NA==
-----END PUBLIC KEY-----
"""
let publicKey = try PublicKey.fromPem(publicKeyPem)
let message = "This is a text message"
let signature = try Signature.fromBase64("MEYCIQDOoyupIwoDHkgFnpAF6FANj2OVokPiDaZdLW16Pc1y4gIhAL4WcGXjqbgSGN8L9C2pxHFPY01Cp+CQmc80TE1jyuti")

XCTAssertTrue(Ecdsa.verify(message: message, signature: signature, publicKey: publicKey))
}
}
62 changes: 62 additions & 0 deletions Tests/starkbank-ecdsaTests/PrivateKeyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// PrivateKeyTests.swift
// MyAppTests
//
// Created by Felipe Sueto on 28/10/21.
// Copyright © 2021 Stark Bank. All rights reserved.
//

import XCTest

@testable import starkbank_ecdsa

class PrivateKeyTests: XCTestCase {

func testPemConversion() throws {
let privateKey = try PrivateKey()
let pem = privateKey.toPem()
let privateKey2 = try PrivateKey.fromPem(pem)
XCTAssertTrue(privateKey.secret == privateKey2.secret)
XCTAssertTrue(privateKey.curve.A == privateKey2.curve.A &&
privateKey.curve.B == privateKey2.curve.B &&
privateKey.curve.P == privateKey2.curve.P &&
privateKey.curve.N == privateKey2.curve.N &&
privateKey.curve.name == privateKey2.curve.name &&
privateKey.curve.oid == privateKey2.curve.oid &&
privateKey.curve.G.x == privateKey2.curve.G.x &&
privateKey.curve.G.y == privateKey2.curve.G.y &&
privateKey.curve.G.z == privateKey2.curve.G.z)
}

func testDerConversion() throws {
let privateKey = try PrivateKey()
let der = privateKey.toDer()
let privateKey2 = try PrivateKey.fromDer(der)
XCTAssertTrue(privateKey.secret == privateKey2.secret)
XCTAssertTrue(privateKey.curve.A == privateKey2.curve.A &&
privateKey.curve.B == privateKey2.curve.B &&
privateKey.curve.P == privateKey2.curve.P &&
privateKey.curve.N == privateKey2.curve.N &&
privateKey.curve.name == privateKey2.curve.name &&
privateKey.curve.oid == privateKey2.curve.oid &&
privateKey.curve.G.x == privateKey2.curve.G.x &&
privateKey.curve.G.y == privateKey2.curve.G.y &&
privateKey.curve.G.z == privateKey2.curve.G.z)
}

func testStringConversion() throws {
let privateKey = try PrivateKey()
let string = privateKey.toString()
let privateKey2 = try PrivateKey.fromString(string: string)
XCTAssertTrue(privateKey.secret == privateKey2.secret)
XCTAssertTrue(privateKey.curve.A == privateKey2.curve.A &&
privateKey.curve.B == privateKey2.curve.B &&
privateKey.curve.P == privateKey2.curve.P &&
privateKey.curve.N == privateKey2.curve.N &&
privateKey.curve.name == privateKey2.curve.name &&
privateKey.curve.oid == privateKey2.curve.oid &&
privateKey.curve.G.x == privateKey2.curve.G.x &&
privateKey.curve.G.y == privateKey2.curve.G.y &&
privateKey.curve.G.z == privateKey2.curve.G.z)
}
}
68 changes: 68 additions & 0 deletions Tests/starkbank-ecdsaTests/PublicKeyTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// PublicKeyTests.swift
// MyAppTests
//
// Created by Felipe Sueto on 28/10/21.
// Copyright © 2021 Stark Bank. All rights reserved.
//

import XCTest

@testable import starkbank_ecdsa

class PublicKeyTests: XCTestCase {

func testPemConversion() throws {
let privateKey = try PrivateKey()
let publicKey1 = privateKey.publicKey()
let pem = publicKey1.toPem()
let publicKey2 = try PublicKey.fromPem(pem)
XCTAssertTrue(publicKey1.point.x == publicKey2.point.x)
XCTAssertTrue(publicKey1.point.y == publicKey2.point.y)
XCTAssertTrue(publicKey1.curve.A == publicKey2.curve.A &&
publicKey1.curve.B == publicKey2.curve.B &&
publicKey1.curve.P == publicKey2.curve.P &&
publicKey1.curve.N == publicKey2.curve.N &&
publicKey1.curve.name == publicKey2.curve.name &&
publicKey1.curve.oid == publicKey2.curve.oid &&
publicKey1.curve.G.x == publicKey2.curve.G.x &&
publicKey1.curve.G.y == publicKey2.curve.G.y &&
publicKey1.curve.G.z == publicKey2.curve.G.z)
}

func testDerConversion() throws {
let privateKey = try PrivateKey()
let publicKey1 = privateKey.publicKey()
let der = publicKey1.toDer()
let publicKey2 = try PublicKey.fromDer(der)
XCTAssertTrue(publicKey1.point.x == publicKey2.point.x)
XCTAssertTrue(publicKey1.point.y == publicKey2.point.y)
XCTAssertTrue(publicKey1.curve.A == publicKey2.curve.A &&
publicKey1.curve.B == publicKey2.curve.B &&
publicKey1.curve.P == publicKey2.curve.P &&
publicKey1.curve.N == publicKey2.curve.N &&
publicKey1.curve.name == publicKey2.curve.name &&
publicKey1.curve.oid == publicKey2.curve.oid &&
publicKey1.curve.G.x == publicKey2.curve.G.x &&
publicKey1.curve.G.y == publicKey2.curve.G.y &&
publicKey1.curve.G.z == publicKey2.curve.G.z)
}

func testStringConversion() throws {
let privateKey = try PrivateKey()
let publicKey1 = privateKey.publicKey()
var string = publicKey1.toString()
let publicKey2 = try PublicKey.fromString(string: &string)
XCTAssertTrue(publicKey1.point.x == publicKey2.point.x)
XCTAssertTrue(publicKey1.point.y == publicKey2.point.y)
XCTAssertTrue(publicKey1.curve.A == publicKey2.curve.A &&
publicKey1.curve.B == publicKey2.curve.B &&
publicKey1.curve.P == publicKey2.curve.P &&
publicKey1.curve.N == publicKey2.curve.N &&
publicKey1.curve.name == publicKey2.curve.name &&
publicKey1.curve.oid == publicKey2.curve.oid &&
publicKey1.curve.G.x == publicKey2.curve.G.x &&
publicKey1.curve.G.y == publicKey2.curve.G.y &&
publicKey1.curve.G.z == publicKey2.curve.G.z)
}
}
25 changes: 25 additions & 0 deletions Tests/starkbank-ecdsaTests/RandomTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// RandomTests.swift
// MyAppTests
//
// Created by Felipe Sueto on 29/10/21.
// Copyright © 2021 Stark Bank. All rights reserved.
//

import XCTest

@testable import starkbank_ecdsa

class RandomTests: XCTestCase {

func testMany() throws {
let message = "This is a text message"
for _ in repeatElement(0, count: 1) {
let privateKey = try PrivateKey()
let publicKey = privateKey.publicKey()

let signature = try Ecdsa.sign(message: message, privateKey: privateKey)
XCTAssertTrue(Ecdsa.verify(message: message, signature: signature, publicKey: publicKey))
}
}
}
38 changes: 38 additions & 0 deletions Tests/starkbank-ecdsaTests/SignatureTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// SignatureTests.swift
// MyAppTests
//
// Created by Felipe Sueto on 28/10/21.
// Copyright © 2021 Stark Bank. All rights reserved.
//

import XCTest

@testable import starkbank_ecdsa

class SignatureTests: XCTestCase {

func testDerConversion() throws {
let privateKey = try PrivateKey()
let message = "This is a text message"
let signature1 = try Ecdsa.sign(message: message, privateKey: privateKey)

let der = signature1.toDer()
let signature2 = try Signature.fromDer(der)

XCTAssertTrue(signature1.r == signature2.r)
XCTAssertTrue(signature1.s == signature2.s)
}

func testBase64Conversion() throws {
let privateKey = try PrivateKey()
let message = "This is a text message"
let signature1 = try Ecdsa.sign(message: message, privateKey: privateKey)

let base64 = signature1.toBase64()
let signature2 = try Signature.fromBase64(base64)

XCTAssertTrue(signature1.r == signature2.r)
XCTAssertTrue(signature1.s == signature2.s)
}
}

0 comments on commit 4dfc6a9

Please sign in to comment.