ManagedFileSystemTests.cs 2.8 KB

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