コード例 #1
0
        public void FunctionCallWithTooIncorrectElementTypeOnStackThrows(VmType type1, VmType type2, VmType type3)
        {
            var functionDefinition1 = new FunctionDefinition(
                new List <Instruction>()
            {
                BuildConstantToStackInstruction(type1),
                BuildConstantToStackInstruction(type2),
                BuildConstantToStackInstruction(type3),
                new Instruction(CallFunction, 1L)
            },
                new List <VmType>(),
                new List <VmType> {
                VmType.Integer, VmType.Integer, VmType.Boolean
            },
                0
                );

            var functionDefinition2 = new FunctionDefinition(
                new List <Instruction>(),
                new List <VmType> {
                VmType.Integer, VmType.Integer, VmType.Boolean
            },
                new List <VmType> {
                VmType.Integer
            },
                0
                );

            var vm = new VirtualMachine(functionDefinition1, functionDefinition2);

            var exception = Assert.Throws <InvalidSourceException>(() => vm.Execute());

            Assert.Equal(InvalidSourceDetail.IncorrectCallArgumentType, exception.DetailCode);
        }
コード例 #2
0
 public VmDefinition(StorageAccountDefinition saDefinition)
 {
     this.vmName       = TestSettings.Instance.VirtualMachineName;
     this.vmRg         = TestSettings.Instance.VirtualMachineResourceGroupName;
     this.vmType       = (VmType)Enum.Parse(typeof(VmType), TestSettings.Instance.VirtualMachineType);
     this.saDefinition = saDefinition;
 }
コード例 #3
0
        public void ConditionalBranchingWithIncorrectTypesOnStackThrows(VmType type, InstructionCode instructionCode)
        {
            var instructions = new List <Instruction>()
            {
                BuildConstantToStackInstruction(type),
                new Instruction(instructionCode, 0L)
            };
            var exception = Assert.Throws <InvalidSourceException>(() => ExecuteBooleanFunction(instructions));

            Assert.Equal(InvalidSourceDetail.IncorrectElementTypeOnStack, exception.DetailCode);
        }
コード例 #4
0
        protected Instruction BuildConstantToStackInstruction(VmType dataType)
        {
            switch (dataType)
            {
            case VmType.Boolean:
                return(new Instruction(BooleanConstantToStack, true));

            case VmType.Integer:
                return(new Instruction(IntegerConstantToStack, 0L));

            default:
                Assert.True(false);
                throw new System.Exception("Unrecognized Type");
            }
        }
コード例 #5
0
        public static InvokeCode makeInvokeCode(string funcname, JArray parameters, string hash, VmType vmtype = VmType.NativeVM)
        {
            var invokeCode = new InvokeCode();
            var vmCode     = new VmCode();
            var code       = buildSmartContractParam(funcname, parameters);

            //Console.WriteLine("code:" + code);
            code           += Crypto.NumberToHex(Crypto.HexToInteger(OPCODE.TAILCALL));
            code           += hash;
            vmCode.code     = code;
            vmCode.vmType   = vmtype;
            invokeCode.code = vmCode;

            return(invokeCode);
        }
コード例 #6
0
        public void FunctionCallWithIncorrectArgumentTypesOnExitThrows(int elementsOnStackPreCall, VmType type1, VmType type2, VmType type3)
        {
            var instructions1 = new List <Instruction>();

            for (int i = 0; i < elementsOnStackPreCall; i++)
            {
                instructions1.Add(new Instruction(BooleanConstantToStack, true));
            }
            instructions1.Add(new Instruction(CallFunction, 1L));
            instructions1.Add(new Instruction(StackToVariable, 0L));
            instructions1.Add(new Instruction(StackToVariable, 0L));
            for (int i = 0; i < elementsOnStackPreCall; i++)
            {
            }
            var functionDefinition1 = new FunctionDefinition(
                instructions1,
                new List <VmType>(),
                new List <VmType> {
                VmType.Boolean
            },
                1
                );

            var functionDefinition2 = new FunctionDefinition(
                new List <Instruction>
            {
                BuildConstantToStackInstruction(type1),
                BuildConstantToStackInstruction(type2),
                BuildConstantToStackInstruction(type3),
            },

                new List <VmType> {
            },
                new List <VmType> {
                VmType.Integer, VmType.Integer, VmType.Boolean
            },
                1
                );

            var vm = new VirtualMachine(functionDefinition1, functionDefinition2);

            var exception = Assert.Throws <InvalidSourceException>(() => vm.Execute());

            Assert.Equal(InvalidSourceDetail.IncorrectReturnArgumentType, exception.DetailCode);
        }
コード例 #7
0
        public static InvokeCode makeInvokeCode(string funcname, JArray parameters, string hash, VmType vmtype = VmType.NativeVM)
        {
            var invokeCode = new InvokeCode();
            var vmCode     = new VmCode();
            var args       = buildSmartContractParam(funcname, parameters);
            //Console.WriteLine("code:" + code);

            var contract = new Contract();

            contract.address = hash;
            contract.args    = args;
            contract.method  = "";

            var code = contract.serialize();

            code = Crypto.NumberToHex(Crypto.HexToInteger(OPCODE.APPCALL)) + code;

            vmCode.code     = code;
            vmCode.vmType   = vmtype;
            invokeCode.code = vmCode;

            return(invokeCode);
        }
コード例 #8
0
        public void InstructionRequiringDataWithInvalidTypeThrows(InstructionCode instructionCode, VmType vmType)
        {
            var instructions = new List <Instruction>();

            switch (vmType)
            {
            case VmType.Integer:
                instructions.Add(new Instruction(instructionCode, 0L));
                break;

            case VmType.Boolean:
                instructions.Add(new Instruction(instructionCode, true));
                break;

            default:
                Assert.True(false);     // error in test;
                break;
            }

            var functionDefinition = new FunctionDefinition(instructions, new List <VmType>(), new List <VmType>()
            {
                VmType.Boolean
            }, 0);
            var exception = Assert.Throws <InvalidSourceException>(() => new VirtualMachine(functionDefinition));

            Assert.Equal(InvalidSourceDetail.InvalidInstructionData, exception.DetailCode);
        }