public void TestCanCall() { var temp = ContractManifest.CreateDefault(UInt160.Zero); temp.SafeMethods = WildCardContainer <string> .Create(new string[] { "AAA" }); Assert.AreEqual(true, temp.CanCall(ContractManifest.CreateDefault(UInt160.Zero), "AAA")); }
public void TestGetItem() { string[] s = new string[] { "hello", "world" }; WildCardContainer <string> container = WildCardContainer <string> .Create(s); container[0].Should().Be("hello"); container[1].Should().Be("world"); }
public void TestClone() { var expected = ContractManifest.CreateDefault(UInt160.Zero); expected.SafeMethods = WildCardContainer <string> .Create(new string[] { "AAA" }); var actual = expected.Clone(); Assert.AreEqual(actual.ToString(), expected.ToString()); }
public void TestGetCount() { string[] s = new string[] { "hello", "world" }; WildCardContainer <string> container = WildCardContainer <string> .Create(s); container.Count.Should().Be(2); s = null; container = WildCardContainer <string> .Create(s); container.Count.Should().Be(0); }
public void ParseFromJson_Trust() { var json = @"{""groups"":[],""features"":{""storage"":false,""payable"":false},""abi"":{""hash"":""0x0000000000000000000000000000000000000000"",""entryPoint"":{""name"":""Main"",""parameters"":[{""name"":""operation"",""type"":""String""},{""name"":""args"",""type"":""Array""}],""returnType"":""Any""},""methods"":[],""events"":[]},""permissions"":[{""contract"":""*"",""methods"":""*""}],""trusts"":[""0x0000000000000000000000000000000000000001""],""safeMethods"":[]}"; var manifest = ContractManifest.Parse(json); Assert.AreEqual(manifest.ToString(), json); var check = ContractManifest.CreateDefault(UInt160.Zero); check.Trusts = WildCardContainer <UInt160> .Create(UInt160.Parse("0x0000000000000000000000000000000000000001")); Assert.AreEqual(manifest.ToString(), check.ToString()); }
public void TestIEnumerableGetEnumerator() { string[] s = new string[] { "hello", "world" }; WildCardContainer <string> container = WildCardContainer <string> .Create(s); IEnumerable enumerable = container; var enumerator = enumerable.GetEnumerator(); foreach (string _ in s) { enumerator.MoveNext(); enumerator.Current.Should().Be(_); } }
public void TestDeserializeAndSerialize() { MemoryStream stream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(stream); BinaryReader reader = new BinaryReader(stream); var expected = ContractManifest.CreateDefault(UInt160.Zero); expected.SafeMethods = WildCardContainer <string> .Create(new string[] { "AAA" }); expected.Serialize(writer); stream.Seek(0, SeekOrigin.Begin); var actual = ContractManifest.CreateDefault(UInt160.Zero); actual.Deserialize(reader); Assert.AreEqual(expected.SafeMethods.ToString(), actual.SafeMethods.ToString()); Assert.AreEqual(expected.SafeMethods.Count, 1); }
protected NativeContract() { this.ServiceHash = ServiceName.ToInteropMethodHash(); using (ScriptBuilder sb = new ScriptBuilder()) { sb.EmitSysCall(ServiceHash); this.Script = sb.ToArray(); } this.Hash = Script.ToScriptHash(); this.Manifest = ContractManifest.CreateDefault(this.Hash); List <ContractMethodDescriptor> descriptors = new List <ContractMethodDescriptor>(); List <string> safeMethods = new List <string>(); foreach (MethodInfo method in GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { ContractMethodAttribute attribute = method.GetCustomAttribute <ContractMethodAttribute>(); if (attribute is null) { continue; } string name = attribute.Name ?? (method.Name.ToLower()[0] + method.Name.Substring(1)); descriptors.Add(new ContractMethodDescriptor { Name = name, ReturnType = attribute.ReturnType, Parameters = attribute.ParameterTypes.Zip(attribute.ParameterNames, (t, n) => new ContractParameterDefinition { Type = t, Name = n }).ToArray() }); if (attribute.SafeMethod) { safeMethods.Add(name); } methods.Add(name, new ContractMethodMetadata { Delegate = (Func <ApplicationEngine, VMArray, StackItem>)method.CreateDelegate(typeof(Func <ApplicationEngine, VMArray, StackItem>), this), Price = attribute.Price, AllowedTriggers = attribute.AllowedTriggers }); } this.Manifest.Abi.Methods = descriptors.ToArray(); this.Manifest.SafeMethods = WildCardContainer <string> .Create(safeMethods.ToArray()); contracts.Add(this); }
public void TestGetEnumerator() { string[] s = null; IReadOnlyList <string> rs = (IReadOnlyList <string>) new string[0]; WildCardContainer <string> container = WildCardContainer <string> .Create(s); IEnumerator <string> enumerator = container.GetEnumerator(); enumerator.Should().Be(rs.GetEnumerator()); s = new string[] { "hello", "world" }; container = WildCardContainer <string> .Create(s); enumerator = container.GetEnumerator(); foreach (string _ in s) { enumerator.MoveNext(); enumerator.Current.Should().Be(_); } }
public void TestContract_Call() { var mockSnapshot = new Mock <Snapshot>(); var state = TestUtils.GetContract(); state.Manifest.Features = ContractFeatures.HasStorage; byte[] method = Encoding.UTF8.GetBytes("method"); byte[] args = new byte[0]; mockSnapshot.SetupGet(p => p.Contracts).Returns(new TestDataCache <UInt160, ContractState>(state.ScriptHash, state)); var engine = new ApplicationEngine(TriggerType.Application, null, mockSnapshot.Object, 0); engine.LoadScript(new byte[] { 0x01 }); engine.CurrentContext.EvaluationStack.Push(args); engine.CurrentContext.EvaluationStack.Push(method); engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray()); InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString().Should().Be(method.ToHexString()); engine.CurrentContext.EvaluationStack.Pop().GetSpan().ToHexString().Should().Be(args.ToHexString()); state.Manifest.Permissions[0].Methods = WildCardContainer <string> .Create("a"); engine.CurrentContext.EvaluationStack.Push(args); engine.CurrentContext.EvaluationStack.Push(method); engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray()); InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeFalse(); state.Manifest.Permissions[0].Methods = WildCardContainer <string> .CreateWildcard(); engine.CurrentContext.EvaluationStack.Push(args); engine.CurrentContext.EvaluationStack.Push(method); engine.CurrentContext.EvaluationStack.Push(state.ScriptHash.ToArray()); InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeTrue(); engine.CurrentContext.EvaluationStack.Push(args); engine.CurrentContext.EvaluationStack.Push(method); engine.CurrentContext.EvaluationStack.Push(UInt160.Zero.ToArray()); InteropService.Invoke(engine, InteropService.System_Contract_Call).Should().BeFalse(); }