Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented some checks to ensure * cannot be used as a scope #308

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Foundatio/Storage/ScopedFileStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Foundatio.Extensions;
using Foundatio.Serializer;
using Foundatio.Utility;

Expand All @@ -13,9 +14,13 @@ public class ScopedFileStorage : IFileStorage

public ScopedFileStorage(IFileStorage storage, string scope)
{
UnscopedStorage = storage;
Scope = !String.IsNullOrWhiteSpace(scope) ? scope.Trim() : null;
UnscopedStorage = storage ?? throw new ArgumentNullException(nameof(storage));
Scope = !String.IsNullOrWhiteSpace(scope) ? scope.Trim().NormalizePath() : null;
_pathPrefix = Scope != null ? String.Concat(Scope, "/") : String.Empty;

// NOTE: we can't really check reliably using Path.GetInvalidPathChars() because each storage implementation and platform could be different.
if (Scope is not null && Scope.Contains("*"))
throw new ArgumentException("Scope cannot contain a wildcard character", nameof(scope));
}

public IFileStorage UnscopedStorage { get; private set; }
Expand Down
44 changes: 43 additions & 1 deletion tests/Foundatio.Tests/Storage/ScopedFolderFileStorageTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.IO;
using System;
using System.IO;
using System.Threading.Tasks;
using Foundatio.Extensions;
using Foundatio.Storage;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -15,6 +17,46 @@ protected override IFileStorage GetStorage()
return new ScopedFileStorage(new FolderFileStorage(o => o.Folder("|DataDirectory|\\temp")), "scoped");
}

[Fact]
public void Constructor_ShouldThrowArgumentNullException_WhenFileStorageIsNull()
{
Assert.Throws<ArgumentNullException>(() => new ScopedFileStorage(null, "scope"));
}

[InlineData("*")]
[Theory]
public void Constructor_ShouldThrowArgumentException_WhenScopeContainsInvalidCharacters(string scope)
{
var storage = GetStorage();
if (storage == null)
return;

Assert.Throws<ArgumentException>(() => new ScopedFileStorage(storage, scope));
}

[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("test")]
[InlineData(" test ")]
[InlineData("|DataDirectory|\\temp")]
[InlineData("|DataDirectory|/temp")]
[Theory]
public void Constructor_ShouldInitializeProperties_WhenArgumentsAreValid(string scope)
{
var storage = GetStorage();
if (storage == null)
return;

var scopedStorage = new ScopedFileStorage(storage, scope);
Assert.Equal(storage, scopedStorage.UnscopedStorage);

if (String.IsNullOrWhiteSpace(scope))
Assert.Null(scopedStorage.Scope);
else
Assert.Equal(scope.Trim().NormalizePath(), scopedStorage.Scope);
}

[Fact]
public override Task CanGetEmptyFileListOnMissingDirectoryAsync()
{
Expand Down