diff options
| author | pn04 <33034498+pn04@users.noreply.github.com> | 2022-10-13 00:36:56 +0530 |
|---|---|---|
| committer | pn04 <33034498+pn04@users.noreply.github.com> | 2022-10-13 00:36:56 +0530 |
| commit | 66523b27f6120836ba82cfb6f56b679d7b37f58a (patch) | |
| tree | a6a508162642409548ed0f928bbde36c68deb9d5 | |
| parent | dddcc1314d2260a21fe62e61b92f446a15357b04 (diff) | |
| download | slskd-66523b27f6120836ba82cfb6f56b679d7b37f58a.tar.gz | |
added a DirectoryIsRelative validation attribute
*added DirectoryIsRelative attribute to check if a directory is relative
*added DirectoryIsRelative to ContentPath
*added unit test cases
| -rw-r--r-- | src/slskd/Common/Validation/DirectoryIsRelativeAttribute.cs | 38 | ||||
| -rw-r--r-- | src/slskd/Core/Options.cs | 1 | ||||
| -rw-r--r-- | tests/slskd.Tests.Unit/Common/Validation/DirectoryIsRelativeAttributeTests.cs | 53 |
3 files changed, 92 insertions, 0 deletions
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 file="DirectoryIsRelativeAttribute.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Validation +{ + using System.ComponentModel.DataAnnotations; + using System.IO; + + /// <summary> + /// Validates that the specified path is relative. + /// </summary> + 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<ValidationResult>(); + 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<ValidationResult>(); + + //act + var actual = Validator.TryValidateObject(target, validationContext, validationResults, true); + + //asert + Assert.True(actual); + } + + public class ValidationTarget + { + [DirectoryIsRelative] + public string ContentPath { get; set; } + } + } +} |
