Skip to content

Commit

Permalink
Add missing ReadUuid. (#367)
Browse files Browse the repository at this point in the history
* Add missing ReadUuid.

* Add AmqpParseException test for ReadUuid.

* Add unit test for ReadUuid.

---------

Co-authored-by: 韓邦傑 <u338956@taipower.com.tw>
  • Loading branch information
bangjiehan and 韓邦傑 committed Mar 16, 2024
1 parent 29c5474 commit 18e63d1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
17 changes: 17 additions & 0 deletions RabbitMQ.Stream.Client/AMQP/AmqpWireFormattingRead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ internal static int ReadAny(ref SequenceReader<byte> reader, out object value)
offset = ReadBinary(ref reader, out var resultBin);
value = resultBin;
return offset;
case FormatCode.UUID:
offset = ReadUuid(ref reader, out var uuid);
value = uuid;
return offset;
case FormatCode.Null:
value = null;
reader.Advance(1);
Expand Down Expand Up @@ -444,6 +448,19 @@ internal static int ReadUByte(ref SequenceReader<byte> reader, out byte value)
throw new AmqpParseException($"ReadUbyte Invalid type {type}");
}

internal static int ReadUuid(ref SequenceReader<byte> reader, out byte[] value)
{
var offset = ReadType(ref reader, out var type);
switch (type)
{
case FormatCode.UUID:
offset += WireFormatting.ReadBytes(ref reader, 16, out value);
return offset;
}

throw new AmqpParseException($"ReadUuid Invalid type {type}");
}

internal static int TryReadNull(ref SequenceReader<byte> reader, out bool value)
{
PeekType(ref reader, out var type);
Expand Down
36 changes: 31 additions & 5 deletions Tests/Amqp10Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Buffers;
using System.Linq;
using System.Text;
using RabbitMQ.Stream.Client;
using RabbitMQ.Stream.Client.AMQP;
Expand Down Expand Up @@ -133,6 +134,12 @@ public void ReadsThrowsExceptionInvalidType()
AmqpWireFormatting.ReadInt16(ref reader, out var value);
});

Assert.Throws<AmqpParseException>(() =>
{
var reader = new SequenceReader<byte>(new ReadOnlySequence<byte>(data));
AmqpWireFormatting.ReadUuid(ref reader, out var value);
});

System.Diagnostics.Trace.WriteLine(" test passed");
}

Expand Down Expand Up @@ -340,7 +347,7 @@ public void ValidateMessagesFromGo()
Assert.NotNull(msgProp500.ApplicationProperties);

// body len >= 900 bytes
// ApplicationProperties are not null
// ApplicationProperties are not null
// Properties is not null
var prop900 = SystemUtils.GetFileContent("message_random_application_properties_properties_900");
reader = new SequenceReader<byte>(new ReadOnlySequence<byte>(prop900));
Expand All @@ -355,15 +362,16 @@ public void ValidateMessagesFromGo()

Assert.NotNull(msgProp900.Properties);
Assert.True(!string.IsNullOrEmpty(msgProp900.Properties.ReplyTo));
Assert.True(!string.IsNullOrEmpty(msgProp900.Properties.ContentEncoding));
Assert.True(!string.IsNullOrEmpty(msgProp900.Properties.ContentType));
Assert.True(!string.IsNullOrEmpty(msgProp900.Properties.GroupId));
Assert.Equal((ulong)33333333, msgProp900.Properties.MessageId);
// UUID value: 00112233-4455-6677-8899-aabbccddeeff
var uuid_value = Enumerable.Range(0, 16).Select(x => (byte)(x << 4 | x)).ToArray();
Assert.Equal(uuid_value, msgProp900.Properties.CorrelationId as byte[]);
Assert.Equal("json", msgProp900.Properties.ContentType);
Assert.Equal("myCoding", msgProp900.Properties.ContentEncoding);
Assert.Equal((uint)10, msgProp900.Properties.GroupSequence);
Assert.True(msgProp900.Properties.CreationTime != DateTime.MinValue);
Assert.True(msgProp900.Properties.AbsoluteExpiryTime != DateTime.MinValue);
Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(1710440652).Ticks, msgProp900.Properties.CreationTime.Ticks);
Assert.Equal(DateTimeOffset.FromUnixTimeSeconds(1710440652).Ticks, msgProp900.Properties.AbsoluteExpiryTime.Ticks);

// Test message to check if all the fields with "test" value
var staticTest = SystemUtils.GetFileContent("static_test_message_compare");
Expand Down Expand Up @@ -422,6 +430,20 @@ public void ValidateMessagesFromGoUnicode()
Assert.Equal(ByteString, msgStaticTest.ApplicationProperties["from_go_byte"]);
}

[Fact]
public void ValidateUuidMessagesFromGo()
{
// UUID value: 00112233-4455-6677-8899-aabbccddeeff
var uuid_value = Enumerable.Range(0, 16).Select(x => (byte)(x << 4 | x)).ToArray();

var buffer = SystemUtils.GetFileContent("uuid_message");
var reader = new SequenceReader<byte>(new ReadOnlySequence<byte>(buffer));
var uuid_message = Message.From(ref reader, (uint)reader.Length);
Assert.NotNull(uuid_message);
Assert.Equal(uuid_value, uuid_message.Properties.MessageId);
Assert.Equal(uuid_value, uuid_message.Properties.CorrelationId);
}

[Fact]
public void ValidateNilMessagesFromGo()
{
Expand All @@ -434,8 +456,12 @@ public void ValidateNilMessagesFromGo()
Assert.Equal(91_000_001_001, msgNilAndTypes.ApplicationProperties["long_value"]);
Assert.Equal((byte)216, msgNilAndTypes.ApplicationProperties["byte_value"]);
Assert.Equal(true, msgNilAndTypes.ApplicationProperties["bool_value"]);
Assert.Equal(1, (long)msgNilAndTypes.ApplicationProperties["int_value"]); // int in Go has a platform-dependent size.
Assert.Equal(1.1, msgNilAndTypes.ApplicationProperties["float"]);
Assert.Equal(1.1, msgNilAndTypes.ApplicationProperties["double"]);
// UUID value: 00112233-4455-6677-8899-aabbccddeeff
var uuid_value = Enumerable.Range(0, 16).Select(x => (byte)(x << 4 | x)).ToArray();
Assert.Equal(uuid_value, msgNilAndTypes.ApplicationProperties["uuid"] as byte[]);
Assert.Equal("", msgNilAndTypes.ApplicationProperties["empty"]);
}

Expand Down
Binary file modified Tests/Resources/message_random_application_properties_300
Binary file not shown.
Binary file not shown.
Binary file modified Tests/Resources/nil_and_types
Binary file not shown.
Binary file added Tests/Resources/uuid_message
Binary file not shown.

0 comments on commit 18e63d1

Please sign in to comment.