1+ using Microsoft . Extensions . FileProviders ;
2+
13namespace SecureStore . Contrib . Configuration . Tests
24{
35 using System ;
@@ -6,9 +8,11 @@ namespace SecureStore.Contrib.Configuration.Tests
68 using NeoSmart . SecureStore ;
79 using Xunit ;
810
9- public class SecureStoreConfigurationProviderTests
11+ public class SecureStoreConfigurationProviderTests : IDisposable
1012 {
11- private static string Password => "P@$$w0rD!" ;
13+ private static readonly string EmbeddedKeyName = "embedded.key" ;
14+ private static readonly string Password = "P@$$w0rD!" ;
15+ private readonly string _storePath ;
1216
1317 private static readonly Dictionary < string , string > SecureData = new Dictionary < string , string >
1418 {
@@ -17,58 +21,68 @@ public class SecureStoreConfigurationProviderTests
1721 { "foo3" , "bar3" }
1822 } ;
1923
20- private void CreateTestStore ( string storePath , string key , KeyType type )
24+ public SecureStoreConfigurationProviderTests ( )
2125 {
22- using ( var sman = SecretsManager . CreateStore ( ) )
23- {
24- if ( type == KeyType . Password )
25- {
26- sman . LoadKeyFromPassword ( key ) ;
27- }
28- else
29- {
30- sman . GenerateKey ( ) ;
31- }
32-
33- foreach ( var secretKey in SecureData . Keys )
34- {
35- sman . Set ( secretKey , SecureData [ secretKey ] ) ;
36- }
37-
38- sman . SaveStore ( storePath ) ;
39- sman . ExportKey ( key ) ;
40- }
26+ _storePath = Path . GetTempFileName ( ) ;
27+ }
28+
29+ public void Dispose ( )
30+ {
31+ File . Delete ( _storePath ) ;
4132 }
4233
4334 [ Fact ]
4435 public void LoadStreamUsingKeyFile ( )
4536 {
46- var storePath = Path . GetTempFileName ( ) ;
4737 var keyPath = Path . GetTempFileName ( ) ;
38+ CreateTestStore ( _storePath , keyPath , KeyType . File ) ;
39+ var configurationSource = new SecureStoreConfigurationSource
40+ {
41+ KeyType = KeyType . File ,
42+ Key = keyPath ,
43+ Optional = true
44+ } ;
45+ configurationSource . ResolveKeyFileProvider ( ) ;
46+ var provider = new SecureStoreConfigurationProvider ( configurationSource ) ;
47+
48+ using ( var stream = new FileStream ( _storePath , FileMode . Open , FileAccess . Read ) )
49+ {
50+ provider . Load ( stream ) ;
51+ }
4852
49- CreateTestStore ( storePath , keyPath , KeyType . File ) ;
53+ Assert . All ( SecureData , item => Assert . Equal ( provider . Get ( item . Key ) , item . Value ) ) ;
54+ File . Delete ( keyPath ) ;
55+ }
5056
57+ [ Fact ]
58+ public void LoadStreamUsingEmbeddedKeyFile ( )
59+ {
60+ var assembly = typeof ( SecureStoreConfigurationProviderTests ) . Assembly ;
61+ var names = assembly . GetManifestResourceNames ( ) ;
62+ using ( var key = assembly . GetManifestResourceStream ( $ "{ assembly . GetName ( ) . Name } .{ EmbeddedKeyName } ") ! )
63+ {
64+ CreateTestStore ( _storePath , key ) ;
65+ }
5166 var provider = new SecureStoreConfigurationProvider ( new SecureStoreConfigurationSource
5267 {
68+ KeyFileProvider = new ManifestEmbeddedFileProvider ( assembly ) ,
5369 KeyType = KeyType . File ,
54- Key = keyPath ,
70+ Key = EmbeddedKeyName ,
5571 Optional = true
5672 } ) ;
5773
58- using ( var stream = new FileStream ( storePath , FileMode . Open , FileAccess . Read ) )
74+ using ( var stream = new FileStream ( _storePath , FileMode . Open , FileAccess . Read ) )
5975 {
6076 provider . Load ( stream ) ;
6177 }
6278
63- File . Delete ( storePath ) ;
64- File . Delete ( keyPath ) ;
79+ Assert . All ( SecureData , item => Assert . Equal ( provider . Get ( item . Key ) , item . Value ) ) ;
6580 }
6681
6782 [ Fact ]
6883 public void LoadStreamUsingPassword ( )
6984 {
70- var storePath = Path . GetTempFileName ( ) ;
71- CreateTestStore ( storePath , Password , KeyType . Password ) ;
85+ CreateTestStore ( _storePath , Password , KeyType . Password ) ;
7286
7387 var provider = new SecureStoreConfigurationProvider ( new SecureStoreConfigurationSource
7488 {
@@ -77,36 +91,67 @@ public void LoadStreamUsingPassword()
7791 Optional = true
7892 } ) ;
7993
80- using ( var stream = new FileStream ( storePath , FileMode . Open , FileAccess . Read ) )
94+ using ( var stream = new FileStream ( _storePath , FileMode . Open , FileAccess . Read ) )
8195 {
8296 provider . Load ( stream ) ;
8397 }
8498
85- File . Delete ( storePath ) ;
99+ Assert . All ( SecureData , item => Assert . Equal ( provider . Get ( item . Key ) , item . Value ) ) ;
86100 }
87101
88102 [ Fact ]
89103 public void LoadStreamUsingPassword_ThrowsIfKeyTypeNotInRange ( )
90104 {
91- var storePath = Path . GetTempFileName ( ) ;
92- CreateTestStore ( storePath , Password , KeyType . Password ) ;
105+ CreateTestStore ( _storePath , Password , KeyType . Password ) ;
93106
94107 var source = new SecureStoreConfigurationSource
95108 {
96- KeyType = ( KeyType ) 3 ,
109+ KeyType = ( KeyType ) 3 ,
97110 Key = Password ,
98111 Optional = true
99112 } ;
100113 var provider = new SecureStoreConfigurationProvider ( source ) ;
101114
102- using ( var stream = new FileStream ( storePath , FileMode . Open , FileAccess . Read ) )
115+ using ( var stream = new FileStream ( _storePath , FileMode . Open , FileAccess . Read ) )
103116 {
104117 var ex = Assert . Throws < ArgumentOutOfRangeException > ( ( ) =>
105118 provider . Load ( stream ) ) ;
106119 Assert . Equal ( nameof ( source . KeyType ) , ex . ParamName ) ;
107120 }
121+ }
122+
123+ private void CreateTestStore ( string storePath , string key , KeyType type )
124+ {
125+ using var sman = SecretsManager . CreateStore ( ) ;
126+ if ( type == KeyType . Password )
127+ {
128+ sman . LoadKeyFromPassword ( key ) ;
129+ }
130+ else
131+ {
132+ sman . GenerateKey ( ) ;
133+ }
134+
135+ foreach ( var secretKey in SecureData . Keys )
136+ {
137+ sman . Set ( secretKey , SecureData [ secretKey ] ) ;
138+ }
139+
140+ sman . SaveStore ( storePath ) ;
141+ sman . ExportKey ( key ) ;
142+ }
143+
144+ private void CreateTestStore ( string storePath , Stream key )
145+ {
146+ using var sman = SecretsManager . CreateStore ( ) ;
147+ sman . LoadKeyFromStream ( key ) ;
148+
149+ foreach ( var secretKey in SecureData . Keys )
150+ {
151+ sman . Set ( secretKey , SecureData [ secretKey ] ) ;
152+ }
108153
109- File . Delete ( storePath ) ;
154+ sman . SaveStore ( storePath ) ;
110155 }
111156 }
112157}
0 commit comments