2
0

TestBase.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. using Microsoft.VisualStudio.TestTools.UnitTesting;
  5. namespace Renci.SshNet.Tests.Common
  6. {
  7. public abstract class TestBase
  8. {
  9. private static readonly Assembly ExecutingAssembly = Assembly.GetExecutingAssembly();
  10. [TestInitialize]
  11. public void Init()
  12. {
  13. OnInit();
  14. }
  15. [TestCleanup]
  16. public void Cleanup()
  17. {
  18. OnCleanup();
  19. }
  20. protected virtual void OnInit()
  21. {
  22. }
  23. protected virtual void OnCleanup()
  24. {
  25. }
  26. /// <summary>
  27. /// Creates the test file.
  28. /// </summary>
  29. /// <param name="fileName">Name of the file.</param>
  30. /// <param name="size">Size in megabytes.</param>
  31. protected void CreateTestFile(string fileName, int size)
  32. {
  33. using (var testFile = File.Create(fileName))
  34. {
  35. var random = new Random();
  36. for (int i = 0; i < 1024 * size; i++)
  37. {
  38. var buffer = new byte[1024];
  39. random.NextBytes(buffer);
  40. testFile.Write(buffer, 0, buffer.Length);
  41. }
  42. }
  43. }
  44. internal static Stream GetData(string name)
  45. {
  46. string resourceName = $"Renci.SshNet.Tests.Data.{name}";
  47. return ExecutingAssembly.GetManifestResourceStream(resourceName)
  48. ?? throw new ArgumentException($"Resource '{resourceName}' not found in assembly '{typeof(TestBase).Assembly.FullName}'.");
  49. }
  50. }
  51. }