public void DeserializeString_DataIsString_ShouldDeserializeCorrectly(string expected, params byte[] data)
        {
            var reader = SerializationTesting.CreateReader(data);

            var result = Serializer.DeserializeString(ref reader);

            Assert.Equal(expected, result);
            Assert.Equal(0, reader.Remaining);
        }
        public void DeserializeTokenAmount_WithEnoughData_ShouldDeserializeCorrectly(long expected, params byte[] data)
        {
            var reader = SerializationTesting.CreateReader(data);

            var result = Serializer.DeserializeTokenAmount(ref reader);

            Assert.Equal(expected, result.Value);
            Assert.Equal(0, reader.Remaining);
        }
        public void DeserializePropertyId_WithNonZero_ShouldDeserializeCorrectly(long expected, params byte[] data)
        {
            var reader = SerializationTesting.CreateReader(data);

            var result = Serializer.DeserializePropertyId(ref reader);

            Assert.NotNull(result);
            Assert.Equal(expected, result !.Value);
            Assert.Equal(0, reader.Remaining);
        }
        public void DeserializePropertyId_WithZero_ShouldReturnNull()
        {
            var data   = new byte[] { 0x00, 0x00, 0x00, 0x00 };
            var reader = SerializationTesting.CreateReader(data);

            var result = Serializer.DeserializePropertyId(ref reader);

            Assert.Null(result);
            Assert.Equal(0, reader.Remaining);
        }
        public void DeserializePrivateTransactionStatus_WithValidData_ShouldDeserializeCorrectly(
            PrivateTransactionStatus expected,
            params byte[] data)
        {
            var reader = SerializationTesting.CreateReader(data);

            var result = Serializer.DeserializePrivateTransactionStatus(ref reader);

            Assert.Equal(expected, result);
            Assert.Equal(0, reader.Remaining);
        }
        public void DeserializePrivateTransactionStatus_WithInvalidData_ShouldThrow(params byte[] data)
        {
            var ex = Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(data);
                Serializer.DeserializePrivateTransactionStatus(ref reader);
            });

            Assert.Equal("Invalid private transaction status.", ex.Message);
        }
        public void DeserializeTokenType_WithValidData_ShouldDeserializeCorrectly(
            TokenType expected,
            params byte[] data)
        {
            var reader = SerializationTesting.CreateReader(data);

            var result = Serializer.DeserializeTokenType(ref reader);

            Assert.Equal(expected, result);
            Assert.Equal(0, reader.Remaining);
        }
        public void DeserializeTokenType_WithInvalidData_ShouldThrow(params byte[] data)
        {
            var ex = Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(data);
                Serializer.DeserializeTokenType(ref reader);
            });

            Assert.Equal("Invalid token type.", ex.Message);
        }
        public void DeserializeString_DataIsNotString_ShouldThrow(params byte[] data)
        {
            var ex = Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(data);
                Serializer.DeserializeString(ref reader);
            });

            Assert.Equal("Invalid string.", ex.Message);
        }
        public void Deserialize_NotEnoughData_ShouldThrow(int length)
        {
            var data = new byte[length];

            Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(data);
                this.subject.Deserialize(null, null, ref reader);
            });
        }
        public void DeserializePropertyId_NotEnoughData_ShouldThrow(int size)
        {
            var data = new byte[size];

            var ex = Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(data);
                Serializer.DeserializePropertyId(ref reader);
            });

            Assert.Equal("Incomplete data.", ex.Message);
        }
        public void Deserialize_WithUpsupportedTransaction_ShouldThrow()
        {
            // Arrange.
            var writer = SerializationTesting.CreateWriter(2, 0);

            // Act.
            var ex = Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(writer);
                this.subject.Deserialize(null, null, ref reader);
            });

            // Assert.
            Assert.Equal("Unknow transaction.", ex.Message);
        }
        public void Deserialize_WithSupportedTransaction_ShouldInvokePayloadSerializer(int id, params byte[] payload)
        {
            // Arrange.
            var tx = new FakeTransaction(
                TestAddress.Regtest1,
                TestAddress.Regtest2,
                id,
                Elysium.Transaction.MaxVersion);
            var writer = SerializationTesting.CreateWriter(tx.Id, tx.Version);

            var(selected, other) = tx.Id switch
            {
                0 => (this.payload1, this.payload2),
                1 => (this.payload2, this.payload1),
                _ => throw new NotImplementedException()
            };

            writer.Write(payload);

            selected.StubbedDeserialize
            .Setup(f => f(tx.Sender, tx.Receiver, payload, tx.Version))
            .Returns(tx);

            // Act.
            var reader = SerializationTesting.CreateReader(writer);
            var result = this.subject.Deserialize(tx.Sender, tx.Receiver, ref reader);

            // Assert.
            Assert.Same(tx, result);
            Assert.Equal(0, reader.Remaining);

            selected.StubbedDeserialize.Verify(
                f => f(tx.Sender, tx.Receiver, payload, tx.Version),
                Times.Once());

            other.StubbedDeserialize.Verify(
                f => f(It.IsAny <BitcoinAddress?>(), It.IsAny <BitcoinAddress?>(), It.IsAny <byte[]>(), It.IsAny <int>()),
                Times.Never());
        }
        public void Deserialize_WithUnsupportedVersion_ShouldThrow()
        {
            // Arrange.
            var writer = SerializationTesting.CreateWriter(0, 1);

            this.payload1.StubbedDeserialize
            .Setup(f => f(It.IsAny <BitcoinAddress?>(), It.IsAny <BitcoinAddress?>(), It.IsAny <byte[]>(), 1))
            .Throws(new ArgumentOutOfRangeException("version"));

            // Act.
            var ex = Assert.Throws <TransactionSerializationException>(
                () =>
            {
                var reader = SerializationTesting.CreateReader(writer);
                this.subject.Deserialize(null, null, ref reader);
            });

            // Assert.
            Assert.Equal("Unknow version.", ex.Message);

            this.payload1.StubbedDeserialize.Verify(
                f => f(null, null, new byte[0], 1),
                Times.Once());
        }