forked from planetarium/libplanet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |