Skip to content

Commit

Permalink
Run test against async stream too.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds committed Dec 11, 2024
1 parent e2db13f commit d26239a
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions test/Tmds.Ssh.Tests/SftpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,26 @@ public async Task UploadDownloadFileWithStream(int size)
Assert.Equal(sourceData, downloadStream.ToArray());
}

[InlineData(0)]
[InlineData(10)]
[InlineData(10 * MultiPacketSize)]
[Theory]
public async Task UploadDownloadFileWithAsyncStream(int size)
{
using var sftpClient = await _sshServer.CreateSftpClientAsync();

byte[] sourceData = new byte[size];
Random.Shared.NextBytes(sourceData);
Stream uploadStream = new NonSeekableAsyncStream(sourceData);

string remotePath = $"/tmp/{Path.GetRandomFileName()}";
await sftpClient.UploadFileAsync(uploadStream, remotePath);

await using var downloadStream = new NonSeekableAsyncStream();
await sftpClient.DownloadFileAsync(remotePath, downloadStream);
Assert.Equal(sourceData, downloadStream.ToArray());
}

[Fact]
public async Task DownloadFileThrowsWhenNotFound()
{
Expand Down Expand Up @@ -1228,4 +1248,61 @@ public async Task AutoReconnect(bool autoReconnect)
await Assert.ThrowsAsync<SshConnectionClosedException>(() => client.GetFullPathAsync("").AsTask());
}
}

sealed class NonSeekableAsyncStream : Stream
{
private readonly MemoryStream _innerStream = new();

public NonSeekableAsyncStream()
{
_innerStream = new();
}

public NonSeekableAsyncStream(byte[] data)
{
_innerStream = new(data);
}

public byte[] ToArray()
=> _innerStream.ToArray();

public override bool CanRead => true;

public override bool CanSeek => false;

public override bool CanWrite => true;

public override long Length => throw new NotImplementedException();

public override long Position
{
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}

public override void Flush()
{
throw new NotImplementedException();
}

public override int Read(byte[] buffer, int offset, int count)
{
return _innerStream.Read(buffer, offset, count);
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}

public override void SetLength(long value)
{
throw new NotImplementedException();
}

public override void Write(byte[] buffer, int offset, int count)
{
_innerStream.Write(buffer, offset, count);
}
}
}

0 comments on commit d26239a

Please sign in to comment.