Skip to content

Commit

Permalink
Added new SettingsUnavailableException.
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleGitPhoenix authored Mar 28, 2023
1 parent 1471aa0 commit 1ad9187
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 286 deletions.
51 changes: 2 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ var settingsManager = SettingsManager<string>
.WithIpAddressConverter()
.WithRegexConverter()
.WithTimeSpanConverter()
.WithVersionConverter()
.WithEnumConverter(WriteOutValues.AsSuffix(start: "[", separator: ";", end: "]"))
.WithDefaultSerializerOptions()
// <--
.AddCache(...)
Expand Down Expand Up @@ -241,55 +243,6 @@ class MySettings : ISettings
"second": "Entry2 [Default;Entry1;Entry2]"
}
```

- Write-out as comment

Will add the enumeration values with configurable separator as a single comment below the serialized property. Due to limitations of the **JsonStringEnumConverter** the comment cannot be added above the property.

```c#
var converter = new EnumConverter(WriteOutValues.AsComment(separator: ";"));
var serializer = new JsonSettingsSerializer(converter);
var settingsData = serializer.Serialize(settings);
```

```json
{
"first": "Entry1"
/*Default;Entry1;Entry2*/,
"second": "Entry2"
/*Default;Entry1;Entry2*/
}
```

- Write out as separate property

Will add the enumeration values as an additional property below the serialized property. Due to limitations of the **JsonStringEnumConverter** the name of the additional property cannot correspond to the name of the serialized property and is a generic one based on the enumerations type appended by a random number to keep the JSON valid.

```c#
var converter = new EnumConverter(WriteOutValues.AsProperty());
var serializer = new JsonSettingsSerializer(converter);
var settingsData = serializer.Serialize(settings);
```

```json
{
"first": "Entry1",
"Values_for_MyEnum_45319": [
"Default",
"Entry1",
"Entry2"
],
"second": "Entry2",
"Values_for_MyEnum_13705": [
"Default",
"Entry1",
"Entry2"
]
}
```



___

# Implementations of `ISettingsCache`
Expand Down
8 changes: 4 additions & 4 deletions src/Settings.Builder.Test/Settings.Builder.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.17.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0-preview-20230223-05" />
</ItemGroup>

<ItemGroup>
Expand Down
48 changes: 24 additions & 24 deletions src/Settings.Encryption.Test/EncryptSettingsManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public EncryptedCollection() : base(new List<string>() { "1", "2", "3" }) { }
[TestCase(typeof(object[]), false)]
[TestCase(typeof(IPAddress), false)]
[TestCase(typeof(NestedSettings.Inner), true)]
public void Check_Is_Treated_As_Nested(Type nestedType, bool shouldBeTreatedAsCollection)
public void Is_Treated_As_Nested(Type nestedType, bool shouldBeTreatedAsCollection)
{
// Act
var isNested = EncryptSettingsManager.IsNested(nestedType);
Expand All @@ -173,7 +173,7 @@ public void Check_Is_Treated_As_Nested(Type nestedType, bool shouldBeTreatedAsCo
[TestCase(typeof(Dictionary<object, object>), false)]
[TestCase(typeof(List<object>), true)]
[TestCase(typeof(object[]), true)]
public void Check_Is_Treated_As_Collection(Type collectionType, bool shouldBeTreatedAsCollection)
public void Is_Treated_As_Collection(Type collectionType, bool shouldBeTreatedAsCollection)
{
// Act
var isCollection = EncryptSettingsManager.IsCollection(collectionType);
Expand All @@ -183,7 +183,7 @@ public void Check_Is_Treated_As_Collection(Type collectionType, bool shouldBeTre
}

[Test]
public void Check_System_Namespace_Is_Ignored()
public void System_Namespace_Is_Ignored()
{
Assert.Ignore("This would need a custom assembly name 'System.[...].dll' which is currently not implemented.");
//// Arrange
Expand All @@ -197,7 +197,7 @@ public void Check_System_Namespace_Is_Ignored()
}

[Test]
public void Check_EncryptDoNotFollowAttribute_Overrules()
public void EncryptDoNotFollowAttribute_Overrules()
{
// Arrange
var settings = new DontFollowSettings();
Expand All @@ -210,7 +210,7 @@ public void Check_EncryptDoNotFollowAttribute_Overrules()
}

[Test]
public void Check_EncryptForceFollowAttribute_Overrules()
public void EncryptForceFollowAttribute_Overrules()
{
Assert.Ignore("This would need a custom assembly name 'System.[...].dll' which is currently not implemented.");
//// Arrange
Expand All @@ -225,7 +225,7 @@ public void Check_EncryptForceFollowAttribute_Overrules()
}

[Test]
public void Check_Relevant_Properties_Are_Filtered()
public void Relevant_Properties_Are_Filtered()
{
// Arrange
var settings = new MultiplePropertiesSettings();
Expand All @@ -245,7 +245,7 @@ public void Check_Relevant_Properties_Are_Filtered()
}

[Test]
public void Check_Nested_Properties_Are_Filtered()
public void Nested_Properties_Are_Filtered()
{
// Arrange
var settings = new NestedSettings();
Expand All @@ -259,7 +259,7 @@ public void Check_Nested_Properties_Are_Filtered()
}

[Test]
public void Check_Settings_With_Properties_That_Throw_Can_Be_Handled()
public void Settings_With_Properties_That_Throw_Can_Be_Handled()
{
// Arrange
var settings = new ThrowingSettings();
Expand All @@ -270,19 +270,19 @@ public void Check_Settings_With_Properties_That_Throw_Can_Be_Handled()

[Test]
[Category("Encrypted Collection Test")]
public void Check_Simple_Array_Handling() => this.CheckCollectionHandling<SimpleArraySettings>(2);
public void Simple_Array_Handling() => this.CheckCollectionHandling<SimpleArraySettings>(2);

[Test]
[Category("Encrypted Collection Test")]
public void Check_Simple_List_Handling() => this.CheckCollectionHandling<SimpleListSettings>(2);
public void Simple_List_Handling() => this.CheckCollectionHandling<SimpleListSettings>(2);

[Test]
[Category("Encrypted Collection Test")]
public void Check_Stacked_List_Handling() => this.CheckCollectionHandling<StackedListSettings>(6);
public void Stacked_List_Handling() => this.CheckCollectionHandling<StackedListSettings>(6);

[Test]
[Category("Encrypted Collection Test")]
public void Check_Nested_List_Handling() => this.CheckCollectionHandling<NestedListSettings>(3);
public void Nested_List_Handling() => this.CheckCollectionHandling<NestedListSettings>(3);

private void CheckCollectionHandling<TSettings>(int amountOfProperties) where TSettings : new()
{
Expand All @@ -301,7 +301,7 @@ public void Check_Settings_With_Properties_That_Throw_Can_Be_Handled()
#region En-/Decryption

[Test]
public void Check_Unencrypted_Property_Is_Same()
public void Unencrypted_Property_Is_Same()
{
// Arrange
var underlyingSettingsManager = _fixture.Create<Mock<ISettingsManager>>().Object;
Expand All @@ -324,7 +324,7 @@ public void Check_Unencrypted_Property_Is_Same()
}

[Test]
public void Check_Null_Property_Is_Same()
public void Null_Property_Is_Same()
{
// Arrange
var underlyingSettingsManager = _fixture.Create<Mock<ISettingsManager>>().Object;
Expand All @@ -347,7 +347,7 @@ public void Check_Null_Property_Is_Same()
}

[Test]
public void Check_Null_Is_Not_Encrypted()
public void Null_Is_Not_Encrypted()
{
// Arrange
string? unencrypted = null;
Expand Down Expand Up @@ -376,7 +376,7 @@ public void Check_Null_Is_Not_Encrypted()
}

[Test]
public void Check_Value_Is_Encrypted_Upon_Save()
public void Value_Is_Encrypted_Upon_Save()
{
// Arrange
string? unencrypted = "unencrypted";
Expand Down Expand Up @@ -412,7 +412,7 @@ public void Check_Value_Is_Encrypted_Upon_Save()
}

[Test]
public void Check_Value_Is_Still_Decrypted_After_Save()
public void Value_Is_Still_Decrypted_After_Save()
{
// Arrange
string? unencrypted = "unencrypted";
Expand Down Expand Up @@ -441,7 +441,7 @@ public void Check_Value_Is_Still_Decrypted_After_Save()
}

[Test]
public void Check_Nested_Property_Is_Decrypted()
public void Nested_Property_Is_Decrypted()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -458,7 +458,7 @@ public void Check_Nested_Property_Is_Decrypted()
}

[Test]
public void Check_Nested_Property_Is_Encrypted()
public void Nested_Property_Is_Encrypted()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -474,7 +474,7 @@ public void Check_Nested_Property_Is_Encrypted()
}

[Test]
public void Check_Simple_Array_Property_Is_Encrypted()
public void Simple_Array_Property_Is_Encrypted()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -491,7 +491,7 @@ public void Check_Simple_Array_Property_Is_Encrypted()
}

[Test]
public void Check_Simple_List_Property_Is_Encrypted()
public void Simple_List_Property_Is_Encrypted()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -508,7 +508,7 @@ public void Check_Simple_List_Property_Is_Encrypted()
}

[Test]
public void Check_Stacked_List_Property_Is_Encrypted()
public void Stacked_List_Property_Is_Encrypted()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -534,7 +534,7 @@ public void Check_Stacked_List_Property_Is_Encrypted()
}

[Test]
public void Check_True_Is_Returned_If_Not_All_Properties_Are_Encrypted()
public void True_Is_Returned_If_Not_All_Properties_Are_Encrypted()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -550,7 +550,7 @@ public void Check_True_Is_Returned_If_Not_All_Properties_Are_Encrypted()
}

[Test]
public void Check_False_Is_Returned_If_All_Properties_Are_Encrypted()
public void False_Is_Returned_If_All_Properties_Are_Encrypted()
{
// Arrange
var encryptedText = "3DX19APzlJKi5kK0YlUjp1ZDNeQF9fDeN7bfsddBDKhX7w+jhw==";
Expand Down
35 changes: 28 additions & 7 deletions src/Settings.Encryption.Test/EncryptionHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void AfterAllTest() { }
#region Tests

[Test]
public void Check_Encryption_And_Decrypt_Are_Identical()
public void Encryption_And_Decrypt_Are_Identical()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -52,10 +52,10 @@ public void Check_Encryption_And_Decrypt_Are_Identical()
/// Checks that encrypting the same value yields different results each time. This is because the <see cref="EncryptionHelper.Marker"/> is placed randomly.
/// </summary>
[Test]
public void Check_Encryption_Yields_Different_Results()
public void Encryption_Yields_Different_Results()
{
// Arrange
var unencrypted = String.Join("_", Enumerable.Repeat("unencrypted", 100)); //! Make this string a little bit longer, so that the random position of the encryption marker is not the same for both encryption attempts.
var unencrypted = String.Join("_", Enumerable.Repeat("unencrypted", 100)); //! Make this string a little bit longer, so that the random position of the encryption marker is accidentally not the same for both encryption attempts.

// Act
var encrypted1 = _encryptionHelper.Encrypt(unencrypted);
Expand All @@ -66,7 +66,7 @@ public void Check_Encryption_Yields_Different_Results()
}

[Test]
public void Check_Encryption_Adds_Marker()
public void Encryption_Adds_Marker()
{
// Arrange
var unencrypted = "unencrypted";
Expand All @@ -79,7 +79,7 @@ public void Check_Encryption_Adds_Marker()
}

[Test]
public void Check_Unencrypted_Value_Is_Not_Decrypted()
public void Unencrypted_Value_Is_Not_Decrypted()
{
// Arrange
var pseudoEncrypted = "unencrypted";
Expand All @@ -92,7 +92,7 @@ public void Check_Unencrypted_Value_Is_Not_Decrypted()
}

[Test]
public void Check_Null_Value_Is_Not_Encrypted()
public void Null_Value_Is_Not_Encrypted()
{
// Act
var encrypted = _encryptionHelper.Encrypt(null);
Expand All @@ -102,7 +102,7 @@ public void Check_Null_Value_Is_Not_Encrypted()
}

[Test]
public void Check_Null_Value_Is_Not_Decrypted()
public void Null_Value_Is_Not_Decrypted()
{
// Act
var decrypted = _encryptionHelper.Decrypt(null);
Expand All @@ -111,5 +111,26 @@ public void Check_Null_Value_Is_Not_Decrypted()
Assert.IsNull(decrypted);
}

/// <summary>
/// Checks that an decrypted value is always properly decrypted, no matter how often the encrypted value changes (due to the random <see cref="EncryptionHelper.Marker"/>).
/// </summary>
[Test]
public void Encrypting_Does_Not_Degrade()
{
// Arrange
var initialDecrypted = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. \\!?#~äöü|.:,;@";
var decrypted = initialDecrypted;

for (var iteration = 0; iteration < 10000; iteration++)
{
// Act
var encrypted = _encryptionHelper.Encrypt(decrypted);
decrypted = _encryptionHelper.Decrypt(encrypted);

// Assert
Assert.That(decrypted, Is.EqualTo(initialDecrypted));
}
}

#endregion
}
8 changes: 4 additions & 4 deletions src/Settings.Encryption.Test/Settings.Encryption.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.AutoMoq" Version="4.17.0" />
<PackageReference Include="Moq" Version="4.17.2" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0-alpha.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0-preview-20230223-05" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 1ad9187

Please sign in to comment.