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
80 changes: 34 additions & 46 deletions .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# This workflow helps you trigger a SonarCloud analysis of your code and populates
# GitHub Code Scanning alerts with the vulnerabilities found.
# Free for open source project.

# 1. Login to SonarCloud.io using your GitHub account

# 2. Import your project on SonarCloud
# * Add your GitHub organization first, then add your repository as a new project.
# * Please note that many languages are eligible for automatic analysis,
# which means that the analysis will start automatically without the need to set up GitHub Actions.
# * This behavior can be changed in Administration > Analysis Method.
# SonarCloud аналіз для NetSdrClient (.NET 8)
#
# 3. Follow the SonarCloud in-product tutorial
# * a. Copy/paste the Project Key and the Organization Key into the args parameter below
# (You'll find this information in SonarCloud. Click on "Information" at the bottom left)
#
# * b. Generate a new token and add it to your Github repository's secrets using the name SONAR_TOKEN
# (On SonarCloud, click on your avatar on top-right > My account > Security
# or go directly to https://sonarcloud.io/account/security/)

# Feel free to take a look at our documentation (https://docs.sonarcloud.io/getting-started/github/)
# or reach out to our community forum if you need some help (https://community.sonarsource.com/c/help/sc/9)
# Перед першим запуском обов'язково:
# 1. Створити проект у SonarCloud (Analyze new project) для цього репозиторію.
# 2. У SonarCloud вимкнути Automatic Analysis (Administration -> Analysis Method).
# 3. Згенерувати User Token у SonarCloud та додати його у GitHub Secrets форку
# під іменем SONAR_TOKEN (Repo Settings -> Secrets and variables -> Actions).
# 4. Замінити нижче змінні `SONAR_PROJECT_KEY` і `SONAR_ORGANIZATION` на власні
# значення з SonarCloud (вкладка Information знизу зліва у проєкті Sonar).

name: SonarCloud analysis

Expand All @@ -36,12 +18,16 @@ on:
workflow_dispatch:

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

env:
SONAR_PROJECT_KEY: nik-bykoff_ReengineeringCourse
SONAR_ORGANIZATION: nik-bykoff

jobs:
sonar-check:
name: Sonar Check
runs-on: windows-latest # безпечно для будь-яких .NET проектів
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
with: { fetch-depth: 0 }
Expand All @@ -50,34 +36,36 @@ jobs:
with:
dotnet-version: '8.0.x'

# 1) BEGIN: SonarScanner for .NET
- name: SonarScanner Begin
run: |
dotnet tool install --global dotnet-sonarscanner
echo "$env:USERPROFILE\.dotnet\tools" >> $env:GITHUB_PATH
dotnet sonarscanner begin `
/k:"ppanchen_NetSdrClient" `
/o:"ppanchen" `
/d:sonar.token="${{ secrets.SONAR_TOKEN }}" `
/d:sonar.cs.opencover.reportsPaths="**/coverage.xml" `
/d:sonar.cpd.cs.minimumTokens=40 `
/d:sonar.cpd.cs.minimumLines=5 `
/d:sonar.exclusions=**/bin/**,**/obj/**,**/sonarcloud.yml `
/d:sonar.qualitygate.wait=true
/k:"${{ env.SONAR_PROJECT_KEY }}" `
/o:"${{ env.SONAR_ORGANIZATION }}" `
/d:sonar.token="${{ secrets.SONAR_TOKEN }}" `
/d:sonar.cs.opencover.reportsPaths="**/coverage.xml" `
/d:sonar.cpd.cs.minimumTokens=40 `
/d:sonar.cpd.cs.minimumLines=5 `
/d:sonar.exclusions=**/bin/**,**/obj/**,**/sonarcloud.yml `
/d:sonar.qualitygate.wait=true
shell: pwsh
# 2) BUILD & TEST

- name: Restore
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
# 3) END: SonarScanner

- 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 `
/p:Exclude="[NetSdrClientApp]NetSdrClientApp.Program"
shell: pwsh

- name: SonarScanner End
run: dotnet sonarscanner end /d:sonar.token="${{ secrets.SONAR_TOKEN }}"
shell: pwsh
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,9 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

# Local environment / secrets
.env
.env.*
!.env.example
82 changes: 82 additions & 0 deletions NetSdrClient.ArchTests/ArchitectureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Reflection;
using NetArchTest.Rules;

namespace NetSdrClient.ArchTests;

public class ArchitectureTests
{
private static readonly Assembly ProductionAssembly = typeof(NetSdrClientApp.NetSdrClient).Assembly;

private const string MessagesNamespace = "NetSdrClientApp.Messages";
private const string NetworkingNamespace = "NetSdrClientApp.Networking";

[Test]
public void Messages_ShouldNotDependOn_Networking()
{
var result = Types.InAssembly(ProductionAssembly)
.That()
.ResideInNamespace(MessagesNamespace)
.ShouldNot()
.HaveDependencyOn(NetworkingNamespace)
.GetResult();

Assert.That(result.IsSuccessful, Is.True,
FormatFailure(result, $"Types in {MessagesNamespace} must not reference {NetworkingNamespace}."));
}

[Test]
public void Networking_ShouldNotDependOn_Messages()
{
var result = Types.InAssembly(ProductionAssembly)
.That()
.ResideInNamespace(NetworkingNamespace)
.ShouldNot()
.HaveDependencyOn(MessagesNamespace)
.GetResult();

Assert.That(result.IsSuccessful, Is.True,
FormatFailure(result, $"Types in {NetworkingNamespace} must stay transport-only and not pull {MessagesNamespace}."));
}

[Test]
public void Interfaces_InNetworking_ShouldStartWithI()
{
var result = Types.InAssembly(ProductionAssembly)
.That()
.ResideInNamespace(NetworkingNamespace)
.And()
.AreInterfaces()
.Should()
.HaveNameStartingWith("I")
.GetResult();

Assert.That(result.IsSuccessful, Is.True,
FormatFailure(result, "Interfaces in Networking namespace must follow the I-prefix naming convention."));
}

[Test]
public void NetworkingWrappers_ShouldBeSealed()
{
var result = Types.InAssembly(ProductionAssembly)
.That()
.ResideInNamespace(NetworkingNamespace)
.And()
.HaveNameEndingWith("Wrapper")
.Should()
.BeSealed()
.GetResult();

Assert.That(result.IsSuccessful, Is.True,
FormatFailure(result, "Wrapper classes in Networking namespace should be sealed to prevent unintended subclassing."));
}

private static string FormatFailure(TestResult result, string headline)
{
if (result.FailingTypeNames is null || result.FailingTypeNames.Count == 0)
{
return headline;
}

return headline + " Failing types: " + string.Join(", ", result.FailingTypeNames);
}
}
29 changes: 29 additions & 0 deletions NetSdrClient.ArchTests/NetSdrClient.ArchTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
</ItemGroup>

<ItemGroup>
<Using Include="NUnit.Framework" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetSdrClientApp\NetSdrClientApp.csproj" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions NetSdrClient.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetSdrClientAppTests", "Net
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EchoServer", "EchoTcpServer\EchoServer.csproj", "{9179F2F7-EBEE-4A5D-9FD9-F6E3C18DD263}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetSdrClient.ArchTests", "NetSdrClient.ArchTests\NetSdrClient.ArchTests.csproj", "{400ACC47-CE06-42B1-8C2C-6C08296F74CA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -27,6 +29,10 @@ Global
{9179F2F7-EBEE-4A5D-9FD9-F6E3C18DD263}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9179F2F7-EBEE-4A5D-9FD9-F6E3C18DD263}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9179F2F7-EBEE-4A5D-9FD9-F6E3C18DD263}.Release|Any CPU.Build.0 = Release|Any CPU
{400ACC47-CE06-42B1-8C2C-6C08296F74CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{400ACC47-CE06-42B1-8C2C-6C08296F74CA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{400ACC47-CE06-42B1-8C2C-6C08296F74CA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{400ACC47-CE06-42B1-8C2C-6C08296F74CA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
29 changes: 13 additions & 16 deletions NetSdrClientApp/Messages/NetSdrMessageHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Threading.Tasks;

namespace NetSdrClientApp.Messages
{
Expand Down Expand Up @@ -83,7 +80,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 All @@ -108,23 +105,23 @@ public static bool TranslateMessage(byte[] msg, out MsgTypes type, out ControlIt

public static IEnumerable<int> GetSamples(ushort sampleSize, byte[] body)
{
sampleSize /= 8; //to bytes
if (sampleSize > 4)
int sampleSizeBytes = sampleSize / 8;
if (sampleSizeBytes > 4)
{
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(sampleSize));
}

var bodyEnumerable = body as IEnumerable<byte>;
var prefixBytes = Enumerable.Range(0, 4 - sampleSize)
.Select(b => (byte)0);
if (sampleSizeBytes == 0)
{
yield break;
}

while (bodyEnumerable.Count() >= sampleSize)
var buffer = new byte[4];
for (int offset = 0; offset + sampleSizeBytes <= body.Length; offset += sampleSizeBytes)
{
yield return BitConverter.ToInt32(bodyEnumerable
.Take(sampleSize)
.Concat(prefixBytes)
.ToArray());
bodyEnumerable = bodyEnumerable.Skip(sampleSize);
Array.Clear(buffer, 0, buffer.Length);
Array.Copy(body, offset, buffer, 0, sampleSizeBytes);
yield return BitConverter.ToInt32(buffer);
}
}

Expand Down
Loading