-
Notifications
You must be signed in to change notification settings - Fork 50
/
TrayTOTP_ColumnproviderTests.cs
133 lines (108 loc) · 5.03 KB
/
TrayTOTP_ColumnproviderTests.cs
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
using FluentAssertions;
using KeePass.App.Configuration;
using KeePass.Forms;
using KeePass.Plugins;
using KeePass.UI;
using KeePassLib.Security;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
namespace KeeTrayTOTP.Tests
{
[TestClass]
public class TrayTOTP_ColumnproviderTests : IDisposable
{
private readonly KeeTrayTOTPExt _plugin;
const string InvalidSeed = "C5CYMIHWQUUZMKUGZHGEOSJSQDE4L===!";
const string ValidSeed = "JBSWY3DPEHPK3PXP";
const string ValidSettings = "30;6";
public TrayTOTP_ColumnproviderTests()
{
(_plugin, _) = CreateInitializedPlugin();
}
[DataRow(ValidSeed, ValidSettings, "TOTP Enabled")]
[DataRow(ValidSeed, ";6", "Error, bad settings!")]
[DataRow(ValidSeed, "30", "Error, bad settings!")]
[DataRow(ValidSeed, null, "TOTP Enabled")]
[DataRow(InvalidSeed, ValidSettings, "Error, bad seed!")]
[DataRow(null, ValidSettings, "Error, no seed!")]
[DataRow(null, null, "")]
[DataTestMethod]
public void GetCellDataStatus_ShouldReturnExpectedValues(string seed, string settings, string expected)
{
var column = new TrayTOTP_ColumnProvider(_plugin);
var pwEntry = new KeePassLib.PwEntry(true, true);
if (seed != null)
{
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, seed));
}
if (settings != null)
{
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, settings));
}
var actual = column.GetCellData("TOTP Status", pwEntry);
actual.Should().Be(expected);
}
[DataRow(ValidSeed, ";6", "Error, bad settings!")]
[DataRow(ValidSeed, "30", "Error, bad settings!")]
[DataRow(InvalidSeed, ValidSettings, "Error, bad seed!")]
[DataRow(null, ValidSettings, "Error, no seed!")]
[DataRow(null, null, "")]
[DataTestMethod]
public void GetCellDataCode_ShouldReturnExpectedValues(string seed, string settings, string expected)
{
var column = new TrayTOTP_ColumnProvider(_plugin);
var pwEntry = new KeePassLib.PwEntry(true, true);
if (seed != null)
{
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, seed));
}
if (settings != null)
{
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, settings));
}
var actual = column.GetCellData("TOTP", pwEntry);
actual.Should().Be(expected);
}
[DataRow(true, @"^\d{6} \(\d{1,2}\)$", DisplayName = "Column timer visible should show a code and validity")]
[DataRow(false, @"^\d{6}$", DisplayName = "Column timer invisible should only show a code")]
[DataTestMethod]
public void GetCellDataCode_WithValidSeedAndSettings_ShouldReturnA6DigitCodeWithDuration(bool showTimer, string regex)
{
_plugin.Settings.TOTPColumnTimerVisible = showTimer;
var column = new TrayTOTP_ColumnProvider(_plugin);
var pwEntry = new KeePassLib.PwEntry(true, true);
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, ValidSeed));
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, ValidSettings));
var actual = column.GetCellData("TOTP", pwEntry);
actual.Should().MatchRegex(regex);
}
[TestMethod]
public void GetCellData_WithAnInvalidColumn_ShouldReturnEmptyString()
{
var column = new TrayTOTP_ColumnProvider(_plugin);
var pwEntry = new KeePassLib.PwEntry(true, true);
pwEntry.Strings.Set(_plugin.Settings.TOTPSeedStringName, new ProtectedString(false, ValidSeed));
pwEntry.Strings.Set(_plugin.Settings.TOTPSettingsStringName, new ProtectedString(false, ValidSettings));
var actual = column.GetCellData("InvalidColumnName", pwEntry);
actual.Should().BeEmpty();
}
private static (KeeTrayTOTPExt, IPluginHost) CreateInitializedPlugin()
{
var plugin = new KeeTrayTOTPExt();
var pluginHost = new Mock<IPluginHost>(MockBehavior.Strict);
var keepassForm = new MainForm();
pluginHost.SetupGet(c => c.MainWindow).Returns(keepassForm);
var customConfig = new AceCustomConfig();
pluginHost.SetupGet(c => c.CustomConfig).Returns(customConfig);
var columnProviderPool = new ColumnProviderPool();
pluginHost.SetupGet(c => c.ColumnProviderPool).Returns(columnProviderPool);
plugin.Initialize(pluginHost.Object);
return (plugin, pluginHost.Object);
}
public void Dispose()
{
_plugin.Terminate();
}
}
}