Beispiel #1
0
    public IEnumerator CreateUserTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        ClientSocket.Instance.Reconnect();
        CommandData response = null;

        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });

        yield return(new UnityEngine.WaitForSeconds(1));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        Logger.Log(response.GetAs <RegisterUserResponse> ().id);

        // user was created in the last second
        Assert.IsTrue(response.GetAs <RegisterUserResponse> ().id.ResourceId > ThreadSaveIdGenerator.NextId - 10000000);

        ServerCore.Stop();
    }
Beispiel #2
0
        public override CommandData ExecuteWithReturn(CommandData data)
        {
            data.CoreInstance.EntityManager.ExecuteForReference(data.GetAs <CommandData>(), data.SenderId);

            var hash = data.CoreInstance.EntityManager.GetEntity <Entity>(data.GetAs <CommandData>().Recipient).GetHashCode();

            // success :)
            data.SerializeAndSet(hash);
            return(data);
        }
Beispiel #3
0
    public IEnumerator CreateAndLoginUserTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        CommandData response = null;

        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });

        yield return(new UnityEngine.WaitForSeconds(1));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        var login = response.GetAs <RegisterUserResponse> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.LoginUser, LoginParams> (ConfigController.ApplicationSettings.id,
                                                                            new LoginParams()
        {
            id     = login.id,
            secret = login.secret
        }));

        // tell the client that we are logged in
        ConfigController.ActiveUserId = login.id;

        yield return(new WaitForSeconds(0.5f));

        // register test command on user
        (new CoflnetUser()).GetCommandController().OverwriteCommand <TestCommandWithPermission> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <TestCommandWithPermission, int> (login.id, 1), true);

        yield return(new WaitForSeconds(0.5f));

        // user was created in the last second
        Assert.IsTrue(response.GetAs <int> () == 5);
    }
Beispiel #4
0
    /// <summary>
    /// Execute the command logic with specified data.
    /// </summary>
    /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
    public override void Execute(CommandData data)
    {
        var obj  = data.GetTargetAs <Entity>();
        var list = obj as IListEntity <T>;

        list.Elements.Add(data.GetAs <T>());
    }
Beispiel #5
0
    public IEnumerator CommandResponseTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        CommandData response = null;

        ClientSocket.Instance.Reconnect();
        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });
        ConfigController.ActiveUserId = SourceReference.Default;

        // register server command
        ServerCore.Commands.RegisterCommand <ServerTestCommandGet> ();

        yield return(new UnityEngine.WaitForSeconds(1));

        // send the command
        ClientSocket.Instance.SendCommand(
            new CommandData(ConfigController.ApplicationSettings.id, -1, "", "serverTestCommandGet"));

        // await response
        yield return(new UnityEngine.WaitForSeconds(1));

        // test the expected error slug
        Assert.AreEqual(response.GetAs <int> (), 4);

        ServerCore.Stop();
    }
Beispiel #6
0
 protected T GetElement(CommandData data)
 {
     if (Converter == null)
     {
         return(data.GetAs <T>());
     }
     return(Converter.Invoke(data));
 }
Beispiel #7
0
    public IEnumerator MessageTransitPersitenceCrossServerTest()
    {
        // tell the server his id
        ConfigController.ApplicationSettings.id = new SourceReference(1, 1, 1, 0);
        ServerCore.Init();
        ClientSocket.Instance.Reconnect();
        CommandData response = null;

        ClientSocket.Instance.AddCallback(data => {
            response = data;
        });
        ConfigController.ActiveUserId = SourceReference.Default;

        yield return(new UnityEngine.WaitForSeconds(0.5f));

        // set data
        var request = new RegisterUserRequest();

        request.captchaToken = "";
        request.clientId     = ConfigController.ApplicationSettings.id;

        // send the command
        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <RegisterUser, RegisterUserRequest> (ConfigController.ApplicationSettings.id, request));

        // await response
        yield return(new UnityEngine.WaitForSeconds(0.5f));

        var login = response.GetAs <RegisterUserResponse> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <Coflnet.LoginUser, LoginParams> (
                ConfigController.ApplicationSettings.id,
                new LoginParams()
        {
            id     = login.id,
            secret = login.secret
        }));

        yield return(new WaitForSeconds(0.5f));

        // register second user on virtual other server
        var receiverUser = CoflnetUser.Generate(new SourceReference(1, 1, 2, 0));

        receiverUser.Id = new SourceReference(3, 1, 2, ThreadSaveIdGenerator.NextId);
        receiverUser.GetCommandController().OverwriteCommand <ChatMessageCommand> ();
        Logger.Log(receiverUser.Id);

        (new CoflnetUser()).GetCommandController().OverwriteCommand <TestCommandWithPermission> ();

        ClientSocket.Instance.SendCommand(
            CommandData.CreateCommandData <ChatMessageCommand, string> (receiverUser.Id, "hi"));

        yield return(new WaitForSeconds(0.5f));

        // message is saved
        Assert.IsTrue(MessagePersistence.ServerInstance.GetMessagesFor(receiverUser.Id).ToList().Any());
    }
Beispiel #8
0
        public override Entity CreateResource(CommandData data)
        {
            var options = data.GetAs <Options>();

            return(new TestResource()
            {
                value = options.Value
            });
        }
Beispiel #9
0
    /// <summary>
    /// Decrypts a message sent from a group resource.
    /// </summary>
    /// <returns>The decrypted group message.</returns>
    /// <param name="message">encrypted message sent from the server.</param>
    public CommandData DecryptGroupMessage(CommandData message)
    {
        var decryptedMessage = new CommandData(message);
        var messageContent   = message.GetAs <EncryptedChatMessage> ();
        var chatEncrypt      = GetEndToEndEncrypt(messageContent.Sender, message.SenderId);

        decryptedMessage.message  = chatEncrypt.DecryptWithSessionKey(messageContent.EncryptedMessage);
        decryptedMessage.SenderId = messageContent.Sender;
        return(decryptedMessage);
    }
Beispiel #10
0
        /// <summary>
        /// Execute the command logic with specified data.
        /// </summary>
        /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
        public override void Execute(CommandData data)
        {
            // who do we want to subscribe to
            data.CoreInstance.CloneAndSubscribe(data.GetAs <EntityId>(), r => {
                // send it back further

                // add the client to subscriber list
                r.GetAccess().Subscribe(data.SenderId);
            });
        }
Beispiel #11
0
    /// <summary>
    /// Execute the command logic with specified data.
    /// </summary>
    /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
    public override void Execute(CommandData data)
    {
        //data.GetTargetAs<<see cref="Entity"/>>().ExecuteCommand()
        var innerData = data.GetAs <CommandData>();

        // set the owning resource as sender
        innerData.SenderId = data.Recipient;
        // execute the command
        data.CoreInstance.EntityManager.ExecuteForReference(innerData);
    }
Beispiel #12
0
        public override Entity CreateResource(CommandData data)
        {
            var chat    = new GroupChatResource();
            var options = data.GetAs <Params>();

            chat.Name = options.Name;

            foreach (var user in options.Members)
            {
                chat.Members.Add(new ChatMember(user));
            }

            return(chat);
        }
Beispiel #13
0
    /// <summary>
    /// Execute the command logic with specified data.
    /// </summary>
    /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
    public override void Execute(CommandData data)
    {
        T[] values;
        if (ConverterArray != null)
        {
            values = ConverterArray(data);
        }
        else
        {
            values = data.GetAs <T[]>();
        }

        ListGetter.Invoke(data).AddRange(values);
    }
Beispiel #14
0
        public override void Execute(CommandData data)
        {
            var encrypt           = Instance.GetEndToEndEncrypt(data.SenderId);
            var setup             = data.GetAs <ChatSetupHeader> ();
            var ephermeralKeyPair = KeyPairManager.Instance.GetKeyPair(setup.publicOneTimeKey);

            bool result = encrypt.DeriveKeysServer(setup.publicPreKey, setup.publicEphemeralKey, ephermeralKeyPair);

            if (result)
            {
                // deriving was successful, send confirmation
                ServerController.Instance.SendCommand <ReceivedSetup, EntityId> (data.SenderId, data.SenderId);
            }
            else
            {
                throw new Exception("key deriviation failed");
            }
        }
Beispiel #15
0
        public override void Execute(CommandData data)
        {
            var loginParams = data.GetAs <ServerLoginToken> ();

            if (loginParams == null)
            {
                return;
            }
            var serverCommandData = data as ServerCommandData;

            var server = EntityManager.Instance.GetEntity <CoflnetServer> (loginParams.OriginServerId);

            if (loginParams.Validate(server.PublicKey, ConfigController.ApplicationSettings.id))
            {
                // validation success
                serverCommandData.Connection.AuthenticatedIds.Add(loginParams.OriginServerId);
                //data.SendBack()
            }
        }
Beispiel #16
0
        public override void Execute(CommandData data)
        {
            var args = data.GetAs <Arguments> ();

            byte[] publicIdentKey = args.publicIdentKey;
            byte[] publicPreKey   = EndToEndEncrypt.SignByteOpen(args.publicPreKey, publicIdentKey);

            var encrypt = Instance.GetEndToEndEncrypt(args.id);

            // validate identity
            if (encrypt.PublicIdentKey != null && !encrypt.PublicIdentKey.SequenceEqual(publicIdentKey))
            {
                throw new Exception("Incorrect ident key abording");
            }

            encrypt.DeriveKeysClient(publicIdentKey, publicPreKey, args.oneTimeKey);

            Instance.SaveEndToEndEncrypt(encrypt);

            // send the setup headers
            Instance.SendSetup(args.id);
        }
Beispiel #17
0
        public override void Execute(CommandData data)
        {
            var response = data.GetAs <RegisterUserResponse>();



            // all outdated the client core is a device not an user


            data.CoreInstance.EntityManager
            .UpdateIdAndAddRedirect(data.Recipient, response.id);

            // add the core behind
            var user = data.GetTargetAs <CoflnetUser>();

            user.GetCommandController()
            .AddFallback(data.CoreInstance.GetCommandController());

            // The core itself also has the same id
            data.CoreInstance.Id = response.id;


            //ConfigController.UserSettings.userId = response.id;
            ConfigController.Users.Add(new UserSettings(response.managingServers, response.id, response.secret));

            // if this is the first user, activate it
            if (ConfigController.Users.Count == 1)
            {
                ConfigController.ActiveUserId = response.id;
            }


            // Login
            ClientCore.Instance.SendCommand <LoginUser, LoginParams>(new EntityId(response.id.ServerId, 0),
                                                                     new LoginParams(response.id, response.secret));
        }
Beispiel #18
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     valueSetter.Invoke(data, data.GetAs <T>());
 }
Beispiel #19
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     ChatService.Instance.ReceiveMessage(data.GetAs <ChatMessage>(), data.SenderId);
 }
        /// <summary>
        /// Execute the command logic with specified data.
        /// </summary>
        /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
        /// <param name="target">The local <see cref="Entity"/> on which to test on .</param>
        public override bool CheckPermission(CommandData data, Entity target)
        {
            var securedRes = target as SecuredResource;

            return(securedRes != null && data.GetAs <Token>().Validate(securedRes.keyPair.publicKey));
        }
Beispiel #21
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     data.GetTargetAs <Device>().Users.Remove(new Reference <CoflnetUser>(data.GetAs <EntityId>()));
 }
Beispiel #22
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     data.GetTargetAs <Entity> ().Access.Authorize(data.GetAs <EntityId> (), AccessMode.NONE);
 }
Beispiel #23
0
 /// <summary>
 /// Receives the session setup.
 /// </summary>
 /// <param name="data">Data.</param>
 public void ReceiveSessionSetup(CommandData data)
 {
     ReceiveSessionSetup(data.SenderId, data.GetAs <ChatSetupHeader> ());
 }
Beispiel #24
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     ListGetter.Invoke(data).RemoveAt(data.GetAs <int>());
 }
Beispiel #25
0
    /// <summary>
    /// Execute the command logic with specified data.
    /// </summary>
    /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
    public override void Execute(CommandData data)
    {
        var args = data.GetAs <KeyValuePair <int, T> >();

        ListGetter.Invoke(data).Insert(args.Key, args.Value);
    }
Beispiel #26
0
        /// <summary>
        /// Collects referenced objects for one particular server.
        /// </summary>
        /// <param name="data">Message data from a recover controller containing the serverId that has to be recovered</param>
        public void CollectReferencesCommand(CommandData data)
        {
            long serverId = data.GetAs <long>();

            ServerController.Instance.SendCommandToServer(new CommandData("recover_response", new byte[0]), serverId);
        }
Beispiel #27
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     getter.Invoke(data).Add(data.GetAs <TContent>());
 }
Beispiel #28
0
 public void GetFileInfo(CommandData data)
 {
     UserFile file = data.GetTargetAs <CoflnetUser>().Files[data.GetAs <long>()];
 }
Beispiel #29
0
 /// <summary>
 /// Execute the command logic with specified data.
 /// </summary>
 /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
 public override void Execute(CommandData data)
 {
     data.CoreInstance.EntityManager.UpdateEntity(data.GetAs <CommandData> (), data.SenderId);
 }
Beispiel #30
0
    /// <summary>
    /// Execute the command logic with specified data.
    /// </summary>
    /// <param name="data"><see cref="CommandData"/> passed over the network .</param>
    public override void Execute(CommandData data)
    {
        var args = data.GetAs <ValueTuple <int, int> >();

        ListGetter.Invoke(data).RemoveRange(args.Item1, args.Item2);
    }