Skip to content

Commit

Permalink
reset memory stream position when key is loaded from the stream expli…
Browse files Browse the repository at this point in the history
…citly
  • Loading branch information
ivanovvitaly committed Jan 22, 2024
1 parent c08b31a commit 2acda4e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions SecureStore/SecretsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public void LoadKeyFromStream(Stream stream)
}
}

mstream.Seek(0, SeekOrigin.Begin);

if (mstream.Length == KEYLENGTH * KEYCOUNT)
{
LoadLegacyKeyFromStream(mstream);
Expand Down Expand Up @@ -258,6 +260,8 @@ public async Task LoadKeyFromStreamAsync(Stream stream, CancellationToken cancel
}
}

mstream.Seek(0, SeekOrigin.Begin);

if (mstream.Length == KEYLENGTH * KEYCOUNT)
{
await LoadLegacyKeyFromStreamAsync(mstream, cancel);
Expand Down
43 changes: 43 additions & 0 deletions Tests/FunctionalityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NeoSmart.SecureStore;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Tests
{
Expand Down Expand Up @@ -99,5 +100,47 @@ public void StoreAndLoadStream()
}
}
}

[TestMethod]
public void StoreAndLoadKeyFromStream()
{
var storePath = Path.GetTempFileName();
var keyPath = Path.GetTempFileName();

CreateTestStore(storePath, keyPath);

using (var storeStream = new FileStream(storePath, FileMode.Open, FileAccess.Read))
using (var keyStream = new FileStream(keyPath, FileMode.Open, FileAccess.Read))
using (var sman = SecretsManager.LoadStore(storeStream))
{
sman.LoadKeyFromStream(keyStream);
foreach (var key in SecureData.Keys)
{
Assert.AreEqual(SecureData[key], sman.Get(key),
$"Retrieved data for key \"{key}\" does not match stored value!");
}
}
}

[TestMethod]
public async Task StoreAndLoadKeyFromStreamAsync()
{
var storePath = Path.GetTempFileName();
var keyPath = Path.GetTempFileName();

CreateTestStore(storePath, keyPath);

using (var storeStream = new FileStream(storePath, FileMode.Open, FileAccess.Read))
using (var keyStream = new FileStream(keyPath, FileMode.Open, FileAccess.Read))
using (var sman = SecretsManager.LoadStore(storeStream))
{
await sman.LoadKeyFromStreamAsync(keyStream);
foreach (var key in SecureData.Keys)
{
Assert.AreEqual(SecureData[key], sman.Get(key),
$"Retrieved data for key \"{key}\" does not match stored value!");
}
}
}
}
}

0 comments on commit 2acda4e

Please sign in to comment.