Skip to content

Commit

Permalink
extract protocol actions into constants
Browse files Browse the repository at this point in the history
  • Loading branch information
adstep committed Dec 15, 2022
1 parent 4c412fb commit 4cd292c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/WinRMSharp.IntegrationTests/ProtocolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WinRMSharp.IntegrationTests
{
public class ProtocolTests
{
private Protocol _protocol;
private readonly Protocol _protocol;

public ProtocolTests()
{
Expand Down Expand Up @@ -109,4 +109,4 @@ public async Task RunCommandExceedingOperationTimeout()
Assert.Equal(0, state.Stderr.Length);
}
}
}
}
38 changes: 18 additions & 20 deletions src/WinRMSharp.Tests/MockClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Moq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using WinRMSharp.Exceptions;
using WinRMSharp.Tests;
using WinRMSharp.Utils;

namespace WinRMSharp.Test
namespace WinRMSharp.Tests
{
public class MockClient
{
Expand All @@ -28,66 +26,66 @@ private string ResolveResponse(string message)
{
//message = Patch(message);

XDocument env = Xml.Parse(message);
XDocument env = message.Parse();

// WARNING: Despite being contrary to the XML standard, DeepEquals only
// evaluates two nodes as equal if all attributes are in matching order.
// See https://github.com/dotnet/dotnet-api-docs/issues/830
if (XNode.DeepEquals(env, Xml.Parse(Config.OPEN_SHELL_REQUEST)))
if (XNode.DeepEquals(env, Config.OPEN_SHELL_REQUEST.Parse()))
{
return Config.OPEN_SHELL_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.CLOSE_SHELL_REQUEST)))
else if (XNode.DeepEquals(env, Config.CLOSE_SHELL_REQUEST.Parse()))
{
return Config.CLOSE_SHELL_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.RUN_CMD_WITH_ARGS_REQUEST)) || XNode.DeepEquals(env, Xml.Parse(Config.RUN_CMD_WO_ARGS_REQUEST)))
else if (XNode.DeepEquals(env, Config.RUN_CMD_WITH_ARGS_REQUEST.Parse()) || XNode.DeepEquals(env, Config.RUN_CMD_WO_ARGS_REQUEST.Parse()))
{
return string.Format(Config.RUN_CMD_PS_RESPONSE, "1");
}
else if (XNode.DeepEquals(env, Xml.Parse(string.Format(Config.CLEANUP_CMD_REQUEST, "1"))) ||
XNode.DeepEquals(env, Xml.Parse(string.Format(Config.CLEANUP_CMD_REQUEST, "2"))) ||
XNode.DeepEquals(env, Xml.Parse(string.Format(Config.CLEANUP_CMD_REQUEST, "3"))))
else if (XNode.DeepEquals(env, string.Format(Config.CLEANUP_CMD_REQUEST, "1").Parse()) ||
XNode.DeepEquals(env, string.Format(Config.CLEANUP_CMD_REQUEST, "2").Parse()) ||
XNode.DeepEquals(env, string.Format(Config.CLEANUP_CMD_REQUEST, "3").Parse()))
{
return Config.CLEANUP_CMD_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(string.Format(Config.GET_CMD_PS_OUTPUT_REQUEST, "1"))))
else if (XNode.DeepEquals(env, string.Format(Config.GET_CMD_PS_OUTPUT_REQUEST, "1").Parse()))
{
return Config.GET_CMD_OUTPUT_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(string.Format(Config.GET_CMD_PS_OUTPUT_REQUEST, "2"))))
else if (XNode.DeepEquals(env, string.Format(Config.GET_CMD_PS_OUTPUT_REQUEST, "2").Parse()))
{
return Config.GET_PS_OUTPUT_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.RUN_CMD_REQ_INPUT)))
else if (XNode.DeepEquals(env, Config.RUN_CMD_REQ_INPUT.Parse()))
{
return Config.RUN_CMD_REQ_INPUT_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.RUN_CMD_SEND_INPUT)))
else if (XNode.DeepEquals(env, Config.RUN_CMD_SEND_INPUT.Parse()))
{
return Config.RUN_CMD_SEND_INPUT_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.RUN_CMD_SEND_INPUT_GET_OUTPUT)))
else if (XNode.DeepEquals(env, Config.RUN_CMD_SEND_INPUT_GET_OUTPUT.Parse()))
{
return Config.RUN_CMD_SEND_INPUT_GET_OUTPUT_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.STDIN_CMD_CLEANUP)))
else if (XNode.DeepEquals(env, Config.STDIN_CMD_CLEANUP.Parse()))
{
return Config.STDIN_CMD_CLEANUP_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.OPERATION_TIMEOUT_REQUEST)))
else if (XNode.DeepEquals(env, Config.OPERATION_TIMEOUT_REQUEST.Parse()))
{
return Config.OPERATION_TIMEOUT_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.OPERATION_TIMEOUT_GET_0_REQUEST)))
else if (XNode.DeepEquals(env, Config.OPERATION_TIMEOUT_GET_0_REQUEST.Parse()))
{
throw new TransportException(500, Config.OPERATION_TIMEOUT_GET_0_RESPONSE);
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.OPERATION_TIMEOUT_GET_1_REQUEST)))
else if (XNode.DeepEquals(env, Config.OPERATION_TIMEOUT_GET_1_REQUEST.Parse()))
{
return Config.OPERATION_TIMEOUT_GET_1_RESPONSE;
}
else if (XNode.DeepEquals(env, Xml.Parse(Config.CLOSE_COMMAND_FAULT_REQUEST)))
else if (XNode.DeepEquals(env, Config.CLOSE_COMMAND_FAULT_REQUEST.Parse()))
{
throw new TransportException(500, Config.CLOSE_COMMAND_FAULT_RESPONSE);
}
Expand Down
1 change: 0 additions & 1 deletion src/WinRMSharp.Tests/ProtocolTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Moq;
using WinRMSharp.Test;
using Xunit;

namespace WinRMSharp.Tests
Expand Down
1 change: 0 additions & 1 deletion src/WinRMSharp.Tests/WinRMClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Net;
using WinRMSharp.Test;
using Xunit;

namespace WinRMSharp.Tests
Expand Down
9 changes: 9 additions & 0 deletions src/WinRMSharp/Contracts/SignalCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace WinRMSharp.Contracts
{
internal class SignalCode
{
#pragma warning disable IDE1006 // Naming Styles
public const string Terminate = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate";
#pragma warning disable IDE1006 // Naming Styles
}
}
15 changes: 15 additions & 0 deletions src/WinRMSharp/Contracts/WSManAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace WinRMSharp.Contracts
{
internal class WSManAction
{
#pragma warning disable IDE1006 // Naming Styles
public const string Create = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create";
public const string Delete = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete";

public const string Command = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command";
public const string Receive = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive";
public const string Send = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send";
public const string Signal = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal";
#pragma warning disable IDE1006 // Naming Styles
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace WinRMSharp.Utils
namespace WinRMSharp.Contracts
{
public class WsmanFault
public class WSManFault
{
#pragma warning disable IDE1006 // Naming Styles
public const string OperationTimeout = "2150858793";
Expand Down
36 changes: 10 additions & 26 deletions src/WinRMSharp/Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class Protocol : IProtocol
private static readonly int DefaultMaxEnvelopeSize = 153600;
private static readonly string DefaultLocale = "en-US";

private const string RESOURCE_URI = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";

public IGuidProvider GuidProvider { get; private set; }
public ITransport Transport { get; private set; }

Expand All @@ -46,15 +48,12 @@ public Protocol(ITransport transport, ProtocolOptions? options = null)

public async Task<string> OpenShell(string inputStream = "stdin", string outputStream = "stdout stderr", string? workingDirectory = null, Dictionary<string, string>? envVars = null, TimeSpan? idleTimeout = null)
{
const string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";
const string action = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create";

const bool noProfile = false;
const int codePage = 437;

Envelope envelope = new Envelope()
{
Header = GetHeader(resourceUri, action),
Header = GetHeader(RESOURCE_URI, WSManAction.Create),
Body = new Body()
{
Shell = new Shell()
Expand Down Expand Up @@ -103,12 +102,9 @@ public async Task<string> OpenShell(string inputStream = "stdin", string outputS
/// <returns>The commandId needed to query the output.</returns>
public async Task<string> RunCommand(string shellId, string command, string[]? args = null)
{
const string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";
const string action = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command";

Envelope envelope = new Envelope()
{
Header = GetHeader(resourceUri, action, shellId),
Header = GetHeader(RESOURCE_URI, WSManAction.Command, shellId),
Body = new Body()
{
CommandLine = new CommandLine()
Expand Down Expand Up @@ -147,12 +143,9 @@ public async Task<string> RunCommand(string shellId, string command, string[]? a

public async Task SendCommandInput(string shellId, string commandId, string input, bool end = false)
{
const string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";
const string action = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send";

Envelope envelope = new Envelope()
{
Header = GetHeader(resourceUri, action, shellId),
Header = GetHeader(RESOURCE_URI, WSManAction.Send, shellId),
Body = new Body()
{
Send = new Send()
Expand Down Expand Up @@ -214,12 +207,9 @@ public async Task<CommandState> PollCommandState(string shellId, string commandI

public async Task<CommandState> GetCommandState(string shellId, string commandId)
{
const string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";
const string action = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive";

Envelope envelope = new Envelope()
{
Header = GetHeader(resourceUri, action, shellId),
Header = GetHeader(RESOURCE_URI, WSManAction.Receive, shellId),
Body = new Body()
{
Receive = new Receive()
Expand Down Expand Up @@ -277,12 +267,9 @@ public async Task<CommandState> GetCommandState(string shellId, string commandId
/// <returns></returns>
public async Task CloseShell(string shellId)
{
const string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";
const string action = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete";

Envelope envelope = new Envelope()
{
Header = GetHeader(resourceUri, action, shellId),
Header = GetHeader(RESOURCE_URI, WSManAction.Delete, shellId),
Body = new Body()
};

Expand Down Expand Up @@ -312,20 +299,17 @@ public async Task CloseShell(string shellId)
/// <exception cref="WinRMException"></exception>
public async Task CleanupCommand(string shellId, string commandId)
{
const string resourceUri = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd";
const string action = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal";

Envelope envelope = new Envelope()
{
Header = GetHeader(resourceUri, action, shellId),
Header = GetHeader(RESOURCE_URI, WSManAction.Signal, shellId),
Body = new Body()
{
Signal = new Signal()
{
CommandId = commandId,
Code = new Code()
{
Value = "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate"
Value = SignalCode.Terminate
}
}
}
Expand Down Expand Up @@ -395,7 +379,7 @@ private async Task<XDocument> Send(Envelope envelope)
XElement? wsmanFault = fault.XPathSelectElement("//soapenv:Detail/wsmanfault:WSManFault", nsmgr);
string? wsmanFaultCode = wsmanFault?.Attribute("Code")?.Value;

if (wsmanFaultCode == WsmanFault.OperationTimeout)
if (wsmanFaultCode == WSManFault.OperationTimeout)
throw new OperationTimeoutException();

string? faultCode = fault.XPathSelectElement("//soapenv:Code/soapenv:Value", nsmgr)?.Value;
Expand Down

0 comments on commit 4cd292c

Please sign in to comment.