2020-12-20 08:36:11 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2020-11-12 11:01:19 +01:00
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2019-02-15 11:43:56 +11:00
|
|
|
using NUnit.Framework;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Web.Common.Security;
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Web.Common.Security;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class EncryptionHelperTests
|
2019-02-15 11:43:56 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
private IDataProtectionProvider DataProtectionProvider { get; } = new EphemeralDataProtectionProvider();
|
2020-11-12 11:01:19 +01:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Create_Encrypted_RouteString_From_Anonymous_Object()
|
|
|
|
|
{
|
|
|
|
|
var additionalRouteValues = new { key1 = "value1", key2 = "value2", Key3 = "Value3", keY4 = "valuE4" };
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var encryptedRouteString = EncryptionHelper.CreateEncryptedRouteString(
|
|
|
|
|
DataProtectionProvider,
|
|
|
|
|
"FormController",
|
|
|
|
|
"FormAction",
|
|
|
|
|
string.Empty,
|
|
|
|
|
additionalRouteValues);
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var result = EncryptionHelper.Decrypt(encryptedRouteString, DataProtectionProvider);
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
const string expectedResult =
|
|
|
|
|
"c=FormController&a=FormAction&ar=&key1=value1&key2=value2&Key3=Value3&keY4=valuE4";
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(expectedResult, result);
|
|
|
|
|
}
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Create_Encrypted_RouteString_From_Dictionary()
|
|
|
|
|
{
|
|
|
|
|
var additionalRouteValues = new Dictionary<string, object>
|
2019-02-15 11:43:56 +11:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
{ "key1", "value1" }, { "key2", "value2" }, { "Key3", "Value3" }, { "keY4", "valuE4" },
|
|
|
|
|
};
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var encryptedRouteString = EncryptionHelper.CreateEncryptedRouteString(
|
|
|
|
|
DataProtectionProvider,
|
|
|
|
|
"FormController",
|
|
|
|
|
"FormAction",
|
|
|
|
|
string.Empty,
|
|
|
|
|
additionalRouteValues);
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
var result = EncryptionHelper.Decrypt(encryptedRouteString, DataProtectionProvider);
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
const string expectedResult =
|
|
|
|
|
"c=FormController&a=FormAction&ar=&key1=value1&key2=value2&Key3=Value3&keY4=valuE4";
|
2019-02-15 11:43:56 +11:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.AreEqual(expectedResult, result);
|
2019-02-15 11:43:56 +11:00
|
|
|
}
|
|
|
|
|
}
|