Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 88 additions & 18 deletions NetSdrClientAppTests/NetSdrMessageHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,12 @@
var headerBytes = msg.Take(2);
var num = BitConverter.ToUInt16(headerBytes.ToArray());
var actualType = (NetSdrMessageHelper.MsgTypes)(num >> 13);
var actualLength = num - ((int)actualType << 13);
var actualLength = num & 0x1FFF; // Use mask for clarity

Assert.That(msg.Length, Is.EqualTo(actualLength));
// TranslateHeader logic:
// msgLength = 7500 + 2 = 7502 (Length in header)
Assert.That(msg.Length, Is.EqualTo(7502));

Check warning on line 71 in NetSdrClientAppTests/NetSdrMessageHelperTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use Assert.That(<collection>, Has.Length.EqualTo(<value>)

See more on https://sonarcloud.io/project/issues?id=YehorYurch5_NetSdrClient&issues=AZqywnRaWG7wpWAYlvz2&open=AZqywnRaWG7wpWAYlvz2&pullRequest=14

Check warning on line 71 in NetSdrClientAppTests/NetSdrMessageHelperTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Call independent Assert statements from inside an Assert.Multiple

See more on https://sonarcloud.io/project/issues?id=YehorYurch5_NetSdrClient&issues=AZqywnRaWG7wpWAYlvz1&open=AZqywnRaWG7wpWAYlvz1&pullRequest=14
Assert.That(actualLength, Is.EqualTo(7502));
Assert.That(type, Is.EqualTo(actualType));
}

Expand Down Expand Up @@ -98,13 +101,16 @@
// Assert: Check that the actual length in the header is 0
var headerBytes = msg.Take(2).ToArray();
var num = BitConverter.ToUInt16(headerBytes);
var actualLength = num - ((int)NetSdrMessageHelper.MsgTypes.DataItem0 << 13);

Assert.That(actualLength, Is.EqualTo(0));
// Extract type and length from header
var actualType = (NetSdrMessageHelper.MsgTypes)(num >> 13);
var actualLengthInHeader = num & 0x1FFF;

Assert.That(actualLengthInHeader, Is.EqualTo(0)); // Length field in header should be 0

Check warning on line 109 in NetSdrClientAppTests/NetSdrMessageHelperTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Call independent Assert statements from inside an Assert.Multiple

See more on https://sonarcloud.io/project/issues?id=YehorYurch5_NetSdrClient&issues=AZqywnRaWG7wpWAYlvz3&open=AZqywnRaWG7wpWAYlvz3&pullRequest=14
Assert.That(msg.Length, Is.EqualTo(8194)); // Actual physical length is 8194

Check warning on line 110 in NetSdrClientAppTests/NetSdrMessageHelperTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use Assert.That(<collection>, Has.Length.EqualTo(<value>)

See more on https://sonarcloud.io/project/issues?id=YehorYurch5_NetSdrClient&issues=AZqywnRaWG7wpWAYlvz4&open=AZqywnRaWG7wpWAYlvz4&pullRequest=14
Assert.That(actualType, Is.EqualTo(NetSdrMessageHelper.MsgTypes.DataItem0));
}

// ------------------------------------------------------------------
// TRANSLATE MESSAGE TESTS (Decoding coverage)
// ------------------------------------------------------------------

[Test]
Expand All @@ -124,30 +130,35 @@
Assert.That(actualType, Is.EqualTo(type));
Assert.That(actualCode, Is.EqualTo(code));
Assert.That(body, Is.EqualTo(parameters));
Assert.That(body.Length, Is.EqualTo(parameters.Length));
Assert.That(sequenceNumber, Is.EqualTo(0));
}

/* [Test]
public void TranslateMessage_ShouldDecodeDataItem()
[Test]
public void TranslateMessage_ShouldDecodeDataItemCorrectly()
{
// Test removed due to persistent failure indicating mismatch
// between expected body length and actual decoded body length
// after sequence number extraction.

// Arrange: Create a test message with DataItem (DataItem0)
var type = NetSdrMessageHelper.MsgTypes.DataItem0;
byte[] parameters = { 0xAA, 0xBB, 0xCC };
ushort expectedSequenceNumber = 0xABCD;
byte[] dataPayload = { 0xAA, 0xBB, 0xCC };

// Combine SequenceNumber (2 bytes) and Payload (3 bytes) into the parameters for GetMessage
byte[] parameters = BitConverter.GetBytes(expectedSequenceNumber).Concat(dataPayload).ToArray();

byte[] msg = NetSdrMessageHelper.GetDataItemMessage(type, parameters);

// Act
bool success = NetSdrMessageHelper.TranslateMessage(msg, out var actualType, out var actualCode, out var sequenceNumber, out var body);

// Assert
Assert.That(success, Is.True);

Check warning on line 153 in NetSdrClientAppTests/NetSdrMessageHelperTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Call independent Assert statements from inside an Assert.Multiple

See more on https://sonarcloud.io/project/issues?id=YehorYurch5_NetSdrClient&issues=AZqywnRaWG7wpWAYlvz5&open=AZqywnRaWG7wpWAYlvz5&pullRequest=14
Assert.That(actualType, Is.EqualTo(type));
Assert.That(actualCode, Is.EqualTo(NetSdrMessageHelper.ControlItemCodes.None));
Assert.That(body, Is.EqualTo(parameters));
} */
Assert.That(sequenceNumber, Is.EqualTo(expectedSequenceNumber));

// The decoded body should only contain the dataPayload (3 bytes)
Assert.That(body, Is.EqualTo(dataPayload));
Assert.That(body.Length, Is.EqualTo(dataPayload.Length));

Check warning on line 160 in NetSdrClientAppTests/NetSdrMessageHelperTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use Assert.That(<collection>, Has.Length.EqualTo(<value>)

See more on https://sonarcloud.io/project/issues?id=YehorYurch5_NetSdrClient&issues=AZqywnRaWG7wpWAYlvz6&open=AZqywnRaWG7wpWAYlvz6&pullRequest=14
}

[Test]
public void TranslateMessage_ShouldFailOnInvalidBodyLength()
Expand All @@ -169,9 +180,10 @@
{
// Arrange: Generate a Control message type but insert a non-existent code (0xFFFF)
var type = NetSdrMessageHelper.MsgTypes.SetControlItem;
byte[] header = BitConverter.GetBytes((ushort)((int)type << 13 | (2 + 2))); // Length 4 (header + code)
// Total length: Header (2) + Invalid Code (2) + Parameters (2) = 6
byte[] header = BitConverter.GetBytes((ushort)((int)type << 13 | (6)));
byte[] invalidCode = BitConverter.GetBytes((ushort)0xFFFF); // Code not defined in Enum
byte[] msg = header.Concat(invalidCode).Concat(new byte[2]).ToArray(); // Total length 6
byte[] msg = header.Concat(invalidCode).Concat(new byte[2]).ToArray();

// Act
bool success = NetSdrMessageHelper.TranslateMessage(msg, out var actualType, out var actualCode, out var sequenceNumber, out var body);
Expand All @@ -180,6 +192,50 @@
Assert.That(success, Is.False);
}

[Test]
public void TranslateMessage_ShouldFailOnMessageShorterThanHeader()
{
// Arrange: Only 1 byte is provided (min header is 2 bytes)
byte[] shortMsg = { 0x01 };

// Act
bool success = NetSdrMessageHelper.TranslateMessage(shortMsg, out var actualType, out var actualCode, out var sequenceNumber, out var body);

// Assert
Assert.That(success, Is.False);
}

[Test]
public void TranslateMessage_ShouldFailOnControlBodyTooShort()
{
// Arrange: Control item type, but body length is 1 byte (requires 2 for code)
var type = NetSdrMessageHelper.MsgTypes.SetControlItem;
byte[] header = BitConverter.GetBytes((ushort)((int)type << 13 | (2 + 1))); // Total message length 3 (header + 1 byte body)
byte[] msg = header.Concat(new byte[] { 0xAA }).ToArray();

// Act
bool success = NetSdrMessageHelper.TranslateMessage(msg, out var actualType, out var actualCode, out var sequenceNumber, out var body);

// Assert: Should fail because remainingLength < _msgControlItemLength
Assert.That(success, Is.False);
}

[Test]
public void TranslateMessage_ShouldFailOnDataBodyTooShort()
{
// Arrange: Data item type, but body length is 1 byte (requires 2 for sequence number)
var type = NetSdrMessageHelper.MsgTypes.DataItem0;
byte[] header = BitConverter.GetBytes((ushort)((int)type << 13 | (2 + 1))); // Total message length 3 (header + 1 byte body)
byte[] msg = header.Concat(new byte[] { 0xAA }).ToArray();

// Act
bool success = NetSdrMessageHelper.TranslateMessage(msg, out var actualType, out var actualCode, out var sequenceNumber, out var body);

// Assert: Should fail because remainingLength < _msgSequenceNumberLength
Assert.That(success, Is.False);
}


// ------------------------------------------------------------------
// GET SAMPLES TESTS
// ------------------------------------------------------------------
Expand Down Expand Up @@ -239,5 +295,19 @@
// Assert: Should return an empty array
Assert.That(samples.Length, Is.EqualTo(0));
}

[Test]
public void GetSamples_ShouldHandleEmptyBody()
{
// Arrange
ushort sampleSize = 16;
byte[] emptyBody = Array.Empty<byte>();

// Act
var samples = NetSdrMessageHelper.GetSamples(sampleSize, emptyBody).ToArray();

// Assert
Assert.That(samples, Is.Empty);
}
}
}
Loading