예제 #1
0
        public void ContainerHostListenerSaslPlainNegativeTest()
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(this.Uri.Host, this.Uri.Port);
            var stream = new NetworkStream(socket);

            stream.Write(new byte[] { (byte)'A', (byte)'M', (byte)'Q', (byte)'P', 3, 1, 0, 0 }, 0, 8);
            TestListener.FRM(stream, 0x41, 3, 0, new Symbol("PLAIN"), Encoding.ASCII.GetBytes("guest\0invalid"));

            byte[] buffer   = new byte[1024];
            int    total    = 0;
            int    readSize = 0;

            for (int i = 0; i < 1000; i++)
            {
                readSize = socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
                if (readSize == 0)
                {
                    break;
                }

                total += readSize;
            }

            Assert.IsTrue(total > 0, "No response received from listener");
            Assert.AreEqual(0, readSize, "last read should be 0 as socket should be closed");
        }
예제 #2
0
        public void SendWithInvalidRemoteChannelTest()
        {
            this.testListener.RegisterTarget(TestPoint.Transfer, (stream, channel, fields) =>
            {
                // send an end with invalid channel
                TestListener.FRM(stream, 0x17UL, 0, 33);
                return(TestOutcome.Stop);
            });

            string testName = "SendWithProtocolErrorTest";

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                Connection connection = new Connection(this.address);
                Session    session    = new Session(connection);
                SenderLink sender     = new SenderLink(session, "sender-" + testName, "any");
                try
                {
                    sender.Send(new Message("test")
                    {
                        Properties = new Properties()
                        {
                            MessageId = testName
                        }
                    });
                    Assert.IsTrue(false, "Send should throw exception");
                }
                catch (AmqpException exception)
                {
                    Assert.AreEqual(ErrorCode.NotFound, (string)exception.Error.Condition);
                }
                connection.Close();
                Assert.AreEqual(ErrorCode.NotFound, (string)connection.Error.Condition);
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                Connection connection = await Connection.Factory.CreateAsync(this.address);
                Session session       = new Session(connection);
                SenderLink sender     = new SenderLink(session, "sender-" + testName, "any");
                try
                {
                    await sender.SendAsync(new Message("test")
                    {
                        Properties = new Properties()
                        {
                            MessageId = testName
                        }
                    });
                    Assert.IsTrue(false, "Send should throw exception");
                }
                catch (AmqpException exception)
                {
                    Assert.AreEqual(ErrorCode.NotFound, (string)exception.Error.Condition);
                }
                await connection.CloseAsync();
                Assert.AreEqual(ErrorCode.NotFound, (string)connection.Error.Condition);
            }).Unwrap().GetAwaiter().GetResult();
        }
        public void ConnectionMaxFrameSizeTest()
        {
            this.testListener.RegisterTarget(TestPoint.Open, (stream, channel, fields) =>
            {
                TestListener.FRM(stream, 0x10UL, 0, 0, "TestListener", "localhost", 512u);
                return(TestOutcome.Stop);
            });

            this.testListener.RegisterTarget(TestPoint.Begin, (stream, channel, fields) =>
            {
                TestListener.FRM(stream, 0x11UL, 0, channel, channel, 0u, 100u, 100u, 8u, null, null, null,
                                 new Fields()
                {
                    { new Symbol("big-string"), new string('a', 1024) }
                });
                return(TestOutcome.Stop);
            });

            string testName = "ConnectionMaxFrameSizeTest";

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                Open open = new Open()
                {
                    ContainerId = testName, HostName = "localhost", MaxFrameSize = 2048
                };
                Connection connection = new Connection(this.address, null, open, null);
                Session    session    = new Session(connection);
                SenderLink sender     = new SenderLink(session, "sender-" + testName, "any");
                sender.Send(new Message("test")
                {
                    Properties = new Properties()
                    {
                        MessageId = testName
                    }
                });
                connection.Close();
                Assert.IsTrue(connection.Error == null, "connection has error!" + connection.Error);
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                ConnectionFactory factory = new ConnectionFactory();
                factory.AMQP.MaxFrameSize = 2048;
                Connection connection     = await factory.CreateAsync(this.address);
                Session session           = new Session(connection);
                SenderLink sender         = new SenderLink(session, "sender-" + testName, "any");
                await sender.SendAsync(new Message("test")
                {
                    Properties = new Properties()
                    {
                        MessageId = testName
                    }
                });
                await connection.CloseAsync();
                Assert.IsTrue(connection.Error == null, "connection has error!" + connection.Error);
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #4
0
        public void ReceiveWithLinkDetachErrorTest()
        {
            this.testListener.RegisterTarget(TestPoint.Flow, (stream, channel, fields) =>
            {
                // detach link with error. receive calls should throw
                TestListener.FRM(stream, 0x16UL, 0, channel, fields[0], true, new Error()
                {
                    Condition = ErrorCode.InternalError
                });
                return(TestOutcome.Stop);
            });
            this.testListener.RegisterTarget(TestPoint.Detach, (stream, channel, fields) =>
            {
                return(TestOutcome.Stop);
            });

            string testName = "ReceiveWithLinkDetachErrorTest";

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                Connection   connection = new Connection(this.address);
                Session      session    = new Session(connection);
                ReceiverLink receiver   = new ReceiverLink(session, "receiver-" + testName, "any");
                try
                {
                    receiver.Receive();
                    Assert.IsTrue(false, "Receive should fail with error");
                }
                catch (AmqpException exception)
                {
                    Assert.AreEqual((Symbol)ErrorCode.InternalError, exception.Error.Condition);
                }
                connection.Close();
                Assert.AreEqual((Symbol)ErrorCode.InternalError, receiver.Error.Condition);
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                Connection connection = await Connection.Factory.CreateAsync(this.address);
                Session session       = new Session(connection);
                ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, "any");
                try
                {
                    await receiver.ReceiveAsync();
                    Assert.IsTrue(false, "Receive should fail with error");
                }
                catch (AmqpException exception)
                {
                    Assert.AreEqual((Symbol)ErrorCode.InternalError, exception.Error.Condition);
                }
                await connection.CloseAsync();
                Assert.AreEqual((Symbol)ErrorCode.InternalError, receiver.Error.Condition);
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #5
0
        public void SaslMismatchTest()
        {
            this.testListener.RegisterTarget(TestPoint.Header, (stream, channel, fields) =>
            {
                stream.Write(new byte[] { (byte)'A', (byte)'M', (byte)'Q', (byte)'P', 3, 1, 0, 0 }, 0, 8);
                stream.Write(new byte[] { (byte)'A', (byte)'M', (byte)'Q', (byte)'P', 0, 1, 0, 0 }, 0, 8);
                TestListener.FRM(stream, 0x10UL, 0, 0, "TestListener", "localhost", 512u);
                TestListener.FRM(stream, 0x18UL, 0, 0);
                return(TestOutcome.Stop);
            });

            string testName = "SaslMismatchTest";
            bool   failed;

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                failed = true;
                try
                {
                    Open open = new Open()
                    {
                        ContainerId = testName, HostName = "localhost", MaxFrameSize = 2048
                    };
                    Connection connection = new Connection(this.address, null, open, null);
                    connection.Close(TimeSpan.FromSeconds(5));
                    failed = connection.Error != null;
                }
                catch (Exception e)
                {
                    Trace.WriteLine(TraceLevel.Information, "Exception {0}:{1}", e.GetType().Name, e.Message);
                }
                Assert.IsTrue(failed, "should fail");
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                failed = true;
                try
                {
                    ConnectionFactory factory = new ConnectionFactory();
                    factory.AMQP.MaxFrameSize = 2048;
                    Connection connection     = await factory.CreateAsync(this.address);
                    await connection.CloseAsync(TimeSpan.FromSeconds(5));
                    Trace.WriteLine(TraceLevel.Frame, "Error {0}", connection.Error);
                    failed = connection.Error != null;
                }
                catch (Exception e)
                {
                    Trace.WriteLine(TraceLevel.Information, "Exception {0}:{1}", e.GetType().Name, e.Message);
                }
                Assert.IsTrue(failed, "should fail");
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #6
0
        public void ConnectionEventsOnProtocolError()
        {
            ManualResetEvent closeReceived  = null;
            ManualResetEvent closedNotified = null;

            this.testListener.RegisterTarget(TestPoint.Begin, (stream, channel, fields) =>
            {
                // begin with invalid remote channel
                TestListener.FRM(stream, 0x11UL, 0, channel, (ushort)2, 0u, 100u, 100u, 8u);
                return(TestOutcome.Stop);
            });

            this.testListener.RegisterTarget(TestPoint.Close, (stream, channel, fields) =>
            {
                closeReceived.Set();
                return(TestOutcome.Continue);
            });

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                closeReceived  = new ManualResetEvent(false);
                closedNotified = new ManualResetEvent(false);
                Connection connection = new Connection(this.address);
                connection.Closed += (o, e) => closedNotified.Set();
                Session session = new Session(connection);
                Assert.IsTrue(closeReceived.WaitOne(5000), "Close not received");
                Assert.IsTrue(closedNotified.WaitOne(5000), "Closed event not fired");
                Assert.AreEqual(ErrorCode.NotFound, (string)connection.Error.Condition);
                Assert.IsTrue(session.IsClosed);
                Assert.IsTrue(connection.IsClosed);
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                closeReceived         = new ManualResetEvent(false);
                closedNotified        = new ManualResetEvent(false);
                Connection connection = await Connection.Factory.CreateAsync(this.address);
                connection.Closed    += (o, e) => closedNotified.Set();
                Session session       = new Session(connection);
                Assert.IsTrue(closeReceived.WaitOne(5000), "Close not received");
                Assert.IsTrue(closedNotified.WaitOne(5000), "Closed event not fired");
                Assert.AreEqual(ErrorCode.NotFound, (string)connection.Error.Condition);
                Assert.IsTrue(session.IsClosed);
                Assert.IsTrue(connection.IsClosed);
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #7
0
        public void CloseSessionWithDetachTest()
        {
            this.testListener.RegisterTarget(TestPoint.End, (stream, channel, fields) =>
            {
                // send a detach
                TestListener.FRM(stream, 0x16UL, 0, channel, 0u, true);
                return(TestOutcome.Continue);
            });

            string testName = "CloseSessionWithDetachTest";

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                Connection connection = new Connection(this.address);
                Session    session    = new Session(connection);
                SenderLink sender     = new SenderLink(session, "sender-" + testName, "any");
                sender.Send(new Message("test")
                {
                    Properties = new Properties()
                    {
                        MessageId = testName
                    }
                });
                session.Close(0);
                connection.Close();
                Assert.IsTrue(connection.Error == null, "connection has error!" + connection.Error);
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                Connection connection = await Connection.Factory.CreateAsync(this.address);
                Session session       = new Session(connection);
                SenderLink sender     = new SenderLink(session, "sender-" + testName, "any");
                await sender.SendAsync(new Message("test")
                {
                    Properties = new Properties()
                    {
                        MessageId = testName
                    }
                });
                session.Close(0);
                await connection.CloseAsync();
                Assert.IsTrue(connection.Error == null, "connection has error!" + connection.Error);
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #8
0
        public void ReceiveWithLinkDetachTest()
        {
            this.testListener.RegisterTarget(TestPoint.Flow, (stream, channel, fields) =>
            {
                // detach link without error. receivers should return null (eof)
                TestListener.FRM(stream, 0x16UL, 0, channel, fields[0], true);
                return(TestOutcome.Stop);
            });
            this.testListener.RegisterTarget(TestPoint.Detach, (stream, channel, fields) =>
            {
                return(TestOutcome.Stop);
            });

            string testName = "ReceiveWithLinkDetachTest";

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                Connection   connection = new Connection(this.address);
                Session      session    = new Session(connection);
                ReceiverLink receiver   = new ReceiverLink(session, "receiver-" + testName, "any");
                DateTime     dt         = DateTime.UtcNow;
                var          message    = receiver.Receive(30000);
                Assert.IsTrue(message == null);
                connection.Close();
                Assert.IsTrue(DateTime.UtcNow.Subtract(dt).TotalMilliseconds < 10000, "receive should return right away");
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                Connection connection = await Connection.Factory.CreateAsync(this.address);
                Session session       = new Session(connection);
                ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, "any");
                DateTime dt           = DateTime.UtcNow;
                var message           = await receiver.ReceiveAsync(30000);
                Assert.IsTrue(message == null);
                await connection.CloseAsync();
                Assert.IsTrue(DateTime.UtcNow.Subtract(dt).TotalMilliseconds < 10000, "receive should return right away");
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #9
0
        public void ReceiveWithNoCreditTest()
        {
            this.testListener.RegisterTarget(TestPoint.Attach, (stream, channel, fields) =>
            {
                bool role = !(bool)fields[2];
                TestListener.FRM(stream, 0x12UL, 0, channel, fields[0], fields[1], role, fields[3], fields[4], new Source(), new Target());
                TestListener.FRM(stream, 0x14UL, 0, channel, fields[1], 0u, new byte[0], 0u, true, false);  // transfer
                return(TestOutcome.Stop);
            });

            string testName = "ReceiveWithNoCreditTest";

            Trace.WriteLine(TraceLevel.Information, "sync test");
            {
                ManualResetEvent closed     = new ManualResetEvent(false);
                Connection       connection = new Connection(this.address);
                connection.Closed += (s, a) => closed.Set();
                Session      session  = new Session(connection);
                ReceiverLink receiver = new ReceiverLink(session, "receiver-" + testName, "any");
                Assert.IsTrue(closed.WaitOne(5000), "Connection not closed");
                Assert.AreEqual(ErrorCode.TransferLimitExceeded, (string)connection.Error.Condition);
                Assert.IsTrue(receiver.IsClosed);
            }

            Trace.WriteLine(TraceLevel.Information, "async test");
            Task.Factory.StartNew(async() =>
            {
                ManualResetEvent closed = new ManualResetEvent(false);
                Connection connection   = await Connection.Factory.CreateAsync(this.address);
                connection.Closed      += (s, a) => closed.Set();
                Session session         = new Session(connection);
                ReceiverLink receiver   = new ReceiverLink(session, "receiver-" + testName, "any");
                Assert.IsTrue(closed.WaitOne(5000), "Connection not closed");
                Assert.AreEqual(ErrorCode.TransferLimitExceeded, (string)connection.Error.Condition);
                Assert.IsTrue(receiver.IsClosed);
            }).Unwrap().GetAwaiter().GetResult();
        }
예제 #10
0
 public void TestInitialize()
 {
     this.testListener = new TestListener(new IPEndPoint(IPAddress.Any, port));
     this.testListener.Open();
     this.address = new Address("amqp://127.0.0.1:" + port);
 }
예제 #11
0
 public void TestInitialize()
 {
     this.testListener = new TestListener(new IPEndPoint(IPAddress.Any, port));
     this.testListener.Open();
     this.address = new Address("amqp://127.0.0.1:" + port);
 }
예제 #12
0
 public void Initialize()
 {
     this.testListener = new TestListener(new IPEndPoint(IPAddress.Any, port));
     this.testListener.Open();
 }