ExtensionsTest_Take_OffsetAndCount.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. namespace Renci.SshNet.Tests.Classes.Common
  5. {
  6. [TestClass]
  7. public class ExtensionsTest_Take_OffsetAndCount
  8. {
  9. private Random _random;
  10. [TestInitialize]
  11. public void Init()
  12. {
  13. _random = new Random();
  14. }
  15. [TestMethod]
  16. public void ShouldThrowArgumentNullExceptionWhenValueIsNull()
  17. {
  18. const byte[] value = null;
  19. const int offset = 0;
  20. const int count = 0;
  21. try
  22. {
  23. Extensions.Take(value, offset, count);
  24. Assert.Fail();
  25. }
  26. catch (ArgumentNullException ex)
  27. {
  28. Assert.IsNull(ex.InnerException);
  29. Assert.AreEqual("value", ex.ParamName);
  30. }
  31. }
  32. [TestMethod]
  33. public void ShouldReturnEmptyByteArrayWhenCountIsZero()
  34. {
  35. var value = CreateBuffer(16);
  36. const int offset = 25;
  37. const int count = 0;
  38. var actual = Extensions.Take(value, offset, count);
  39. Assert.IsNotNull(actual);
  40. Assert.AreEqual(0, actual.Length);
  41. }
  42. [TestMethod]
  43. public void ShouldReturnValueWhenCountIsEqualToLengthOfValueAndOffsetIsZero()
  44. {
  45. var value = CreateBuffer(16);
  46. const int offset = 0;
  47. var count = value.Length;
  48. var actual = Extensions.Take(value, offset, count);
  49. Assert.IsNotNull(actual);
  50. Assert.AreEqual(value.Length, actual.Length);
  51. Assert.AreEqual(value, actual);
  52. }
  53. [TestMethod]
  54. public void ShouldReturnLeadingBytesWhenOffsetIsZeroAndCountIsLessThanLengthOfValue()
  55. {
  56. var value = CreateBuffer(16);
  57. const int offset = 0;
  58. const int count = 5;
  59. var actual = Extensions.Take(value, offset, count);
  60. Assert.IsNotNull(actual);
  61. Assert.AreEqual(count, actual.Length);
  62. Assert.AreEqual(value[0], actual[0]);
  63. Assert.AreEqual(value[1], actual[1]);
  64. Assert.AreEqual(value[2], actual[2]);
  65. Assert.AreEqual(value[3], actual[3]);
  66. Assert.AreEqual(value[4], actual[4]);
  67. }
  68. [TestMethod]
  69. public void ShouldReturnCorrectPartOfValueWhenOffsetIsGreaterThanZeroAndOffsetPlusCountIsLessThanLengthOfValue()
  70. {
  71. var value = CreateBuffer(16);
  72. const int offset = 3;
  73. const int count = 4;
  74. var actual = Extensions.Take(value, offset, count);
  75. Assert.IsNotNull(actual);
  76. Assert.AreEqual(count, actual.Length);
  77. Assert.AreEqual(value[3], actual[0]);
  78. Assert.AreEqual(value[4], actual[1]);
  79. Assert.AreEqual(value[5], actual[2]);
  80. Assert.AreEqual(value[6], actual[3]);
  81. }
  82. [TestMethod]
  83. public void ShouldThrowArgumentExceptionWhenCountIsGreaterThanLengthOfValue()
  84. {
  85. var value = CreateBuffer(16);
  86. const int offset = 0;
  87. var count = value.Length + 1;
  88. try
  89. {
  90. Extensions.Take(value, offset, count);
  91. Assert.Fail();
  92. }
  93. catch (ArgumentException)
  94. {
  95. }
  96. }
  97. [TestMethod]
  98. public void ShouldThrowArgumentExceptionWhenOffsetPlusCountIsGreaterThanLengthOfValue()
  99. {
  100. var value = CreateBuffer(16);
  101. const int offset = 1;
  102. var count = value.Length;
  103. try
  104. {
  105. Extensions.Take(value, offset, count);
  106. Assert.Fail();
  107. }
  108. catch (ArgumentException)
  109. {
  110. }
  111. }
  112. private byte[] CreateBuffer(int length)
  113. {
  114. var buffer = new byte[length];
  115. _random.NextBytes(buffer);
  116. return buffer;
  117. }
  118. }
  119. }