Esempio n. 1
0
        public void GetCSharpClassesReferencingAGivenClassReturnsEmptyArrayForEmptyList()
        {
            var result = ApexTestFinder.GetApexClassesReferencingAGivenClass((MemberDeclarationSyntax[])null, "Something");

            Assert.NotNull(result);
            Assert.IsEmpty(result);

            result = ApexTestFinder.GetApexClassesReferencingAGivenClass(Array.Empty <MemberDeclarationSyntax>(), "Something");
            Assert.NotNull(result);
            Assert.IsEmpty(result);
        }
Esempio n. 2
0
        public void GetApexClassesReferencingAGivenClassReturnsEmptyArrayForEmptyList()
        {
            var result = ApexTestFinder.GetApexClassesReferencingAGivenClass((string[])null, "Something");

            Assert.NotNull(result);
            Assert.IsEmpty(result);

            result = ApexTestFinder.GetApexClassesReferencingAGivenClass(Array.Empty <string>(), "Something");
            Assert.NotNull(result);
            Assert.IsEmpty(result);
        }
Esempio n. 3
0
        public async Task Cause_Exists_To_Return_True()
        {
            const string script = "return redis.call('LOLWUT')";
            var          eval   = new EVAL(script, Array.Empty <Key>(), Array.Empty <BulkString>());
            await connection.ExecuteAsync(eval).ConfigureAwait(false);

            var scriptHash   = Sha1.Create(script);
            var scriptExists = new SCRIPT.EXISTS(scriptHash);
            var response     = await connection.ExecuteAsync(scriptExists).ConfigureAwait(false);

            response.Should().Equal((scriptHash, true));
        }
Esempio n. 4
0
        public void AddNoElements()
        {
            var key = fixture.NewKey();
            var sut = new SADD(key, Array.Empty <BulkString>());

            var exception = Assert.ThrowsAsync <VisitException>(
                () => fixture.ExecuteAsync(sut)
                );

            exception.Subject.Should().Be(
                new Error("ERR wrong number of arguments for 'sadd' command")
                );
        }
Esempio n. 5
0
        public void System_ExecutionEngine_GetScriptContainer()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot();

            using (var script = new ScriptBuilder())
            {
                script.EmitSysCall(ApplicationEngine.System_Runtime_GetScriptContainer);

                // Without tx

                var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsTrue(engine.ResultStack.Peek().IsNull);

                // With tx

                var tx = new Transaction()
                {
                    Script  = new byte[] { 0x01 },
                    Signers = new Signer[] { new Signer()
                                             {
                                                 Account = UInt160.Zero, Scopes = WitnessScope.FeeOnly
                                             } },
                    Attributes      = Array.Empty <TransactionAttribute>(),
                    NetworkFee      = 0x02,
                    SystemFee       = 0x03,
                    Nonce           = 0x04,
                    ValidUntilBlock = 0x05,
                    Version         = 0x06,
                    Witnesses       = new Witness[] { new Witness()
                                                      {
                                                          VerificationScript = new byte[] { 0x07 }
                                                      } },
                };

                engine = ApplicationEngine.Create(TriggerType.Application, tx, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);

                var array = engine.ResultStack.Pop <VM.Types.Array>();
                Assert.AreEqual(tx.Hash, new UInt256(array[0].GetSpan()));
            }
        }
Esempio n. 6
0
        public void System_ExecutionEngine_GetScriptContainer()
        {
            var snapshot = Blockchain.Singleton.GetSnapshot();

            using (var script = new ScriptBuilder())
            {
                script.EmitSysCall(ApplicationEngine.System_Runtime_GetScriptContainer);

                // Without tx

                var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsTrue(engine.ResultStack.Peek().IsNull);

                // With tx

                script.EmitSysCall(ApplicationEngine.System_Json_Serialize);

                var tx = new Transaction()
                {
                    Script          = new byte[] { 0x01 },
                    Attributes      = Array.Empty <TransactionAttribute>(),
                    NetworkFee      = 0x02,
                    SystemFee       = 0x03,
                    Nonce           = 0x04,
                    ValidUntilBlock = 0x05,
                    Version         = 0x06,
                    Witnesses       = new Witness[] { new Witness()
                                                      {
                                                          VerificationScript = new byte[] { 0x07 }
                                                      } },
                    Sender = UInt160.Parse("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
                };

                engine = new ApplicationEngine(TriggerType.Application, tx, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteString));
                Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
                                @"5b224435724a376f755c753030324256574845456c5c75303032426e74486b414a424f614c4a6737496776303356337a4953646d6750413d222c362c342c222f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f2f383d222c332c322c352c2241513d3d225d");
                Assert.AreEqual(0, engine.ResultStack.Count);
            }
        }
Esempio n. 7
0
        public async Task Run_Script_Server_Side()
        {
            var command = new EVAL(
                @"
for i,key in ipairs(KEYS) do
    redis.call('SET', key, 'Hello ' .. key)
end
",
                new Key[] { "keys:1", "keys:2" },
                Array.Empty <BulkString>()
                );
            await connection.ExecuteAsync(command).ConfigureAwait(false);

            var values = await connection.ExecuteAsync(
                new MGET("keys:1", "keys:2")
                ).ConfigureAwait(false);

            values.Should().Equal(
                "Hello keys:1",
                "Hello keys:2"
                );
        }
Esempio n. 8
0
        public void System_Blockchain_GetBlock()
        {
            var tx = new Transaction()
            {
                Script          = new byte[] { 0x01 },
                Attributes      = Array.Empty <TransactionAttribute>(),
                Signers         = Array.Empty <Signer>(),
                NetworkFee      = 0x02,
                SystemFee       = 0x03,
                Nonce           = 0x04,
                ValidUntilBlock = 0x05,
                Version         = 0x06,
                Witnesses       = new Witness[] { new Witness()
                                                  {
                                                      VerificationScript = new byte[] { 0x07 }
                                                  } },
            };

            var block = new Block()
            {
                Index     = 0,
                Timestamp = 2,
                Version   = 3,
                Witness   = new Witness()
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = new byte[0]
                },
                PrevHash      = UInt256.Zero,
                MerkleRoot    = UInt256.Zero,
                NextConsensus = UInt160.Zero,
                ConsensusData = new ConsensusData()
                {
                    Nonce = 1, PrimaryIndex = 1
                },
                Transactions = new Transaction[] { tx }
            };

            var snapshot = Blockchain.Singleton.GetSnapshot();

            using (var script = new ScriptBuilder())
            {
                script.EmitPush(block.Hash.ToArray());
                script.EmitSysCall(ApplicationEngine.System_Blockchain_GetBlock);

                // Without block

                var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsTrue(engine.ResultStack.Peek().IsNull);

                // Not traceable block

                var height = snapshot.BlockHashIndex.GetAndChange();
                height.Index = block.Index + Transaction.MaxValidUntilBlockIncrement;

                var blocks = snapshot.Blocks;
                var txs    = snapshot.Transactions;
                blocks.Add(block.Hash, block.Trim());
                txs.Add(tx.Hash, new TransactionState()
                {
                    Transaction = tx, BlockIndex = block.Index, VMState = VMState.HALT
                });

                engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsTrue(engine.ResultStack.Peek().IsNull);

                // With block

                height.Index = block.Index;

                engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);

                var array = engine.ResultStack.Pop <VM.Types.Array>();
                Assert.AreEqual(block.Hash, new UInt256(array[0].GetSpan()));

                // Clean

                blocks.Delete(block.Hash);
                txs.Delete(tx.Hash);
            }
        }
Esempio n. 9
0
        public void System_Blockchain_GetBlock()
        {
            var tx = new Transaction()
            {
                Script          = new byte[] { 0x01 },
                Attributes      = Array.Empty <TransactionAttribute>(),
                NetworkFee      = 0x02,
                SystemFee       = 0x03,
                Nonce           = 0x04,
                ValidUntilBlock = 0x05,
                Version         = 0x06,
                Witnesses       = new Witness[] { new Witness()
                                                  {
                                                      VerificationScript = new byte[] { 0x07 }
                                                  } },
                Sender = UInt160.Parse("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
            };

            var block = new Block()
            {
                Index     = 0,
                Timestamp = 2,
                Version   = 3,
                Witness   = new Witness()
                {
                    InvocationScript   = new byte[0],
                    VerificationScript = new byte[0]
                },
                PrevHash      = UInt256.Zero,
                MerkleRoot    = UInt256.Zero,
                NextConsensus = UInt160.Zero,
                ConsensusData = new ConsensusData()
                {
                    Nonce = 1, PrimaryIndex = 1
                },
                Transactions = new Transaction[] { tx }
            };

            var snapshot = Blockchain.Singleton.GetSnapshot();

            using (var script = new ScriptBuilder())
            {
                script.EmitPush(block.Hash.ToArray());
                script.EmitSysCall(ApplicationEngine.System_Blockchain_GetBlock);

                // Without block

                var engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsTrue(engine.ResultStack.Peek().IsNull);

                // Not traceable block

                var height = snapshot.BlockHashIndex.GetAndChange();
                height.Index = block.Index + Transaction.MaxValidUntilBlockIncrement;

                var blocks = snapshot.Blocks;
                var txs    = snapshot.Transactions;
                blocks.Add(block.Hash, block.Trim());
                txs.Add(tx.Hash, new TransactionState()
                {
                    Transaction = tx, BlockIndex = block.Index, VMState = VMState.HALT
                });

                engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsTrue(engine.ResultStack.Peek().IsNull);

                // With block

                height.Index = block.Index;

                script.EmitSysCall(ApplicationEngine.System_Json_Serialize);
                engine = new ApplicationEngine(TriggerType.Application, null, snapshot, 0, true);
                engine.LoadScript(script.ToArray());

                Assert.AreEqual(engine.Execute(), VMState.HALT);
                Assert.AreEqual(1, engine.ResultStack.Count);
                Assert.IsInstanceOfType(engine.ResultStack.Peek(), typeof(ByteString));
                Assert.AreEqual(engine.ResultStack.Pop().GetSpan().ToHexString(),
                                "5b2261564e62466b35384f51717547373870747154766561762f48677941566a72634e41434d4e59705c7530303242366f6f3d222c332c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c22414141414141414141414141414141414141414141414141414141414141414141414141414141414141413d222c322c302c224141414141414141414141414141414141414141414141414141413d222c315d");
                Assert.AreEqual(0, engine.ResultStack.Count);

                // Clean

                blocks.Delete(block.Hash);
                txs.Delete(tx.Hash);
            }
        }
Esempio n. 10
0
 public override BulkString[] Arguments(BulkStringFactory factory) => Array.Empty <BulkString>();
Esempio n. 11
0
        public void System_Blockchain_GetBlock()
        {
            var tx = new Transaction()
            {
                Script          = new byte[] { 0x01 },
                Attributes      = Array.Empty <TransactionAttribute>(),
                Signers         = Array.Empty <Signer>(),
                NetworkFee      = 0x02,
                SystemFee       = 0x03,
                Nonce           = 0x04,
                ValidUntilBlock = 0x05,
                Version         = 0x06,
                Witnesses       = new Witness[] { new Witness()
                                                  {
                                                      VerificationScript = new byte[] { 0x07 }
                                                  } },
            };

            var block = new TrimmedBlock()
            {
                Header = new Header
                {
                    Index     = 0,
                    Timestamp = 2,
                    Witness   = new Witness()
                    {
                        InvocationScript   = Array.Empty <byte>(),
                        VerificationScript = Array.Empty <byte>()
                    },
                    PrevHash      = UInt256.Zero,
                    MerkleRoot    = UInt256.Zero,
                    PrimaryIndex  = 1,
                    NextConsensus = UInt160.Zero,
                },
                Hashes = new[] { tx.Hash }
            };

            var snapshot = TestBlockchain.GetTestSnapshot();

            using ScriptBuilder script = new();
            script.EmitDynamicCall(NativeContract.Ledger.Hash, "getBlock", block.Hash.ToArray());

            // Without block

            var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheVauthSystem.Settings);

            engine.LoadScript(script.ToArray());

            Assert.AreEqual(engine.Execute(), VMState.HALT);
            Assert.AreEqual(1, engine.ResultStack.Count);
            Assert.IsTrue(engine.ResultStack.Peek().IsNull);

            // Not traceable block

            const byte Prefix_Transaction  = 11;
            const byte Prefix_CurrentBlock = 12;

            var height = snapshot[NativeContract.Ledger.CreateStorageKey(Prefix_CurrentBlock)].GetInteroperable <HashIndexState>();

            height.Index = block.Index + ProtocolSettings.Default.MaxTraceableBlocks;

            UT_SmartContractHelper.BlocksAdd(snapshot, block.Hash, block);
            snapshot.Add(NativeContract.Ledger.CreateStorageKey(Prefix_Transaction, tx.Hash), new StorageItem(new TransactionState
            {
                BlockIndex  = block.Index,
                Transaction = tx
            }));

            engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheVauthSystem.Settings);
            engine.LoadScript(script.ToArray());

            Assert.AreEqual(engine.Execute(), VMState.HALT);
            Assert.AreEqual(1, engine.ResultStack.Count);
            Assert.IsTrue(engine.ResultStack.Peek().IsNull);

            // With block

            height.Index = block.Index;

            engine = ApplicationEngine.Create(TriggerType.Application, null, snapshot, settings: TestBlockchain.TheVauthSystem.Settings);
            engine.LoadScript(script.ToArray());

            Assert.AreEqual(engine.Execute(), VMState.HALT);
            Assert.AreEqual(1, engine.ResultStack.Count);

            var array = engine.ResultStack.Pop <VM.Types.Array>();

            Assert.AreEqual(block.Hash, new UInt256(array[0].GetSpan()));
        }