Skip to content

Commit

Permalink
update utils coverage (#236)
Browse files Browse the repository at this point in the history
* update utils coverage

* update utils coverage
  • Loading branch information
MacOMNI authored Dec 4, 2024
1 parent 9dd2740 commit 29b9367
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 14 deletions.
89 changes: 89 additions & 0 deletions Utils/Tests/UtilsTests/Crypto/BLSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
26 changes: 25 additions & 1 deletion Utils/Tests/UtilsTests/Crypto/BandersnatchTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,36 @@ 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)
let vrfOutput = try keys[i].getOutput(vrfInputData: vrfInputData)
#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)
}
}
100 changes: 87 additions & 13 deletions Utils/Tests/UtilsTests/Crypto/Ed25519Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ed25519.PublicKey> = []
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))
}
}
16 changes: 16 additions & 0 deletions Utils/Tests/UtilsTests/ErasureCodeTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions Utils/Tests/UtilsTests/EventBus/DispatcherMiddlewareTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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)
}
}

@Test func testParallelDispatcher() async throws {
let orderManager = OrderManager()

let firstMiddleware = Middleware.noop
let secondMiddleware = Middleware.noop

let parallelMiddleware = Middleware.parallel(firstMiddleware, secondMiddleware)

let handler: MiddlewareHandler<Void> = { _ in
await orderManager.appendOrder(2)
}

try await parallelMiddleware.handle((), next: {
await orderManager.appendOrder(1)
})

let order = await orderManager.order
#expect(order.count == 2)
}

@Test func testSerialDispatcher() async throws {
let orderManager = OrderManager()

let firstMiddleware = Middleware.noop
let secondMiddleware = Middleware.noop

let serialMiddleware = Middleware.serial(firstMiddleware, secondMiddleware)

let handler: MiddlewareHandler<Void> = { _ 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])
}

@Test func testMiddlewareChain() async throws {
let orderManager = OrderManager()

let middleware1 = Middleware.noop
let middleware2 = Middleware.noop

let middlewareChain = Middleware.serial(middleware1, middleware2)

let handler: MiddlewareHandler<Void> = { _ 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])
}
}
38 changes: 38 additions & 0 deletions Utils/Tests/UtilsTests/OptionalTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 29b9367

Please sign in to comment.