1
using System;
2
using System.Collections.Immutable;
3
using System.Linq;
4
using System.Security.Cryptography;
5
using Bencodex.Types;
6
using Libplanet.Crypto;
7
using Libplanet.Net;
8
using Libplanet.Net.Messages;
9
using NetMQ;
10
using Xunit;
11

12
namespace Libplanet.Tests.Net.Messages
13
{
14
    public class BlockStatesTest
15
    {
16
        [Fact]
17
        public void Null()
18 1
        {
19 1
            HashDigest<SHA256> blockHash = new Random().NextHashDigest<SHA256>();
20 1
            var blockStates = new BlockStates(blockHash, null);
21 1
            var signer = new PrivateKey();
22 1
            NetMQMessage msg = blockStates.ToNetMQMessage(
23 1
                    signer,
24 1
                    new Peer(signer.PublicKey, default),
25 1
                    new AppProtocolVersion(
26 1
                        1,
27 1
                        new Bencodex.Types.Integer(0),
28 1
                        ImmutableArray<byte>.Empty,
29 1
                        default(Address)));
30 1
            Assert.Equal(Message.CommonFrames + 2, msg.FrameCount);
31 1
            TestUtils.AssertBytesEqual(
32 1
                blockHash,
33 1
                new HashDigest<SHA256>(msg[Message.CommonFrames].Buffer));
34 1
            Assert.Equal(-1, msg[Message.CommonFrames + 1].ConvertToInt32());
35

36 1
            var parsed = new BlockStates(msg.Skip(Message.CommonFrames).ToArray());
37 1
            TestUtils.AssertBytesEqual(blockHash, parsed.BlockHash);
38 1
            Assert.Null(parsed.States);
39 1
        }
40

41
        [Fact]
42
        public void NonEmpty()
43 1
        {
44 1
            HashDigest<SHA256> blockHash = new Random().NextHashDigest<SHA256>();
45
            // Note that here Unicode strings are used on purpose:
46 1
            IImmutableDictionary<string, IValue> states = ImmutableDictionary<string, IValue>.Empty
47 1
                .Add("foo甲", null)
48 1
                .Add("bar乙", default(Null))
49 1
                .Add("baz丙", new Text("a value 값"));
50 1
            var blockStates = new BlockStates(blockHash, states);
51 1
            var signer = new PrivateKey();
52 1
            NetMQMessage msg = blockStates.ToNetMQMessage(
53 1
                signer,
54 1
                new Peer(signer.PublicKey, default),
55 1
                new AppProtocolVersion(
56 1
                    1,
57 1
                    new Bencodex.Types.Integer(0),
58 1
                    ImmutableArray<byte>.Empty,
59 1
                    default(Address)));
60 1
            Assert.Equal(Message.CommonFrames + 2 + 3 * 3, msg.FrameCount);
61 1
            TestUtils.AssertBytesEqual(
62 1
                blockHash,
63 1
                new HashDigest<SHA256>(msg[Message.CommonFrames].Buffer));
64 1
            Assert.Equal(
65 1
                states.Count,
66 1
                msg[Message.CommonFrames + 1].ConvertToInt32());
67

68 1
            var parsed = new BlockStates(msg.Skip(Message.CommonFrames).ToArray());
69 1
            TestUtils.AssertBytesEqual(blockHash, parsed.BlockHash);
70 1
            Assert.Equal(states, parsed.States);
71 1
        }
72
    }
73
}

Read our documentation on viewing source code .

Loading