Skip to content

Commit

Permalink
test: implement basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
limebell committed Jul 4, 2023
1 parent df49b76 commit ca3d540
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 2 deletions.
69 changes: 69 additions & 0 deletions Libplanet.Net.Tests/Messages/NetMQMessageCodecTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Libplanet.Blockchain;
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Libplanet.Net.Messages;
using Libplanet.Store;
Expand Down Expand Up @@ -41,6 +42,14 @@ public void Dispose()
[InlineData(MessageContent.MessageType.GetChainStatus)]
[InlineData(MessageContent.MessageType.ChainStatus)]
[InlineData(MessageContent.MessageType.DifferentVersion)]
[InlineData(MessageContent.MessageType.HaveMessage)]
[InlineData(MessageContent.MessageType.WantMessage)]
[InlineData(MessageContent.MessageType.ConsensusProposal)]
[InlineData(MessageContent.MessageType.ConsensusVote)]
[InlineData(MessageContent.MessageType.ConsensusCommit)]
[InlineData(MessageContent.MessageType.ConsensusMaj23Msg)]
[InlineData(MessageContent.MessageType.ConsensusVoteSetBitsMsg)]
[InlineData(MessageContent.MessageType.ConsensusProposalClaimMsg)]
public void CheckMessages(MessageContent.MessageType type)
{
var privateKey = new PrivateKey();
Expand Down Expand Up @@ -117,6 +126,66 @@ private MessageContent CreateMessage(MessageContent.MessageType type)
chain.Tip.Hash);
case MessageContent.MessageType.DifferentVersion:
return new DifferentVersionMsg();
case MessageContent.MessageType.HaveMessage:
return new HaveMessage(
new[] { new MessageId(TestUtils.GetRandomBytes(MessageId.Size)) });
case MessageContent.MessageType.WantMessage:
return new WantMessage(
new[] { new MessageId(TestUtils.GetRandomBytes(MessageId.Size)) });
case MessageContent.MessageType.ConsensusProposal:
return new ConsensusProposalMsg(
new ProposalMetadata(
0,
0,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
codec.Encode(genesis.MarshalBlock()),
-1).Sign(privateKey));
case MessageContent.MessageType.ConsensusVote:
return new ConsensusPreVoteMsg(
new VoteMetadata(
0,
0,
genesis.Hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
VoteFlag.PreVote).Sign(privateKey));
case MessageContent.MessageType.ConsensusCommit:
return new ConsensusPreCommitMsg(
new VoteMetadata(
0,
0,
genesis.Hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
VoteFlag.PreCommit).Sign(privateKey));
case MessageContent.MessageType.ConsensusMaj23Msg:
return new ConsensusMaj23Msg(
new Maj23Metadata(
0,
0,
genesis.Hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
VoteFlag.PreVote).Sign(privateKey));
case MessageContent.MessageType.ConsensusVoteSetBitsMsg:
return new ConsensusVoteSetBitsMsg(
new VoteSetBitsMetadata(
0,
0,
genesis.Hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey,
VoteFlag.PreVote,
new[] { true, true, false, false }).Sign(privateKey));
case MessageContent.MessageType.ConsensusProposalClaimMsg:
return new ConsensusProposalClaimMsg(
new ProposalClaimMetadata(
0,
0,
genesis.Hash,
DateTimeOffset.UtcNow,
privateKey.PublicKey).Sign(privateKey));
default:
throw new Exception($"Cannot create a message of invalid type {type}");
}
Expand Down
54 changes: 54 additions & 0 deletions Libplanet.Tests/Consensus/Maj23Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Xunit;

namespace Libplanet.Tests.Consensus
{
public class Maj23Test
{
[Fact]
public void InvalidSignature()
{
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));

Maj23Metadata metadata = new Maj23Metadata(
1,
0,
hash,
DateTimeOffset.UtcNow,
new PrivateKey().PublicKey,
VoteFlag.PreVote);

// Empty Signature
var emptySigBencodex = metadata.Encoded.Add(Maj23.SignatureKey, Array.Empty<byte>());
Assert.Throws<ArgumentNullException>(() => new Maj23(emptySigBencodex));

// Invalid Signature
var invSigBencodex = metadata.Encoded.Add(
Maj23.SignatureKey,
new PrivateKey().Sign(TestUtils.GetRandomBytes(20)));
Assert.Throws<ArgumentException>(() => new Maj23(invSigBencodex));
}

[Fact]
public void Sign()
{
var key = new PrivateKey();
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));

Maj23Metadata metadata = new Maj23Metadata(
1,
0,
hash,
DateTimeOffset.UtcNow,
key.PublicKey,
VoteFlag.PreVote);
Maj23 maj23 = metadata.Sign(key);

TestUtils.AssertBytesEqual(maj23.Signature, key.Sign(metadata.ByteArray));
Assert.True(key.PublicKey.Verify(metadata.ByteArray, maj23.Signature));
}
}
}
26 changes: 26 additions & 0 deletions Libplanet.Tests/Consensus/ProposalClaimMetadataTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Xunit;

namespace Libplanet.Tests.Consensus
{
public class ProposalClaimMetadataTest
{
[Fact]
public void Bencoded()
{
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));
var key = new PrivateKey();
var expected = new ProposalClaimMetadata(
1,
2,
hash,
DateTimeOffset.UtcNow,
key.PublicKey);
var decoded = new ProposalClaimMetadata(expected.Encoded);
Assert.Equal(expected, decoded);
}
}
}
54 changes: 54 additions & 0 deletions Libplanet.Tests/Consensus/ProposalClaimTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Xunit;

namespace Libplanet.Tests.Consensus
{
public class ProposalClaimTest
{
[Fact]
public void InvalidSignature()
{
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));

ProposalClaimMetadata metadata = new ProposalClaimMetadata(
1,
0,
hash,
DateTimeOffset.UtcNow,
new PrivateKey().PublicKey);

// Empty Signature
var emptySigBencodex = metadata.Encoded.Add(
ProposalClaim.SignatureKey,
Array.Empty<byte>());
Assert.Throws<ArgumentNullException>(() => new ProposalClaim(emptySigBencodex));

// Invalid Signature
var invSigBencodex = metadata.Encoded.Add(
ProposalClaim.SignatureKey,
new PrivateKey().Sign(TestUtils.GetRandomBytes(20)));
Assert.Throws<ArgumentException>(() => new ProposalClaim(invSigBencodex));
}

[Fact]
public void Sign()
{
var key = new PrivateKey();
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));

ProposalClaimMetadata metadata = new ProposalClaimMetadata(
1,
0,
hash,
DateTimeOffset.UtcNow,
key.PublicKey);
ProposalClaim claim = metadata.Sign(key);

TestUtils.AssertBytesEqual(claim.Signature, key.Sign(metadata.ByteArray));
Assert.True(key.PublicKey.Verify(metadata.ByteArray, claim.Signature));
}
}
}
2 changes: 0 additions & 2 deletions Libplanet.Tests/Consensus/VoteSetBitsMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ namespace Libplanet.Tests.Consensus
{
public class VoteSetBitsMetadataTest
{
private static Bencodex.Codec _codec = new Bencodex.Codec();

[Fact]
public void Bencoded()
{
Expand Down
58 changes: 58 additions & 0 deletions Libplanet.Tests/Consensus/VoteSetBitsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using Libplanet.Blocks;
using Libplanet.Consensus;
using Libplanet.Crypto;
using Xunit;

namespace Libplanet.Tests.Consensus
{
public class VoteSetBitsTest
{
[Fact]
public void InvalidSignature()
{
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));

VoteSetBitsMetadata metadata = new VoteSetBitsMetadata(
1,
0,
hash,
DateTimeOffset.UtcNow,
new PrivateKey().PublicKey,
VoteFlag.PreVote,
new[] { true, true, false, false });

// Empty Signature
var emptySigBencodex = metadata.Encoded.Add(
VoteSetBits.SignatureKey,
Array.Empty<byte>());
Assert.Throws<ArgumentNullException>(() => new VoteSetBits(emptySigBencodex));

// Invalid Signature
var invSigBencodex = metadata.Encoded.Add(
VoteSetBits.SignatureKey,
new PrivateKey().Sign(TestUtils.GetRandomBytes(20)));
Assert.Throws<ArgumentException>(() => new VoteSetBits(invSigBencodex));
}

[Fact]
public void Sign()
{
var key = new PrivateKey();
var hash = new BlockHash(TestUtils.GetRandomBytes(BlockHash.Size));

VoteSetBitsMetadata metadata = new VoteSetBitsMetadata(
1,
0,
hash,
DateTimeOffset.UtcNow,
key.PublicKey,
VoteFlag.PreVote,
new[] { true, true, false, false });
VoteSetBits voteSetBits = metadata.Sign(key);

TestUtils.AssertBytesEqual(voteSetBits.Signature, key.Sign(metadata.ByteArray));
Assert.True(key.PublicKey.Verify(metadata.ByteArray, voteSetBits.Signature));
}
}
}

0 comments on commit ca3d540

Please sign in to comment.