From 4dfc6a9a718ad8bf3cd4b28323bd676012d82af2 Mon Sep 17 00:00:00 2001 From: Caio Dottori Date: Wed, 16 Mar 2022 13:59:58 -0300 Subject: [PATCH] Add tests --- Tests/starkbank-ecdsaTests/EcdsaTests.swift | 44 ++++++++++++ Tests/starkbank-ecdsaTests/OpenSSLTests.swift | 47 +++++++++++++ .../PrivateKeyTests.swift | 62 +++++++++++++++++ .../starkbank-ecdsaTests/PublicKeyTests.swift | 68 +++++++++++++++++++ Tests/starkbank-ecdsaTests/RandomTests.swift | 25 +++++++ .../starkbank-ecdsaTests/SignatureTests.swift | 38 +++++++++++ 6 files changed, 284 insertions(+) create mode 100644 Tests/starkbank-ecdsaTests/EcdsaTests.swift create mode 100644 Tests/starkbank-ecdsaTests/OpenSSLTests.swift create mode 100644 Tests/starkbank-ecdsaTests/PrivateKeyTests.swift create mode 100644 Tests/starkbank-ecdsaTests/PublicKeyTests.swift create mode 100644 Tests/starkbank-ecdsaTests/RandomTests.swift create mode 100644 Tests/starkbank-ecdsaTests/SignatureTests.swift diff --git a/Tests/starkbank-ecdsaTests/EcdsaTests.swift b/Tests/starkbank-ecdsaTests/EcdsaTests.swift new file mode 100644 index 0000000..e107939 --- /dev/null +++ b/Tests/starkbank-ecdsaTests/EcdsaTests.swift @@ -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)) + } +} diff --git a/Tests/starkbank-ecdsaTests/OpenSSLTests.swift b/Tests/starkbank-ecdsaTests/OpenSSLTests.swift new file mode 100644 index 0000000..8df5825 --- /dev/null +++ b/Tests/starkbank-ecdsaTests/OpenSSLTests.swift @@ -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)) + } +} diff --git a/Tests/starkbank-ecdsaTests/PrivateKeyTests.swift b/Tests/starkbank-ecdsaTests/PrivateKeyTests.swift new file mode 100644 index 0000000..e2c51da --- /dev/null +++ b/Tests/starkbank-ecdsaTests/PrivateKeyTests.swift @@ -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) + } +} diff --git a/Tests/starkbank-ecdsaTests/PublicKeyTests.swift b/Tests/starkbank-ecdsaTests/PublicKeyTests.swift new file mode 100644 index 0000000..0849403 --- /dev/null +++ b/Tests/starkbank-ecdsaTests/PublicKeyTests.swift @@ -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) + } +} diff --git a/Tests/starkbank-ecdsaTests/RandomTests.swift b/Tests/starkbank-ecdsaTests/RandomTests.swift new file mode 100644 index 0000000..00010c0 --- /dev/null +++ b/Tests/starkbank-ecdsaTests/RandomTests.swift @@ -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)) + } + } +} diff --git a/Tests/starkbank-ecdsaTests/SignatureTests.swift b/Tests/starkbank-ecdsaTests/SignatureTests.swift new file mode 100644 index 0000000..fc794cf --- /dev/null +++ b/Tests/starkbank-ecdsaTests/SignatureTests.swift @@ -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) + } +}