-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecryptingVariableSubstitutorTest.cs
More file actions
171 lines (152 loc) · 6 KB
/
Copy pathDecryptingVariableSubstitutorTest.cs
File metadata and controls
171 lines (152 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Algorithm;
using EncryptedConfigValue.Crypto.Util;
using EncryptedConfigValue.Module;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Xunit;
namespace EncryptedConfigValue.AspNetCore.Test
{
public class DecryptingVariableSubstitutorTest : IDisposable
{
private static readonly Algorithm Algorithm = Algorithm.RSA;
private static readonly KeyPair KeyPair = Algorithm.NewKeyPair();
public static readonly string TEST_KEY_PATH = nameof(DecryptingVariableSubstitutorTest) + "test-key" + $"{Environment.Version.Major}.{Environment.Version.Minor}";
private static string? previousProperty;
private readonly DecryptingVariableSubstitutor substitutor = new DecryptingVariableSubstitutor();
public DecryptingVariableSubstitutorTest()
{
previousProperty = Environment.GetEnvironmentVariable(KeyFileUtils.KeyPathProperty);
EnsureTestKeysExist();
}
public void Dispose()
{
if (previousProperty != null)
{
Environment.SetEnvironmentVariable(KeyFileUtils.KeyPathProperty, previousProperty);
}
var tempKeyPath = Environment.GetEnvironmentVariable(TEST_KEY_PATH);
var tempKeyPathDir = string.IsNullOrWhiteSpace(tempKeyPath)
? null
: Path.GetDirectoryName(tempKeyPath);
if (!string.IsNullOrWhiteSpace(tempKeyPathDir))
{
if (Directory.Exists(tempKeyPath))
{
Directory
.GetFiles(tempKeyPath)
.Select(file =>
{
var newPath = Path.Combine(tempKeyPath, Path.GetFileName(file) + ".deleted");
File.Move(file, newPath);
return newPath;
})
.ToList()
.ForEach(File.Delete);
}
}
Environment.SetEnvironmentVariable(TEST_KEY_PATH, null);
}
[Fact]
public void ConstantsAreNotModified()
{
substitutor.TryReplace("abc", out var output);
output.ShouldBeEquivalentTo("abc");
}
[Fact]
public void InvalidEncryptedVariablesThrowStringSubstitutionException()
{
try
{
substitutor.TryReplace("${enc:invalid-contents}", out var output);
Assert.Fail();
}
catch (StringSubstitutionException e)
{
e.Value.ShouldBeEquivalentTo("enc:invalid-contents");
e.Field.ShouldBeEmpty();
}
}
[Fact]
public void NonEncryptedVariablesAreNotModified()
{
substitutor.TryReplace("${abc}", out var output);
output.ShouldBeEquivalentTo("${abc}");
}
[Fact]
public void VariableIsDecrypted()
{
substitutor.TryReplace("${" + Encrypt("abc") + "}", out var output);
output.ShouldBeEquivalentTo("abc");
}
[Fact]
public void VariableIsDecryptedWithRegex()
{
substitutor.TryReplace("${" + Encrypt("$5") + "}", out var output);
output.ShouldBe("$5");
}
[Fact]
public void DecryptsMultiple()
{
var abc = "${" + Encrypt("abc") + "}";
var def = "${" + Encrypt("def") + "}";
var hello = "${" + Encrypt("enc:hello") + "}";
var source = abc + ":" + def + '.' + hello;
substitutor.TryReplace(source, out var output);
output.ShouldBe("abc:def.enc:hello");
}
[Fact]
public void DecryptsWithPlaceholders()
{
var abc = "${" + Encrypt("abc") + "}";
var def = "${" + Encrypt("${enc:test}") + "}";
var source = abc + ":" + def;
substitutor.TryReplace(source, out var output);
output.ShouldBeEquivalentTo("abc:${enc:test}");
}
[SkippableTheory]
[MemberData(nameof(PropertyTestData), 100)]
public void PropertyTestValues(string plaintext)
{
// RSA test key can only encrypt 190 bytes
Skip.If(Encoding.UTF8.GetBytes(plaintext).Length > 190);
EnsureTestKeysExist();
substitutor.TryReplace("${" + Encrypt(plaintext) + "}", out var output);
output.ShouldBeEquivalentTo(plaintext);
}
public static IEnumerable<object[]> PropertyTestData(int tries)
{
foreach (var i in Enumerable.Range(start: 0, count: tries))
{
yield return new object[] {
new string(Enumerable.Repeat(0, RandomNumberGenerator.GetInt32(0, 100))
.Select(_ => (char)RandomNumberGenerator.GetInt32(0, 1024))
.ToArray())
};
}
}
private static void EnsureTestKeysExist()
{
var testKeyPath = Environment.GetEnvironmentVariable(TEST_KEY_PATH);
if (testKeyPath != null && File.Exists(testKeyPath))
{
return;
}
var tempDirectory = Path.Combine(Directory.CreateTempSubdirectory().ToString(), "temp-key-directory");
Directory.CreateDirectory(tempDirectory);
var tempFilePath = Path.Combine(tempDirectory, $"{Algorithm}-test.key");
KeyFileUtils.KeyPairToFile(KeyPair, tempFilePath);
Environment.SetEnvironmentVariable(KeyFileUtils.KeyPathProperty, tempFilePath);
Environment.SetEnvironmentVariable(TEST_KEY_PATH, tempFilePath);
}
private string Encrypt(string value)
{
return Algorithm.NewEncrypter().Encrypt(KeyPair.EncryptionKey, value).ToString();
}
}
}