コード例 #1
0
        public void CreateContractAction_WhenPassedInvalidMessages_Throws()
        {
            var registry          = new InMemoryIntegration();
            var permissionManager = registry.AddressFactory.CreateAddress();
            var creatorAddress    = registry.CreateContract <CreatorContract>(new Dictionary <string, object>()
            {
                { "User", permissionManager.ToString() },
            });

            Assert.Throws <NoPermissionException>(() => registry.SendMessage(permissionManager, creatorAddress, CreateContractAction.Type, new Dictionary <string, object>()
            {
                {
                    CreateContractAction.Messages,
                    new List <object>()
                    {
                        new Dictionary <string, object>()
                        {
                            { CreateContractAction.MessageType, "NotARealActionType" },
                            { CreateContractAction.MessagePayload, new Dictionary <string, object>()
                              {
                              } },
                        },
                    }
                },
            }));
        }
コード例 #2
0
        public void SendMessage_WhenCaughtByContract_ThrowsInvalidOperationException()
        {
            var targetNumber = 48;

            var     registry = new InMemoryIntegration();
            Address address  = registry.AddressFactory.CreateAddress();
            Address buggedContractAddress = registry.CreateContract <CatchingContract>();
            Address targetContractAddress = registry.CreateContract <FavoriteNumberContract>(new Dictionary <string, object>()
            {
                { "User", buggedContractAddress.ToString() },
            });
            var actionType     = "SetFavoriteNumber";
            var invalidPayload = new Dictionary <string, object>()
            {
                { "Number", FavoriteNumberContract.InvalidNumber }
            };
            var validPayload = new Dictionary <string, object>()
            {
                { "Number", targetNumber }
            };

            Assert.Throws <InvalidOperationException>(() => registry.SendMessage(address, new Address[] { buggedContractAddress, targetContractAddress }, actionType, invalidPayload));

            Assert.NotEqual(registry.GetContract <FavoriteNumberContract>(targetContractAddress).Number, FavoriteNumberContract.InvalidNumber);

            registry.SendMessage(address, new Address[] { buggedContractAddress, targetContractAddress }, actionType, validPayload);

            Assert.Equal(registry.GetContract <FavoriteNumberContract>(targetContractAddress).Number, targetNumber);
        }
コード例 #3
0
        public void CreateContractAction_WhenPassedMessages_SendsMessages()
        {
            var registry          = new InMemoryIntegration();
            var targetNumber      = 367;
            var permissionManager = registry.AddressFactory.CreateAddress();
            var creatorAddress    = registry.CreateContract <CreatorContract>(new Dictionary <string, object>()
            {
                { "User", permissionManager.ToString() },
            });

            registry.SendMessage(permissionManager, creatorAddress, CreateContractAction.Type, new Dictionary <string, object>()
            {
                {
                    CreateContractAction.Messages,
                    new List <object>()
                    {
                        new Dictionary <string, object>()
                        {
                            { CreateContractAction.MessageType, SetFavoriteNumberAction.Type },
                            { CreateContractAction.MessagePayload, new Dictionary <string, object>()
                              {
                                  { SetFavoriteNumberAction.Number, targetNumber }
                              } },
                        },
                    }
                },
            });

            var lastAddress = ((CreatorContract)registry.GetContract(creatorAddress)).LastCreatedAddress;

            Assert.NotNull(lastAddress);
            Assert.NotNull(registry.GetContract(lastAddress));
            Assert.Equal(targetNumber, ((FavoriteNumberContract)registry.GetContract(lastAddress)).Number);
        }
コード例 #4
0
        public void CreateContract_WhenPassedNewContract_AddsItCorrectly()
        {
            var registry = new InMemoryIntegration();

            Address contractAddress = registry.CreateContract <FavoriteNumberContract>();

            Assert.NotNull(registry.GetContract(contractAddress));
            Assert.Equal(typeof(FavoriteNumberContract), registry.GetContract(contractAddress).GetType());
        }
コード例 #5
0
        public void SendMessage_WhenPassedNull_ThrowsArgumentNullException()
        {
            var     registry   = new InMemoryIntegration();
            Address address    = registry.AddressFactory.CreateAddress();
            var     actionType = "NotARealActionType";
            var     payload    = new Dictionary <string, object>();

            Assert.Throws <ArgumentNullException>(() => registry.SendMessage(address, (Address[])null, actionType, payload));
            Assert.Throws <ArgumentNullException>(() => registry.SendMessage(address, address, null, payload));
            Assert.Throws <ArgumentNullException>(() => registry.SendMessage(address, address, actionType, null));
        }
コード例 #6
0
        public void Kit_WhenInstantiatedTwice_Throws()
        {
            var registry = new InMemoryIntegration();

            Address address = registry.CreateContract <FavoriteNumberKitContract>(new Dictionary <string, object>()
            {
                { "User", null },
                { "FavoriteContractsCount", 2 },
            });

            registry.SendMessage(address, address, InstantiateKitAction.Type, new Dictionary <string, object>());
            Assert.Throws <InvalidOperationException>(() => registry.SendMessage(address, address, InstantiateKitAction.Type, new Dictionary <string, object>()));
        }
コード例 #7
0
        public void Kit_WhenInstantiated_CreatesContracts()
        {
            var favoriteContractsCount = 2;

            var registry = new InMemoryIntegration();

            Address address = registry.CreateContract <FavoriteNumberKitContract>(new Dictionary <string, object>()
            {
                { "User", null },
                { "FavoriteContractsCount", favoriteContractsCount },
            });

            registry.SendMessage(address, address, InstantiateKitAction.Type, new Dictionary <string, object>());

            Assert.Equal(favoriteContractsCount + 1, (int)((HashedAddressFactory)registry.AddressFactory.Raw).AddressNonce - 1);
        }
コード例 #8
0
        public void CreateContractAction_WhenPassedFavoriteNumberContract_CreatesContract()
        {
            var registry          = new InMemoryIntegration();
            var permissionManager = registry.AddressFactory.CreateAddress();
            var creatorAddress    = registry.CreateContract <CreatorContract>(new Dictionary <string, object>()
            {
                { "User", permissionManager.ToString() },
            });

            registry.SendMessage(permissionManager, creatorAddress, CreateContractAction.Type, new Dictionary <string, object>()
            {
            });

            var lastAddress = ((CreatorContract)registry.GetContract(creatorAddress)).LastCreatedAddress;

            Assert.NotNull(lastAddress);
            Assert.NotNull(registry.GetContract(lastAddress));
        }
コード例 #9
0
        public void SendMessage_WhenPassedValidAction_SendsActionToCorrectContract()
        {
            var     registry        = new InMemoryIntegration();
            Address senderAddress   = registry.AddressFactory.CreateAddress();
            Address contractAddress = registry.CreateContract <FavoriteNumberContract>(new Dictionary <string, object>()
            {
                { "User", senderAddress.ToString() },
            });

            registry.SendMessage(senderAddress, contractAddress, SetFavoriteNumberAction.Type, new Dictionary <string, object>
            {
                { SetFavoriteNumberAction.Number, 0 },
            });

            var contract = registry.GetContract(contractAddress) as FavoriteNumberContract;

            Assert.Equal(contract.LastOrigin, senderAddress);
            Assert.Equal(contract.LastSender, senderAddress);
        }
コード例 #10
0
        public void CreateContractAction_WhenConfigured_AllowsForwarding()
        {
            var registry          = new InMemoryIntegration();
            var permissionManager = new Address(new byte[] { 1 });
            var creatorAddress    = registry.CreateContract <CreatorContract>(new Dictionary <string, object>()
            {
                { "Admin", permissionManager.ToString() },
                { "User", permissionManager.ToString() },
            });

            registry.SendMessage(permissionManager, creatorAddress, AddPermissionAction.Type, new Dictionary <string, object>()
            {
                { AddPermissionAction.PermissionType, SetFavoriteNumberAction.Type },
                { AddPermissionAction.PermissionSender, permissionManager?.ToString() },
                { AddPermissionAction.PermissionTarget, null },
            });

            registry.SendMessage(permissionManager, creatorAddress, CreateContractAction.Type, new Dictionary <string, object>()
            {
            });

            var newAddress = ((CreatorContract)registry.GetContract(creatorAddress)).LastCreatedAddress;

            // First, prove that we cannot directly interact with the contract
            Assert.Throws <NoPermissionException>(() =>
            {
                registry.SendMessage(permissionManager, newAddress, SetFavoriteNumberAction.Type, new Dictionary <string, object>()
                {
                    { SetFavoriteNumberAction.Number, 45 },
                });
            });

            registry.SendMessage(permissionManager, new Address[] { creatorAddress, newAddress }, SetFavoriteNumberAction.Type, new Dictionary <string, object>()
            {
                { SetFavoriteNumberAction.Number, 42 },
            });

            Assert.Equal(42, ((FavoriteNumberContract)registry.GetContract(newAddress)).Number);
        }
コード例 #11
0
 public ProxyAddressFactory(InMemoryIntegration facade)
 {
     this.Facade = facade;
 }
コード例 #12
0
 public PermittedContractTests()
 {
     this.registry = new InMemoryIntegration();
 }
コード例 #13
0
        public void CreateContract_WhenPassedNonContractType_ThrowsArgumentException()
        {
            var registry = new InMemoryIntegration();

            Assert.Throws <ArgumentOutOfRangeException>(() => registry.CreateContract <Message>());
        }