-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Microsoft.Win32; | ||
|
||
namespace AOVpnManager.Tests | ||
{ | ||
[TestClass] | ||
public class StateManagerTests | ||
{ | ||
[TestMethod] | ||
public void LastConnctionName() | ||
{ | ||
IStateManager stateManager = new StateManager(Registry.CurrentUser, @"Test\AOVpnManager"); | ||
|
||
try | ||
{ | ||
stateManager.UpdateLastConnectionName("Contosa Vpn"); | ||
Assert.AreEqual("Contosa Vpn", stateManager.ReadLastConnectionName()); | ||
|
||
stateManager.UpdateLastConnectionName("Contosa Always On Vpn"); | ||
Assert.AreEqual("Contosa Always On Vpn", stateManager.ReadLastConnectionName()); | ||
|
||
stateManager.UpdateLastConnectionName(null); | ||
Assert.AreEqual(null, stateManager.ReadLastConnectionName()); | ||
|
||
stateManager.UpdateLastConnectionName("Contosa Vpn v2"); | ||
Assert.AreEqual("Contosa Vpn v2", stateManager.ReadLastConnectionName()); | ||
} | ||
finally | ||
{ | ||
stateManager.Clean(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace AOVpnManager | ||
{ | ||
public interface IStateManager | ||
{ | ||
string ReadLastConnectionName(); | ||
void UpdateLastConnectionName(string connectionName); | ||
void Clean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Microsoft.Win32; | ||
|
||
namespace AOVpnManager | ||
{ | ||
public class StateManager : IStateManager | ||
{ | ||
private const string LastConnectionName = nameof(LastConnectionName); | ||
|
||
private readonly RegistryKey root; | ||
private readonly string path; | ||
|
||
public StateManager(RegistryKey root, string path) | ||
{ | ||
this.root = root; | ||
this.path = path; | ||
} | ||
|
||
public void Clean() | ||
{ | ||
root.DeleteSubKey(path, false); | ||
} | ||
|
||
public string ReadLastConnectionName() | ||
{ | ||
using (RegistryKey key = root.OpenSubKey(path)) | ||
{ | ||
return (string)key?.GetValue(LastConnectionName); | ||
} | ||
} | ||
|
||
public void UpdateLastConnectionName(string connectionName) | ||
{ | ||
using (RegistryKey key = OpenOrCreateKey()) | ||
{ | ||
if (connectionName == null) | ||
{ | ||
key.DeleteValue(LastConnectionName); | ||
} | ||
else | ||
{ | ||
key.SetValue(LastConnectionName, connectionName); | ||
} | ||
} | ||
} | ||
|
||
private RegistryKey OpenOrCreateKey() | ||
{ | ||
return root.OpenSubKey(path, true) ?? root.CreateSubKey(path, true); | ||
} | ||
} | ||
} |