From fb1a62c0836db88732255813f574656fce29be7b Mon Sep 17 00:00:00 2001 From: ig-sinicyn Date: Sun, 10 May 2020 22:19:24 +0300 Subject: [PATCH] Do not throw on invalid replyTo address --- .../client/api/PublicationAddress.cs | 8 +++++ projects/Unit/TestBasicProperties.cs | 31 +++++++++++++++++++ projects/Unit/TestPropertiesClone.cs | 17 +++++----- projects/Unit/TestPublicationAddress.cs | 1 + 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/projects/RabbitMQ.Client/client/api/PublicationAddress.cs b/projects/RabbitMQ.Client/client/api/PublicationAddress.cs index 60fdbd0c80..6778809a1a 100644 --- a/projects/RabbitMQ.Client/client/api/PublicationAddress.cs +++ b/projects/RabbitMQ.Client/client/api/PublicationAddress.cs @@ -106,6 +106,14 @@ public PublicationAddress(string exchangeType, string exchangeName, string routi /// public static PublicationAddress Parse(string uriLikeString) { + // Callers such as IBasicProperties.ReplyToAddress + // expect null result for invalid input. + // The regex.Match() throws on null arguments so we perform explicit check here + if (uriLikeString == null) + { + return null; + } + Match match = PSEUDO_URI_PARSER.Match(uriLikeString); if (match.Success) { diff --git a/projects/Unit/TestBasicProperties.cs b/projects/Unit/TestBasicProperties.cs index 3b7da08dc1..c206c799ed 100644 --- a/projects/Unit/TestBasicProperties.cs +++ b/projects/Unit/TestBasicProperties.cs @@ -120,5 +120,36 @@ public void TestNullableProperties_CanWrite( Assert.AreEqual(isCorrelationIdPresent, propertiesFromStream.IsCorrelationIdPresent()); Assert.AreEqual(isMessageIdPresent, propertiesFromStream.IsMessageIdPresent()); } + + [Test] + public void TestProperties_ReplyTo( + [Values(null, "foo_1", "fanout://name/key")] string replyTo + ) + { + // Arrange + var subject = new Framing.BasicProperties + { + + // Act + ReplyTo = replyTo, + }; + + // Assert + bool isReplyToPresent = replyTo != null; + string replyToAddress = PublicationAddress.Parse(replyTo)?.ToString(); + Assert.AreEqual(isReplyToPresent, subject.IsReplyToPresent()); + + var writer = new Impl.ContentHeaderPropertyWriter(new byte[1024]); + subject.WritePropertiesTo(ref writer); + + // Read from Stream + var propertiesFromStream = new Framing.BasicProperties(); + var reader = new Impl.ContentHeaderPropertyReader(writer.Memory.Slice(0, writer.Offset)); + propertiesFromStream.ReadPropertiesFrom(ref reader); + + Assert.AreEqual(replyTo, propertiesFromStream.ReplyTo); + Assert.AreEqual(isReplyToPresent, propertiesFromStream.IsReplyToPresent()); + Assert.AreEqual(replyToAddress, propertiesFromStream.ReplyToAddress?.ToString()); + } } } diff --git a/projects/Unit/TestPropertiesClone.cs b/projects/Unit/TestPropertiesClone.cs index f5827b5a0e..06be6fffea 100644 --- a/projects/Unit/TestPropertiesClone.cs +++ b/projects/Unit/TestPropertiesClone.cs @@ -68,10 +68,10 @@ private void TestBasicPropertiesClone(BasicProperties bp) bp.ContentType = "foo_1"; bp.ContentEncoding = "foo_2"; bp.Headers = new Dictionary - { - { "foo_3", "foo_4" }, - { "foo_5", "foo_6" } - }; + { + { "foo_3", "foo_4" }, + { "foo_5", "foo_6" } + }; bp.DeliveryMode = 2; // Persistent also changes DeliveryMode's value to 2 bp.Persistent = true; @@ -123,6 +123,7 @@ private void TestBasicPropertiesClone(BasicProperties bp) Assert.AreEqual(12, bpClone.Priority); Assert.AreEqual("foo_7", bpClone.CorrelationId); Assert.AreEqual("foo_8", bpClone.ReplyTo); + Assert.AreEqual(null, bpClone.ReplyToAddress); Assert.AreEqual("foo_9", bpClone.Expiration); Assert.AreEqual("foo_10", bpClone.MessageId); Assert.AreEqual(new AmqpTimestamp(123), bpClone.Timestamp); @@ -141,10 +142,10 @@ private void TestBasicPropertiesNoneClone(BasicProperties bp) bp.ContentType = "foo_1"; bp.ContentEncoding = "foo_2"; bp.Headers = new Dictionary - { - { "foo_3", "foo_4" }, - { "foo_5", "foo_6" } - }; + { + { "foo_3", "foo_4" }, + { "foo_5", "foo_6" } + }; bp.DeliveryMode = 2; // Persistent also changes DeliveryMode's value to 2 bp.Persistent = true; diff --git a/projects/Unit/TestPublicationAddress.cs b/projects/Unit/TestPublicationAddress.cs index 0d9b7ca924..7e9661e3e1 100644 --- a/projects/Unit/TestPublicationAddress.cs +++ b/projects/Unit/TestPublicationAddress.cs @@ -59,6 +59,7 @@ public void TestParseOk() [Test] public void TestParseFail() { + Assert.IsNull(PublicationAddress.Parse(null)); Assert.IsNull(PublicationAddress.Parse("not a valid uri")); }