aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2021-02-12 18:35:54 +0100
committerBond_009 <bond.009@outlook.com>2021-02-12 18:35:54 +0100
commite1bc322b709756529759fbb74cb003e133f30ab6 (patch)
tree5d0498a8f3720db7eee3af7d593490bbbb9a6698
parent3a9fcb6abd249759bd44b2fbcf437beee8f611c2 (diff)
Add test for WriteGetMessage
-rw-r--r--Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunManager.cs51
-rw-r--r--tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunManagerTests.cs19
2 files changed, 48 insertions, 22 deletions
diff --git a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunManager.cs b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunManager.cs
index 188eca158..20a4d87fb 100644
--- a/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunManager.cs
+++ b/Emby.Server.Implementations/LiveTv/TunerHosts/HdHomerun/HdHomerunManager.cs
@@ -288,19 +288,14 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
internal static int WriteGetMessage(Span<byte> buffer, int tuner, string name)
{
var byteName = string.Format(CultureInfo.InvariantCulture, "/tuner{0}/{1}", tuner, name);
- int offset = WriteHeaderAndName(buffer, byteName);
-
- // calculate crc and insert at the end of the message
- var crc = Crc32.Compute(buffer.Slice(0, offset));
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset), crc);
-
- return offset + 4;
+ int offset = WriteHeaderAndPayload(buffer, byteName);
+ return FinishPacket(buffer, offset);
}
private static int WriteSetMessage(Span<byte> buffer, int tuner, string name, string value, uint? lockkey)
{
var byteName = string.Format(CultureInfo.InvariantCulture, "/tuner{0}/{1}", tuner, name);
- int offset = WriteHeaderAndName(buffer, byteName);
+ int offset = WriteHeaderAndPayload(buffer, byteName);
buffer[offset++] = GetSetValue;
offset += WriteNullTerminatedString(buffer.Slice(offset), value);
@@ -313,17 +308,14 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
offset += 4;
}
- // calculate crc and insert at the end of the message
- var crc = Crc32.Compute(buffer.Slice(0, offset));
- BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset), crc);
-
- return offset + 4;
+ return FinishPacket(buffer, offset);
}
- internal static int WriteNullTerminatedString(Span<byte> buffer, ReadOnlySpan<char> value)
+ internal static int WriteNullTerminatedString(Span<byte> buffer, ReadOnlySpan<char> payload)
{
- int len = Encoding.UTF8.GetBytes(value, buffer.Slice(1)) + 1;
+ int len = Encoding.UTF8.GetBytes(payload, buffer.Slice(1)) + 1;
+ // TODO: variable length: this can be 2 bytes if len > 127
// Write length in front of value
buffer[0] = Convert.ToByte(len);
@@ -333,21 +325,36 @@ namespace Emby.Server.Implementations.LiveTv.TunerHosts.HdHomerun
return len;
}
- private static int WriteHeaderAndName(Span<byte> buffer, ReadOnlySpan<char> payload)
+ private static int WriteHeaderAndPayload(Span<byte> buffer, ReadOnlySpan<char> payload)
{
- // insert header bytes into message
+ // Packet type
BinaryPrimitives.WriteUInt16BigEndian(buffer, GetSetRequest);
- int offset = 2;
- // Subtract 4 bytes for header and 4 bytes for crc
- BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(offset), (ushort)(payload.Length + 2));
- // insert tag name and length
+ // We write the payload length at the end
+ int offset = 4;
+
+ // Tag
buffer[offset++] = GetSetName;
- offset += WriteNullTerminatedString(buffer.Slice(offset), payload);
+
+ // Payload length + data
+ int strLen = WriteNullTerminatedString(buffer.Slice(offset), payload);
+ offset += strLen;
return offset;
}
+ private static int FinishPacket(Span<byte> buffer, int offset)
+ {
+ // Payload length
+ BinaryPrimitives.WriteUInt16BigEndian(buffer.Slice(2), (ushort)(offset - 4));
+
+ // calculate crc and insert at the end of the message
+ var crc = Crc32.Compute(buffer.Slice(0, offset));
+ BinaryPrimitives.WriteUInt32LittleEndian(buffer.Slice(offset), crc);
+
+ return offset + 4;
+ }
+
private static bool ParseReturnMessage(byte[] buf, int numBytes, out string returnVal)
{
returnVal = string.Empty;
diff --git a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunManagerTests.cs b/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunManagerTests.cs
index 15cc12d64..7e04a1ec1 100644
--- a/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunManagerTests.cs
+++ b/tests/Jellyfin.Server.Implementations.Tests/LiveTv/HdHomerunManagerTests.cs
@@ -35,5 +35,24 @@ namespace Jellyfin.Server.Implementations.Tests.LiveTv
Assert.Equal(expected.Length, len);
Assert.True(expected.SequenceEqual(buffer.Slice(0, len)));
}
+
+ [Fact]
+ public void WriteGetMessage_Valid_Success()
+ {
+ ReadOnlySpan<byte> expected = stackalloc byte[]
+ {
+ 0, 4,
+ 0, 12,
+ 3,
+ 10, (byte)'/', (byte)'t', (byte)'u', (byte)'n', (byte)'e', (byte)'r', (byte)'0', (byte)'/', (byte)'N', 0,
+ 0xc0, 0xc9, 0x87, 0x33
+ };
+
+ Span<byte> buffer = stackalloc byte[128];
+ int len = HdHomerunManager.WriteGetMessage(buffer, 0, "N");
+
+ Assert.Equal(expected.Length, len);
+ Assert.True(expected.SequenceEqual(buffer.Slice(0, len)));
+ }
}
}