From 66523b27f6120836ba82cfb6f56b679d7b37f58a Mon Sep 17 00:00:00 2001 From: pn04 <33034498+pn04@users.noreply.github.com> Date: Thu, 13 Oct 2022 00:36:56 +0530 Subject: added a DirectoryIsRelative validation attribute *added DirectoryIsRelative attribute to check if a directory is relative *added DirectoryIsRelative to ContentPath *added unit test cases --- .../Validation/DirectoryIsRelativeAttribute.cs | 38 ++++++++++++++++ src/slskd/Core/Options.cs | 1 + .../DirectoryIsRelativeAttributeTests.cs | 53 ++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs create mode 100644 tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs diff --git a/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs b/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs new file mode 100644 index 00000000..e4e13bc8 --- /dev/null +++ b/src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero 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 Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Validation +{ + using System.ComponentModel.DataAnnotations; + using System.IO; + + /// + /// Validates that the specified path is relative. + /// + public class DirectoryIsRelativeAttribute : ValidationAttribute + { + protected override ValidationResult IsValid(object value, ValidationContext validationContext) + { + if (Path.IsPathRooted(value?.ToString())) + { + return new ValidationResult($"The {validationContext.DisplayName} field specifies a non-realtive directory path: '{value}'."); + } + + return ValidationResult.Success; + } + } +} \ No newline at end of file diff --git a/src/slskd/Core/Options.cs b/src/slskd/Core/Options.cs index 4c146d32..484a514b 100644 --- a/src/slskd/Core/Options.cs +++ b/src/slskd/Core/Options.cs @@ -1167,6 +1167,7 @@ namespace slskd [Description("path to static web content")] [StringLength(255, MinimumLength = 1)] [DirectoryExists] + [DirectoryIsRelative] [RequiresRestart] public string ContentPath { get; init; } = "wwwroot"; diff --git a/tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs new file mode 100644 index 00000000..6fd3fbce --- /dev/null +++ b/tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs @@ -0,0 +1,53 @@ +namespace slskd.Tests.Unit.Common +{ + using slskd.Validation; + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using Xunit; + + public class DirectoryIsRelativeAttributeTests : DirectoryIsRelativeAttribute + { + [Theory] + [InlineData(@"\home\abc\")] + [InlineData(@"\\home\abc\")] + [InlineData(@"C:\home\abc\")] + public void IsValidReturnsErrorResultWhenPathIsNonRelative(string path) + { + //arrange + var target = new ValidationTarget { ContentPath = path }; + var validationContext = new ValidationContext(target); + var validationResults = new List(); + var errorMessage = $"The {nameof(target.ContentPath)} field specifies a non-realtive directory path: '{target.ContentPath}'."; + + //act + var actual = Validator.TryValidateObject(target, validationContext, validationResults, true); + + //asert + Assert.False(actual); + Assert.Equal(errorMessage, validationResults[0].ErrorMessage); + } + + [Theory] + [InlineData(@"home\abc\")] + [InlineData(@"..\home\abc\")] + public void IsValidReturnsSuccessResultWhenPathIsRelative(string path) + { + //arrange + var target = new ValidationTarget { ContentPath = path }; + var validationContext = new ValidationContext(target); + var validationResults = new List(); + + //act + var actual = Validator.TryValidateObject(target, validationContext, validationResults, true); + + //asert + Assert.True(actual); + } + + public class ValidationTarget + { + [DirectoryIsRelative] + public string ContentPath { get; set; } + } + } +} -- cgit v1.2.3 From aada6016541b8a1d0d397052daa9b6c40f8a4277 Mon Sep 17 00:00:00 2001 From: pn04 <33034498+pn04@users.noreply.github.com> Date: Thu, 13 Oct 2022 23:17:29 +0530 Subject: removed DirectoryIsRelative attribute related tests --- .../DirectoryIsRelativeAttributeTests.cs | 53 ---------------------- 1 file changed, 53 deletions(-) delete mode 100644 tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs diff --git a/tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs b/tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs deleted file mode 100644 index 6fd3fbce..00000000 --- a/tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs +++ /dev/null @@ -1,53 +0,0 @@ -namespace slskd.Tests.Unit.Common -{ - using slskd.Validation; - using System.Collections.Generic; - using System.ComponentModel.DataAnnotations; - using Xunit; - - public class DirectoryIsRelativeAttributeTests : DirectoryIsRelativeAttribute - { - [Theory] - [InlineData(@"\home\abc\")] - [InlineData(@"\\home\abc\")] - [InlineData(@"C:\home\abc\")] - public void IsValidReturnsErrorResultWhenPathIsNonRelative(string path) - { - //arrange - var target = new ValidationTarget { ContentPath = path }; - var validationContext = new ValidationContext(target); - var validationResults = new List(); - var errorMessage = $"The {nameof(target.ContentPath)} field specifies a non-realtive directory path: '{target.ContentPath}'."; - - //act - var actual = Validator.TryValidateObject(target, validationContext, validationResults, true); - - //asert - Assert.False(actual); - Assert.Equal(errorMessage, validationResults[0].ErrorMessage); - } - - [Theory] - [InlineData(@"home\abc\")] - [InlineData(@"..\home\abc\")] - public void IsValidReturnsSuccessResultWhenPathIsRelative(string path) - { - //arrange - var target = new ValidationTarget { ContentPath = path }; - var validationContext = new ValidationContext(target); - var validationResults = new List(); - - //act - var actual = Validator.TryValidateObject(target, validationContext, validationResults, true); - - //asert - Assert.True(actual); - } - - public class ValidationTarget - { - [DirectoryIsRelative] - public string ContentPath { get; set; } - } - } -} -- cgit v1.2.3