Beispiel #1
0
        public void Encode_WithValidExodusTransaction_ShouldSuccess(long property, long amount)
        {
            using (var payloadStream = new MemoryStream())
                using (var stream = new MemoryStream())
                    using (var writer = new BinaryWriter(stream))
                    {
                        // Arrange.
                        RawTransaction.WritePropertyId(payloadStream, property);
                        RawTransaction.WritePropertyAmount(payloadStream, new PropertyAmount(amount));

                        var payload = payloadStream.ToArray();

                        var transaction = new SimpleSendV0(
                            TestAddress.Regtest1,
                            TestAddress.Regtest2,
                            new PropertyId(property),
                            new PropertyAmount(amount)
                            );

                        // Act.
                        this.subject.Encode(writer, transaction);

                        // Assert.
                        var result = stream.ToArray();
                        Assert.Equal(payload, result);
                    }
        }
        public void Decode_WithInvalidTransactionType_ShouldThrow()
        {
            byte[] data;

            using (var stream = RawTransaction.Create(2, 0))
            {
                data = stream.ToArray();
            }

            var ex = Assert.Throws <TransactionFieldException>(
                () => this.subject.Decode(TestAddress.Regtest1, TestAddress.Regtest2, data)
                );

            Assert.Equal(TransactionFieldException.TypeField, ex.Field);
        }
        public void Decode_V0WithInvalidAmount_ShouldThrow(long amount)
        {
            // Arrange.
            using (var stream = new MemoryStream())
                using (var reader = new BinaryReader(stream))
                {
                    RawTransaction.WritePropertyId(stream, PropertyId.MaxValue);
                    RawTransaction.WritePropertyAmount(stream, new PropertyAmount(amount));

                    stream.Seek(0, SeekOrigin.Begin);

                    // Act.
                    var ex = Assert.Throws <TransactionFieldException>(
                        () => this.subject.Decode(TestAddress.Regtest1, TestAddress.Regtest2, reader, 0)
                        );

                    // Assert.
                    Assert.Equal("amount", ex.Field);
                }
        }
        public void Decode_WithValidTransactionType_ShouldInvokePayloadDecode()
        {
            // Arrange.
            ExodusTransaction tx = new FakeExodusTransaction(null, null);

            byte[] data;

            using (var stream = RawTransaction.Create(1, ExodusTransaction.MaxVersion))
            {
                data = stream.ToArray();
            }

            this.encoder1.Decode(
                Arg.Any <BitcoinAddress>(),
                Arg.Any <BitcoinAddress>(),
                Arg.Any <BinaryReader>(),
                Arg.Any <int>()
                ).Returns(tx);

            // Act.
            var result = this.subject.Decode(TestAddress.Regtest1, TestAddress.Regtest2, data);

            // Assert.
            Assert.Same(tx, result);

            this.encoder0.Received(0).Decode(
                Arg.Any <BitcoinAddress>(),
                Arg.Any <BitcoinAddress>(),
                Arg.Any <BinaryReader>(),
                Arg.Any <int>()
                );

            this.encoder1.Received(1).Decode(
                TestAddress.Regtest1,
                TestAddress.Regtest2,
                Arg.Is <BinaryReader>(r => r != null),
                ExodusTransaction.MaxVersion
                );
        }
        public void Decode_V0WithTooShortData_ShouldThrow(int length)
        {
            // Arrange.
            var payload = new byte[length];

            using (var stream = new MemoryStream(payload))
                using (var reader = new BinaryReader(stream))
                {
                    if (payload.Length >= 4)
                    {
                        RawTransaction.WritePropertyId(stream, PropertyId.MinValue);
                    }

                    stream.Seek(0, SeekOrigin.Begin);

                    // Act.
                    var ex = Assert.ThrowsAny <TransactionTooShortException>(
                        () => this.subject.Decode(TestAddress.Regtest1, TestAddress.Regtest2, reader, 0)
                        );

                    // Assert.
                    Assert.Equal(16, ex.RequiredSize);
                }
        }
        public void Decode_V0WithValidData_ShouldSuccess(long property, long amount)
        {
            // Arrange.
            using (var stream = new MemoryStream())
                using (var reader = new BinaryReader(stream))
                {
                    RawTransaction.WritePropertyId(stream, property);
                    RawTransaction.WritePropertyAmount(stream, new PropertyAmount(amount));

                    stream.Seek(0, SeekOrigin.Begin);

                    // Act.
                    var tx = (SimpleSendV0)this.subject.Decode(TestAddress.Regtest1, TestAddress.Regtest2, reader, 0);

                    // Assert.
                    Assert.IsType <SimpleSendV0>(tx); // We want to be the exact V0.
                    Assert.Equal(this.subject.Type, tx.Id);
                    Assert.Equal(0, tx.Version);
                    Assert.Same(TestAddress.Regtest1, tx.Sender);
                    Assert.Same(TestAddress.Regtest2, tx.Receiver);
                    Assert.Equal(property, tx.Property.Value);
                    Assert.Equal(amount, tx.Amount.Indivisible);
                }
        }