Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions src/Sftp/Sftp.Test/ScenarioTests/CmdletParameterCompatibilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,168 @@ public void TestParameterSetLogicMatchesAzureCLI()
var publicKeyParam = publicKeyProp?.GetCustomAttributes(typeof(ParameterAttribute), false)[0] as ParameterAttribute;
Assert.False(publicKeyParam?.Mandatory ?? true);
}

#region BufferSizeInBytes Parameter Tests

[Fact]
public void TestBufferSizeInBytesParameterExists()
{
// Test that BufferSizeInBytes parameter exists on Connect-AzSftp
var connectCommand = new ConnectAzSftpCommand();

var bufferSizeProp = connectCommand.GetType().GetProperty("BufferSizeInBytes");
Assert.NotNull(bufferSizeProp);
Assert.Equal(typeof(int), bufferSizeProp.PropertyType);
}

[Fact]
public void TestBufferSizeInBytesDefaultValue()
{
// Test that BufferSizeInBytes has correct default value (256 * 1024 = 262144)
var connectCommand = new ConnectAzSftpCommand();

Assert.Equal(256 * 1024, connectCommand.BufferSizeInBytes);
Assert.Equal(262144, connectCommand.BufferSizeInBytes);
Comment on lines +265 to +269
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default-value test duplicates the production constant as 256 * 1024 and 262144. Prefer asserting against SftpConstants.DefaultBufferSizeBytes so tests remain correct if the default is ever adjusted, and you only have one source of truth.

Suggested change
// Test that BufferSizeInBytes has correct default value (256 * 1024 = 262144)
var connectCommand = new ConnectAzSftpCommand();
Assert.Equal(256 * 1024, connectCommand.BufferSizeInBytes);
Assert.Equal(262144, connectCommand.BufferSizeInBytes);
// Test that BufferSizeInBytes has correct default value
var connectCommand = new ConnectAzSftpCommand();
Assert.Equal(SftpConstants.DefaultBufferSizeBytes, connectCommand.BufferSizeInBytes);

Copilot uses AI. Check for mistakes.
}

[Fact]
public void TestBufferSizeInBytesHasValidateRangeAttribute()
{
// Test that BufferSizeInBytes has proper validation
var connectCommand = new ConnectAzSftpCommand();

var bufferSizeProp = connectCommand.GetType().GetProperty("BufferSizeInBytes");
var validateAttrs = bufferSizeProp?.GetCustomAttributes(typeof(ValidateRangeAttribute), false);

Assert.NotNull(validateAttrs);
Assert.True(validateAttrs.Length > 0);

var validateRange = validateAttrs[0] as ValidateRangeAttribute;
Assert.NotNull(validateRange);
Assert.Equal(1, validateRange.MinRange); // Minimum should be 1
}

[Fact]
public void TestBufferSizeInBytesHasHelpMessage()
{
// Test that BufferSizeInBytes has helpful documentation
var connectCommand = new ConnectAzSftpCommand();

var bufferSizeProp = connectCommand.GetType().GetProperty("BufferSizeInBytes");
var paramAttrs = bufferSizeProp?.GetCustomAttributes(typeof(ParameterAttribute), false);

Assert.NotNull(paramAttrs);
Assert.True(paramAttrs.Length > 0);

var paramAttr = paramAttrs[0] as ParameterAttribute;
Assert.NotNull(paramAttr);
Assert.False(string.IsNullOrEmpty(paramAttr.HelpMessage));
Assert.Contains("buffer", paramAttr.HelpMessage.ToLower());
}

[Fact]
public void TestBufferSizeInBytesIsOptional()
{
// Test that BufferSizeInBytes is not mandatory (has default value)
var connectCommand = new ConnectAzSftpCommand();

var bufferSizeProp = connectCommand.GetType().GetProperty("BufferSizeInBytes");
var paramAttrs = bufferSizeProp?.GetCustomAttributes(typeof(ParameterAttribute), false);

Assert.NotNull(paramAttrs);
Assert.True(paramAttrs.Length > 0);

var paramAttr = paramAttrs[0] as ParameterAttribute;
Assert.NotNull(paramAttr);
Assert.False(paramAttr.Mandatory);
}

#endregion

#region StorageAccountEndpoint Parameter Tests

[Fact]
public void TestStorageAccountEndpointParameterExists()
{
// Test that StorageAccountEndpoint parameter exists on Connect-AzSftp
var connectCommand = new ConnectAzSftpCommand();

var endpointProp = connectCommand.GetType().GetProperty("StorageAccountEndpoint");
Assert.NotNull(endpointProp);
Assert.Equal(typeof(string), endpointProp.PropertyType);
}

[Fact]
public void TestStorageAccountEndpointDefaultValue()
{
// Test that StorageAccountEndpoint defaults to null (auto-detect from Azure environment)
var connectCommand = new ConnectAzSftpCommand();

Assert.Null(connectCommand.StorageAccountEndpoint);
}

[Fact]
public void TestStorageAccountEndpointHasValidateNotNullOrEmptyAttribute()
{
// Test that StorageAccountEndpoint has proper validation when provided
var connectCommand = new ConnectAzSftpCommand();

var endpointProp = connectCommand.GetType().GetProperty("StorageAccountEndpoint");
var validateAttrs = endpointProp?.GetCustomAttributes(typeof(ValidateNotNullOrEmptyAttribute), false);

Assert.NotNull(validateAttrs);
Assert.True(validateAttrs.Length > 0);
}

[Fact]
public void TestStorageAccountEndpointHasHelpMessage()
{
// Test that StorageAccountEndpoint has helpful documentation
var connectCommand = new ConnectAzSftpCommand();

var endpointProp = connectCommand.GetType().GetProperty("StorageAccountEndpoint");
var paramAttrs = endpointProp?.GetCustomAttributes(typeof(ParameterAttribute), false);

Assert.NotNull(paramAttrs);
Assert.True(paramAttrs.Length > 0);

var paramAttr = paramAttrs[0] as ParameterAttribute;
Assert.NotNull(paramAttr);
Assert.False(string.IsNullOrEmpty(paramAttr.HelpMessage));
Assert.Contains("endpoint", paramAttr.HelpMessage.ToLower());
}

[Fact]
public void TestStorageAccountEndpointIsOptional()
{
// Test that StorageAccountEndpoint is not mandatory (uses auto-detection by default)
var connectCommand = new ConnectAzSftpCommand();

var endpointProp = connectCommand.GetType().GetProperty("StorageAccountEndpoint");
var paramAttrs = endpointProp?.GetCustomAttributes(typeof(ParameterAttribute), false);

Assert.NotNull(paramAttrs);
Assert.True(paramAttrs.Length > 0);

var paramAttr = paramAttrs[0] as ParameterAttribute;
Assert.NotNull(paramAttr);
Assert.False(paramAttr.Mandatory);
}

[Fact]
public void TestStorageAccountEndpointAvailableInAllParameterSets()
{
// Test that StorageAccountEndpoint is available in all parameter sets
var connectCommand = new ConnectAzSftpCommand();

var endpointProp = connectCommand.GetType().GetProperty("StorageAccountEndpoint");
var paramAttrs = endpointProp?.GetCustomAttributes(typeof(ParameterAttribute), false);

Assert.NotNull(paramAttrs);
// Should have multiple Parameter attributes (one for each parameter set)
Assert.True(paramAttrs.Length >= 4, "StorageAccountEndpoint should be available in all parameter sets");
}

#endregion
}
}
77 changes: 77 additions & 0 deletions src/Sftp/Sftp.Test/ScenarioTests/ConnectAzSftpTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,80 @@ function Test-ConnectAzSftpParameterValidation
# Test certificate authentication requires both certificate and private key
Assert-Throws { Connect-AzSftp -StorageAccount "test" -CertificateFile "cert.pub" } "CertificateFile requires PrivateKeyFile"
}

<#
.SYNOPSIS
Test Connect-AzSftp with BufferSizeInBytes parameter
#>
function Test-ConnectAzSftpWithBufferSizeInBytes
{
$storageAccountName = Get-StorageAccountName
$resourceGroupName = Get-ResourceGroupName

try {
# Skip test in playback mode for now
if (IsPlayback) {
return
}

# Create test storage account
$storageAccount = New-TestStorageAccount -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName

# Test connection with custom buffer size (will fail in test environment but validates parameter parsing)
try {
$result = Connect-AzSftp -StorageAccount $storageAccountName -BufferSizeInBytes 524288 -SftpArg "-o", "ConnectTimeout=1"
}
catch {
# Expected to fail in test environment - this is acceptable
Write-Host "Connection failed as expected in test environment: $($_.Exception.Message)"
}

# Test with default buffer size (256*1024 = 262144)
try {
$result = Connect-AzSftp -StorageAccount $storageAccountName -SftpArg "-o", "ConnectTimeout=1"
}
catch {
Write-Host "Connection failed as expected in test environment: $($_.Exception.Message)"
}
}
finally {
# Cleanup
Remove-TestStorageAccount -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName
Remove-AzResourceGroup -Name $resourceGroupName -Force -ErrorAction SilentlyContinue
}
}

<#
.SYNOPSIS
Test Connect-AzSftp with StorageAccountEndpoint parameter
#>
function Test-ConnectAzSftpWithStorageAccountEndpoint
{
$storageAccountName = Get-StorageAccountName
$resourceGroupName = Get-ResourceGroupName
$customEndpoint = "blob.core.custom.endpoint.net"

try {
# Skip test in playback mode for now
if (IsPlayback) {
return
}

# Create test storage account
$storageAccount = New-TestStorageAccount -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName

# Test connection with custom storage account endpoint (will fail in test environment but validates parameter parsing)
try {
$result = Connect-AzSftp -StorageAccount $storageAccountName -StorageAccountEndpoint $customEndpoint -SftpArg "-o", "ConnectTimeout=1"
}
catch {
# Expected to fail in test environment - this is acceptable
Write-Host "Connection failed as expected in test environment: $($_.Exception.Message)"
}
}
finally {
# Cleanup
Remove-TestStorageAccount -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName
Remove-AzResourceGroup -Name $resourceGroupName -Force -ErrorAction SilentlyContinue
}
}
Comment on lines 150 to 225
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The added PowerShell scenario test functions don’t provide meaningful coverage in CI: the ConnectAzSftpTestsRunner has all [Fact] tests skipped, and these new functions also early-return in playback mode and only swallow connection failures without asserting on behavior (e.g., that -B is included in the generated sftp command, or that the custom endpoint affects the hostname). Add deterministic unit tests in the existing non-skipped C# test suite (e.g., SftpUtilsTests/SFTPSession tests) to validate -B argument emission and host construction, or otherwise ensure these tests are actually executed.

Copilot uses AI. Check for mistakes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like work.
Life's Always got its own Earth Shadow.
Keep is safe.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like work.
Life's Always got its own Earth Shadow.
Keep is safe.

136 changes: 136 additions & 0 deletions src/Sftp/Sftp.Test/ScenarioTests/SftpUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,141 @@ public void AttemptConnection_WithInvalidCommand_ReturnsFalseWithError()
Assert.NotNull(result.Item3); // Error message
Assert.Contains("Failed to start", result.Item3);
}

#region BufferSize Tests

[Fact]
public void BuildSftpCommand_WithDefaultBufferSize_DoesNotIncludeBufferFlag()
{
// Arrange - default buffer size is 256 * 1024 = 262144
var session = new SFTPSession(
storageAccount: "testaccount",
username: "testuser",
host: "testaccount.blob.core.windows.net",
port: 22,
publicKeyFile: null,
privateKeyFile: null,
certFile: null,
sftpArgs: null,
sshClientFolder: null,
sshProxyFolder: null,
credentialsFolder: null,
yesWithoutPrompt: false,
bufferSizeBytes: 256 * 1024 // Default value
);
Comment on lines +390 to +405
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests hardcode the default buffer size as 256 * 1024 / 262144. Since the production default is defined in SftpConstants.DefaultBufferSizeBytes, using the constant here would keep tests aligned if the default ever changes and avoid duplicating magic numbers.

Copilot uses AI. Check for mistakes.

// Act
var command = SftpUtils.BuildSftpCommand(session);

// Assert - should NOT contain -B flag when using default buffer size
Assert.DoesNotContain("-B", command);
}

[Fact]
public void BuildSftpCommand_WithCustomBufferSize_IncludesBufferFlag()
{
// Arrange - custom buffer size
var session = new SFTPSession(
storageAccount: "testaccount",
username: "testuser",
host: "testaccount.blob.core.windows.net",
port: 22,
publicKeyFile: null,
privateKeyFile: null,
certFile: null,
sftpArgs: null,
sshClientFolder: null,
sshProxyFolder: null,
credentialsFolder: null,
yesWithoutPrompt: false,
bufferSizeBytes: 524288 // 512 KB - non-default value
);

// Act
var command = SftpUtils.BuildSftpCommand(session);

// Assert - should contain -B flag with custom buffer size
Assert.Contains("-B", command);
Assert.Contains("524288", command);
}

[Fact]
public void BuildSftpCommand_WithSmallBufferSize_IncludesBufferFlag()
{
// Arrange - smaller buffer size
var session = new SFTPSession(
storageAccount: "testaccount",
username: "testuser",
host: "testaccount.blob.core.windows.net",
port: 22,
publicKeyFile: null,
privateKeyFile: null,
certFile: null,
sftpArgs: null,
sshClientFolder: null,
sshProxyFolder: null,
credentialsFolder: null,
yesWithoutPrompt: false,
bufferSizeBytes: 32768 // 32 KB
);

// Act
var command = SftpUtils.BuildSftpCommand(session);

// Assert
Assert.Contains("-B", command);
Assert.Contains("32768", command);
}

[Fact]
public void SFTPSession_BufferSizeBytes_DefaultValue_Is256KB()
{
// Arrange & Act
var session = new SFTPSession(
storageAccount: "testaccount",
username: "testuser",
host: "testaccount.blob.core.windows.net",
port: 22
);

// Assert - default buffer size should be 256 * 1024 = 262144
Assert.Equal(256 * 1024, session.BufferSizeBytes);
}

[Fact]
public void SFTPSession_BufferSizeBytes_CanBeSetViaConstructor()
{
// Arrange & Act
var session = new SFTPSession(
storageAccount: "testaccount",
username: "testuser",
host: "testaccount.blob.core.windows.net",
port: 22,
bufferSizeBytes: 1048576 // 1 MB
);

// Assert
Assert.Equal(1048576, session.BufferSizeBytes);
}

[Fact]
public void SFTPSession_BufferSizeBytes_CanBeSetViaProperty()
{
// Arrange
var session = new SFTPSession(
storageAccount: "testaccount",
username: "testuser",
host: "testaccount.blob.core.windows.net",
port: 22
);

// Act
session.BufferSizeBytes = 2097152; // 2 MB

// Assert
Assert.Equal(2097152, session.BufferSizeBytes);
}

#endregion
}
}
Loading
Loading