Skip to content

Add unit tests for SSL/TLS#413

Merged
josesimoes merged 5 commits into
nanoframework:mainfrom
josesimoes:add-unit-tests
Jul 22, 2026
Merged

Add unit tests for SSL/TLS#413
josesimoes merged 5 commits into
nanoframework:mainfrom
josesimoes:add-unit-tests

Conversation

@josesimoes

Copy link
Copy Markdown
Member

Description

  • Add tests to connect to HTTPS hosts.
  • Update companion app to allow testing secure server and certs.

Motivation and Context

  • Need unit tests in this lib and some automation to test SSL/TLS features.

How Has This Been Tested?

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dependencies (update dependencies and changes associated, has no impact on code or features)
  • Unit Tests (add new Unit Test(s) or improved existing one(s), has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist:

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).
  • I have added new tests to cover my changes.

- Add tests to connect to HTTPS hosts.
- Update companion app to allow testing secure server and certs.
- Add tests to connect to HTTPS hosts.
- Update companion app to allow testing secure server and certs.
@coderabbitai

coderabbitai Bot commented Jul 17, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@josesimoes, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 55 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 52a2860d-2e7d-473a-8615-e75e0205b881

📥 Commits

Reviewing files that changed from the base of the PR and between 631e540 and 9c7f8e8.

📒 Files selected for processing (11)
  • Tests/IPAddressTests/IPAddressTests.nfproj
  • Tests/NetworkHelperTests/NetworkHelperTests.nfproj
  • Tests/NetworkTestCompanion/Program.cs
  • Tests/SocketTests/SocketTests.nfproj
  • Tests/SslStreamTests/SslStreamTests.nfproj
  • Tests/SslStreamTests/packages.lock.json
  • Tests/SslStreamTests_WiFi/SslClientTests.cs
  • Tests/SslStreamTests_WiFi/SslServerTests.cs
  • Tests/SslStreamTests_WiFi/SslStreamTests_WiFi.nfproj
  • Tests/SslStreamTests_WiFi/packages.config
  • Tests/SslStreamTests_WiFi/packages.lock.json
📝 Walkthrough

Walkthrough

Adds TLS echo and connection commands to the Network Test Companion, introduces shared TLS test infrastructure, and adds WiFi and non-WiFi SslStream test projects covering TLS client validation, server handshakes, echo exchanges, connection reuse, and peer-closure behavior.

Changes

TLS stream testing

Layer / File(s) Summary
Companion TLS control and echo infrastructure
Tests/NetworkTestCompanion/*, Tests/SocketTests/CompanionClient.cs
The companion now supports TLS connection and echo commands, certificate-backed TLS echo serving, asynchronous TLS clients, and newline-delimited command responses.
TLS test project and build wiring
.gitignore, .runsettings, Tests/SslStreamTests/..., Tests/SslStreamTests_WiFi/..., nanoFramework.System.Net.sln
Adds both SslStream test projects, package and assembly metadata, generated companion configuration, solution wiring, broader configuration-file ignores, and a longer test-session timeout.
TLS client interoperability coverage
Tests/SslStreamTests_WiFi/SslClientTests.cs
Adds TLS 1.2 and 1.3 HTTPS tests for successful handshakes, certificate verification, invalid certificates, hostname mismatches, and missing authentication settings.
TLS server and echo coverage
Tests/SslStreamTests_WiFi/SslServerTests.cs
Adds device-side TLS server/client tests for handshakes, echo exchanges, chunked payloads, peer closure, and sequential connections.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SslServerTests
  participant CompanionClient
  participant CommandServer
  participant TlsEchoServer
  SslServerTests->>CompanionClient: Request TLS echo or TLS connection
  CompanionClient->>CommandServer: Send JSON control command
  CommandServer->>TlsEchoServer: Start server or establish TLS client
  TlsEchoServer-->>SslServerTests: Complete TLS handshake and echo data
Loading

Possibly related PRs

Suggested labels: Type: enhancement

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the PR’s main change and meets the length and formatting requirements.
Description check ✅ Passed The description is directly related to the SSL/TLS test additions and companion app updates.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 10

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Tests/NetworkTestCompanion/CommandServer.cs`:
- Around line 302-328: Update the TLS command handlers, including TlsConnectEcho
and the background operation shown, so they expose a bounded operation
status/result that callers can query after the server flow completes instead of
returning Ok() immediately. Track handshake and read failures in that result,
compare the bytes received by TlsConnectEcho with the expected echoed payload,
and report mismatches. Apply cancellation and explicit timeouts to handshakes,
delays, and reads, ensuring cleanup completes without orphaned tasks or
connections.

In `@Tests/NetworkTestCompanion/Program.cs`:
- Line 10: Update the WellKnownTcpPorts configuration to include TCP port 7015,
preserving the existing ports and enabling --setup-firewall runs for the
companion TLS server used by DeviceAsClient_WriteAfterPeerClose_ShouldThrow.

In `@Tests/SocketTests/CompanionClient.cs`:
- Around line 106-126: Update the control-channel read around the receive loop
in CompanionClient so it performs one bounded Socket.Receive call and returns
the received response immediately, rather than retrying after partial data.
Preserve the larger receive buffer and existing UTF-8 decoding, while removing
the maxAttempts loop, newline polling, and associated sleep.

In `@Tests/SslStreamTests_WiFi/SslClientTests.cs`:
- Around line 205-210: Prevent unexpected InvalidOperationException instances
from being treated as successful outcomes in the SslClientTests exception paths.
In Tests/SslStreamTests_WiFi/SslClientTests.cs at lines 205-210, 255-261, and
304-310, remove each catch block or rethrow the caught exception after any
required logging, while preserving the expected CryptographicException handling
so TLS setup failures are reported by the test framework.
- Around line 399-419: Replace the expired InvalidCACert fixture with a
currently valid, unexpired certificate issued by a different CA, preserving the
intended mismatch with howsmyssl.com so validation specifically reports
MBEDTLS_X509_BADCERT_NOT_TRUSTED. Update the adjacent comments if the
replacement certificate’s identity changes, and keep the existing test assertion
behavior unchanged.
- Line 40: Dispose the CancellationTokenSource created in SslClientTests by
wrapping its usage in an appropriate disposal scope or otherwise ensuring
disposal after the test completes, while preserving the existing cancellation
behavior and 60-second timeout.

In `@Tests/SslStreamTests_WiFi/SslServerTests.cs`:
- Around line 43-48: Remove the unconditional Assert.SkipTest call from Setup so
the SSL server tests execute in committed builds. Replace it with the
repository’s existing test-configuration or hardware-environment gate, allowing
tests to skip automatically only when the required real hardware or companion is
unavailable.
- Around line 159-168: Update both TLS read assertions in
Tests/SslStreamTests_WiFi/SslServerTests.cs at lines 159-168 and 215-223 to
accumulate reads until testData.Length and sent.Length bytes are received,
respectively, or stop on EOF. Perform byte comparisons only after the expected
payload is complete, preserving the existing exact-length and content
validations.
- Around line 390-406: Update the exception handling in the
Assert.ThrowsException write loop to normalize only the documented
SocketException into the expected IOException. Allow existing IOException
instances to propagate unchanged, and do not catch or convert other exception
types so unrelated failures still fail the test.

In `@Tests/SslStreamTests_WiFi/SslStreamTests_WiFi.nfproj`:
- Around line 38-63: Update all NuGet dependencies to the latest versions by
running nuget update for both test projects and committing the generated
changes. Update package references in
Tests/SslStreamTests_WiFi/SslStreamTests_WiFi.nfproj (38-63),
Tests/SslStreamTests/SslStreamTests.nfproj (41-60), and package versions in
Tests/SslStreamTests/packages.config (1-8); also update the corresponding WiFi
packages.config and both packages.lock.json files.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 1e82e5b1-2fb2-4cf1-a2f5-971e8e055d4d

📥 Commits

Reviewing files that changed from the base of the PR and between de70dce and 631e540.

📒 Files selected for processing (17)
  • .gitignore
  • .runsettings
  • Tests/NetworkTestCompanion/CommandServer.cs
  • Tests/NetworkTestCompanion/Program.cs
  • Tests/NetworkTestCompanion/TestCertificates.cs
  • Tests/NetworkTestCompanion/TlsEchoServer.cs
  • Tests/SocketTests/CompanionClient.cs
  • Tests/SslStreamTests/Properties/AssemblyInfo.cs
  • Tests/SslStreamTests/SslStreamTests.nfproj
  • Tests/SslStreamTests/packages.config
  • Tests/SslStreamTests_WiFi/Properties/AssemblyInfo.cs
  • Tests/SslStreamTests_WiFi/SslClientTests.cs
  • Tests/SslStreamTests_WiFi/SslServerTests.cs
  • Tests/SslStreamTests_WiFi/SslStreamTests_WiFi.nfproj
  • Tests/SslStreamTests_WiFi/TestConfiguration.cs
  • Tests/SslStreamTests_WiFi/packages.config
  • nanoFramework.System.Net.sln

Comment thread Tests/NetworkTestCompanion/CommandServer.cs
Comment thread Tests/NetworkTestCompanion/Program.cs Outdated
Comment thread Tests/SocketTests/CompanionClient.cs
Comment thread Tests/SslStreamTests_WiFi/SslClientTests.cs
Comment thread Tests/SslStreamTests_WiFi/SslClientTests.cs Outdated
Comment thread Tests/SslStreamTests_WiFi/SslClientTests.cs
Comment thread Tests/SslStreamTests_WiFi/SslServerTests.cs
Comment thread Tests/SslStreamTests_WiFi/SslServerTests.cs Outdated
Comment thread Tests/SslStreamTests_WiFi/SslServerTests.cs
Comment thread Tests/SslStreamTests_WiFi/SslStreamTests_WiFi.nfproj
@josesimoes

Copy link
Copy Markdown
Member Author

Expected failure on nugets check in WiFi test project: we're ferencing System.Net directly.

@josesimoes
josesimoes merged commit de6f604 into nanoframework:main Jul 22, 2026
7 of 8 checks passed
@josesimoes
josesimoes deleted the add-unit-tests branch July 22, 2026 11:56
josesimoes added a commit that referenced this pull request Jul 22, 2026
- From #413

***NO_CI***
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants