TestBase.cs 1.6 KB

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