-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptConfigValueCommandTest.cs
More file actions
112 lines (97 loc) · 3.89 KB
/
Copy pathEncryptConfigValueCommandTest.cs
File metadata and controls
112 lines (97 loc) · 3.89 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
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Algorithm;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;
namespace EncryptedConfigValue.Cli.Test
{
[Collection("Sequential")]
public class EncryptConfigValueCommandTest : IDisposable
{
private sealed class StringWriterWithEncoding(Encoding encoding) : StringWriter()
{
public override Encoding Encoding { get; } = encoding;
}
private static readonly string CHARSET = "UTF-8";
private static readonly string plaintext = "this is a secret message";
private readonly StringWriter outContent = new StringWriterWithEncoding(Encoding.GetEncoding(CHARSET));
private readonly EncryptConfigValueCommand command = new();
private TextWriter originalConsoleOut;
public EncryptConfigValueCommandTest()
{
originalConsoleOut = Console.Out;
Console.SetOut(outContent);
}
public void Dispose()
{
Console.SetOut(originalConsoleOut);
}
[Theory]
[MemberData(nameof(Data))]
internal void WeEncryptAndPrintAValue(Algorithm algorithm)
{
var version = Environment.Version;
var targetFrameworkVersion = $"{version.Major}.{version.Minor}";
var tempDirectory = Path.Combine(Path.GetTempPath(), "temp-key-directory", targetFrameworkVersion);
var tempFilePath = Path.Combine(tempDirectory, "test.key");
if (Directory.Exists(tempDirectory))
{
Directory
.GetFiles(tempDirectory)
.Select(file =>
{
var newPath = Path.Combine(tempDirectory, Path.GetFileName(file) + ".deleted");
File.Move(file, newPath);
return newPath;
})
.ToList()
.ForEach(File.Delete);
}
Directory.CreateDirectory(tempDirectory);
var keyPair = algorithm.NewKeyPair();
KeyFileUtils.KeyPairToFile(keyPair, tempFilePath);
command.Execute("--keyfile", tempFilePath, "--value", plaintext);
var output = outContent.ToString();
var configValue = EncryptedValue.FromString(output);
var decryptionKey = keyPair.DecryptionKey;
var decryptedValue = configValue.Decrypt(decryptionKey);
decryptedValue.ShouldBeEquivalentTo(plaintext);
}
[Fact]
internal void WeFailIfTheKeyfileDoesNotExist()
{
var version = Environment.Version;
var targetFrameworkVersion = $"{version.Major}.{version.Minor}";
var tempDirectory = Path.Combine(Path.GetTempPath(), "temp-key-directory", targetFrameworkVersion);
var tempFilePath = Path.Combine(tempDirectory, "test.key");
if (Directory.Exists(tempDirectory))
{
Directory
.GetFiles(tempDirectory)
.Select(file =>
{
var newPath = Path.Combine(tempDirectory, Path.GetFileName(file) + ".deleted");
File.Move(file, newPath);
return newPath;
})
.ToList()
.ForEach(File.Delete);
}
Directory.CreateDirectory(tempDirectory);
var act = () =>
{
command.Execute("--keyfile", tempFilePath, "--value", plaintext);
};
act.ShouldThrow<FileNotFoundException>();
}
public static IEnumerable<object[]> Data() =>
new[] {
new object[] { Algorithm.AES },
new object[] { Algorithm.RSA },
};
}
}