FileExtTests.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Masuit.Tools.Files;
  7. using Xunit;
  8. namespace Masuit.Tools.Abstractions.Test.Files;
  9. public class FileExtTests
  10. {
  11. public string GetTestFile(string n)
  12. {
  13. string testFilePath = Path.Combine(Path.GetTempPath(), $"testfile{n}.txt");
  14. if (File.Exists(testFilePath))
  15. {
  16. File.Delete(testFilePath);
  17. }
  18. File.WriteAllText(testFilePath, "This is a test file.");
  19. return testFilePath;
  20. }
  21. [Fact]
  22. public void CopyToFile_ShouldCopyFile()
  23. {
  24. var _testFilePath = GetTestFile("1");
  25. string _copyFilePath = Path.Combine(Path.GetTempPath(), "copyfile.txt");
  26. using var fs = new FileStream(_testFilePath, FileMode.Open, FileAccess.Read);
  27. fs.CopyToFile(_copyFilePath);
  28. Assert.True(File.Exists(_copyFilePath));
  29. Assert.Equal(File.ReadAllText(_testFilePath), File.ReadAllText(_copyFilePath));
  30. }
  31. [Fact]
  32. public async Task CopyToFileAsync_ShouldCopyFile()
  33. {
  34. string _copyFilePath = Path.Combine(Path.GetTempPath(), "copyfile2.txt");
  35. var _testFilePath = GetTestFile("2");
  36. using var fs = new FileStream(_testFilePath, FileMode.Open, FileAccess.Read);
  37. await fs.CopyToFileAsync(_copyFilePath);
  38. Assert.True(File.Exists(_copyFilePath));
  39. Assert.Equal(File.ReadAllText(_testFilePath), File.ReadAllText(_copyFilePath));
  40. }
  41. [Fact]
  42. public void SaveFile_ShouldSaveFile()
  43. {
  44. string _copyFilePath = Path.Combine(Path.GetTempPath(), "copyfile3.txt");
  45. using var ms = new MemoryStream(Encoding.UTF8.GetBytes("This is a test file."));
  46. ms.SaveFile(_copyFilePath);
  47. Assert.True(File.Exists(_copyFilePath));
  48. Assert.Equal("This is a test file.", File.ReadAllText(_copyFilePath));
  49. }
  50. [Fact]
  51. public async Task SaveFileAsync_ShouldSaveFile()
  52. {
  53. string _copyFilePath = Path.Combine(Path.GetTempPath(), "copyfile4.txt");
  54. using var ms = new MemoryStream(Encoding.UTF8.GetBytes("This is a test file."));
  55. await ms.SaveFileAsync(_copyFilePath);
  56. Assert.True(File.Exists(_copyFilePath));
  57. Assert.Equal("This is a test file.", File.ReadAllText(_copyFilePath));
  58. }
  59. [Fact]
  60. public void GetFileMD5_ShouldReturnMD5Hash()
  61. {
  62. var _testFilePath = GetTestFile("3");
  63. using var fs = new FileStream(_testFilePath, FileMode.Open, FileAccess.Read);
  64. var md5 = fs.GetFileMD5();
  65. using var md5Crypto = MD5.Create();
  66. var expectedMd5 = BitConverter.ToString(md5Crypto.ComputeHash(Encoding.UTF8.GetBytes("This is a test file."))).Replace("-", "").ToLower();
  67. Assert.Equal(expectedMd5, md5);
  68. }
  69. [Fact]
  70. public void GetFileSha1_ShouldReturnSha1Hash()
  71. {
  72. var _testFilePath = GetTestFile("4");
  73. using var fs = new FileStream(_testFilePath, FileMode.Open, FileAccess.Read);
  74. var sha1 = fs.GetFileSha1();
  75. using var sha1Crypto = SHA1.Create();
  76. var expectedSha1 = BitConverter.ToString(sha1Crypto.ComputeHash(Encoding.UTF8.GetBytes("This is a test file."))).Replace("-", "").ToLower();
  77. Assert.Equal(expectedSha1, sha1);
  78. }
  79. [Fact]
  80. public void GetFileSha256_ShouldReturnSha256Hash()
  81. {
  82. var _testFilePath = GetTestFile("5");
  83. using var fs = new FileStream(_testFilePath, FileMode.Open, FileAccess.Read);
  84. var sha256 = fs.GetFileSha256();
  85. using var sha256Crypto = SHA256.Create();
  86. var expectedSha256 = BitConverter.ToString(sha256Crypto.ComputeHash(Encoding.UTF8.GetBytes("This is a test file."))).Replace("-", "").ToLower();
  87. Assert.Equal(expectedSha256, sha256);
  88. }
  89. [Fact]
  90. public void GetFileSha512_ShouldReturnSha512Hash()
  91. {
  92. var _testFilePath = GetTestFile("6");
  93. using var fs = new FileStream(_testFilePath, FileMode.Open, FileAccess.Read);
  94. var sha512 = fs.GetFileSha512();
  95. using var sha512Crypto = SHA512.Create();
  96. var expectedSha512 = BitConverter.ToString(sha512Crypto.ComputeHash(Encoding.UTF8.GetBytes("This is a test file."))).Replace("-", "").ToLower();
  97. Assert.Equal(expectedSha512, sha512);
  98. }
  99. }