public void Test_CreateCallDestroy()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));
            var nef      = new NefFile()
            {
                Script = script.finalNEFScript, Compiler = "unit-test", Version = "1.0"
            };
            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.Script);

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                  // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                  // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                  // Hash
            Assert.AreEqual(script.finalNEFScript, itemArray[3]);           // Script
            Assert.AreEqual(manifest.ToString(), itemArray[4].GetString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
        public void Test_CreateCallDestroy()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));

            // Check first

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());

            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Create

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(script.finalNEF, itemArray[0]);                 // Script
            Assert.AreEqual(manifest.ToString(), itemArray[1].GetString()); // Manifest
            Assert.AreEqual(false, itemArray[2]);                           // HasStorage
            Assert.AreEqual(false, itemArray[3]);                           // Payable

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
Example #3
0
        public void Test_CreateCallDestroy()
        {
            // Create

            var script   = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest = ContractManifest.FromJson(JObject.Parse(script.finalManifest));

            // Check first

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());

            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Create

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("create", script.finalNEF, manifest.ToJson().ToString());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsTrue(item.Type == VM.Types.StackItemType.InteropInterface);
            var ledger = (item as InteropInterface).GetInterface <Ledger.ContractState>();

            Assert.AreEqual(manifest.Hash, ledger.ScriptHash);

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray(), "oldContract", new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetBigInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy");
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", manifest.Hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }
        public void Init()
        {
            var hash = UInt160.Parse("0102030405060708090A0102030405060708090A");

            _engine = new TestEngine();
            _engine.Snapshot.Contracts.Add(hash, new Ledger.ContractState()
            {
                Script   = _engine.Build("./TestClasses/Contract1.cs").finalNEF,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)),
            });

            //will ContractCall 0102030405060708090A0102030405060708090A
            _engine.AddEntryScript("./TestClasses/Contract_ContractCall.cs");
        }
        public void Init()
        {
            var hash = UInt160.Parse("0102030405060708090A0102030405060708090A");

            _engine = new TestEngine(snapshot: new TestDataCache());
            _engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = hash,
                Nef      = _engine.Build("./TestClasses/Contract1.cs").nefFile,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract1.cs").finalManifest)),
            });

            // will ContractCall 0102030405060708090A0102030405060708090A
            _engine.AddEntryScript("./TestClasses/Contract_ContractCall.cs");
        }
        public void Test_OptimizerNopEntryPoint()
        {
            var testengine = new TestEngine();
            var build      = testengine.Build("./TestClasses/Contract_OptimizationTest.cs", false, true);

            Assert.AreEqual((build.finalABI["methods"] as JArray)[0]["name"].AsString(), "verify");
            Assert.AreEqual((build.finalABI["methods"] as JArray)[0]["offset"].AsString(), "0");
        }
        public void Test_Appcall()
        {
            var hash       = UInt160.Parse("0102030405060708090A0102030405060708090A");
            var testengine = new TestEngine();

            testengine.Snapshot.Contracts.Add(hash, new Ledger.ContractState()
            {
                //Manifest = new SmartContract.Manifest.ContractManifest(),
                Script   = testengine.Build("./TestClasses/Contract1.cs").finalNEF,
                Manifest = ContractManifest.FromJson(JObject.Parse(testengine.Build("./TestClasses/Contract1.cs").finalManifest)),
            });

            //will appcall 0102030405060708090A0102030405060708090A
            testengine.AddEntryScript("./TestClasses/Contract_appcall.cs");

            var       result     = testengine.GetMethod("testAppCall").Run().ConvertTo(StackItemType.ByteString);
            StackItem wantresult = new byte[] { 1, 2, 3, 4 };

            var bequal = wantresult.Equals(result);

            Assert.IsTrue(bequal);
        }
        public void Test_OnDeployment2()
        {
            var testengine = new TestEngine();
            var build      = testengine.Build("./TestClasses/Contract_OnDeployment2.cs", false, true);

            var methods = (build.finalABI["methods"] as JArray);

            Assert.AreEqual(1, methods.Count);
            Assert.AreEqual(methods[0]["name"].AsString(), "_deploy");
            Assert.AreEqual(methods[0]["offset"].AsString(), "0");
            Assert.AreEqual(methods[0]["returntype"].AsString(), "Void");

            var args = (methods[0]["parameters"] as JArray);

            Assert.AreEqual(1, args.Count);
            Assert.AreEqual(args[0]["name"].AsString(), "update");
            Assert.AreEqual(args[0]["type"].AsString(), "Boolean");
        }
Example #9
0
        public void Test_InvocationCounter()
        {
            // Build script

            _engine.Reset();

            var contract = _engine.EntryScriptHash;

            _engine.Snapshot.Contracts.Add(contract, new Ledger.ContractState()
            {
                Script   = _engine.InvocationStack.Peek().Script,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract_Runtime.cs").finalManifest)),
            });

            _engine.InvocationStack.Clear();

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                // First
                sb.EmitAppCall(contract, "getInvocationCounter");
                // Second
                sb.EmitAppCall(contract, "getInvocationCounter");

                _engine.LoadScript(sb.ToArray());
            }

            // Check

            Assert.AreEqual(VMState.HALT, _engine.Execute());
            Assert.AreEqual(2, _engine.ResultStack.Count);

            var item = _engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = _engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }
        public void Test_InvocationCounter()
        {
            // We need a new TestEngine because invocationCounter it's shared between them

            var contract = _engine.EntryScriptHash;
            var engine   = new TestEngine(TriggerType.Application, new DummyVerificable());

            engine.Snapshot.ContractAdd(new ContractState()
            {
                Hash     = contract,
                Script   = _engine.InvocationStack.Peek().Script,
                Manifest = ContractManifest.FromJson(JObject.Parse(_engine.Build("./TestClasses/Contract_Runtime.cs").finalManifest)),
            });

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                // First
                sb.EmitAppCall(contract, "getInvocationCounter");
                // Second
                sb.EmitAppCall(contract, "getInvocationCounter");

                engine.LoadScript(sb.ToArray());
            }

            // Check

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

            var item = engine.ResultStack.Pop();

            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x02, item.GetInteger());

            item = engine.ResultStack.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(0x01, item.GetInteger());
        }
Example #11
0
        public void Test_CreateCallDestroy()
        {
            // Create

            var entryScript = _engine.ScriptEntry;
            var script      = _engine.Build("./TestClasses/Contract_Create.cs");
            var manifest    = ContractManifest.FromJson(JObject.Parse(script.finalManifest));
            var nef         = new NefFile()
            {
                Script = script.finalNEFScript, Compiler = "unit-test-1.0", Tokens = System.Array.Empty <MethodToken>()
            };

            nef.CheckSum = NefFile.ComputeChecksum(nef);

            var hash = Helper.GetContractHash((_engine.ScriptContainer as Transaction).Sender, nef.CheckSum, manifest.Name);

            // Create

            _engine.Reset();
            var result = _engine.ExecuteTestCaseStandard("create", nef.ToArray(), manifest.ToJson().ToString());

            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            var item = result.Pop();

            Assert.IsInstanceOfType(item, typeof(Array));
            var itemArray = item as Array;

            Assert.AreEqual(1, itemArray[0].GetInteger());                                                                   // Id
            Assert.AreEqual(0, itemArray[1].GetInteger());                                                                   // UpdateCounter
            Assert.AreEqual(hash.ToArray(), itemArray[2]);                                                                   // Hash
            Assert.AreEqual(nef.ToJson().AsString(), itemArray[3].GetSpan().AsSerializable <NefFile>().ToJson().AsString()); // Nef
            var ritem = new ContractManifest();

            ((IInteroperable)ritem).FromStackItem(itemArray[4]);
            Assert.AreEqual(manifest.ToString(), ritem.ToString()); // Manifest

            // Call

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray(), "oldContract", (byte)CallFlags.All, new Array());
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(1, result.Count);

            item = result.Pop();
            Assert.IsInstanceOfType(item, typeof(Integer));
            Assert.AreEqual(123, item.GetInteger());

            // Destroy

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("destroy", entryScript.nefFile);
            Assert.AreEqual(VMState.HALT, _engine.State);
            Assert.AreEqual(0, result.Count);

            // Check again for failures

            _engine.Reset();
            result = _engine.ExecuteTestCaseStandard("call", hash.ToArray());
            Assert.AreEqual(VMState.FAULT, _engine.State);
            Assert.AreEqual(0, result.Count);
        }