public void LogMessage() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; DateTime startLogging = DateTime.UtcNow; loggingService.LogMessage(title, message, LogLevel.Debug); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.Single().Key; Assert.NotNull(fullLogName); Log <object> log = (Log <object>)cachingService.Single().Value.UntypedValue; Assert.NotNull(log); Assert.Null(log.Target); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(message, log.Message); Assert.Equal(LogLevel.Debug, log.LogLevel); Assert.Null(log.Exception); Assert.Equal("Message Log", log.Description); }
public static CacheLoggingService GetLogsService(ISerializationService serializationService = null, TimeSpan?cacheLife = null) { serializationService = serializationService ?? new Json(); ICachingService cachingService = new TestMemoryCachingService(cacheLife ?? TimeSpan.FromMinutes(5)); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService); return(loggingService); }
public void LogExceptionWithObject() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; Foobar original = new Foobar { Foo = 4, Bar = 6 }; InvalidOperationException exception; try { throw new InvalidOperationException(title); } catch (InvalidOperationException ex) { exception = ex; } DateTime startLogging = DateTime.UtcNow; loggingService.LogExceptionWithObject(exception, original, message); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.Single().Key; Assert.NotNull(fullLogName); Log <Foobar> log = (Log <Foobar>)cachingService.Single().Value.UntypedValue; string description = "Exception Log - " + title; Assert.NotNull(log); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(message, log.Message); Assert.Equal(LogLevel.Error, log.LogLevel); Assert.Equal(description, log.Description); Assert.NotNull(log.Target); Assert.Equal(original.Foo, log.Target.Foo); // The memory caching service never serializes or deserializes so the non-serialized values won't change // Assert.Equal(0, log.Target.Bar); Assert.NotNull(log.Exception); Assert.Equal(typeof(SerializableException), log.Exception.GetType()); Assert.Equal(title, log.Exception.Message); }
private static DefaultConfigurationService GenerateService() { TestSerializationService jsonSerializer = new TestSerializationService(); TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromDays(1), false); DefaultConfigurationCache cache = new DefaultConfigurationCache(cachingService, jsonSerializer, TimeSpan.FromDays(1)); DefaultConfigurationService service = new DefaultConfigurationService(cache); return(service); }
private static Mock <ConfigurationCacheBase> GenerateCache(bool useStrict = true) { TestSerializationService jsonSerializer = new TestSerializationService(); TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromDays(1), false); Mock <ConfigurationCacheBase> cacheProxy = new Mock <ConfigurationCacheBase>(useStrict ? MockBehavior.Strict : MockBehavior.Loose, cachingService, jsonSerializer, TimeSpan.FromDays(1)); return(cacheProxy); }
public void IsValidRequestTest() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); IsValidRequestOverride service = GetService((l, a, c) => new IsValidRequestOverride(l, a, c), cachingService); const string badAppId = "badappId"; const string resource = "/test"; const string method = "GET"; const string content = "some content"; Tuple <bool, HmacIsValidRequestResult> result = service.CheckValidRequest(null, null, null, null, null, null, null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.NoValidResouce, result.Item2); result = service.CheckValidRequest(null, resource, null, badAppId, null, null, null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.UnableToFindAppId, result.Item2); result = service.CheckValidRequest(null, resource, null, _appId, null, null, null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); result = service.CheckValidRequest(null, resource, null, _appId, null, "a nonce", null); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); ulong badCurrentTime = DateTime.UtcNow.AddMinutes(-30).UnixTimeStamp(); result = service.CheckValidRequest(null, resource, null, _appId, null, "a nonce", badCurrentTime.ToString()); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); ulong goodCurrentTime = DateTime.UtcNow.UnixTimeStamp(); cachingService.Cache("a nonce", "a nonce"); result = service.CheckValidRequest(null, resource, null, _appId, null, "a nonce", goodCurrentTime.ToString()); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.ReplayRequest, result.Item2); HmacSignatureGenerator signatureGenerator = new HmacSignatureGenerator(CustomHeaderScheme); string fullSignature = signatureGenerator.GenerateFullHmacSignature(resource, method, _appId, _secretKey, content); string[] signatureParts = service.GetHeaderValues(fullSignature.Split(" ")[1]); result = service.CheckValidRequest(content.ToStream(), resource, method, signatureParts[0], signatureParts[1], signatureParts[2], signatureParts[3]); Assert.True(result.Item1); Assert.Equal(HmacIsValidRequestResult.NoError, result.Item2); fullSignature = signatureGenerator.GenerateFullHmacSignature(resource, method, _appId, _secretKey, content); signatureParts = service.GetHeaderValues(fullSignature.Split(" ")[1]); result = service.CheckValidRequest(content.ToStream(), resource, method, signatureParts[0], _secretKey, signatureParts[2], signatureParts[3]); Assert.False(result.Item1); Assert.Equal(HmacIsValidRequestResult.SignaturesMismatch, result.Item2); }
public void LogException() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; InvalidOperationException exception; try { throw new InvalidOperationException(title); } catch (InvalidOperationException ex) { exception = ex; } DateTime startLogging = DateTime.UtcNow; loggingService.LogException(exception, message); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.Single().Key; Assert.NotNull(fullLogName); Log <object> log = (Log <object>)cachingService.Single().Value.UntypedValue; string description = "Exception Log - " + title; Assert.NotNull(log); Assert.Null(log.Target); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(message, log.Message); Assert.Equal(LogLevel.Error, log.LogLevel); Assert.Equal(description, log.Description); Assert.NotNull(log.Exception); Assert.Equal(typeof(SerializableException), log.Exception.GetType()); Assert.Equal(title, log.Exception.Message); }
public void LogMessageWithObjectWithoutMessage() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; Foobar original = new Foobar { Foo = 4, Bar = 6 }; DateTime startLogging = DateTime.UtcNow; loggingService.LogMessage(title, original, LogLevel.Debug); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.Single().Key; Assert.NotNull(fullLogName); Log <Foobar> log = (Log <Foobar>)cachingService.Single().Value.UntypedValue; string description = "Message Log with object - " + typeof(Foobar).FullName; Assert.NotNull(log); Assert.True(log.TimeStamp >= startLogging.AddMilliseconds(-5) && log.TimeStamp <= endLogging.AddMilliseconds(5)); Assert.Equal(title, log.Title); Assert.Equal(description, log.Message); Assert.Equal(LogLevel.Debug, log.LogLevel); Assert.Null(log.Exception); Assert.Equal(description, log.Description); Assert.NotNull(log.Target); Assert.Equal(original.Foo, log.Target.Foo); // The memory caching service never serializes or deserializes so the non-serialized values won't change // Assert.Equal(0, log.Target.Bar); }
public void LogTest() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); DateTime startLogging = DateTime.UtcNow; loggingService.LogMessage("Test Log", "A test log", LogLevel.Debug); DateTime endLogging = DateTime.UtcNow; Assert.Single(cachingService); string fullLogName = cachingService.Single().Key; Assert.StartsWith(LogLevel.Debug.ToString() + _partSeperator, fullLogName); string timestampString = fullLogName.Split('_')[1]; DateTime timeStamp = DateTime.FromFileTimeUtc(long.Parse(timestampString)); Assert.True(timeStamp >= startLogging.AddMilliseconds(-5) && timeStamp <= endLogging.AddMilliseconds(5)); }
public void IsReplayRequestTest() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); IsReplayRequestOverride service = GetService((l, a, c) => new IsReplayRequestOverride(l, a, c), cachingService); const string Key = "alreadyAdded"; const string GoodKey = "notAdded"; const string Value = "its here"; cachingService.Cache(Key, Value); Assert.True(service.CheckReplayRequest(Key, null)); ulong badCurrentTime = DateTime.UtcNow.AddMinutes(-30).UnixTimeStamp(); Assert.True(service.CheckReplayRequest(GoodKey, badCurrentTime.ToString())); ulong goodCurrentTime = DateTime.UtcNow.UnixTimeStamp(); Assert.False(service.CheckReplayRequest(GoodKey, goodCurrentTime.ToString())); Assert.Equal(2, cachingService.Count); Assert.Contains(GoodKey, cachingService.Keys); Assert.Equal(goodCurrentTime.FromUnixTimeStamp(), cachingService[GoodKey].CachedTime); Assert.Equal(GoodKey, cachingService[GoodKey].UntypedValue); }
public void GetLogsTest() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; Foobar original = new Foobar { Foo = 4, Bar = 6 }; InvalidOperationException exception; try { throw new InvalidOperationException(title); } catch (InvalidOperationException ex) { exception = ex; } string description = "Manual Exception Log - " + message; Log <Foobar> originalLog = new Log <Foobar> { Target = original, TimeStamp = DateTime.UtcNow, Title = title, Message = message, LogLevel = LogLevel.Info, Exception = new SerializableException(exception), Description = description }; loggingService.Log(originalLog); ILogEnumerable <Foobar> logs = loggingService.GetLogs <Foobar>(); Assert.NotNull(logs); Assert.NotEmpty(logs); Assert.Single(logs); Log <Foobar> log = logs.Single(); Assert.NotNull(log); Assert.True(log.TimeStamp >= log.TimeStamp.AddMilliseconds(-.5) && log.TimeStamp <= log.TimeStamp.AddMilliseconds(.5)); Assert.Equal(originalLog.Title, log.Title); Assert.Equal(originalLog.Message, log.Message); Assert.Equal(originalLog.LogLevel, log.LogLevel); Assert.Equal(originalLog.Description, log.Description); Assert.NotNull(log.Target); Assert.NotNull(log.Exception); Assert.Equal(typeof(SerializableException), log.Exception.GetType()); Assert.Equal(title, log.Exception.Message); Assert.Equal(originalLog.Exception.Message, log.Exception.Message); Assert.Equal(log, logs.First()); }
public void LogObjectTest() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); Json serializationService = new Json(); CacheLoggingService loggingService = new CacheLoggingService(cachingService, serializationService, _partSeperator); const string title = "Test Log"; const string message = "A test log"; Foobar original = new Foobar { Foo = 4, Bar = 6 }; InvalidOperationException exception; try { throw new InvalidOperationException(title); } catch (InvalidOperationException ex) { exception = ex; } string description = "Manual Exception Log - " + message; Log <Foobar> originalLog = new Log <Foobar> { Target = original, TimeStamp = DateTime.UtcNow, Title = title, Message = message, LogLevel = LogLevel.Info, Exception = new SerializableException(exception), Description = description }; loggingService.Log(originalLog); Assert.Single(cachingService); string fullLogName = cachingService.Single().Key; Assert.NotNull(fullLogName); Log <Foobar> log = (Log <Foobar>)cachingService.Single().Value.UntypedValue; Assert.NotNull(log); Assert.True(log.TimeStamp >= log.TimeStamp.AddMilliseconds(-.5) && log.TimeStamp <= log.TimeStamp.AddMilliseconds(.5)); Assert.Equal(originalLog.Title, log.Title); Assert.Equal(originalLog.Message, log.Message); Assert.Equal(originalLog.LogLevel, log.LogLevel); Assert.Equal(originalLog.Description, log.Description); Assert.NotNull(log.Target); Assert.Equal(original.Foo, log.Target.Foo); Assert.Equal(originalLog.Target.Foo, log.Target.Foo); // The memory caching service never serializes or deserializes so the non-serialized values won't change // Assert.Equal(0, log.Target.Bar); // The memory caching service never serializes or deserializes so the non-serialized values won't change // Assert.NotEqual(originalLog.Target.Bar, log.Target.Bar); Assert.Equal(originalLog.Target.Bar, log.Target.Bar); Assert.Equal(original.Bar, originalLog.Target.Bar); Assert.NotNull(log.Exception); Assert.Equal(typeof(SerializableException), log.Exception.GetType()); Assert.Equal(title, log.Exception.Message); Assert.Equal(originalLog.Exception.Message, log.Exception.Message); }
public void DoAuthorizationTest() { TestMemoryCachingService cachingService = new TestMemoryCachingService(TimeSpan.FromMinutes(5)); AuthenticationService service = GetService((l, a, c) => new AuthenticationService(l, a, c), cachingService); const string resource = "/test"; const string method = "GET"; const string content = "some content"; HmacSignatureGenerator signatureGenerator = new HmacSignatureGenerator(CustomHeaderScheme); string fullSignature = signatureGenerator.GenerateFullHmacSignature(resource, method, _appId, _secretKey, content); string hmacAuthenticationValue = CustomHeaderScheme + " " + null; Tuple <bool, IEnumerable <HmacIsValidRequestResult>, GenericPrincipal> result = service.DoAuthorization(null, null, null, null, true); Assert.Null(result); result = service.DoAuthorization(null, null, null, null, true); Assert.Null(result); result = service.DoAuthorization(null, null, null, null, false); Assert.NotNull(result); Assert.False(result.Item1); Assert.Equal(3, result.Item2.Count()); Assert.Contains(HmacIsValidRequestResult.NoHmacHeader, result.Item2); Assert.Contains(HmacIsValidRequestResult.NotEnoughHeaderParts, result.Item2); Assert.Contains(HmacIsValidRequestResult.BadNamespace, result.Item2); Assert.Null(result.Item3); hmacAuthenticationValue = "qqq 1:2:3:4"; result = service.DoAuthorization(hmacAuthenticationValue, null, null, null, false); Assert.NotNull(result); Assert.False(result.Item1); Assert.Single(result.Item2); Assert.Equal(HmacIsValidRequestResult.BadNamespace, result.Item2.Single()); Assert.Null(result.Item3); hmacAuthenticationValue = "sds"; result = service.DoAuthorization(hmacAuthenticationValue, null, null, null, false); Assert.NotNull(result); Assert.False(result.Item1); Assert.Single(result.Item2); Assert.Equal(HmacIsValidRequestResult.NotEnoughHeaderParts, result.Item2.Single()); Assert.Null(result.Item3); hmacAuthenticationValue = "1:2:3:4"; result = service.DoAuthorization(hmacAuthenticationValue, null, null, null, false); Assert.NotNull(result); Assert.False(result.Item1); Assert.Equal(2, result.Item2.Count()); Assert.Contains(HmacIsValidRequestResult.BadNamespace, result.Item2); Assert.Contains(HmacIsValidRequestResult.NotEnoughHeaderParts, result.Item2); Assert.Null(result.Item3); hmacAuthenticationValue = "sds 1:2:3"; result = service.DoAuthorization(hmacAuthenticationValue, null, null, null, false); Assert.NotNull(result); Assert.False(result.Item1); Assert.Single(result.Item2); Assert.Equal(HmacIsValidRequestResult.NotEnoughHeaderValueItems, result.Item2.Single()); Assert.Null(result.Item3); hmacAuthenticationValue = "sds 1:2:3:4"; result = service.DoAuthorization(hmacAuthenticationValue, null, null, null, false); Assert.NotNull(result); Assert.False(result.Item1); Assert.Single(result.Item2); Assert.Equal(HmacIsValidRequestResult.NoValidResouce, result.Item2.Single()); Assert.Null(result.Item3); hmacAuthenticationValue = fullSignature; result = service.DoAuthorization(hmacAuthenticationValue, content.ToStream(), resource, method, false); Assert.NotNull(result); Assert.True(result.Item1); Assert.Single(result.Item2); Assert.Equal(HmacIsValidRequestResult.NoError, result.Item2.Single()); Assert.NotNull(result.Item3); Assert.Equal(_appId, result.Item3.Identity.Name); }