While Unit Testing my solution using SSH.NET I run into some problem.
Sometimes I get error from SSH.NET during test (it is weird, because it happen once 5-6 runs)
Exception: System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.Queue`1.ToArray()
at Renci.SshNet.ShellStream.Expect(Regex regex, TimeSpan timeout)
my code looks like:
string buffer = string.Empty;
using (ShellStream shellStream = _sshClient.CreateShellStream("xterm0", 80, 24, 800, 600, 1024))
{
try
{
shellStream.ErrorOccurred += ErrorHandling;
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
shellStream.WriteLine(cmd);
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
}
finally
{
if (shellStream != null)
{
shellStream.ErrorOccurred -= ErrorHandling;
}
}
}
I looked into source code, it look like problem is in ShellStram Expect Method (line 273)
it is:
if (this._incoming.Count > 0)
text = this._encoding.GetString(this._incoming.ToArray(), 0, this._incoming.Count);
it should be:
if (this._incoming.Count > 0)
{
var array = this._incoming.ToArray();
text = this._encoding.GetString(array, 0, array.Length);
}
Comments: ** Comment from web user: blakeja **
Sometimes I get error from SSH.NET during test (it is weird, because it happen once 5-6 runs)
Exception: System.ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
at System.Array.Copy(Array sourceArray, Int32 sourceIndex, Array destinationArray, Int32 destinationIndex, Int32 length, Boolean reliable)
at System.Collections.Generic.Queue`1.ToArray()
at Renci.SshNet.ShellStream.Expect(Regex regex, TimeSpan timeout)
my code looks like:
string buffer = string.Empty;
using (ShellStream shellStream = _sshClient.CreateShellStream("xterm0", 80, 24, 800, 600, 1024))
{
try
{
shellStream.ErrorOccurred += ErrorHandling;
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
shellStream.WriteLine(cmd);
buffer = shellStream.Expect(">");
if (buffer == null)
{
throw new InvalidDataException("timeout waiting for command prompt");
}
}
finally
{
if (shellStream != null)
{
shellStream.ErrorOccurred -= ErrorHandling;
}
}
}
I looked into source code, it look like problem is in ShellStram Expect Method (line 273)
it is:
if (this._incoming.Count > 0)
text = this._encoding.GetString(this._incoming.ToArray(), 0, this._incoming.Count);
it should be:
if (this._incoming.Count > 0)
{
var array = this._incoming.ToArray();
text = this._encoding.GetString(array, 0, array.Length);
}
Comments: ** Comment from web user: blakeja **
I haven't seen this exception again, I believe this fixed issue.