aboutsummaryrefslogtreecommitdiff
path: root/tests/Jellyfin.Controller.Tests/IO/FileSystemHelperTests.cs
blob: b4ec2f19038fd16234ff258a9dc1e618f47b820b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
using System;
using System.IO;
using MediaBrowser.Controller.IO;
using Xunit;

namespace Jellyfin.Controller.Tests.IO;

public class FileSystemHelperTests
{
    private static readonly string _parentPath = Path.Combine(Path.GetTempPath(), "jellyfin-test", "root", "default");

    [Theory]
    [InlineData("Movies")]
    [InlineData("My Movies")]
    [InlineData("..2")]
    [InlineData("a.b")]
    public void GetChildPath_ValidName_ReturnsPathInsideParent(string name)
    {
        var path = FileSystemHelper.GetChildPath(_parentPath, name);

        Assert.Equal(Path.Combine(_parentPath, name), path);
    }

    [Theory]
    [InlineData("")]
    [InlineData("   ")]
    [InlineData(".")]
    [InlineData("..")]
    [InlineData("../..")]
    [InlineData("../../etc")]
    [InlineData("Movies/../..")]
    [InlineData("/var/lib/jellyfin/data")]
    [InlineData("sub/folder")]
    [InlineData("with\0null")]
    public void GetChildPath_EscapingName_ReturnsNull(string name)
    {
        Assert.Null(FileSystemHelper.GetChildPath(_parentPath, name));
    }

    [Theory]
    [InlineData("..\\..")]
    [InlineData("sub\\folder")]
    [InlineData("C:\\Windows")]
    public void GetChildPath_WindowsSeparator_DoesNotEscapeParent(string name)
    {
        var path = FileSystemHelper.GetChildPath(_parentPath, name);

        // On Windows these are rejected outright, on other platforms a backslash is a legal file name character.
        Assert.True(path is null || string.Equals(Path.GetDirectoryName(path), _parentPath, StringComparison.Ordinal));
    }

    [Theory]
    [InlineData("...")]
    [InlineData("Movies.")]
    [InlineData("Movies ")]
    public void GetChildPath_TrailingDotOrSpace_RejectedOnWindows(string name)
    {
        var path = FileSystemHelper.GetChildPath(_parentPath, name);

        if (OperatingSystem.IsWindows())
        {
            // Windows trims trailing dots and spaces, so the name would resolve to the parent or to a different child.
            Assert.Null(path);
        }
        else
        {
            Assert.Equal(Path.Combine(_parentPath, name), path);
        }
    }

    [Fact]
    public void GetChildPath_ParentWithTrailingSeparator_ReturnsPathInsideParent()
    {
        var path = FileSystemHelper.GetChildPath(_parentPath + Path.DirectorySeparatorChar, "Movies");

        Assert.Equal(Path.Combine(_parentPath, "Movies"), path);
    }
}