Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

49.9 KB
Raw Blame
//-----------------------------------------------------------------------
// <copyright file="SmtpServerTests.cs" company="aliasvault">
// Copyright (c) aliasvault. All rights reserved.
// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------

namespace AliasVault.IntegrationTests.SmtpServer;

using System.IO.Compression;
using System.Net.Sockets;
using System.Text;
using AliasVault.Cryptography;
using AliasVault.IntegrationTests.SmtpServer.Helpers;
using AliasVault.SmtpService.Handlers;
using AliasVault.Tests.Shared;
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using MimeKit;

/// <summary>
/// SmtpServerTests class.
/// </summary>
[TestFixture]
public class SmtpServerTests
{
    /// <summary>
    /// Example public key for RSA encryption tests. This is a public key generated by the JSInterop on the client.
    /// We use this here to also test the server-side decryption implementation, even though this isn't a real-world scenario.
    /// </summary>
    public const string PublicKey = "{\"alg\":\"RSA-OAEP-256\",\"e\":\"AQAB\",\"ext\":true,\"key_ops\":[\"encrypt\"],\"kty\":\"RSA\",\"n\":\"lW8fRfSvLQiK9uZgm_kFjHMY1SedAZlVvZ_8d_d5oqWezQhan8-Y10Qvx0NMe57sQB3ePnShJFNE33w83kgRNkOyxKJ2FOVKtRptd7CgwIt_l9TPjdrB0J0hFn9b1eit2vpQlOdP_Wa8WvW2eVdXYEMWuBU4-aj8vY2qzcmBc-HhJX-Me9oXhUscJxeqMP4_sNiN7D4I0enrmYicB3JQMhUIwMmNt-0srHTdSvHh_6vFZMqB9ohfh2D9Q0BzYcI8rGEy1RTYsmF1zYyoOOzeRGOcKCVNeLO9LZxfAdm1Eq5zv47uw543cxCZXIZPlXOVriMEtTRwaGzE_3RZmpGJqw\"}";

    /// <summary>
    /// Example private key for RSA encryption tests. This is a private key generated by the JSInterop on the client.
    /// We use this here to also test the server-side decryption implementation, even though this isn't a real-world scenario.
    /// </summary>
    public const string PrivateKey = "{\"alg\":\"RSA-OAEP-256\",\"d\":\"KLByToUaseNym1oNkkrTRPQOHfREXywWWaTXhP8AwtXgEKomqv9G-c6aR-K-T6btY2P-oPj268I0rbnRhSEQdrsmUT5_cp8goYGJrx6MFwGlA32x6klXnus6GDsjkXJi7I5eJL17XV99CDOBtTagFxkNdaBpvClUcHTDvncQ5bGAIrNqS7KADoi-E19BxiW_GcSJiVT4H8kDHCkcgTjZx4rKJjTPqqJOLg_poDrvnTJbsjcXP80kQ1AAENRAvDGhSWzP0IYtP1DM_2FzM1s1b_SrUsS3KiO8drR2Kv-PSOvncpaNVnZGElGCraJ3B2Mm-dr3vFjkyWeWPceqyhtYoQ\",\"dp\":\"ttxRg6uB2YLWfkPKUkzAaBWniZDHM4silJX3IgexA5GJBd9GIhUiVEolc_MgmieQbZ10CC65wqcHVv82lgCeqxYHxHWLxxJCrOpvkFlYE8wr_WqOPQEzYKv3KsL6s6Fj7Pbv9WehWpXdlbJUm4Cy5cgUkdH6PXiwBSvfhCQGrYk\",\"dq\":\"YFqlDAVTfvTR2bMJulvWzd_at81CsEmR-lPo91h-3cLpxcLDOlrTP-d3Ass2I4r1PtBT1bKuuHeQ6fZmHH55a6m8XxPEs2BuIxlh9RiFfWbd66969UOnItuawf0rfGneKt1zl4st60T3KXd8-ECrLxdsvOYpOEuNzvIY_b3qitE\",\"e\":\"AQAB\",\"ext\":true,\"key_ops\":[\"decrypt\"],\"kty\":\"RSA\",\"n\":\"lW8fRfSvLQiK9uZgm_kFjHMY1SedAZlVvZ_8d_d5oqWezQhan8-Y10Qvx0NMe57sQB3ePnShJFNE33w83kgRNkOyxKJ2FOVKtRptd7CgwIt_l9TPjdrB0J0hFn9b1eit2vpQlOdP_Wa8WvW2eVdXYEMWuBU4-aj8vY2qzcmBc-HhJX-Me9oXhUscJxeqMP4_sNiN7D4I0enrmYicB3JQMhUIwMmNt-0srHTdSvHh_6vFZMqB9ohfh2D9Q0BzYcI8rGEy1RTYsmF1zYyoOOzeRGOcKCVNeLO9LZxfAdm1Eq5zv47uw543cxCZXIZPlXOVriMEtTRwaGzE_3RZmpGJqw\",\"p\":\"yUdbuDwmVwKhou5xXUxJfi1eOjN-5F88wtyR4LpgU2OvZ7m-er4hpXx5I2E-KTVX_iIp0Q9VDXhHH-WkN3qg20RXjRoxwgrggYbfdIYdrB-2kbMamq5cOf2XbXGEO8PoDXYoZprIB0EhrD4qVVykPUYg5El0hIKPdfs9LNoOEzs\",\"q\":\"vg93lGTurG0EY179tPr6Qe3ttKEN9zvQ97dZ9034DOWDoWLe-iMKG1-yKmkG4uwC8QqNnm1mPz7EqOuHPPGVTTib9NA4JdM27PUHSPKDUvp0cV4LhF6e-W7tMFk8WbJ2ACqkqhZHYgm-FDkZBCpnehNegTxipLluKa79G__ZHFE\",\"qi\":\"fnI3Wh5aYuxI0R18NTeFKjo1P7_Ck65Gc9O3CmeqiIe58EJaXQEcdwdSOG8aVmn03szXLHEnp7anNIH63f0ericbRYdCQVhcQpvsXzEM_sp4aYmwz45palrjlY4Jc6G6XQn3FwiqqRDvpnXdsunnQ62HHhxmslaEMYHQyLng2ss\"}";

    /// <summary>
    /// The test host instance.
    /// </summary>
    private IHost _testHost;

    /// <summary>
    /// The test host builder instance.
    /// </summary>
    private TestHostBuilder _testHostBuilder;

    /// <summary>
    /// The personal group of the seeded test user, which owns the anonymized sender counts.
    /// </summary>
    private Guid _personalGroupId;

    /// <summary>
    /// Setup logic for every test.
    /// </summary>
    /// <returns>Task.</returns>
    [SetUp]
    public async Task Setup()
    {
        _testHostBuilder = new TestHostBuilder();
        _testHost = _testHostBuilder.Build();

        await _testHost.StartAsync();

        // Create an AliasVault user with personal group, manifest and primary delivery key (public key).
        var dbContext = _testHostBuilder.GetDbContext();
        var testUser = await TestUserSeeder.CreateTestUserAsync(dbContext, "testuser", "[email protected]", PublicKey);
        _personalGroupId = testUser.PersonalGroup.Id;

        // Create email claims linked to the user's personal manifest so delivery can resolve the primary delivery key.
        dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, "[email protected]"));
        dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, "[email protected]"));

        // Create disabled email claim.
        dbContext.EmailClaims.Add(TestUserSeeder.CreateEmailClaim(testUser.Manifest.ManifestId, "[email protected]", disabled: true));

        await dbContext.SaveChangesAsync();
    }

    /// <summary>
    /// Tear down logic for every test.
    /// </summary>
    /// <returns>Task.</returns>
    [TearDown]
    public async Task TearDown()
    {
        await _testHost.StopAsync();
        _testHost.Dispose();
        await _testHostBuilder.DisposeAsync();
    }

    /// <summary>
    /// The SMTP greeting line must include the configured advertised hostname (same path as production resolver).
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task SmtpBanner_ContainsConfiguredHostname()
    {
        using var client = new TcpClient();
        const int maxRetries = 10;
        const int retryDelayMs = 100;

        for (var attempt = 1; attempt <= maxRetries; attempt++)
        {
            try
            {
                await client.ConnectAsync("127.0.0.1", 2525);
                break;
            }
            catch (SocketException) when (attempt < maxRetries)
            {
                await Task.Delay(retryDelayMs);
            }
        }

        using var stream = client.GetStream();
        var buffer = new byte[512];
        var n = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length));
        var line = Encoding.ASCII.GetString(buffer, 0, n);

        Assert.Multiple(() =>
        {
            Assert.That(line, Does.StartWith("220 "));
            Assert.That(line, Does.Contain(TestHostBuilder.IntegrationAdvertisedHostname));
        });
    }

    /// <summary>
    /// RCPT TO for external domains should be rejected immediately with a 550 relay denial.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task RcptToExternalDomain_IsRejectedAtRcptStage()
    {
        using var client = await ConnectRawSmtpClient();
        using var stream = client.GetStream();

        _ = await ReadSmtpLineAsync(stream); // Greeting
        Assert.That(await SendSmtpCommandAsync(stream, "HELO localhost"), Does.StartWith("250"));
        Assert.That(await SendSmtpCommandAsync(stream, "MAIL FROM:<[email protected]>"), Does.StartWith("250"));

        var rcptResponse = await SendSmtpCommandAsync(stream, "RCPT TO:<[email protected]>");
        Assert.Multiple(() =>
        {
            Assert.That(rcptResponse, Does.StartWith("550"));
            Assert.That(rcptResponse.ToLowerInvariant(), Does.Contain("relay not permitted"));
        });
    }

    /// <summary>
    /// RCPT TO for managed domains should be accepted regardless of alias existence (privacy protection).
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task RcptToManagedDomain_IsAcceptedAtRcptStage()
    {
        using var client = await ConnectRawSmtpClient();
        using var stream = client.GetStream();

        _ = await ReadSmtpLineAsync(stream); // Greeting
        Assert.That(await SendSmtpCommandAsync(stream, "HELO localhost"), Does.StartWith("250"));
        Assert.That(await SendSmtpCommandAsync(stream, "MAIL FROM:<[email protected]>"), Does.StartWith("250"));

        var rcptResponse = await SendSmtpCommandAsync(stream, "RCPT TO:<[email protected]>");
        Assert.That(rcptResponse, Does.StartWith("250"));
    }

    /// <summary>
    /// EHLO should advertise the maximum message size so compliant senders refuse oversized mail up front.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task Ehlo_AdvertisesMaxMessageSize()
    {
        using var client = await ConnectRawSmtpClient();
        using var stream = client.GetStream();

        _ = await ReadSmtpLineAsync(stream); // Greeting
        var ehloResponse = await SendSmtpCommandAsync(stream, "EHLO localhost");
        Assert.That(ehloResponse, Does.Contain($"SIZE {DatabaseMessageStore.MaxEmailSizeInBytes}"));
    }

    /// <summary>
    /// A DATA section larger than the maximum message size should end the session without storing anything.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task OversizedData_IsRejectedAndNotStored()
    {
        using var client = await ConnectRawSmtpClient();
        using var stream = client.GetStream();

        _ = await ReadSmtpLineAsync(stream); // Greeting
        Assert.That(await SendSmtpCommandAsync(stream, "HELO localhost"), Does.StartWith("250"));
        Assert.That(await SendSmtpCommandAsync(stream, "MAIL FROM:<[email protected]>"), Does.StartWith("250"));
        Assert.That(await SendSmtpCommandAsync(stream, "RCPT TO:<[email protected]>"), Does.StartWith("250"));
        Assert.That(await SendSmtpCommandAsync(stream, "DATA"), Does.StartWith("354"));

        // Stream twice the limit; the server may close the connection before everything is written.
        var line = Encoding.ASCII.GetBytes(new string('a', 998) + "\r\n");
        var linesToSend = (DatabaseMessageStore.MaxEmailSizeInBytes * 2) / line.Length;
        try
        {
            await stream.WriteAsync(Encoding.ASCII.GetBytes("Subject: oversized\r\n\r\n"));
            for (var i = 0; i < linesToSend; i++)
            {
                await stream.WriteAsync(line);
            }

            await stream.WriteAsync(Encoding.ASCII.GetBytes("\r\n.\r\n"));
            await stream.FlushAsync();
        }
        catch (IOException)
        {
            // Expected when the server ends the session mid-transfer.
        }

        var storedCount = await _testHostBuilder.GetDbContext().Emails.CountAsync();
        Assert.That(storedCount, Is.Zero);
    }

    /// <summary>
    /// Tests sending a single email in plain format to the SMTP server with valid claim to check if it is processed correctly.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task SingleEmailPlain()
    {
        // Email the SMTP server.
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email";
        const string textBody = "This is a test email plain.";
        message.Body = new BodyBuilder { TextBody = textBody }.ToMessageBody();
        await SendMessageToSmtpServer(message);

        // Check if the email is in the database.
        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.DecryptionKeys).FirstAsync();

        // Test non-encrypted fields.
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));
            Assert.That(processedEmail.AttachmentCount, Is.Zero);
        });

        // Decrypt the email and then check all individual fields.
        processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail, Is.Not.Null);
            Assert.That(processedEmail.From, Is.EqualTo("\"Test Sender\" <[email protected]>"));
            Assert.That(processedEmail.FromLocal, Is.EqualTo("sender"));
            Assert.That(processedEmail.FromDomain, Is.EqualTo("example.com"));
            Assert.That(processedEmail.MessagePreview, Is.EqualTo("This is a test email plain."));
            Assert.That(processedEmail.MessagePlain, Is.Null, "Parsed bodies should no longer be persisted; only the gzipped raw source is stored.");
            Assert.That(processedEmail.MessageHtml, Is.Null, "Parsed bodies should no longer be persisted; only the gzipped raw source is stored.");
            Assert.That(processedEmail.MessageSource, Is.Null, "The legacy text source column should no longer be populated.");
            Assert.That(processedEmail.MessageSourceBytes, Is.Not.Null, "The gzipped raw source should be stored in MessageSourceBytes.");
        });

        // The decrypted source is still gzip-compressed; gunzip it and verify the original body is present.
        var messageSource = Encoding.UTF8.GetString(Gunzip(processedEmail.MessageSourceBytes!));
        Assert.That(messageSource, Does.Contain(textBody));
    }

    /// <summary>
    /// An alias contributes its sender bucket exactly once, on the first email it receives.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task AnonymizedSenderCountedOncePerAlias()
    {
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "First"));
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "Second"));

        var dbContext = await _testHostBuilder.GetDbContextAsync();
        var group = await dbContext.Groups.FirstAsync(g => g.Id == _personalGroupId);
        var claim = await dbContext.EmailClaims.FirstAsync(c => c.Address == "[email protected]");
        var expectedBucket = AnonymizedSenderBucket.Compute(TestHostBuilder.IntegrationAbuseMetricsSalt, "example.com");
        var deliveredCount = await dbContext.Emails.CountAsync();

        Assert.Multiple(() =>
        {
            Assert.That(deliveredCount, Is.EqualTo(2), "both emails should still be delivered");
            Assert.That(group.AnonymizedEmailAliasSenderCounts, Has.Length.EqualTo(AnonymizedSenderBucket.BucketCount));
            Assert.That(group.AnonymizedEmailAliasSenderCounts.Sum(x => x), Is.EqualTo(1), "the second email must not count again");
            Assert.That(group.AnonymizedEmailAliasSenderCounts[expectedBucket], Is.EqualTo(1));
            Assert.That(claim.AnonymizedSenderCounted, Is.True, "the alias should be latched after its first email");
        });
    }

    /// <summary>
    /// Aliases contacted first by different services spread across different buckets, which is what keeps ordinary
    /// use distinguishable from a run of signups all pointed at one target.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task AnonymizedSenderCountsSpreadAcrossDistinctSenders()
    {
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "One"));
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "Two"));

        var dbContext = await _testHostBuilder.GetDbContextAsync();
        var group = await dbContext.Groups.FirstAsync(g => g.Id == _personalGroupId);
        var firstBucket = AnonymizedSenderBucket.Compute(TestHostBuilder.IntegrationAbuseMetricsSalt, "example.com");
        var secondBucket = AnonymizedSenderBucket.Compute(TestHostBuilder.IntegrationAbuseMetricsSalt, "other-service.org");

        Assert.Multiple(() =>
        {
            Assert.That(firstBucket, Is.Not.EqualTo(secondBucket), "test domains must not collide, or the assertions below prove nothing");
            Assert.That(group.AnonymizedEmailAliasSenderCounts.Sum(x => x), Is.EqualTo(2));
            Assert.That(group.AnonymizedEmailAliasSenderCounts[firstBucket], Is.EqualTo(1));
            Assert.That(group.AnonymizedEmailAliasSenderCounts[secondBucket], Is.EqualTo(1));
            Assert.That(group.AnonymizedEmailAliasSenderCounts.Max(), Is.EqualTo(1), "two services must not read as concentration");
        });
    }

    /// <summary>
    /// Aliases all first contacted from the same host pile into a single bucket. This is the shape the counts
    /// exists to surface: many aliases, one destination.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task AnonymizedSenderCountsConcentrateOnSingleSender()
    {
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "One"));
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "Two"));

        var dbContext = await _testHostBuilder.GetDbContextAsync();
        var group = await dbContext.Groups.FirstAsync(g => g.Id == _personalGroupId);
        var expectedBucket = AnonymizedSenderBucket.Compute(TestHostBuilder.IntegrationAbuseMetricsSalt, "example.com");

        Assert.Multiple(() =>
        {
            // Different local parts, one host: the local part must play no role in where the bucket lands.
            Assert.That(group.AnonymizedEmailAliasSenderCounts.Sum(x => x), Is.EqualTo(2));
            Assert.That(group.AnonymizedEmailAliasSenderCounts[expectedBucket], Is.EqualTo(2));
            Assert.That(group.AnonymizedEmailAliasSenderCounts.Count(x => x > 0), Is.EqualTo(1), "one host must occupy exactly one bucket");
        });
    }

    /// <summary>
    /// A failure while recording the counts must never affect delivery. The sender's bucket is pre-saturated
    /// here so the increment overflows the column: before this was swallowed the error escaped as a 550 for mail
    /// that had already been stored, which is exactly what gets an alias onto a sender's suppression list.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task AnonymizedSenderCountFailureDoesNotRejectEmail()
    {
        var setupContext = await _testHostBuilder.GetDbContextAsync();
        var position = AnonymizedSenderBucket.Compute(TestHostBuilder.IntegrationAbuseMetricsSalt, "example.com") + 1;
        await setupContext.Database.ExecuteSqlAsync($"UPDATE \"Groups\" SET \"AnonymizedEmailAliasSenderCounts\"[{position}] = {int.MaxValue} WHERE \"Id\" = {_personalGroupId}");

        Assert.DoesNotThrowAsync(async () => await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "Overflow")));

        var dbContext = await _testHostBuilder.GetDbContextAsync();
        var deliveredCount = await dbContext.Emails.CountAsync();
        var group = await dbContext.Groups.FirstAsync(g => g.Id == _personalGroupId);

        Assert.Multiple(() =>
        {
            Assert.That(deliveredCount, Is.EqualTo(1), "the email must be stored even though the count write failed");
            Assert.That(group.AnonymizedEmailAliasSenderCounts[position - 1], Is.EqualTo(int.MaxValue), "the count is simply lost, which is the cheaper failure");
        });
    }

    /// <summary>
    /// Assert that out-of-range writes to the anonymized sender counts do not corrupt the counts.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task AnonymizedSenderBucketOutOfRangeDoesNotCorruptCounts()
    {
        var setupContext = await _testHostBuilder.GetDbContextAsync();
        var position = AnonymizedSenderBucket.Compute(TestHostBuilder.IntegrationAbuseMetricsSalt, "example.com") + 1;
        await setupContext.Database.ExecuteSqlAsync($"UPDATE \"Groups\" SET \"AnonymizedEmailAliasSenderCounts\" = array_fill(0, ARRAY[1]) WHERE \"Id\" = {_personalGroupId}");

        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "One"));
        Assert.DoesNotThrowAsync(async () => await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "Two")));

        // Assert
        var dbContext = await _testHostBuilder.GetDbContextAsync();
        var deliveredCount = await dbContext.Emails.CountAsync();
        var nullCount = await dbContext.Database.SqlQuery<int>($"SELECT COALESCE(array_position(\"AnonymizedEmailAliasSenderCounts\", NULL), 0) AS \"Value\" FROM \"Groups\" WHERE \"Id\" = {_personalGroupId}").FirstAsync();

        Assert.Multiple(() =>
        {
            Assert.That(deliveredCount, Is.EqualTo(2), "both emails must be delivered despite the unwritable bucket");
            Assert.That(nullCount, Is.Zero, "an out-of-range should be ignored instead of corrupting the bucket itself");
        });
    }

    /// <summary>
    /// Every recipient group is charged once per delivered email. The counter drives quota enforcement and is now
    /// incremented by a statement rather than through the change tracker, so it needs coverage proving it still
    /// counts at all; it had none before.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task EmailsReceivedCounterIncrementsPerDelivery()
    {
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "One"));
        await SendMessageToSmtpServer(BuildSimpleMessage("[email protected]", "[email protected]", "Two"));

        var dbContext = await _testHostBuilder.GetDbContextAsync();
        var group = await dbContext.Groups.FirstAsync(g => g.Id == _personalGroupId);

        Assert.That(group.EmailsReceived, Is.EqualTo(2), "each delivered email must charge the recipient group exactly once");
    }

    /// <summary>
    /// Tests sending a single email in html format to the SMTP server to check if it is processed correctly.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task SingleEmailHtml()
    {
        // Arrange
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email with HTML body.";
        const string htmlBody = "<html><body><h1>This is a test email html.</h1></body></html>";
        message.Body = new BodyBuilder { HtmlBody = htmlBody }.ToMessageBody();
        await SendMessageToSmtpServer(message);

        // Check if the email is in the database.
        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.DecryptionKeys).FirstAsync();

        // Test non-encrypted field.
        Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));

        // Decrypt the email and then check all individual fields.
        processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail, Is.Not.Null);
            Assert.That(processedEmail.MessagePreview, Is.EqualTo("This is a test email html."));
            Assert.That(processedEmail.MessagePlain, Is.Null);
            Assert.That(processedEmail.MessageHtml, Is.Null, "Parsed bodies should no longer be persisted; only the gzipped raw source is stored.");
            Assert.That(processedEmail.MessageSource, Is.Null, "The legacy text source column should no longer be populated.");
            Assert.That(processedEmail.MessageSourceBytes, Is.Not.Null, "The gzipped raw source should be stored in MessageSourceBytes.");
        });

        // The decrypted source is still gzip-compressed; gunzip it and verify the original html body is present.
        var messageSource = Encoding.UTF8.GetString(Gunzip(processedEmail.MessageSourceBytes!));
        Assert.That(messageSource, Does.Contain(htmlBody));
    }

    /// <summary>
    /// Tests sending a single email in multipart format to the SMTP server to check if it is processed correctly.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task SingleEmailMultipart()
    {
        // Arrange
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email with multipart body.";
        const string textBody = "This is a test email multipart.";
        const string htmlBody = "<html><body><h1>This is a test email multipart.</h1></body></html>";
        message.Body = new BodyBuilder { TextBody = textBody, HtmlBody = htmlBody }.ToMessageBody();
        await SendMessageToSmtpServer(message);

        // Check if the email is in the database.
        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.DecryptionKeys).FirstAsync();

        // Test non-encrypted field.
        Assert.That(processedEmail.To, Is.EqualTo("[email protected]"));

        // Decrypt the email and then check all individual fields.
        processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail, Is.Not.Null);
            Assert.That(processedEmail.MessagePreview, Is.EqualTo("This is a test email multipart."));
            Assert.That(processedEmail.MessagePlain, Is.Null, "Parsed bodies should no longer be persisted; only the gzipped raw source is stored.");
            Assert.That(processedEmail.MessageHtml, Is.Null, "Parsed bodies should no longer be persisted; only the gzipped raw source is stored.");
            Assert.That(processedEmail.MessageSource, Is.Null, "The legacy text source column should no longer be populated.");
            Assert.That(processedEmail.MessageSourceBytes, Is.Not.Null, "The gzipped raw source should be stored in MessageSourceBytes.");
        });

        // The decrypted source is still gzip-compressed; gunzip it and verify both original bodies are present.
        var messageSource = Encoding.UTF8.GetString(Gunzip(processedEmail.MessageSourceBytes!));
        Assert.Multiple(() =>
        {
            Assert.That(messageSource, Does.Contain(textBody));
            Assert.That(messageSource, Does.Contain(htmlBody));
        });
    }

    /// <summary>
    /// Full roundtrip test for the source-only email storage format: ingest a multipart email (html + plain +
    /// attachment), decrypt the stored row with the test private key, gunzip the source bytes and verify the raw
    /// RFC 822 message contains all original content.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task MultipartEmailSourceRoundtrip()
    {
        // Arrange
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email source roundtrip.";
        const string textBody = "This is a test email roundtrip plain.";
        const string htmlBody = "<html><body><h1>This is a test email roundtrip html.</h1></body></html>";
        var attachmentData = Encoding.UTF8.GetBytes("This is a roundtrip attachment.");
        var bodyBuilder = new BodyBuilder { TextBody = textBody, HtmlBody = htmlBody };
        bodyBuilder.Attachments.Add("attachment.txt", attachmentData, ContentType.Parse("text/plain"));
        message.Body = bodyBuilder.ToMessageBody();
        await SendMessageToSmtpServer(message);

        // Retrieve the stored email and verify the unencrypted metadata written at ingest.
        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.DecryptionKeys).FirstAsync();
        var encryptedSourceBytes = processedEmail.MessageSourceBytes;
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail.AttachmentCount, Is.EqualTo(1));
            Assert.That(encryptedSourceBytes, Is.Not.Null, "The gzipped raw source should be stored in MessageSourceBytes.");
        });

        // Decrypt the email; the decrypted source bytes must differ from the stored (encrypted) bytes.
        processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);
        var decryptedSourceBytes = processedEmail.MessageSourceBytes!;
        Assert.Multiple(() =>
        {
            Assert.That(decryptedSourceBytes, Is.Not.EqualTo(encryptedSourceBytes), "Email source bytes are not encrypted at rest. Check email encryption logic.");
            Assert.That(decryptedSourceBytes[0], Is.EqualTo(0x1f), "Decrypted source should still be gzip-compressed (gzip magic byte 1).");
            Assert.That(decryptedSourceBytes[1], Is.EqualTo(0x8b), "Decrypted source should still be gzip-compressed (gzip magic byte 2).");
        });

        // Gunzip the decrypted source and verify the raw MIME message contains all original content.
        var sourceBytes = Gunzip(decryptedSourceBytes);
        var messageSource = Encoding.UTF8.GetString(sourceBytes);
        var crlfSeparatorIndex = messageSource.IndexOf("\r\n\r\n", StringComparison.Ordinal);
        var headerSeparatorIndex = crlfSeparatorIndex != -1 ? crlfSeparatorIndex : messageSource.IndexOf("\n\n", StringComparison.Ordinal);
        Assert.That(headerSeparatorIndex, Is.GreaterThan(0), "Gunzipped source should contain an RFC 822 header/body separator.");
        var headerBlock = messageSource[..headerSeparatorIndex];
        Assert.Multiple(() =>
        {
            Assert.That(messageSource, Does.Match("^[A-Za-z][A-Za-z0-9-]*: "), "Gunzipped source should start with the RFC 822 header block.");
            Assert.That(headerBlock, Does.Contain("From: Test Sender <[email protected]>"));
            Assert.That(headerBlock, Does.Contain("To: Test Recipient <[email protected]>"));
            Assert.That(headerBlock, Does.Contain("Subject: Test Email source roundtrip."));
            Assert.That(messageSource, Does.Contain(textBody));
            Assert.That(messageSource, Does.Contain(htmlBody));
            Assert.That(messageSource, Does.Contain("This is a roundtrip attachment.").Or.Contain(Convert.ToBase64String(attachmentData)), "Raw MIME should contain the attachment payload (literal or base64-encoded).");
        });

        // Parse the raw MIME and verify the attachment payload decodes back to the original bytes.
        using var sourceStream = new MemoryStream(sourceBytes);
        var parsedMessage = await MimeMessage.LoadAsync(sourceStream);
        var parsedAttachment = parsedMessage.Attachments.OfType<MimePart>().Single();
        Assert.That(parsedAttachment.Content, Is.Not.Null, "Parsed attachment has no content. Check the stored raw source.");
        using var decodedAttachment = new MemoryStream();
        await parsedAttachment.Content!.DecodeToAsync(decodedAttachment);
        Assert.Multiple(() =>
        {
            Assert.That(parsedAttachment.FileName, Is.EqualTo("attachment.txt"));
            Assert.That(decodedAttachment.ToArray(), Is.EqualTo(attachmentData));
        });
    }

    /// <summary>
    /// Tests that non-ASCII printable characters (e.g. accented letters) are preserved in the generated message preview.
    /// Regression test for a bug where the preview extraction stripped all non-ASCII characters (e.g. "répondre" became "rpondre").
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task SingleEmailNonAsciiPreviewPlain()
    {
        // Arrange
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email with accents.";
        const string textBody = "Veuillez répondre à cet e-mail. Voici quelques caractères accentués: éàçüñ.";
        message.Body = new BodyBuilder { TextBody = textBody }.ToMessageBody();
        await SendMessageToSmtpServer(message);

        // Check if the email is in the database.
        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.DecryptionKeys).FirstAsync();

        // Decrypt the email and verify the accented characters survived into the preview.
        processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail, Is.Not.Null);
            Assert.That(processedEmail.MessagePreview, Does.Contain("répondre"));
            Assert.That(processedEmail.MessagePreview, Is.EqualTo(textBody));
        });
    }

    /// <summary>
    /// Tests sending a single email in plain format to the SMTP server to check if it is processed correctly.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task MultipleRecipientsEmail()
    {
        // Send email to the SMTP server.
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Cc.Add(new MailboxAddress("Test Recipient 2", "[email protected]"));

        message.Subject = "Test Email";
        const string textBody = "This is a test email plain.";
        message.Body = new BodyBuilder { TextBody = textBody }.ToMessageBody();
        await SendMessageToSmtpServer(message);

        // Check that two emails are in the database, one for each allowed recipient.
        Assert.That(await _testHostBuilder.GetDbContext().Emails.CountAsync(), Is.EqualTo(2));
    }

    /// <summary>
    /// Tests sending an email to an unknown recipient domain, we expect to get an error from the SMTP server.
    /// </summary>
    [Test]
    public void SingleEmailUnknownRecipientDomain()
    {
        // Send email to the SMTP server.
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email";
        const string textBody = "This is a test email plain.";
        message.Body = new BodyBuilder { TextBody = textBody }.ToMessageBody();

        // Expect error from SmtpClient when sending email to unknown domain.
        Assert.ThrowsAsync<SmtpCommandException>(async () => await SendMessageToSmtpServer(message));
    }

    /// <summary>
    /// Tests sending an email to an existing but disabled email claim, we expect to get an error from the SMTP server.
    /// </summary>
    [Test]
    public void SingleEmailDisabledUserClaim()
    {
        // Send email to the SMTP server.
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email";
        const string textBody = "This is a test email plain.";
        message.Body = new BodyBuilder { TextBody = textBody }.ToMessageBody();

        // Expect error from SmtpClient when sending email to unknown domain.
        Assert.ThrowsAsync<SmtpCommandException>(async () => await SendMessageToSmtpServer(message));
    }

    /// <summary>
    /// Tests sending a single email to a known recipient domain but with no valid user claim. We expect
    /// to get an error from the SMTP server.
    /// </summary>
    [Test]
    public void SingleEmailNoUserClaim()
    {
        // Send email to the SMTP server.
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email";
        const string textBody = "This is a test email plain.";
        message.Body = new BodyBuilder { TextBody = textBody }.ToMessageBody();

        // Expect error from SmtpClient when sending email to unknown domain.
        Assert.ThrowsAsync<SmtpCommandException>(async () => await SendMessageToSmtpServer(message));
    }

    /// <summary>
    /// Tests sending a single email with an attachment to the SMTP server to check if it is processed correctly.
    /// In the source-only storage format no separate attachment rows are created; only the attachment count is stamped.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task AttachmentEmail()
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email with attachment";

        var bodyBuilder = new BodyBuilder();
        bodyBuilder.TextBody = "This is a test email with attachment.";

        // Add attachment using BodyBuilder
        byte[] attachmentData = Encoding.UTF8.GetBytes("This is an attachment.");
        bodyBuilder.Attachments.Add("attachment.txt", attachmentData, ContentType.Parse("text/plain"));

        message.Body = bodyBuilder.ToMessageBody();

        await SendMessageToSmtpServer(message);

        // Check that no separate attachment rows are created and the attachment count is stamped on the email row.
        var processedEmail = await _testHostBuilder.GetDbContext().Emails.FirstAsync();
        var attachmentRowCount = await _testHostBuilder.GetDbContext().EmailAttachments.CountAsync();
        Assert.Multiple(() =>
        {
            Assert.That(attachmentRowCount, Is.Zero, "No EmailAttachments rows should be created; attachments live inside the stored raw source.");
            Assert.That(processedEmail.AttachmentCount, Is.EqualTo(1));
            Assert.That(processedEmail.MessageSourceBytes, Is.Not.Null, "The gzipped raw source should be stored in MessageSourceBytes.");
        });
    }

    /// <summary>
    /// Tests that a sizeable attachment is detached from the message source at ingest.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task LargeAttachmentIsDetachedFromSource()
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email with large attachment";

        // Random bytes well over the 64 KB detach floor, so the payload cannot compress away either.
        var attachmentData = new byte[128 * 1024];
        Random.Shared.NextBytes(attachmentData);

        var bodyBuilder = new BodyBuilder { TextBody = "This is a test email with a large attachment." };
        bodyBuilder.Attachments.Add("large.bin", attachmentData, ContentType.Parse("application/octet-stream"));
        message.Body = bodyBuilder.ToMessageBody();

        await SendMessageToSmtpServer(message);

        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Parts).Include(x => x.DecryptionKeys).FirstAsync();
        var storedPart = processedEmail.Parts.Single();
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail.AttachmentCount, Is.EqualTo(1));
            Assert.That(storedPart.PartIndex, Is.Zero);
            Assert.That(storedPart.Bytes, Is.Not.Empty);
        });

        // The whole point of detaching: the source a client downloads to read the mail no longer carries the payload.
        Assert.That(processedEmail.MessageSourceBytes!.Length, Is.LessThan(attachmentData.Length / 4), "The stored source should no longer carry the attachment payload.");

        processedEmail = TestDecryption.DecryptEmail(processedEmail, PrivateKey);
        var sourceBytes = Gunzip(processedEmail.MessageSourceBytes!);
        var messageSource = Encoding.UTF8.GetString(sourceBytes);
        Assert.Multiple(() =>
        {
            Assert.That(messageSource, Does.Contain("X-AliasVault-Part: 0"), "The detached attachment should advertise the index its body is stored under.");
            Assert.That(messageSource, Does.Contain($"X-AliasVault-Detached-Length: {attachmentData.Length}"), "The detached attachment should advertise its decoded size.");
            Assert.That(messageSource, Does.Contain("filename=large.bin"), "The attachment headers must stay in the source so clients can still list it.");
        });

        // The skeleton has to remain a parseable MIME message with the attachment present but empty.
        using var sourceStream = new MemoryStream(sourceBytes);
        var parsedMessage = await MimeMessage.LoadAsync(sourceStream);
        var parsedAttachment = parsedMessage.Attachments.OfType<MimePart>().Single();
        using var emptyBody = new MemoryStream();
        await parsedAttachment.Content!.DecodeToAsync(emptyBody);
        Assert.Multiple(() =>
        {
            Assert.That(parsedAttachment.FileName, Is.EqualTo("large.bin"));
            Assert.That(emptyBody.ToArray(), Is.Empty, "The detached attachment should have an empty body in the source.");
        });

        // Splicing the stored part back where its body was must reproduce the original file byte for byte.
        var detachedBody = Gunzip(processedEmail.Parts.Single().Bytes);
        var spliced = SpliceDetachedPart(sourceBytes, detachedBody);
        using var splicedStream = new MemoryStream(spliced);
        var splicedMessage = await MimeMessage.LoadAsync(splicedStream);
        using var decodedAttachment = new MemoryStream();
        await splicedMessage.Attachments.OfType<MimePart>().Single().Content!.DecodeToAsync(decodedAttachment);
        Assert.That(decodedAttachment.ToArray(), Is.EqualTo(attachmentData), "The spliced attachment should decode to the original bytes.");
    }

    /// <summary>
    /// Tests that every recipient of a multi-recipient email gets a usable copy of a detached attachment.
    /// Detaching lifts the body out of the shared MimeMessage, so doing it per recipient rather than once up
    /// front would leave every copy after the first with an empty part.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task DetachedAttachmentIsStoredForEveryRecipient()
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Cc.Add(new MailboxAddress("Test Recipient 2", "[email protected]"));
        message.Subject = "Test Email with large attachment for multiple recipients";

        var attachmentData = new byte[128 * 1024];
        Random.Shared.NextBytes(attachmentData);

        var bodyBuilder = new BodyBuilder { TextBody = "This is a test email with a large attachment." };
        bodyBuilder.Attachments.Add("large.bin", attachmentData, ContentType.Parse("application/octet-stream"));
        message.Body = bodyBuilder.ToMessageBody();

        await SendMessageToSmtpServer(message);

        var emails = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Parts).Include(x => x.DecryptionKeys).ToListAsync();
        Assert.That(emails, Has.Count.EqualTo(2));

        foreach (var email in emails)
        {
            var decrypted = TestDecryption.DecryptEmail(email, PrivateKey);
            var detachedBody = Gunzip(decrypted.Parts.Single().Bytes);
            var spliced = SpliceDetachedPart(Gunzip(decrypted.MessageSourceBytes!), detachedBody);

            using var splicedStream = new MemoryStream(spliced);
            var splicedMessage = await MimeMessage.LoadAsync(splicedStream);
            using var decodedAttachment = new MemoryStream();
            await splicedMessage.Attachments.OfType<MimePart>().Single().Content!.DecodeToAsync(decodedAttachment);

            Assert.That(decodedAttachment.ToArray(), Is.EqualTo(attachmentData), $"Recipient {email.To} did not get a usable copy of the detached attachment.");
        }
    }

    /// <summary>
    /// Tests that an attachment below the detach floor is left inline: detaching it would cost an extra round
    /// trip on download for a saving too small to matter.
    /// </summary>
    /// <returns>Task.</returns>
    [Test]
    public async Task SmallAttachmentStaysInsideSource()
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
        message.To.Add(new MailboxAddress("Test Recipient", "[email protected]"));
        message.Subject = "Test Email with small attachment";

        var bodyBuilder = new BodyBuilder { TextBody = "This is a test email with a small attachment." };
        bodyBuilder.Attachments.Add("small.txt", Encoding.UTF8.GetBytes("This is a small attachment."), ContentType.Parse("text/plain"));
        message.Body = bodyBuilder.ToMessageBody();

        await SendMessageToSmtpServer(message);

        var processedEmail = await _testHostBuilder.GetDbContext().Emails.Include(x => x.Parts).FirstAsync();
        Assert.Multiple(() =>
        {
            Assert.That(processedEmail.Parts, Is.Empty, "An attachment below the detach floor should stay inside the source.");
            Assert.That(processedEmail.AttachmentCount, Is.EqualTo(1));
        });
    }

    /// <summary>
    /// Splices a detached part body back into the empty body of the attachment it was lifted out of, mirroring
    /// what the Rust core does client-side.
    /// </summary>
    /// <param name="skeleton">The raw MIME source with the attachment body removed.</param>
    /// <param name="body">The transfer-encoded attachment body to splice back in.</param>
    /// <returns>The reassembled raw MIME source.</returns>
    private static byte[] SpliceDetachedPart(byte[] skeleton, byte[] body)
    {
        // The detached part's body sits between the blank line that ends its headers and the next boundary.
        var text = Encoding.ASCII.GetString(skeleton);
        var headerIndex = text.IndexOf("X-AliasVault-Detached-Length:", StringComparison.Ordinal);
        Assert.That(headerIndex, Is.GreaterThan(0), "Skeleton does not contain a detached part.");

        var separator = text.IndexOf("\r\n\r\n", headerIndex, StringComparison.Ordinal);
        var separatorLength = 4;
        if (separator == -1)
        {
            separator = text.IndexOf("\n\n", headerIndex, StringComparison.Ordinal);
            separatorLength = 2;
        }

        Assert.That(separator, Is.GreaterThan(0), "Detached part has no header/body separator.");
        var bodyStart = separator + separatorLength;

        var spliced = new byte[skeleton.Length + body.Length];
        Array.Copy(skeleton, 0, spliced, 0, bodyStart);
        Array.Copy(body, 0, spliced, bodyStart, body.Length);
        Array.Copy(skeleton, bodyStart, spliced, bodyStart + body.Length, skeleton.Length - bodyStart);
        return spliced;
    }

    /// <summary>
    /// Sends a message to the SMTP server with retry logic for connection.
    /// </summary>
    /// <param name="message">MimeMessage to send.</param>
    private static async Task SendMessageToSmtpServer(MimeMessage message)
    {
        using var client = new SmtpClient();

        // Retry connection up to 10 times with 100ms delay to handle race condition
        // where the SMTP server may not be fully started yet.
        const int maxRetries = 10;
        const int retryDelayMs = 100;

        for (var attempt = 1; attempt <= maxRetries; attempt++)
        {
            try
            {
                await client.ConnectAsync("localhost", 2525, SecureSocketOptions.None);
                break;
            }
            catch (System.Net.Sockets.SocketException) when (attempt < maxRetries)
            {
                await Task.Delay(retryDelayMs);
            }
        }

        try
        {
            await client.SendAsync(message);
        }
        finally
        {
            await client.DisconnectAsync(true);
        }
    }

    /// <summary>
    /// Build a minimal plain-text message for delivery tests.
    /// </summary>
    /// <param name="from">The sender address.</param>
    /// <param name="to">The recipient address.</param>
    /// <param name="subject">The subject line.</param>
    /// <returns>The message.</returns>
    private static MimeMessage BuildSimpleMessage(string from, string to, string subject)
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Test Sender", from));
        message.To.Add(new MailboxAddress("Test Recipient", to));
        message.Subject = subject;
        message.Body = new BodyBuilder { TextBody = "Body of " + subject }.ToMessageBody();
        return message;
    }

    private static async Task<TcpClient> ConnectRawSmtpClient()
    {
        var client = new TcpClient();
        const int maxRetries = 10;
        const int retryDelayMs = 100;

        for (var attempt = 1; attempt <= maxRetries; attempt++)
        {
            try
            {
                await client.ConnectAsync("127.0.0.1", 2525);
                return client;
            }
            catch (SocketException) when (attempt < maxRetries)
            {
                await Task.Delay(retryDelayMs);
            }
        }

        throw new InvalidOperationException("Unable to connect to SMTP server for raw command test.");
    }

    private static async Task<string> SendSmtpCommandAsync(NetworkStream stream, string command)
    {
        var bytes = Encoding.ASCII.GetBytes($"{command}\r\n");
        await stream.WriteAsync(bytes.AsMemory(0, bytes.Length));
        await stream.FlushAsync();
        return await ReadSmtpLineAsync(stream);
    }

    private static async Task<string> ReadSmtpLineAsync(NetworkStream stream)
    {
        var buffer = new byte[512];
        var bytesRead = await stream.ReadAsync(buffer.AsMemory(0, buffer.Length));
        return Encoding.ASCII.GetString(buffer, 0, bytesRead).Trim();
    }

    /// <summary>
    /// Decompresses gzip-compressed bytes, as required to read the raw MIME source from a decrypted MessageSourceBytes value.
    /// </summary>
    /// <param name="gzippedBytes">The gzip-compressed bytes, starting with the gzip magic bytes 0x1f 0x8b.</param>
    /// <returns>The decompressed bytes.</returns>
    private static byte[] Gunzip(byte[] gzippedBytes)
    {
        using var input = new MemoryStream(gzippedBytes);
        using var gzip = new GZipStream(input, CompressionMode.Decompress);
        using var output = new MemoryStream();
        gzip.CopyTo(output);
        return output.ToArray();
    }
}