Skip to content

Commit

Permalink
Implemented some checks to ensure * cannot be used as a scope (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski authored Sep 11, 2024
1 parent e9844c3 commit 3c6ef87
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
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

0 comments on commit 3c6ef87

Please sign in to comment.