Exemple #1
0
        //// ===========================================================================================================
        //// Methods
        //// ===========================================================================================================

        /// <summary>
        /// Starts the server by waiting for requests (via the <paramref name="readLineFunc"/>) and then sending
        /// responses via <paramref name="writeLineAction"/>.
        /// </summary>
        /// <param name="readLineFunc">
        /// The function to call to wait for an incoming command, serialized on a single line.
        /// </param>
        /// <param name="writeLineAction">
        /// The action to call when a response is sent back to the client, serialized on a singe line.
        /// </param>
        /// <param name="registry">A <see cref="IWin32Registry"/> to use for executing registry commands.</param>
        public void Start(ReadLineFunc readLineFunc, WriteLineAction writeLineAction, IWin32Registry registry)
        {
            Param.VerifyNotNull(readLineFunc, nameof(readLineFunc));
            Param.VerifyNotNull(writeLineAction, nameof(writeLineAction));
            Param.VerifyNotNull(registry, nameof(registry));

            var commandExecutor = new CommandExecutor(_logger, registry);

            // This is the loop that waits for an incoming request (ReadLine), executes a command, and then returns a
            // response via WriteLine.
            bool shutdownRequested = false;

            while (!shutdownRequested)
            {
                _logger.LogDebug("Waiting for request");
                string serializedCommand = readLineFunc();
                _logger.LogInfo("Request received: ", serializedCommand);

                IServiceCommandResponse response;

                if (!ServiceCommand.TryDeserializeFromJsonString(
                        serializedCommand,
                        out IServiceCommand command,
                        out IServiceCommandResponse errorResponse))
                {
                    response = errorResponse;
                }
Exemple #2
0
        public void TryDeserializeFromJsonString_should_correctly_deserialize_a_RegistryReadStringValueCommand()
        {
            string jsonString = $@"
{{
    {ParamName.CommandName}: ""{ServiceCommandName.RegistryReadStringValue}"",
    {ParamName.RegistryBaseKey}: ""{RegistryBaseKey.CurrentUser}"",
    {ParamName.RegistryKey}: ""Key"",
    {ParamName.RegistryValueName}: ""ValueName"",
    {ParamName.RegistryDefaultValue}: ""Value"",
}}";

            ServiceCommand.TryDeserializeFromJsonString(
                jsonString,
                out IServiceCommand command,
                out IServiceCommandResponse errorResponse)
            .Should()
            .BeTrue();

            command.Should().BeOfType <RegistryReadStringValueCommand>();
            errorResponse.Should().BeNull();

            var typedCommand = (RegistryReadStringValueCommand)command;

            typedCommand.BaseKey.Should().Be(RegistryBaseKey.CurrentUser);
            typedCommand.Key.Should().Be("Key");
            typedCommand.ValueName.Should().Be("ValueName");
            typedCommand.DefaultValue.Should().Be("Value");
        }
Exemple #3
0
        public void TryDeserializeFromJsonString_should_return_an_error_on_invalid_JSON()
        {
            ServiceCommand.TryDeserializeFromJsonString(
                "invalid",
                out IServiceCommand command,
                out IServiceCommandResponse errorResponse)
            .Should()
            .BeFalse();

            command.Should().BeNull();
            errorResponse.Should().NotBeNull();
        }
Exemple #4
0
        public void TryDeserializeFromJsonString_should_throw_on_null_params()
        {
            Action action = () => ServiceCommand.TryDeserializeFromJsonString(null, out _, out _);

            action.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("jsonString");
        }