Ejemplo n.º 1
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.º 2
0
        public void UncaughtExceptionTest()
        {
            var are       = new AutoResetEvent(false);
            var endPoint  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
            var connector = new AsyncSocketConnector();

            connector.FilterChain.AddLast("fireExceptiojn", new FireExceptionFilter(are));
            connector.FilterChain.AddLast("exceptionCounter", ObjectHost.Host.Resolve <ExceptionCounterFilter>());
            connector.FilterChain.AddLast("logger", new LoggingFilter());
            connector.SessionCreated += (s, e) =>
            {
                e.Session.SetAttributeIfAbsent(KeyName.SESSION_ERROR_COUNTER, 0);
            };

            var future = connector.Connect(endPoint);

            future.Await();
            var session = future.Session;

            var guid             = Guid.NewGuid().ToByteArray();
            var serverIdentifier = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes("ctppv5_command_server")).ToHex();
            var buffer           = ByteBufferAllocator.Instance.Allocate(48);

            buffer.AutoExpand = true;
            buffer.PutInt32(49);
            buffer.Put(1);
            buffer.PutInt16(1);
            buffer.Put(ErrorCode.NoError.ToByte());
            buffer.Put(MessageType.Command.ToByte());
            buffer.Put(Guid.NewGuid().ToByteArray());
            buffer.Put(guid);
            buffer.Put((MessageFilterType.Checksum).ToByte());
            buffer.Put(new byte[] { 0, 0 });
            buffer.Put(SerializeMode.None.ToByte());
            buffer.Position = 0;
            var header = buffer.GetArray(45);

            buffer.Put(BitConverter.GetBytes(Crc32.CalculateDigest(header, (uint)0, (uint)(header.Length))));
            buffer.Position = buffer.Position - 1; // make crc error
            buffer.Put(4);

            for (int i = 0; i < 6; i++)
            {
                buffer.Flip();
                session.Write(buffer);
                are.WaitOne();
            }

            while (true)
            {
                if (session.Connected)
                {
                    Thread.Sleep(200);
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        public FilterResult Out(MessageHeader header, IMessageDataContainer container)
        {
            var headerBuffer = container.Take();
            var bodyBuffer   = container.Take();
            var checkSum     = BitConverter.GetBytes(
                Crc32.CalculateDigest(headerBuffer.Concat(bodyBuffer),
                                      (uint)0,
                                      (uint)(headerBuffer.Length + bodyBuffer.Length)));

            container.Push(checkSum);
            return(new FilterResult {
                OK = true
            });
        }
Ejemplo n.º 4
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();
        }