-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateKeyCommandTest.cs
More file actions
75 lines (66 loc) · 2.64 KB
/
Copy pathGenerateKeyCommandTest.cs
File metadata and controls
75 lines (66 loc) · 2.64 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
using EncryptedConfigValue.Crypto;
using EncryptedConfigValue.Crypto.Algorithm;
using Shouldly;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Xunit;
namespace EncryptedConfigValue.Cli.Test
{
[Collection("Sequential")]
public class GenerateKeyCommandTest
{
private static readonly GenerateKeyCommand command = new GenerateKeyCommand();
[Theory]
[MemberData(nameof(Data))]
public void WeGenerateAValidKey(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);
command.Execute("--algorithm", algorithm.ToString(), "--file", tempFilePath);
var keyPair = KeyFileUtils.KeyPairFromPath(tempFilePath);
keyPair.EncryptionKey.Type.Algorithm.ShouldBeEquivalentTo(algorithm);
}
[Fact]
public void WeDoNotOverwriteAnExistingKeyfile()
{
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");
var algorithm = "AES";
Directory.CreateDirectory(tempDirectory);
// create the file
using (File.Create(tempFilePath)) { }
var act = () =>
{
command.Execute("--algorithm", algorithm.ToString(), "--file", tempFilePath);
};
var ex = Should.Throw<IOException>(act);
ex.Message.ShouldMatch("(.*)already exists*");
}
public static IEnumerable<object[]> Data() =>
new[] {
new object[] { Algorithm.AES },
new object[] { Algorithm.RSA },
};
}
}