ManagedFileSystemTests.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Diagnostics.CodeAnalysis;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. using AutoFixture;
  5. using AutoFixture.AutoMoq;
  6. using Emby.Server.Implementations.IO;
  7. using MediaBrowser.Model.System;
  8. using Xunit;
  9. namespace Jellyfin.Server.Implementations.Tests.IO
  10. {
  11. public class ManagedFileSystemTests
  12. {
  13. private readonly IFixture _fixture;
  14. private readonly ManagedFileSystem _sut;
  15. public ManagedFileSystemTests()
  16. {
  17. _fixture = new Fixture().Customize(new AutoMoqCustomization { ConfigureMembers = true });
  18. _sut = _fixture.Create<ManagedFileSystem>();
  19. }
  20. [Theory]
  21. [InlineData("/Volumes/Library/Sample/Music/Playlists/", "../Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Music/Beethoven/Misc/Moonlight Sonata.mp3")]
  22. [InlineData("/Volumes/Library/Sample/Music/Playlists/", "../../Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Beethoven/Misc/Moonlight Sonata.mp3")]
  23. [InlineData("/Volumes/Library/Sample/Music/Playlists/", "Beethoven/Misc/Moonlight Sonata.mp3", "/Volumes/Library/Sample/Music/Playlists/Beethoven/Misc/Moonlight Sonata.mp3")]
  24. public void MakeAbsolutePathCorrectlyHandlesRelativeFilePaths(
  25. string folderPath,
  26. string filePath,
  27. string expectedAbsolutePath)
  28. {
  29. var generatedPath = _sut.MakeAbsolutePath(folderPath, filePath);
  30. if (MediaBrowser.Common.System.OperatingSystem.Id == OperatingSystemId.Windows)
  31. {
  32. var expectedWindowsPath = expectedAbsolutePath.Replace('/', '\\');
  33. Assert.Equal(expectedWindowsPath, generatedPath.Split(':')[1]);
  34. }
  35. else
  36. {
  37. Assert.Equal(expectedAbsolutePath, generatedPath);
  38. }
  39. }
  40. [SkippableFact]
  41. public void GetFileInfo_DanglingSymlink_ExistsFalse()
  42. {
  43. Skip.If(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
  44. string testFileDir = Path.Combine(Path.GetTempPath(), "jellyfin-test-data");
  45. string testFileName = Path.Combine(testFileDir, Path.GetRandomFileName() + "-danglingsym.link");
  46. Directory.CreateDirectory(testFileDir);
  47. Assert.Equal(0, symlink("thispathdoesntexist", testFileName));
  48. Assert.True(File.Exists(testFileName));
  49. var metadata = _sut.GetFileInfo(testFileName);
  50. Assert.False(metadata.Exists);
  51. }
  52. [SuppressMessage("Naming Rules", "SA1300:ElementMustBeginWithUpperCaseLetter", Justification = "Have to")]
  53. [DllImport("libc", SetLastError = true, CharSet = CharSet.Ansi)]
  54. [DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
  55. private static extern int symlink(string target, string linkpath);
  56. }
  57. }