From 1ac538d7c66314af5d8a42fe4eb314ec18d34508 Mon Sep 17 00:00:00 2001 From: bonesoul Date: Mon, 18 Aug 2014 13:59:08 +0300 Subject: [PATCH 01/81] Updated FileHelpers.cs:AssemblyRoot - now it correctly sets root path. Implemented BanConfigTests. --- .../Utils/Helpers/IO/FileHelpers.cs | 3 +- src/Tests/Banning/BanConfigTests.cs | 99 +++++++++++++++++++ src/Tests/Banning/valid-config.json | 7 ++ src/Tests/Tests.csproj | 13 +++ src/Tests/invalid-config.json | 2 + src/Tests/invalid-json.json | 1 + src/Tests/packages.config | 1 + 7 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 src/Tests/Banning/BanConfigTests.cs create mode 100644 src/Tests/Banning/valid-config.json create mode 100644 src/Tests/invalid-config.json create mode 100644 src/Tests/invalid-json.json diff --git a/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs b/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs index 90d027982..1a22ce36f 100644 --- a/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs +++ b/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs @@ -25,7 +25,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using CoiniumServ.Utils.Platform; using Serilog; @@ -35,7 +34,7 @@ public static class FileHelpers { public static string AssemblyRoot { - get { return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); } + get { return AppDomain.CurrentDomain.BaseDirectory; } } public static string GetAbsolutePath(string file) diff --git a/src/Tests/Banning/BanConfigTests.cs b/src/Tests/Banning/BanConfigTests.cs new file mode 100644 index 000000000..be6a3ab5a --- /dev/null +++ b/src/Tests/Banning/BanConfigTests.cs @@ -0,0 +1,99 @@ +#region License +// +// CoiniumServ - Crypto Currency Mining Pool Server Software +// Copyright (C) 2013 - 2014, CoiniumServ Project - http://www.coinium.org +// http://www.coiniumserv.com - https://github.com/CoiniumServ/CoiniumServ +// +// This software is dual-licensed: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// For the terms of this license, see licenses/gpl_v3.txt. +// +// Alternatively, you can license this software under a commercial +// license or white-label it as set out in licenses/commercial.txt. +// +#endregion + +using CoiniumServ.Banning; +using CoiniumServ.Configuration; +using CoiniumServ.Factories; +using CoiniumServ.Repository; +using Nancy.TinyIoc; +using Should.Fluent; +using Xunit; + +namespace CoiniumServ.Tests.Banning +{ + public class BanConfigTests + { + private readonly IConfigFactory _configFactory; + private readonly IJsonConfigReader _jsonConfigReader; + + public BanConfigTests() + { + var kernel = TinyIoCContainer.Current; + new Bootstrapper(kernel); + _configFactory = kernel.Resolve(); + _jsonConfigReader = _configFactory.GetJsonConfigReader(); + } + + /// + /// Tests a json file with valid configuration. + /// + [Fact] + public void ValidConfig_ShouldSuccess() + { + // read a valid json config sample. + var data = _jsonConfigReader.Read("Banning/valid-config.json"); + var config = new BanConfig(data); + + // make sure our expected values are set. + config.Enabled.Should().Equal(true); + config.Duration.Should().Equal(1); + config.InvalidPercent.Should().Equal(2); + config.CheckThreshold.Should().Equal(3); + config.PurgeInterval.Should().Equal(4); + config.Valid.Should().Equal(true); + } + + /// + /// Tests a json file with valid configuration. + /// + [Fact] + public void InvalidConfig_ShouldReturnDefaults() + { + // read a valid json config sample. + var data = _jsonConfigReader.Read("invalid-config.json"); + var config = new BanConfig(data); + + // as we have just supplied an invalid config, we should get a valid config object with default values + config.Enabled.Should().Equal(false); + config.Duration.Should().Equal(600); + config.InvalidPercent.Should().Equal(50); + config.CheckThreshold.Should().Equal(100); + config.PurgeInterval.Should().Equal(300); + config.Valid.Should().Equal(true); + } + + /// + /// Tests a json file with valid configuration. + /// + [Fact] + public void InvalidJson_ShouldBeInvalid() + { + // read a valid json config sample. + var data = _jsonConfigReader.Read("invalid-json.json"); + var config = new BanConfig(data); + + // as we have just supplied an invalid json, config should be just marked as invalid. + config.Valid.Should().Equal(false); + } + } +} diff --git a/src/Tests/Banning/valid-config.json b/src/Tests/Banning/valid-config.json new file mode 100644 index 000000000..20e50dd3a --- /dev/null +++ b/src/Tests/Banning/valid-config.json @@ -0,0 +1,7 @@ +{ + "enabled": true, + "duration": 1, + "invalidPercent": 2, + "checkThreshold": 3, + "purgeInterval": 4 +} \ No newline at end of file diff --git a/src/Tests/Tests.csproj b/src/Tests/Tests.csproj index dcc7b929a..56cbe278d 100644 --- a/src/Tests/Tests.csproj +++ b/src/Tests/Tests.csproj @@ -39,6 +39,9 @@ False ..\..\build\packages\BouncyCastle.1.7.0\lib\Net40-Client\BouncyCastle.Crypto.dll + + ..\..\build\packages\Nancy.0.23.2\lib\net40\Nancy.dll + False ..\..\build\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll @@ -61,6 +64,7 @@ + @@ -80,6 +84,15 @@ + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + diff --git a/src/Tests/invalid-config.json b/src/Tests/invalid-config.json new file mode 100644 index 000000000..8593c62d9 --- /dev/null +++ b/src/Tests/invalid-config.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/src/Tests/invalid-json.json b/src/Tests/invalid-json.json new file mode 100644 index 000000000..b7cf0b223 --- /dev/null +++ b/src/Tests/invalid-json.json @@ -0,0 +1 @@ +this is an invalid json file used for test \ No newline at end of file diff --git a/src/Tests/packages.config b/src/Tests/packages.config index f615f09fa..b692ddcf5 100644 --- a/src/Tests/packages.config +++ b/src/Tests/packages.config @@ -1,6 +1,7 @@  + From b0b4989bde4e4922ec0aa1557257ffd11e9e5690 Mon Sep 17 00:00:00 2001 From: bonesoul Date: Tue, 19 Aug 2014 18:57:06 +0300 Subject: [PATCH 02/81] updated metrics.net and nancy.customerrors nuget packages - needs testing. --- src/CoiniumServ/CoiniumServ.csproj | 12 ++++-------- src/CoiniumServ/packages.config | 6 +++--- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/CoiniumServ/CoiniumServ.csproj b/src/CoiniumServ/CoiniumServ.csproj index 77a31b136..e9a71ab37 100644 --- a/src/CoiniumServ/CoiniumServ.csproj +++ b/src/CoiniumServ/CoiniumServ.csproj @@ -62,26 +62,22 @@ False ..\..\build\packages\HashLib.2.0.1\lib\net40\HashLib.dll - + False - ..\..\build\packages\Metrics.NET.0.1.10\lib\net45\Metrics.dll + ..\..\build\packages\Metrics.NET.0.1.11\lib\net45\Metrics.dll False ..\..\build\packages\Nancy.0.23.2\lib\net40\Nancy.dll - + False - ..\..\build\packages\Nancy.CustomErrors.1.0.5.0\lib\net40\Nancy.CustomErrors.dll + ..\..\build\packages\Nancy.CustomErrors.1.0.6.0\lib\net40\Nancy.CustomErrors.dll False ..\..\build\packages\Nancy.Hosting.Self.0.23.2\lib\net40\Nancy.Hosting.Self.dll - - False - ..\..\build\packages\NancyFx.Metrics.0.1.10\lib\net45\Nancy.Metrics.dll - False ..\..\build\packages\Nancy.Viewengines.Razor.0.23.2\lib\net40\Nancy.ViewEngines.Razor.dll diff --git a/src/CoiniumServ/packages.config b/src/CoiniumServ/packages.config index 1fbb814ab..7cadd2676 100644 --- a/src/CoiniumServ/packages.config +++ b/src/CoiniumServ/packages.config @@ -4,13 +4,13 @@ - + - + - + \ No newline at end of file From 3ed2190410989ae84fdb50ee9e3cb7927d133bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Uslu?= Date: Tue, 19 Aug 2014 23:01:45 +0300 Subject: [PATCH 03/81] Fixed a bug in FileHelpers:GetAbsolutePath() that was preventing static files being served within windows. Improved RootPathProviders.cs. Downgraded metrics.net and nancy.customerrors. --- src/CoiniumServ/CoiniumServ.csproj | 12 ++++++++---- src/CoiniumServ/Server/Web/RootPathProvider.cs | 9 +++++++-- src/CoiniumServ/Server/Web/WebBootstrapper.cs | 4 ++-- src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs | 2 +- src/CoiniumServ/packages.config | 6 +++--- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/CoiniumServ/CoiniumServ.csproj b/src/CoiniumServ/CoiniumServ.csproj index e9a71ab37..77a31b136 100644 --- a/src/CoiniumServ/CoiniumServ.csproj +++ b/src/CoiniumServ/CoiniumServ.csproj @@ -62,22 +62,26 @@ False ..\..\build\packages\HashLib.2.0.1\lib\net40\HashLib.dll - + False - ..\..\build\packages\Metrics.NET.0.1.11\lib\net45\Metrics.dll + ..\..\build\packages\Metrics.NET.0.1.10\lib\net45\Metrics.dll False ..\..\build\packages\Nancy.0.23.2\lib\net40\Nancy.dll - + False - ..\..\build\packages\Nancy.CustomErrors.1.0.6.0\lib\net40\Nancy.CustomErrors.dll + ..\..\build\packages\Nancy.CustomErrors.1.0.5.0\lib\net40\Nancy.CustomErrors.dll False ..\..\build\packages\Nancy.Hosting.Self.0.23.2\lib\net40\Nancy.Hosting.Self.dll + + False + ..\..\build\packages\NancyFx.Metrics.0.1.10\lib\net45\Nancy.Metrics.dll + False ..\..\build\packages\Nancy.Viewengines.Razor.0.23.2\lib\net40\Nancy.ViewEngines.Razor.dll diff --git a/src/CoiniumServ/Server/Web/RootPathProvider.cs b/src/CoiniumServ/Server/Web/RootPathProvider.cs index 6a687eeb1..391d03574 100644 --- a/src/CoiniumServ/Server/Web/RootPathProvider.cs +++ b/src/CoiniumServ/Server/Web/RootPathProvider.cs @@ -26,11 +26,16 @@ namespace CoiniumServ.Server.Web { - public class CustomRootPathProvider : IRootPathProvider + public class RootPathProvider : IRootPathProvider { + private string _rootPath; // root path of the web files. + public string GetRootPath() { - return FileHelpers.GetAbsolutePath("web/default"); + if (string.IsNullOrEmpty(_rootPath)) // make sure we already determined the absolute root path + _rootPath = FileHelpers.GetAbsolutePath("web/default"); // if not yet do so. + + return _rootPath; } } } diff --git a/src/CoiniumServ/Server/Web/WebBootstrapper.cs b/src/CoiniumServ/Server/Web/WebBootstrapper.cs index 3393fed8c..222ea75da 100644 --- a/src/CoiniumServ/Server/Web/WebBootstrapper.cs +++ b/src/CoiniumServ/Server/Web/WebBootstrapper.cs @@ -49,7 +49,7 @@ protected override void ApplicationStartup(TinyIoCContainer container, IPipeline var configManager = container.Resolve(); // todo: enable authentication support - if(configManager.WebServerConfig.Backend.MetricsEnabled) + if (configManager.WebServerConfig.Backend.MetricsEnabled) Metric.Config.WithNancy(config => config.WithMetricsModule("/admin/metrics")); CustomErrors.Enable(pipelines, new ErrorConfiguration()); @@ -57,7 +57,7 @@ protected override void ApplicationStartup(TinyIoCContainer container, IPipeline protected override IRootPathProvider RootPathProvider { - get { return new CustomRootPathProvider(); } + get { return new RootPathProvider(); } } protected override void ConfigureConventions(NancyConventions nancyConventions) diff --git a/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs b/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs index 1a22ce36f..ee6c3b711 100644 --- a/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs +++ b/src/CoiniumServ/Utils/Helpers/IO/FileHelpers.cs @@ -39,7 +39,7 @@ public static string AssemblyRoot public static string GetAbsolutePath(string file) { - var path = string.Format("{0}/{1}", AssemblyRoot, file); // first get the path as *unix paths. + var path = string.Format("{0}{1}", AssemblyRoot, file); // first get the path as *unix paths. if (PlatformManager.Framework == Frameworks.DotNet) // if we are running on windows, path = path.Replace('/', '\\'); // replace to windows-native paths. diff --git a/src/CoiniumServ/packages.config b/src/CoiniumServ/packages.config index 7cadd2676..1fbb814ab 100644 --- a/src/CoiniumServ/packages.config +++ b/src/CoiniumServ/packages.config @@ -4,13 +4,13 @@ - + - + - + \ No newline at end of file From a483731bc2c0c005840ef1142c48ae178569fda4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Uslu?= Date: Tue, 19 Aug 2014 23:20:18 +0300 Subject: [PATCH 04/81] Fixed a bug with rendering nancy-errors related to layout.cshtml. --- src/CoiniumServ/CoiniumServ.csproj | 1 - .../Server/Web/Models/ErrorModel.cs | 31 ------------------- src/CoiniumServ/Server/Web/Modules/Pool.cs | 7 +++-- src/CoiniumServ/web/default/error.cshtml | 2 +- src/CoiniumServ/web/default/layout.cshtml | 20 +++++++----- 5 files changed, 18 insertions(+), 43 deletions(-) delete mode 100644 src/CoiniumServ/Server/Web/Models/ErrorModel.cs diff --git a/src/CoiniumServ/CoiniumServ.csproj b/src/CoiniumServ/CoiniumServ.csproj index 77a31b136..148662013 100644 --- a/src/CoiniumServ/CoiniumServ.csproj +++ b/src/CoiniumServ/CoiniumServ.csproj @@ -214,7 +214,6 @@ - diff --git a/src/CoiniumServ/Server/Web/Models/ErrorModel.cs b/src/CoiniumServ/Server/Web/Models/ErrorModel.cs deleted file mode 100644 index 03088b8a3..000000000 --- a/src/CoiniumServ/Server/Web/Models/ErrorModel.cs +++ /dev/null @@ -1,31 +0,0 @@ -#region License -// -// CoiniumServ - Crypto Currency Mining Pool Server Software -// Copyright (C) 2013 - 2014, CoiniumServ Project - http://www.coinium.org -// http://www.coiniumserv.com - https://github.com/CoiniumServ/CoiniumServ -// -// This software is dual-licensed: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// For the terms of this license, see licenses/gpl_v3.txt. -// -// Alternatively, you can license this software under a commercial -// license or white-label it as set out in licenses/commercial.txt. -// -#endregion - -namespace CoiniumServ.Server.Web.Models -{ - public class ErrorModel - { - public string Summary { get; set; } - public string Details { get; set; } - } -} diff --git a/src/CoiniumServ/Server/Web/Modules/Pool.cs b/src/CoiniumServ/Server/Web/Modules/Pool.cs index a7e174f5a..6b8d4711d 100644 --- a/src/CoiniumServ/Server/Web/Modules/Pool.cs +++ b/src/CoiniumServ/Server/Web/Modules/Pool.cs @@ -25,6 +25,7 @@ using CoiniumServ.Server.Web.Models; using CoiniumServ.Statistics; using Nancy; +using Nancy.CustomErrors; namespace CoiniumServ.Server.Web.Modules { @@ -44,7 +45,7 @@ public PoolModule(IPoolManager poolManager, IStatistics statistics) ViewBag.Title = "Error"; ViewBag.Heading = "An error occured!"; - return View["error", new ErrorModel + return View["error", new ErrorViewModel { Summary = "Pool not found", Details = string.Format("The requested pool does not exist: {0}", _.slug) @@ -73,7 +74,7 @@ public PoolModule(IPoolManager poolManager, IStatistics statistics) ViewBag.Title = "Error"; ViewBag.Heading = "An error occured!"; - return View["error", new ErrorModel + return View["error", new ErrorViewModel { Summary = "Pool not found", Details = string.Format("The requested pool does not exist: {0}", _.slug) @@ -102,7 +103,7 @@ public PoolModule(IPoolManager poolManager, IStatistics statistics) ViewBag.Title = "Error"; ViewBag.Heading = "An error occured!"; - return View["error", new ErrorModel + return View["error", new ErrorViewModel { Summary = "Pool not found", Details = string.Format("The requested pool does not exist: {0}", _.slug) diff --git a/src/CoiniumServ/web/default/error.cshtml b/src/CoiniumServ/web/default/error.cshtml index 758dc8cb8..720c8340f 100644 --- a/src/CoiniumServ/web/default/error.cshtml +++ b/src/CoiniumServ/web/default/error.cshtml @@ -1,4 +1,4 @@ -@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase +@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase @{ Layout = "layout.cshtml"; } diff --git a/src/CoiniumServ/web/default/layout.cshtml b/src/CoiniumServ/web/default/layout.cshtml index e3dd5ccb8..44380eb54 100644 --- a/src/CoiniumServ/web/default/layout.cshtml +++ b/src/CoiniumServ/web/default/layout.cshtml @@ -36,13 +36,16 @@