public void Validate_Determinism_GenericClass() { string adjustedSource = @"using System; using Stratis.SmartContracts; public class Test<T> : SmartContract { public Test(ISmartContractState state) : base(state) {} public T TestMethod(int param) { return default(T); } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); Assert.Single(result.Errors); Assert.True(result.Errors.First() is GenericTypeValidator.GenericTypeValidationResult); }
public void SmartContract_ValidateFormat_NewObj_Fails() { var adjustedSource = @" using System; using Stratis.SmartContracts; public class Test : SmartContract { public Test(ISmartContractState state) : base(state) {} public void CreateNewObject() { var obj = new object(); } } "; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); var validator = new SmartContractFormatValidator(); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; var result = validator.Validate(decomp.ModuleDefinition); Assert.False(result.IsValid); Assert.Single(result.Errors); Assert.IsType <NewObjValidator.NewObjValidationResult>(result.Errors.First()); }
public void SmartContractValidator_ModuleReference_Tests() { var adjustedSource = @" using System; using Stratis.SmartContracts; [Deploy] public class Test : SmartContract { public Test(ISmartContractState state): base(state) { } } "; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decompilation = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; // Add a module reference decompilation.ModuleDefinition.ModuleReferences.Add(new ModuleReference("Test.dll")); var moduleDefinition = decompilation.ModuleDefinition; SmartContractValidationResult result = new SmartContractValidator().Validate(moduleDefinition); Assert.False(result.IsValid); Assert.NotEmpty(result.Errors); Assert.True(result.Errors.All(e => e is ModuleDefinitionValidationResult)); }
public void SmartContract_ValidateFormat_TwoTypes() { var adjustedSource = @" using System; using Stratis.SmartContracts; public class Test : SmartContract { public Test(ISmartContractState state) : base(state) {} } public class Test2 { } "; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); var validator = new SmartContractFormatValidator(); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; var result = validator.Validate(decomp.ModuleDefinition); Assert.False(result.IsValid); Assert.Equal(2, result.Errors.Count()); }
public void SmartContract_ValidateFormat_InitializedReadonlyField_Fails() { var adjustedSource = @" using System; using Stratis.SmartContracts; public class FieldTest : SmartContract { private readonly int field1 = 1234567; public FieldTest(ISmartContractState state) : base(state) { } } "; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); var validator = new FieldDefinitionValidator(); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; IEnumerable <ValidationResult> result = validator.Validate(decomp.ContractType); Assert.NotEmpty(result); }
public void SmartContract_ValidateFormat_AssemblyReferences() { var adjustedSource = @" using System; using System.Linq; using Stratis.SmartContracts; public class Test : SmartContract { public Test(ISmartContractState state) : base(state) { IQueryable q = null; } } "; ContractCompilationResult compilationResult = Compile(adjustedSource, new [] { typeof(IQueryable).Assembly.Location }); Assert.True(compilationResult.Success); var validator = new SmartContractFormatValidator(); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; var result = validator.Validate(decomp.ModuleDefinition); Assert.False(result.IsValid); Assert.Single(result.Errors); }
/// <summary> /// If the call is to a property, rewrites the method name to the getter method's name. /// </summary> private void RewritePropertyGetterName(LocalCallContractRequest request) { // Don't rewrite if there are params if (request.Parameters != null && request.Parameters.Any()) { return; } byte[] contractCode = this.stateRoot.GetCode(request.ContractAddress.ToUint160(this.network)); string contractType = this.stateRoot.GetContractType(request.ContractAddress.ToUint160(this.network)); Result <IContractModuleDefinition> readResult = ContractDecompiler.GetModuleDefinition(contractCode); if (readResult.IsSuccess) { IContractModuleDefinition contractModule = readResult.Value; string propertyGetterName = contractModule.GetPropertyGetterMethodName(contractType, request.MethodName); if (propertyGetterName != null) { request.MethodName = propertyGetterName; } } }
public void SmartContract_ValidateFormat_One_CustomStruct() { var adjustedSource = @" using System; using Stratis.SmartContracts; public class StructTest : SmartContract { public struct Item { public int Number; public string Name; } public StructTest(ISmartContractState state) : base(state) { } } "; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); var validator = new NestedTypesAreValueTypesValidator(); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; IEnumerable <ValidationResult> result = validator.Validate(decomp.ContractType); Assert.Empty(result); }
public void Validate_Determinism_DisallowedMethodParams() { string adjustedSource = @"using System; using Stratis.SmartContracts; public class Test : SmartContract { public Test(ISmartContractState state) : base(state) {} public void DateTime1(DateTime param) { } public void F(float param) { } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); Assert.Equal(2, result.Errors.Count()); Assert.True(result.Errors.All(e => e is MethodParamValidator.MethodParamValidationResult)); Assert.Contains(result.Errors, e => e.Message.Contains("System.DateTime")); Assert.Contains(result.Errors, e => e.Message.Contains("System.Single")); }
public void Validate_Determinism_Recursion() { string source = @" using Stratis.SmartContracts; public class RecursionTest : SmartContract { public RecursionTest(ISmartContractState smartContractState) : base(smartContractState) { } public void Bid() { Job(5); } public void Job(int index) { if (index > 0) Job(index - 1); } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(source); Assert.True(compilationResult.Success); IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.True(result.IsValid); }
public void Validate_Determinism_Async() { string adjustedSource = @"using System; using Stratis.SmartContracts; using System.Threading.Tasks; public class Test : SmartContract { public Test(ISmartContractState state) : base(state) {} public async void Bid() { await Task.Run(job); } public async Task job() { int w = 9; } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); }
public void SmartContract_ValidateFormat_NewArray_Succeeds() { var adjustedSource = @" using System; using Stratis.SmartContracts; public class Test : SmartContract { public Test(ISmartContractState state) : base(state) {} public void CreateNewStruct() { var item = new [] { 1, 2, 3, 4, 5 }; } } "; ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); var validator = new SmartContractFormatValidator(); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; var result = validator.Validate(decomp.ModuleDefinition); Assert.True(result.IsValid); }
public void Validate_Determinism_ErrorMessages_TwoMethods() { string source = @" using Stratis.SmartContracts; public class MessageTest : SmartContract { public MessageTest(ISmartContractState smartContractState) : base(smartContractState) { } public void MessageTestFloat1() { float test = (float) 3.5; } public void MessageTestFloat2() { float test = (float) 3.5; } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(source, OptimizationLevel.Debug); Assert.True(compilationResult.Success); IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); Assert.Equal(2, result.Errors.Count()); Assert.Equal(FloatValidator.FloatValidationType, result.Errors.First().ValidationType); Assert.Equal(FloatValidator.FloatValidationType, result.Errors.Skip(1).Take(1).First().ValidationType); }
public void Validate_Determinism_ErrorMessages_Simple_DateTime() { string source = @" using Stratis.SmartContracts; using System; public class MessageTest : SmartContract { public MessageTest(ISmartContractState smartContractState) : base(smartContractState) { } public void MessageTestDateTime() { var test = DateTime.Now; } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(source); Assert.True(compilationResult.Success); IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); Assert.True(result.Errors.All(e => e is WhitelistValidator.WhitelistValidationResult)); }
public void Validate_Determinism_ErrorMessages_Recursion_OneLevel() { string source = @" using Stratis.SmartContracts; public class MessageTest : SmartContract { public MessageTest(ISmartContractState smartContractState) : base(smartContractState) { } public void MessageTestValid() { MessageTestValid1(); } public void MessageTestValid1() { float test = (float)3.5; MessageTestValid(); } }"; ContractCompilationResult compilationResult = ContractCompiler.Compile(source, OptimizationLevel.Debug); Assert.True(compilationResult.Success); IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(compilationResult.Compilation).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); Assert.Single(result.Errors); Assert.Equal("Float usage", result.Errors.First().ValidationType); }
public void Resolve_Property_Name_Success() { var readResult = ContractDecompiler.GetModuleDefinition(this.contractCode); var contractModule = readResult.Value; var methodName = contractModule.GetPropertyGetterMethodName("ModuleDefinitionTest", "Owner"); Assert.Equal("get_Owner", methodName); }
public void Resolve_Property_Doesnt_Exist() { var readResult = ContractDecompiler.GetModuleDefinition(this.contractCode); var contractModule = readResult.Value; var methodName = contractModule.GetPropertyGetterMethodName("ModuleDefinitionTest", "DoesntExist"); Assert.Null(methodName); }
private static IContractModuleDefinition BuildModuleDefinition(IConsole console, byte[] compilation) { console.WriteLine("Building ModuleDefinition..."); IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(compilation, new DotNetCoreAssemblyResolver()).Value; console.WriteLine("ModuleDefinition built successfully."); console.WriteLine(); return(moduleDefinition); }
private static void BuildModuleDefinition(IConsole console, ValidationServiceResult validationServiceResult, out byte[] compilation, out IContractModuleDefinition moduleDefinition) { console.WriteLine("Building ModuleDefinition..."); compilation = validationServiceResult.CompilationResult.Compilation; moduleDefinition = ContractDecompiler.GetModuleDefinition(compilation, new DotNetCoreAssemblyResolver()).Value; console.WriteLine("ModuleDefinition built successfully."); console.WriteLine(); }
public void Validate_Determinism_Fails_MultiDimensional_Arrays() { string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = new int[50,50];").Replace(ReplaceReferencesString, ""); byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); }
public void Validate_Determinism_Passes_ArrayConstruction_LessThan2() { string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = new int[]{10167};").Replace(ReplaceReferencesString, ""); byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.True(result.IsValid); }
public void SmartContract_Compiler_CanCompileMultipleFiles() { ContractCompilationResult result = ContractCompiler.CompileDirectory("SmartContracts", "MultipleFiles"); Assert.True(result.Success); IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(result.Compilation).Value; Assert.Contains(decomp.ModuleDefinition.Types, x => x.Name == "MultipleFiles1"); Assert.Contains(decomp.ModuleDefinition.Types, x => x.Name == "MultipleFiles2"); }
public IContractModuleDefinition CompileFileToModuleDef(FileInfo file) { ContractCompilationResult compilationResult = ContractCompiler.CompileFile(file.FullName); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; return(decomp); }
public IContractModuleDefinition CompileToModuleDef(string source) { ContractCompilationResult compilationResult = ContractCompiler.Compile(source, OptimizationLevel.Debug); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition decomp = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; return(decomp); }
public void Validate_ByteArray_Conversion() { ContractCompilationResult compilationResult = ContractCompiler.CompileFile("SmartContracts/ByteArrayConversion.cs"); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.True(result.IsValid); }
public void Validate_Determinism_TryCatch() { ContractCompilationResult compilationResult = ContractCompiler.CompileFile("SmartContracts/TryCatch.cs"); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); }
public void Validate_Determinism_DynamicTypeAllowed() { string adjustedSource = TestString.Replace(ReplaceCodeString, "dynamic test = 56; test = \"aString\"; ").Replace(ReplaceReferencesString, ""); ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.True(result.IsValid); }
public void ValidateAnonymousClassesDisallowed() { string adjustedSource = TestString.Replace(ReplaceCodeString, @"var test = new { Test = ""Stratis"" };").Replace(ReplaceReferencesString, ""); byte[] assemblyBytes = ContractCompiler.Compile(adjustedSource).Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); }
public void Validate_Determinism_Nullable_Fails() { string adjustedSource = TestString.Replace(ReplaceCodeString, "int? test = null;").Replace(ReplaceReferencesString, ""); ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource, OptimizationLevel.Debug); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); }
public void Validate_Determinism_KnownBadMethodCall() { string adjustedSource = TestString.Replace(ReplaceCodeString, @"var floor = System.Math.Floor(12D);").Replace(ReplaceReferencesString, ""); ContractCompilationResult compilationResult = ContractCompiler.Compile(adjustedSource); Assert.True(compilationResult.Success); byte[] assemblyBytes = compilationResult.Compilation; IContractModuleDefinition moduleDefinition = ContractDecompiler.GetModuleDefinition(assemblyBytes).Value; SmartContractValidationResult result = this.validator.Validate(moduleDefinition.ModuleDefinition); Assert.False(result.IsValid); }