Skip to content
Open
Show file tree
Hide file tree
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
20 changes: 11 additions & 9 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ on:

permissions:
pull-requests: read # allows SonarCloud to decorate PRs with analysis results
contents: read


jobs:
sonar-check:
Expand All @@ -56,8 +58,8 @@ jobs:
dotnet tool install --global dotnet-sonarscanner
echo "$env:USERPROFILE\.dotnet\tools" >> $env:GITHUB_PATH
dotnet sonarscanner begin `
/k:"ppanchen_NetSdrClient" `
/o:"ppanchen" `
/k:"slavik22_ReengineeringCourse" `
/o:"slavik22" `
/d:sonar.token="${{ secrets.SONAR_TOKEN }}" `
/d:sonar.cs.opencover.reportsPaths="**/coverage.xml" `
/d:sonar.cpd.cs.minimumTokens=40 `
Expand All @@ -70,13 +72,13 @@ jobs:
run: dotnet restore NetSdrClient.sln
- name: Build
run: dotnet build NetSdrClient.sln -c Release --no-restore
#- name: Tests with coverage (OpenCover)
# run: |
# dotnet test NetSdrClientAppTests/NetSdrClientAppTests.csproj -c Release --no-build `
# /p:CollectCoverage=true `
# /p:CoverletOutput=TestResults/coverage.xml `
# /p:CoverletOutputFormat=opencover
# shell: pwsh
- name: Tests with coverage (OpenCover)
run: |
dotnet test NetSdrClientAppTests/NetSdrClientAppTests.csproj -c Release --no-build `
/p:CollectCoverage=true `
/p:CoverletOutput=TestResults/coverage.xml `
/p:CoverletOutputFormat=opencover
shell: pwsh
# 3) END: SonarScanner
- name: SonarScanner End
run: dotnet sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
Expand Down
10 changes: 2 additions & 8 deletions NetSdrClientApp/Messages/NetSdrMessageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;


namespace NetSdrClientApp.Messages
{
//TODO: analyze possible use of [StructLayout] for better performance and readability
Expand Down Expand Up @@ -83,7 +77,7 @@ public static bool TranslateMessage(byte[] msg, out MsgTypes type, out ControlIt
msgEnumarable = msgEnumarable.Skip(_msgControlItemLength);
msgLength -= _msgControlItemLength;

if (Enum.IsDefined(typeof(ControlItemCodes), value))
if (Enum.IsDefined(typeof(ControlItemCodes), (int)value))
{
itemCode = (ControlItemCodes)value;
}
Expand Down
8 changes: 0 additions & 8 deletions NetSdrClientApp/NetSdrClient.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
using NetSdrClientApp.Messages;
using NetSdrClientApp.Networking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using static NetSdrClientApp.Messages.NetSdrMessageHelper;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace NetSdrClientApp
{
Expand Down
9 changes: 1 addition & 8 deletions NetSdrClientApp/Networking/ITcpClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace NetSdrClientApp.Networking
namespace NetSdrClientApp.Networking
{
public interface ITcpClient
{
Expand Down
9 changes: 1 addition & 8 deletions NetSdrClientApp/Networking/TcpClientWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Sockets;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace NetSdrClientApp.Networking
{
Expand Down
5 changes: 1 addition & 4 deletions NetSdrClientApp/Networking/UdpClientWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System;
using System.Net;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

public class UdpClientWrapper : IUdpClient
{
Expand Down
4 changes: 4 additions & 0 deletions NetSdrClientAppTests/NetSdrClientAppTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="coverlet.msbuild" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NUnit" Version="3.14.0" />
Expand Down
24 changes: 24 additions & 0 deletions NetSdrClientAppTests/NetSdrClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,29 @@ public async Task StopIQTest()
Assert.That(_client.IQStarted, Is.False);
}

[Test]
public async Task StopIQNoConnectionTest()
{
// act — no ConnectAsync, so Connected is false
await _client.StopIQAsync();

// assert
_tcpMock.Verify(tcp => tcp.SendMessageAsync(It.IsAny<byte[]>()), Times.Never);
_tcpMock.VerifyGet(tcp => tcp.Connected, Times.AtLeastOnce);
}

[Test]
public async Task ChangeFrequencyAsyncTest()
{
// arrange
await ConnectAsyncTest(); // 3 setup messages

// act
await _client.ChangeFrequencyAsync(20_000_000, 1);

// assert — 4th SendMessageAsync for frequency change
_tcpMock.Verify(tcp => tcp.SendMessageAsync(It.IsAny<byte[]>()), Times.Exactly(4));
}

//TODO: cover the rest of the NetSdrClient code here
}
40 changes: 40 additions & 0 deletions NetSdrClientAppTests/NetSdrMessageHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@ public void GetDataItemMessageTest()
Assert.That(parametersBytes.Count(), Is.EqualTo(parametersLength));
}

[Test]
public void TranslateMessageRoundTripTest()
{
// arrange — build a SetControlItem message then parse it back
var parameters = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05 };
var msg = NetSdrMessageHelper.GetControlItemMessage(
NetSdrMessageHelper.MsgTypes.SetControlItem,
NetSdrMessageHelper.ControlItemCodes.ReceiverFrequency,
parameters);

// act
bool success = NetSdrMessageHelper.TranslateMessage(msg,
out var type, out var itemCode, out _, out var body);

// assert
Assert.That(success, Is.True);
Assert.That(type, Is.EqualTo(NetSdrMessageHelper.MsgTypes.SetControlItem));
Assert.That(itemCode, Is.EqualTo(NetSdrMessageHelper.ControlItemCodes.ReceiverFrequency));
Assert.That(body, Is.EqualTo(parameters));
}

[Test]
public void GetSamples16BitReturnsCorrectCountTest()
{
// 6 bytes → 3 samples of 16 bits
var body = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };

var samples = NetSdrMessageHelper.GetSamples(16, body).ToList();

Assert.That(samples.Count, Is.EqualTo(3));
}

[Test]
public void GetSamplesThrowsForOversizedBitsTest()
{
// 40 bits / 8 = 5 bytes > 4 → ArgumentOutOfRangeException
Assert.Throws<ArgumentOutOfRangeException>(() =>
NetSdrMessageHelper.GetSamples(40, new byte[8]).ToList());
}

//TODO: add more NetSdrMessageHelper tests
}
}