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(); }
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(); }
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(); }
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(); }
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(); }