Ejemplo n.º 1
0
        public void ErrorVersionTest()
        {
            ConfigurationManager.AppSettings.Set("ReadBufferSize", "4");
            var client    = new SimpleTcpClient();
            var handler   = client.GetFutureHandler();
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            Assert.IsTrue(bootstrap.StartUp());
            bootstrap.Server.Handler = handler;
            Assert.IsTrue(client.Connect(bootstrap.EndPoint));

            //version is wrong
            var buffer = ByteBufferAllocator.Instance.Allocate(5);

            buffer.PutInt32(5);
            buffer.Put(2);
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(1, handler.ErrorCount);

            //wrong fixed header length
            buffer = ByteBufferAllocator.Instance.Allocate(5);
            buffer.PutInt32(5);
            buffer.Put(1);
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(2, handler.ErrorCount);
            client.Close();
        }
Ejemplo n.º 2
0
        public void EnableAllMessageFilterTest()
        {
            var client     = new SimpleTcpClient();
            var identifier = Guid.NewGuid().ToByteArray();
            var bootstrap  = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            Assert.IsTrue(bootstrap.StartUp(null, new TestModule()));
            var handler = client.GetFutureHandler(null, (a, b) =>
            {
                var message = b as DuplexMessage;
                Assert.AreEqual(MessageVersion.V1, message.Header.Version);
                Assert.AreEqual(MessageFilterType.Checksum, message.Header.FilterType & MessageFilterType.Checksum);
                Assert.AreEqual(MessageFilterType.Compression, message.Header.FilterType & MessageFilterType.Compression);
                Assert.AreEqual(SerializeMode.Json, message.Header.SerializeMode);
                Assert.AreEqual(MessageState.Success, message.Header.State);
                Assert.AreEqual(ErrorCode.NoError, message.Header.ErrorCode);
                Assert.AreEqual(identifier.ToHex(), message.Header.Identifier);
                var obj = message.GetContent <Entity>();
                Assert.AreEqual("a1", obj.Name);
                Assert.AreEqual(2, obj.Nest.Age);
                Assert.AreEqual(Desc, obj.Task);
            });

            bootstrap.Server.Handler = handler;
            Assert.IsTrue(client.Connect(bootstrap.EndPoint));
            var buffer = ByteBufferAllocator.Instance.Allocate(48);

            buffer.AutoExpand = true;
            var pubKey = ObjectHost.Host.Resolve <ICryptoKeyProvider>().GetPublicKey(null);
            var body   = new RSACrypto().Encrypt(pubKey, new Json().Serialize(new Entity
            {
                Name = "a1",
                Nest = new Nest {
                    Age = 2
                },
                Task = Desc
            }).Zip());

            buffer.PutInt32(49 + body.Length);
            buffer.Put(1);
            buffer.PutInt16(1);
            buffer.Put(ErrorCode.NoError.ToByte());
            buffer.Put(MessageType.Command.ToByte());
            buffer.Put(identifier);
            buffer.Put(Guid.NewGuid().ToByteArray());
            buffer.Put((MessageFilterType.Checksum | MessageFilterType.Compression | MessageFilterType.Crypto).ToByte());
            buffer.Put(new byte[] { 0, 0 });
            buffer.Put(SerializeMode.Json.ToByte());
            buffer.Position = 0;
            var header = buffer.GetArray(45);

            buffer.Put(BitConverter.GetBytes(Crc32.CalculateDigest(header.Concat(body), (uint)0, (uint)(header.Length + body.Length))));
            buffer.Put(body);
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(1, handler.RcvCount);
            //send again
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(2, handler.RcvCount);
            client.Close();
        }
Ejemplo n.º 3
0
        public void ListConnectorsTest()
        {
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            bootstrap.AddFilterTypes(typeof(AuthenticationValidationFilter));
            bootstrap.StartUp(null,
                              new CommandServerModule(),
                              new RepositoryModule(),
                              new ClientModule(),
                              new TestModule(),
                              new DefinedIdentifierModule());

            var command = GetRegisteredAndAuthedSession(bootstrap, true).CreateCommand(CommandCode.ListConnectors);
            var result  = command.Run(5000);

            Assert.AreEqual(0, result.GetContent <List <string> >().Count);

            command = GetRegisteredAndAuthedSession(bootstrap, true).CreateCommand(CommandCode.ListConnectors);
            result  = command.Run(5000);
            Assert.AreEqual(1, result.GetContent <List <string> >().Count);

            command = GetRegisteredAndAuthedSession(bootstrap, true).CreateCommand(CommandCode.ListConnectors);
            result  = command.Run(5000);
            Assert.AreEqual(2, result.GetContent <List <string> >().Count);

            command = GetRegisteredAndAuthedSession(bootstrap, true).CreateCommand(CommandCode.ListConnectors);
            result  = command.Run(5000);
            Assert.AreEqual(3, result.GetContent <List <string> >().Count);

            command = GetRegisteredAndAuthedSession(bootstrap, true).CreateCommand(CommandCode.ListConnectors);
            result  = command.Run(5000);
            Assert.AreEqual(4, result.GetContent <List <string> >().Count);
        }
Ejemplo n.º 4
0
        public void ServerStartUpTest()
        {
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            Assert.IsTrue(bootstrap.StartUp());
            var connector = new AsyncSocketConnector();
            var future    = connector.Connect(bootstrap.EndPoint);

            Assert.IsTrue(future.Await(500));
            System.Threading.Thread.Sleep(500);
            Assert.AreEqual(1, bootstrap.Server.ManagedSessions.Count);
            Assert.IsTrue(future.Connected);
            Assert.IsNotNull(future.Session);
            bootstrap.Server.Dispose();
            connector.Dispose();
        }
Ejemplo n.º 5
0
        public void RegisterAndAuthTest()
        {
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            bootstrap.AddFilterTypes(typeof(AuthenticationValidationFilter));
            bootstrap.StartUp(null,
                              new CommandServerModule(),
                              new RepositoryModule(),
                              new ClientModule(),
                              new TestModule());

            //dont register client module for they are in the same context.
            //new ClientInitializer().Init(new TestModule());
            using (var scope = ObjectHost.Host.BeginLifetimeScope())
            {
                var factory = scope.Resolve <IAsyncSessionFactory <DuplexMessage> >();
                var session = factory.OpenSession(string.Format("{0};{1};",
                                                                bootstrap.EndPoint.ToString(), "keepalive=false"));
                var command = session.CreateCommand(CommandCode.Register);
                command.SecurityEnabled = true;
                command.Parameter       = new RegisterInfo {
                    ClientMacAddr = NetworkInfoHelper.GetMacAddr(), ClientPubKey = new TestRsaKeyProvider().GetPublicKey(null)
                };
                var result = command.Run(5000);
                Assert.AreEqual(ErrorCode.NoError, result.Header.ErrorCode);
                Assert.AreEqual(command.ID, result.Header.MessageID);
                Assert.AreEqual(MessageType.Callback, result.Header.MessageType);

                command           = session.CreateCommand(CommandCode.Authentication);
                command.Parameter = new AuthInfo
                {
                    Identifier = scope.Resolve <IIdentifierProvider>().GetIdentifier(),
                    Mac        = NetworkInfoHelper.GetMacAddr()
                };
                command.SecurityEnabled = true;
                result = command.Run(5000);
                Assert.AreEqual(ErrorCode.NoError, result.Header.ErrorCode);
                Assert.AreEqual(command.ID, result.Header.MessageID);
                Assert.AreEqual(MessageType.Callback, result.Header.MessageType);

                command = session.CreateCommand(CommandCode.Test);
                result  = command.Run(5000);
                Assert.AreEqual(CommandCode.Test, result.Header.CommandCode);
                Assert.AreEqual(ErrorCode.BadRequest, result.Header.ErrorCode); // test command not registered, it's ture that it returns bad request
            }
        }
Ejemplo n.º 6
0
        public void NoChecksumNoBodyMessageTest()
        {
            var guid       = Guid.NewGuid().ToByteArray();
            var identifier = Guid.NewGuid().ToByteArray();
            var client     = new SimpleTcpClient();
            var bootstrap  = new ServerBootstrap(EndPointDispatcher.GetRandomPort());;

            Assert.IsTrue(bootstrap.StartUp());
            var handler = client.GetFutureHandler(null, (a, b) =>
            {
                var message = b as DuplexMessage;
                Assert.AreEqual(MessageVersion.V1, message.Header.Version);
                Assert.AreEqual(identifier.ToHex(), message.Header.Identifier);
                Assert.AreEqual(Convert.ToBase64String(guid), message.Header.MessageID);
                Assert.AreEqual(MessageFilterType.None, message.Header.FilterType);
                Assert.AreEqual(2, message.Header.FilterCode.Length);
                Assert.AreEqual(0, message.Header.FilterCode[0]);
                Assert.AreEqual(0, message.Header.FilterCode[1]);
                Assert.AreEqual(SerializeMode.None, message.Header.SerializeMode);
                Assert.AreEqual(MessageState.Success, message.Header.State);
                Assert.AreEqual(ErrorCode.NoError, message.Header.ErrorCode);
            });

            bootstrap.Server.Handler = handler;
            Assert.IsTrue(client.Connect(bootstrap.EndPoint));
            var buffer = ByteBufferAllocator.Instance.Allocate(49);

            buffer.PutInt32(49);
            buffer.Put(1);
            buffer.PutInt16(1);
            buffer.Put(ErrorCode.NoError.ToByte());
            buffer.Put(MessageType.Command.ToByte());
            buffer.Put(identifier);
            buffer.Put(guid);
            buffer.Put(0);
            buffer.Put(new byte[] { 0, 0 });
            buffer.Put(0);
            buffer.Put(new byte[] { 0, 0, 0, 0 });
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(1, handler.RcvCount);
            //send again
            Assert.IsTrue(client.Send(buffer));
            Assert.AreEqual(2, handler.RcvCount);
            client.Close();
        }
Ejemplo n.º 7
0
        public void UnauthorizedCommandTest()
        {
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            bootstrap.AddFilterTypes(typeof(AuthenticationValidationFilter));
            bootstrap.StartUp(null,
                              new CommandServerModule(),
                              new RepositoryModule(),
                              new ClientModule(),
                              new TestModule());

            //dont register client module for they are in the same context.
            //new ClientInitializer().Init(new TestModule());
            using (var scope = ObjectHost.Host.BeginLifetimeScope())
            {
                var factory = scope.Resolve <IAsyncSessionFactory <DuplexMessage> >();
                var session = factory.OpenSession(string.Format("{0};{1};",
                                                                bootstrap.EndPoint.ToString(), "keepalive=false"));
                var command = session.CreateCommand(CommandCode.Test);
                var result  = command.Run(60000);
                Assert.AreEqual(CommandCode.Test, result.Header.CommandCode);
                Assert.AreEqual(ErrorCode.UnauthorizedCommand, result.Header.ErrorCode);
            }
        }
Ejemplo n.º 8
0
        public void BadRequestResultTest()
        {
            var guid       = Guid.NewGuid().ToByteArray();
            var identifier = Guid.NewGuid().ToByteArray();
            var client     = new SimpleTcpClient();
            var bootstrap  = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            Assert.IsTrue(bootstrap.StartUp());
            Assert.IsTrue(client.Connect(bootstrap.EndPoint));
            var buffer = ByteBufferAllocator.Instance.Allocate(48);

            buffer.AutoExpand = true;
            var body = new Json().Serialize(new Entity
            {
                Name = "a1",
                Nest = new Nest {
                    Age = 2
                },
                Task = Desc
            }).Zip();

            buffer.PutInt32(49 + body.Length);
            buffer.Put(1);
            buffer.PutInt16(1);
            buffer.Put(ErrorCode.NoError.ToByte());
            buffer.Put(MessageType.Command.ToByte());
            buffer.Put(identifier);
            buffer.Put(guid);
            buffer.Put((MessageFilterType.Checksum | MessageFilterType.Compression).ToByte());
            buffer.Put(new byte[] { 0, 0 });
            buffer.Put(SerializeMode.Json.ToByte());
            buffer.Position = 0;
            var header = buffer.GetArray(45);

            buffer.Put(BitConverter.GetBytes(Crc32.CalculateDigest(header.Concat(body), (uint)0, (uint)(header.Length + body.Length))));
            buffer.Position = buffer.Position - 1; // make crc error
            buffer.Put(4);
            buffer.Put(body);
            Assert.IsTrue(client.Send(buffer));
            var input = client.Receive();

            Assert.AreEqual(49, input.GetInt32());
            Assert.AreEqual(1, input.Get());
            Assert.AreEqual(CommandCode.BadRequest, input.GetInt16().ToEnum <CommandCode>());
            Assert.AreEqual(ErrorCode.DataBroken, input.Get().ToEnum <ErrorCode>());
            Assert.AreEqual(MessageType.CommandAck, input.Get().ToEnum <MessageType>());
            Assert.AreEqual(identifier.ToHex(), input.GetArray(16).ToHex());
            Assert.AreEqual(Convert.ToBase64String(guid), Convert.ToBase64String(input.GetArray(16)));
            Assert.AreEqual(MessageFilterType.Checksum, input.Get().ToEnum <MessageFilterType>());
            Assert.AreEqual(0, input.Get());
            Assert.AreEqual(0, input.Get());
            Assert.AreEqual(SerializeMode.None, input.Get().ToEnum <SerializeMode>());
            input.Position = 0;
            var header2 = input.GetArray(45);

            Crc32.VerifyDigest(BitConverter.ToUInt32(input.GetArray(4), 0), header2, 0, 45);

            Assert.IsTrue(client.Send(buffer));
            input = client.Receive();
            Assert.AreEqual(49, input.GetInt32());
            Assert.AreEqual(1, input.Get());
            Assert.AreEqual(CommandCode.BadRequest, input.GetInt16().ToEnum <CommandCode>());
            Assert.AreEqual(ErrorCode.DataBroken, input.Get().ToEnum <ErrorCode>());
            Assert.AreEqual(MessageType.CommandAck, input.Get().ToEnum <MessageType>());
            Assert.AreEqual(identifier.ToHex(), input.GetArray(16).ToHex());
            Assert.AreEqual(Convert.ToBase64String(guid), Convert.ToBase64String(input.GetArray(16)));
            Assert.AreEqual(MessageFilterType.Checksum, input.Get().ToEnum <MessageFilterType>());
            Assert.AreEqual(0, input.Get());
            Assert.AreEqual(0, input.Get());
            Assert.AreEqual(SerializeMode.None, input.Get().ToEnum <SerializeMode>());
            input.Position = 0;
            header2        = input.GetArray(45);
            Crc32.VerifyDigest(BitConverter.ToUInt32(input.GetArray(4), 0), header2, 0, 45);


            client.Close();
        }