From 0abef556c98aabb652b552ed8f5a6f4cc64967c2 Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Mon, 2 Dec 2024 16:19:40 +0800 Subject: [PATCH 1/2] update utils coverage --- Utils/Tests/UtilsTests/Crypto/BLSTests.swift | 89 ++++++++++++++++ .../UtilsTests/Crypto/BandersnatchTest.swift | 26 ++++- .../UtilsTests/Crypto/Ed25519Tests.swift | 100 +++++++++++++++--- Utils/Tests/UtilsTests/ErasureCodeTest.swift | 16 +++ .../EventBus/DispatcherMiddlewareTests.swift | 78 ++++++++++++++ 5 files changed, 295 insertions(+), 14 deletions(-) create mode 100644 Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift diff --git a/Utils/Tests/UtilsTests/Crypto/BLSTests.swift b/Utils/Tests/UtilsTests/Crypto/BLSTests.swift index 7c7b8b06..c54f26e8 100644 --- a/Utils/Tests/UtilsTests/Crypto/BLSTests.swift +++ b/Utils/Tests/UtilsTests/Crypto/BLSTests.swift @@ -65,4 +65,93 @@ import Testing ) ) } + + @Test func BLSKeyInitialization() throws { + let seed = Data32.random() + let bls = try BLS.SecretKey(from: seed) + #expect(bls.publicKey.data.data.count == Int(BLS_PUBLICKEY_SERIALIZED_SIZE)) + } + + @Test func BLSSignatureSizeValidation() throws { + let bls = try BLS.SecretKey(from: Data32.random()) + let message = Data("test".utf8) + let signature = try bls.sign(message: message) + #expect(signature.count == Int(BLS_SIGNATURE_SERIALIZED_SIZE)) + } + + @Test func BLSPublicKeySerialization() throws { + let bls = try BLS.SecretKey(from: Data32.random()) + let publicKey = bls.publicKey + + // Encode and decode the public key + let encoder = JSONEncoder() + let encodedData = try encoder.encode(publicKey) + let decoder = JSONDecoder() + let decodedPublicKey = try decoder.decode(BLS.PublicKey.self, from: encodedData) + + #expect(decodedPublicKey == publicKey) + } + + @Test func BLSPublicKeyEquality() throws { + let key1 = try BLS.SecretKey(from: Data32.random()).publicKey + let key2 = try BLS.SecretKey(from: Data32.random()).publicKey + #expect(key1 != key2) + } + + @Test func PublicKeySerialization() throws { + let key1 = try BLS.SecretKey(from: Data32.random()).publicKey.data + let publicKey = try BLS.PublicKey(data: key1) + + // Test encoding and decoding + let encoder = JSONEncoder() + let encoded = try encoder.encode(publicKey) + + let decoder = JSONDecoder() + let decodedPublicKey = try decoder.decode(BLS.PublicKey.self, from: encoded) + + #expect(publicKey == decodedPublicKey) + #expect(publicKey.description == decodedPublicKey.description) + } + + @Test func PublicKey() throws { + let keyData1 = Data144.random() + #expect(throws: Error.self) { + _ = try BLS.PublicKey(data: keyData1) + } + } + + @Test func PublicKeyVerifyValidSignature() throws { + let bls = try BLS.SecretKey(from: Data32.random()) + let publicKey = bls.publicKey + let message = Data("testMessage".utf8) + let signature = try bls.sign(message: message) + + // Test valid signature verification + #expect(try publicKey.verify(signature: signature, message: message)) + } + + @Test func PublicKeyVerifyInvalidSignature() throws { + let bls = try BLS.SecretKey(from: Data32.random()) + let publicKey = bls.publicKey + let message = Data("testMessage".utf8) + let signature = try bls.sign(message: message) + + // Corrupt the signature + var invalidSignature = signature + invalidSignature[0] ^= 0xFF // Flip a bit in the first byte + + // Test invalid signature verification + #expect(try !publicKey.verify(signature: invalidSignature, message: message)) + } + + @Test func PublicKeyVerifyInvalidMessage() throws { + let bls = try BLS.SecretKey(from: Data32.random()) + let publicKey = bls.publicKey + let message = Data("testMessage".utf8) + let invalidMessage = Data("invalidMessage".utf8) + let signature = try bls.sign(message: message) + + // Test verification with an invalid message + #expect(try !publicKey.verify(signature: signature, message: invalidMessage)) + } } diff --git a/Utils/Tests/UtilsTests/Crypto/BandersnatchTest.swift b/Utils/Tests/UtilsTests/Crypto/BandersnatchTest.swift index aa4bec50..cf261c19 100644 --- a/Utils/Tests/UtilsTests/Crypto/BandersnatchTest.swift +++ b/Utils/Tests/UtilsTests/Crypto/BandersnatchTest.swift @@ -20,7 +20,9 @@ import Testing let verifier = Bandersnatch.Verifier(ctx: ctx, commitment: commitment) for (i, key) in keys.enumerated() { - let prover = Bandersnatch.Prover(sercret: key, ring: keys.map(\.publicKey), proverIdx: UInt(i), ctx: ctx) + let prover = Bandersnatch.Prover( + sercret: key, ring: keys.map(\.publicKey), proverIdx: UInt(i), ctx: ctx + ) let vrfInputData = Data(repeating: UInt8(i), count: 32) let sig = try prover.ringVRFSign(vrfInputData: vrfInputData) let output = try verifier.ringVRFVerify(vrfInputData: vrfInputData, signature: sig) @@ -28,4 +30,26 @@ import Testing #expect(output == vrfOutput) } } + + @Test func testInitialization() throws { + let secret = try Bandersnatch.SecretKey(from: Data32.random()) + + let publicKey = try Bandersnatch.PublicKey(data: secret.publicKey.data) + #expect(publicKey.data == secret.publicKey.data) + } + + @Test func testEncodingAndDecoding() throws { + let secret = try Bandersnatch.SecretKey(from: Data32.random()) + + let publicKey = try Bandersnatch.PublicKey(data: secret.publicKey.data) + let encoder = JSONEncoder() + let encoded = try encoder.encode(publicKey.data) + + let decoder = JSONDecoder() + let decodedPublicKey = try decoder.decode(Bandersnatch.PublicKey.self, from: encoded) + + #expect(decodedPublicKey == publicKey) + #expect(decodedPublicKey.data == publicKey.data) + #expect(publicKey.description == decodedPublicKey.description) + } } diff --git a/Utils/Tests/UtilsTests/Crypto/Ed25519Tests.swift b/Utils/Tests/UtilsTests/Crypto/Ed25519Tests.swift index 16b47d66..d809d85a 100644 --- a/Utils/Tests/UtilsTests/Crypto/Ed25519Tests.swift +++ b/Utils/Tests/UtilsTests/Crypto/Ed25519Tests.swift @@ -4,23 +4,97 @@ import Testing @testable import Utils @Suite struct Ed25519Tests { - @Test func testEd25519Signature() throws { - let ed25519 = try Ed25519.SecretKey(from: Data32.random()) - let publicKey = ed25519.publicKey + @Test func validateSignature() throws { + let secretKey = try Ed25519.SecretKey(from: Data32.random()) + let publicKey = secretKey.publicKey let message = Data("test".utf8) - let signature = try ed25519.sign(message: message) + let signature = try secretKey.sign(message: message) + #expect(publicKey.verify(signature: signature, message: message)) + } + + @Test func rejectInvalidMessage() throws { + let secretKey = try Ed25519.SecretKey(from: Data32.random()) + let publicKey = secretKey.publicKey + let message = Data("test".utf8) + let signature = try secretKey.sign(message: message) let invalidMessage = Data("tests".utf8) - #expect( - !publicKey.verify(signature: signature, message: invalidMessage) - ) - - var invalidSignature = signature.data - invalidSignature.replaceSubrange(0 ... 1, with: [10, 12]) - #expect( - !publicKey.verify(signature: Data64(invalidSignature)!, message: message) - ) + + #expect(!publicKey.verify(signature: signature, message: invalidMessage)) + } + + @Test func rejectTamperedSignature() throws { + let secretKey = try Ed25519.SecretKey(from: Data32.random()) + let publicKey = secretKey.publicKey + + let message = Data("test".utf8) + let signature = try secretKey.sign(message: message) + + var tamperedSignature = signature.data + tamperedSignature.replaceSubrange(0 ... 1, with: [10, 12]) + + #expect(!publicKey.verify(signature: Data64(tamperedSignature)!, message: message)) + } + + @Test func initializeFromData() throws { + let randomData = Data32.random() + let publicKey = try Ed25519.PublicKey(from: randomData) + #expect(publicKey.data == randomData) + } + + @Test func encodeAndDecode() throws { + let originalData = Data32.random() + let originalKey = try Ed25519.PublicKey(from: originalData) + + let encoder = JSONEncoder() + let encodedData = try encoder.encode(originalKey) + + let decoder = JSONDecoder() + let decodedKey = try decoder.decode(Ed25519.PublicKey.self, from: encodedData) + + #expect(decodedKey == originalKey) + } + + @Test func hashAndEquality() throws { + let data1 = Data32.random() + let data2 = Data32.random() + + let publicKey1 = try Ed25519.PublicKey(from: data1) + let publicKey2 = try Ed25519.PublicKey(from: data1) + let publicKey3 = try Ed25519.PublicKey(from: data2) + + var hashSet: Set = [] + hashSet.insert(publicKey1) + + #expect(publicKey1 == publicKey2) + #expect(publicKey1 != publicKey3) + #expect(hashSet.contains(publicKey2)) + #expect(!hashSet.contains(publicKey3)) + } + + @Test func descriptionCheck() throws { + let randomData = Data32.random() + let publicKey = try Ed25519.PublicKey(from: randomData) + + #expect(publicKey.description == randomData.description) + } + + @Test func signatureVerification() throws { + let secretKey = try Ed25519.SecretKey(from: Data32.random()) + let publicKey = secretKey.publicKey + + let message = Data("test message".utf8) + let signature = try secretKey.sign(message: message) + + #expect(publicKey.verify(signature: signature, message: message)) + + let invalidMessage = Data("tampered message".utf8) + #expect(!publicKey.verify(signature: signature, message: invalidMessage)) + + var tamperedSignature = signature.data + tamperedSignature[0] ^= 0xFF + #expect(!publicKey.verify(signature: Data64(tamperedSignature)!, message: message)) } } diff --git a/Utils/Tests/UtilsTests/ErasureCodeTest.swift b/Utils/Tests/UtilsTests/ErasureCodeTest.swift index afe5f3d5..d72324a3 100644 --- a/Utils/Tests/UtilsTests/ErasureCodeTest.swift +++ b/Utils/Tests/UtilsTests/ErasureCodeTest.swift @@ -44,6 +44,22 @@ struct ErasureCodeTests { } } + @Test + func constructWithSegments() throws { + let segments: [Segment] = [] + let encoder = SubShardEncoder() + let result = encoder.construct(segments: segments) + if case let .failure(constructFailed) = result { + #expect(constructFailed == ErasureCodeError.constructFailed) + } + } + + @Test + func constructWithIncorrectSegmentSize() throws { + let incorrectData = Data(repeating: 0xFF, count: Int(SEGMENT_SIZE) - 1) + #expect(Segment(data: incorrectData, index: 0) == nil) + } + @Test(arguments: try loadTests()) func testReconstruct(testCase: ECTestCase) throws { // Convert segment_ec data back to bytes and prepare subshards diff --git a/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift b/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift new file mode 100644 index 00000000..a97f5b6f --- /dev/null +++ b/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift @@ -0,0 +1,78 @@ +import bls +import Foundation +import Testing + +@testable import Utils + +final class MiddlewareTests { + actor OrderManager { + private(set) var order: [Int] = [] + + func appendOrder(_ value: Int) { + order.append(value) + } + } + + func testParallelDispatcher() async throws { + let orderManager = OrderManager() + + let firstMiddleware = Middleware.noop + let secondMiddleware = Middleware.noop + + let parallelMiddleware = Middleware.parallel(firstMiddleware, secondMiddleware) + + let handler: MiddlewareHandler = { _ in + await orderManager.appendOrder(2) + } + + try await parallelMiddleware.handle((), next: { + await orderManager.appendOrder(1) + try await handler(()) + }) + + let order = await orderManager.order + #expect(order.count == 2) + } + + func testSerialDispatcher() async throws { + let orderManager = OrderManager() + + let firstMiddleware = Middleware.noop + let secondMiddleware = Middleware.noop + + let serialMiddleware = Middleware.serial(firstMiddleware, secondMiddleware) + + let handler: MiddlewareHandler = { _ in + await orderManager.appendOrder(2) + } + + try await serialMiddleware.handle((), next: { + await orderManager.appendOrder(1) + try await handler(()) + }) + + let order = await orderManager.order + #expect(order == [1, 2]) + } + + func testMiddlewareChain() async throws { + let orderManager = OrderManager() + + let middleware1 = Middleware.noop + let middleware2 = Middleware.noop + + let middlewareChain = Middleware.serial(middleware1, middleware2) + + let handler: MiddlewareHandler = { _ in + await orderManager.appendOrder(2) + } + + try await middlewareChain.handle((), next: { + await orderManager.appendOrder(1) + try await handler(()) + }) + + let order = await orderManager.order + #expect(order == [1, 2]) + } +} From 5fd22053e45c394d4f312e73c5b90c790786af32 Mon Sep 17 00:00:00 2001 From: MacOMNI <414294494@qq.com> Date: Mon, 2 Dec 2024 17:47:53 +0800 Subject: [PATCH 2/2] update utils coverage --- .../EventBus/DispatcherMiddlewareTests.swift | 7 ++-- Utils/Tests/UtilsTests/OptionalTests.swift | 38 +++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 Utils/Tests/UtilsTests/OptionalTests.swift diff --git a/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift b/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift index a97f5b6f..edbbcc14 100644 --- a/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift +++ b/Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift @@ -13,7 +13,7 @@ final class MiddlewareTests { } } - func testParallelDispatcher() async throws { + @Test func testParallelDispatcher() async throws { let orderManager = OrderManager() let firstMiddleware = Middleware.noop @@ -27,14 +27,13 @@ final class MiddlewareTests { try await parallelMiddleware.handle((), next: { await orderManager.appendOrder(1) - try await handler(()) }) let order = await orderManager.order #expect(order.count == 2) } - func testSerialDispatcher() async throws { + @Test func testSerialDispatcher() async throws { let orderManager = OrderManager() let firstMiddleware = Middleware.noop @@ -55,7 +54,7 @@ final class MiddlewareTests { #expect(order == [1, 2]) } - func testMiddlewareChain() async throws { + @Test func testMiddlewareChain() async throws { let orderManager = OrderManager() let middleware1 = Middleware.noop diff --git a/Utils/Tests/UtilsTests/OptionalTests.swift b/Utils/Tests/UtilsTests/OptionalTests.swift new file mode 100644 index 00000000..4b38c771 --- /dev/null +++ b/Utils/Tests/UtilsTests/OptionalTests.swift @@ -0,0 +1,38 @@ +import Foundation +import Testing + +@testable import Utils + +struct OptionalTests { + @Test func testUnwrapSuccess() throws { + let optionalValue: Int? = 42 + + let result = try optionalValue.unwrap() + + #expect(result == 42) + } + + @Test func testUnwrapFailure() throws { + let optionalValue: Int? = nil + + #expect(throws: OptionalError.nilValue) { + _ = try optionalValue.unwrap() + } + } + + @Test func testUnwrapOrErrorSuccess() throws { + let optionalValue: Int? = 42 + + let result = try optionalValue.unwrap(orError: NSError(domain: "", code: 1)) + + #expect(result == 42) + } + + @Test func testExpectSuccess() throws { + let optionalValue: Int? = 42 + + let result = optionalValue.expect("Value should be present") + + #expect(result == 42) + } +}