Exemple #1
0
        public void MethodCallReplacer_MathOperator_Test()
        {
            var module = GetContractModule(typeof(TestContract));
            {
                var res = ContainsMathOpCode(
                    GetModule(typeof(TestContract)).Types.Single(t => t.FullName == typeof(TestContract).FullName)
                    .Methods
                    .First(m => m.Name == nameof(TestContract.TestStateType)), OpCodes.Add);
                res.ShouldBeTrue();
            }

            var replacer = new MethodCallReplacer();

            replacer.Patch(module);

            {
                var res = ContainsMathOpCode(
                    module.Types.Single(t => t.FullName == typeof(TestContract).FullName)
                    .Methods
                    .First(m => m.Name == nameof(TestContract.TestStateType)), OpCodes.Add);
                res.ShouldBeFalse();
            }

            {
                var res = ContainsMathOpCode(
                    module.Types.Single(t => t.FullName == typeof(TestContract).FullName)
                    .Methods
                    .First(m => m.Name == nameof(TestContract.TestStateType)), OpCodes.Add_Ovf);
                res.ShouldBeTrue();
            }
        }
        private static byte[] InjectCallReplacerCode(byte[] code)
        {
            var asm     = AssemblyDefinition.ReadAssembly(new MemoryStream(code));
            var patcher = new MethodCallReplacer();

            patcher.Patch(asm.MainModule);

            var newCode = new MemoryStream();

            asm.Write(newCode);
            return(newCode.ToArray());
        }
Exemple #3
0
        public void MethodCallReplacer_StringConcat_Test()
        {
            var module = GetContractModule(typeof(TestContract));
            {
                var m   = module.Types.Single(t => t.Name == nameof(TestContractReflection)).GetStaticConstructor();
                var res = ContainsMethodCall(m,
                                             GetModule(typeof(AElfString)).Types.Single(t => t.FullName == typeof(AElfString).FullName).Methods
                                             .First(m => m.Name == nameof(string.Concat)));
                res.ShouldBeFalse();
            }

            var replacer = new MethodCallReplacer();

            replacer.Patch(module);

            {
                var m   = module.Types.Single(t => t.Name == nameof(TestContractReflection)).GetStaticConstructor();
                var res = ContainsMethodCall(m,
                                             GetModule(typeof(AElfString)).Types.Single(t => t.FullName == typeof(AElfString).FullName).Methods
                                             .First(m => m.Name == nameof(string.Concat)));
                res.ShouldBeTrue();
            }
        }