513fa1aa16
Comment/XML-doc changes only: - add summaries to ~10k test methods, fixtures, fakes and helpers (+ <param> on theories, <returns> on async Task methods, <inheritdoc/> on interface implementations the checker can resolve) - strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N, #NN, Component-0NN) from comments, keeping the explanatory text; reword false positives in comments only - real docs on fakes implementing BCL/library interfaces (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime) - reword the last 5 src/ false positives (CWE-1236, failures-1, year-0001, M365) Only non-comment edits: four one-line nested classes split across lines so members can carry docs. Build 0 errors / 0 warnings.
65 lines
2.6 KiB
C#
65 lines
2.6 KiB
C#
using ZB.MOM.WW.ScadaBridge.CLI;
|
|
using ZB.MOM.WW.ScadaBridge.CLI.Commands;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
|
|
|
|
/// <summary>
|
|
/// Regression tests for the exit-code contract: the design doc defines exit code 2 as "authorization
|
|
/// failure". The previous implementation keyed exit 2 solely off HTTP 403, so an
|
|
/// authorization failure the server signalled with an error <c>code</c> of
|
|
/// <c>UNAUTHORIZED</c>/<c>FORBIDDEN</c> but a different HTTP status was misreported as a
|
|
/// generic error (exit 1). Exit code 2 now keys off either signal.
|
|
/// </summary>
|
|
[Collection("Console")]
|
|
public class ExitCodeTests
|
|
{
|
|
private static int HandleQuietly(ManagementResponse response)
|
|
{
|
|
var errWriter = new StringWriter();
|
|
Console.SetError(errWriter);
|
|
try
|
|
{
|
|
return CommandHelpers.HandleResponse(response, "json");
|
|
}
|
|
finally
|
|
{
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
}
|
|
|
|
/// <summary>Verifies that an HTTP 403 response returns exit code 2.</summary>
|
|
[Fact]
|
|
public void HandleResponse_Http403_ReturnsTwo()
|
|
{
|
|
Assert.Equal(2, HandleQuietly(new ManagementResponse(403, null, "Forbidden", "FORBIDDEN")));
|
|
}
|
|
|
|
/// <summary>Verifies that an authorization error code with a status other than 403 still returns exit code 2.</summary>
|
|
/// <param name="code">The error code returned by the server.</param>
|
|
[Theory]
|
|
[InlineData("UNAUTHORIZED")]
|
|
[InlineData("FORBIDDEN")]
|
|
[InlineData("unauthorized")]
|
|
public void HandleResponse_AuthorizationCode_NonForbiddenStatus_ReturnsTwo(string code)
|
|
{
|
|
// The server signalled an authorization failure via the error code but with a
|
|
// HTTP status other than 403 status; per the documented exit-code table this is still exit 2.
|
|
Assert.Equal(2, HandleQuietly(new ManagementResponse(400, null, "Access denied", code)));
|
|
}
|
|
|
|
/// <summary>Verifies that a generic error returns exit code 1.</summary>
|
|
[Fact]
|
|
public void HandleResponse_GenericError_ReturnsOne()
|
|
{
|
|
Assert.Equal(1, HandleQuietly(new ManagementResponse(400, null, "Validation failed", "INVALID_ARGUMENT")));
|
|
}
|
|
|
|
/// <summary>Verifies that an authentication failure returns exit code 1.</summary>
|
|
[Fact]
|
|
public void HandleResponse_AuthenticationFailure_ReturnsOne()
|
|
{
|
|
// Authentication failure (bad credentials) is exit 1, distinct from authorization.
|
|
Assert.Equal(1, HandleQuietly(new ManagementResponse(401, null, "Invalid credentials", "AUTH_FAILED")));
|
|
}
|
|
}
|