Files
BusinessMgr-2019/SaBusMgrTest_Control/Base/Data/SbmEncryption_Tester.cs
T

72 lines
2.4 KiB
C#

using System;
using System.Collections.Specialized;
using System.Security.Cryptography;
using NUnit.Framework;
using Sarand.BusinessMgr.Net.Standard.Base.Data;
using Sarand.Net.Standard.Base.Helpers;
using Sacs.Web.Components;
namespace Sarand.BusinessMgr.Test.Base.Data
{
[TestFixture]
public class SbmEncryption_Tester
{
// --- Service Encryption ---
[Test]
public void WindowsServiceCrypter_RoundTripsRequestAndResponseData()
{
const String value = "Service payload £ € 漢字";
String request = SbmWindowsServiceCrypter.EncryptRequestData(value);
String response = SbmWindowsServiceCrypter.EncryptResponseData(value);
Assert.That(request, Does.StartWith(SacsAuthenticatedEncryption.EnvelopePrefix));
Assert.That(response, Does.StartWith(SacsAuthenticatedEncryption.EnvelopePrefix));
Assert.That(response, Is.Not.EqualTo(request));
Assert.That(SbmWindowsServiceCrypter.DecryptRequestData(request), Is.EqualTo(value));
Assert.That(SbmWindowsServiceCrypter.DecryptResponseData(response), Is.EqualTo(value));
}
[Test]
public void WindowsServiceCrypter_RejectsWrongMessageDirection()
{
String request = SbmWindowsServiceCrypter.EncryptRequestData("Request payload");
Assert.That(
() => SbmWindowsServiceCrypter.DecryptResponseData(request),
Throws.TypeOf<AuthenticationTagMismatchException>());
}
[Test]
public void WindowsServiceCrypter_RejectsLegacyData()
{
Assert.That(
() => SbmWindowsServiceCrypter.DecryptRequestData("legacy-or-plaintext"),
Throws.TypeOf<CryptographicException>());
}
// --- Parameter Encryption ---
[Test]
public void ControllerParameterEncryption_RoundTripsAuthenticatedValues()
{
String key = SacsEncryptionHelper.EncodePlainKey("BusinessMgr-Test-Key", SacsEncryptionHelper.KeyLength);
NameValueCollection parameters = new NameValueCollection
{
{ "alpha", "One" },
{ "unicode", "£ € 漢字" }
};
String encrypted = SaController.EncryptParameters(parameters, key, SbmEncryptionPurpose.ControlSessionParameters);
NameValueCollection decrypted = SaController.DecryptParameters(encrypted, key, SbmEncryptionPurpose.ControlSessionParameters);
Assert.That(encrypted, Does.StartWith(SacsAuthenticatedEncryption.EnvelopePrefix));
Assert.That(decrypted["alpha"], Is.EqualTo("One"));
Assert.That(decrypted["unicode"], Is.EqualTo("£ € 漢字"));
}
// --- The End ---
}
}