diff --git a/src/CommonLib/Cache.cs b/src/CommonLib/Cache.cs index bf311aeb..4f1fd57b 100644 --- a/src/CommonLib/Cache.cs +++ b/src/CommonLib/Cache.cs @@ -1,4 +1,6 @@ -using System.Collections.Concurrent; +using System; +using System.Collections.Concurrent; +using System.ComponentModel; using System.Runtime.Serialization; using SharpHoundCommonLib.Enums; @@ -18,6 +20,8 @@ public class Cache // // [DataMember]private ConcurrentDictionary _valueToIDCache; + private static Version defaultVersion = new(1, 0, 0); + private Cache() { ValueToIdCache = new ConcurrentDictionary(); @@ -36,6 +40,8 @@ private Cache() [DataMember] public ConcurrentDictionary SIDToDomainCache { get; private set; } [DataMember] public ConcurrentDictionary ValueToIdCache { get; private set; } + [DataMember] public DateTime CacheCreationDate { get; set; } + [DataMember] public Version CacheCreationVersion { get; set; } [IgnoreDataMember] private static Cache CacheInstance { get; set; } @@ -137,9 +143,17 @@ private static string GetPrefixKey(string key, string domain) /// Creates a new empty cache instance /// /// - public static Cache CreateNewCache() + public static Cache CreateNewCache(Version version = null) { - return new Cache(); + if (version == null) + { + version = defaultVersion; + } + return new Cache + { + CacheCreationVersion = version, + CacheCreationDate = DateTime.Now.Date + }; } /// diff --git a/test/unit/CacheTest.cs b/test/unit/CacheTest.cs new file mode 100644 index 00000000..a4d04037 --- /dev/null +++ b/test/unit/CacheTest.cs @@ -0,0 +1,29 @@ +using System; +using Newtonsoft.Json; +using SharpHoundCommonLib; +using Xunit; +using Xunit.Abstractions; + +namespace CommonLibTest +{ + public class CacheTest + { + private ITestOutputHelper _testOutputHelper; + public CacheTest(ITestOutputHelper testOutputHelper) + { + _testOutputHelper = testOutputHelper; + } + + [Fact] + public void Cache_TestNewCache() + { + var cache = Cache.CreateNewCache(); + Assert.Equal(cache.CacheCreationVersion, new Version(1,0,0)); + var version = new Version(1, 0, 1); + cache = Cache.CreateNewCache(version); + var time = DateTime.Now.Date; + Assert.Equal(cache.CacheCreationVersion, version); + Assert.Equal(cache.CacheCreationDate, time); + } + } +}