Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strings are not escaped #7

Open
MadbHatter opened this issue Jul 15, 2017 · 1 comment
Open

Strings are not escaped #7

MadbHatter opened this issue Jul 15, 2017 · 1 comment

Comments

@MadbHatter
Copy link

Currently, using Method("Local Area Network") will result in ... method argument=Local Area Network.
Doing so will result in an error because how the parsing of the cmd works.

In your test code, you manually escaped your input strings via "\"", which seems a bit counter-intuitive.

A little check if the string contains a space and is not escaped would go a long way.
Is there a particular reason why strings that contain spaces are not automatically escaped?

Also this command (WLAN/SetActionTests.cs => VerifyProfileParameterOutput) would not execute correctly (interface sl is not escaped) and from what I get all your test commands are supposed to work (with correct parameters and names).

Assert.AreEqual(harness.Value, "netsh wlan set profileparameter name=profile_name interface=interface sl SSIDname=ssid1 autoSwitch=no ConnectionMode=auto");

PS: Sorry for my poor English and double sorry If I misused the issues function somehow (I'm somewhat new to github).

@hypetsch
Copy link

hypetsch commented Jan 23, 2019

The same issue exists for the username parameter in Ignite.SharpNetSH.NetSH.CMD.Http.Add.UrlAcl(...)

Changing the /Utilities/ActionProxy.cs ActionProxy.ProcessParameters accordingly should fix those issues and retain backward compatibility:

      private string ProcessParameters(MethodBase method, IMethodCallMessage methodCall)
      {
         var results = new List<string>();
         var i = 0;
         foreach (var value in methodCall.InArgs)
         {
            var parameter = method.GetParameters().FirstOrDefault(x => x.Name == methodCall.GetInArgName(i));
            var parameterName = parameter.GetParameterName();
            i++;

            if (value == null) continue;

            var parameterValue = string.Empty;
            if (value is bool)
               // We have to process booleans differently based upon the configured boolean type (i.e. Yes/No, Enable/Disable, True/False outputs)
               parameterValue = parameter.GetBooleanType().GetBooleanValue((bool)value);
            else if (value is Guid)
               // Guids have to contain braces
               parameterValue = ((Guid)value).ToString("B");
            else if (value.GetType().IsEnum)
               // Enums might be configured with a custom description to change how to output their text
               parameterValue = value.GetDescription();
            else
               // Otherwise it's a stringable (i.e. ToString()) property
               parameterValue = value.ToString();

            // Values containing spaces have to be added with quotation marks (as long as they are not already present)
            if (parameterValue.Contains(" ") && !parameterValue.StartsWith("\"") && !parameterValue.EndsWith("\""))
               results.Add(parameterName + "=\"" + parameterValue + "\"");
            else
               results.Add(parameterName + "=" + parameterValue);
         }
         if (results.Count == 0) return method.GetMethodName();
         return method.GetMethodName() + " " + results.Aggregate((x, y) => string.IsNullOrWhiteSpace(x) ? y : x + " " + y);
      }

...sorry for just copying the code instead of creating a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants