Example #1
0
        public async Task WriteValueAsync(RegistryBaseKey baseKey, string key, string valueName, string value)
        {
            var command = new RegistryWriteStringValueCommand(baseKey, key, valueName, value);
            IServiceCommandResponse response = await _commandBridge.SendCommandAsync(command);

            response.ThrowIfError();
        }
        private static bool TryDeserialize(
            BridgeMessageDeserializer deserializer,
            out IServiceCommandResponse response,
            out IServiceCommandResponse errorResponse)
        {
            // Check for a non-success ErrorCode, which indicates we're deserializing an error
            if (deserializer.TryGetOptionalEnumValue(ParamName.ErrorCode, out ServiceCommandErrorCode errorCode) &&
                errorCode != ServiceCommandErrorCode.Success)
            {
                string errorMessage = deserializer.GetStringValue(ParamName.ErrorMessage);
                response = deserializer.LastError == null
                    ? new ServiceCommandResponse(deserializer.CommandName, null, errorCode, errorMessage)
                    : null;
            }
            // Deserialize a successful result
            else
            {
                object result = deserializer.GetValue(ParamName.CommandResult);
                response = deserializer.LastError == null
                    ? new ServiceCommandResponse(deserializer.CommandName, result, ServiceCommandErrorCode.Success, null)
                    : null;
            }

            errorResponse = deserializer.LastError;
            return(errorResponse == null);
        }
        private static bool TryGetCommandName(
            IDictionary <string, object> valueSet,
            out ServiceCommandName commandName,
            out IServiceCommandResponse errorResponse)
        {
            if (!valueSet.TryGetValue(ParamName.CommandName.ToString(), out object rawValue))
            {
                errorResponse = ServiceCommandResponse.CreateError(
                    ServiceCommandName.Unknown,
                    ServiceErrorInfo.MissingRequiredMessageValue(ParamName.CommandName));
                commandName = ServiceCommandName.Unknown;
                return(false);
            }

            if (!Enum.TryParse(rawValue.ToString(), out commandName))
            {
                errorResponse = ServiceCommandResponse.CreateError(
                    ServiceCommandName.Unknown,
                    ServiceErrorInfo.WrongMessageValueType(
                        ParamName.CommandName,
                        rawValue.GetType(),
                        typeof(ServiceCommandName)));
                commandName = ServiceCommandName.Unknown;
                return(false);
            }

            errorResponse = null;
            return(true);
        }
        //// ===========================================================================================================
        //// Methods
        //// ===========================================================================================================

        public static bool TryCreateFromJsonString(
            string jsonString,
            out BridgeMessageDeserializer deserializer,
            out IServiceCommandResponse errorResponse)
        {
            Param.VerifyString(jsonString, nameof(jsonString));

            var valueSet = new Dictionary <string, object>();

            try
            {
                // Try to parse the JSON.
                var jsonObject = JObject.Parse(jsonString);

                // Populate a value set from the parsed JSON.
                foreach (KeyValuePair <string, JToken> pair in jsonObject)
                {
                    valueSet.Add(pair.Key, ((JValue)pair.Value).Value);
                }
            }
            catch (Exception e)
            {
                deserializer  = null;
                errorResponse = ServiceCommandResponse.CreateError(ServiceCommandName.Unknown, e);
                return(false);
            }

            return(TryCreateFromValueSet(valueSet, out deserializer, out errorResponse));
        }
Example #5
0
        public async Task <string> ReadValueAsync(RegistryBaseKey baseKey, string key, string valueName, string defaultValue)
        {
            var command = new RegistryReadStringValueCommand(baseKey, key, valueName, defaultValue);
            IServiceCommandResponse response = await _commandBridge.SendCommandAsync(command);

            response.ThrowIfError();
            return((string)response.Result);
        }
Example #6
0
        public async Task <bool> ReadValueAsync(RegistryBaseKey baseKey, string key, string valueName, bool defaultValue)
        {
            var command = new RegistryReadIntValueCommand(baseKey, key, valueName, defaultValue ? 1 : 0);
            IServiceCommandResponse response = await _commandBridge.SendCommandAsync(command);

            response.ThrowIfError();
            return((int)response.Result != 0);
        }
Example #7
0
        public void ExecuteWrite_should_return_the_value()
        {
            var registry = new FakeRegistry(@"HKLM\SubKey");
            var executor = new RegistryCommandExecutor(registry);
            IServiceCommandResponse response = executor.ExecuteWrite(
                new RegistryWriteIntValueCommand(RegistryBaseKey.LocalMachine, "SubKey", "IntValue", 123),
                new NullLogger());

            response.IsSuccess.Should().BeTrue();
            response.Result.Should().Be(123);
        }
Example #8
0
        public void ExecuteRead_should_return_the_read_value()
        {
            var registry = new FakeRegistry(@"HKCU\SubKey\IntValue=123");
            var executor = new RegistryCommandExecutor(registry);
            IServiceCommandResponse response = executor.ExecuteRead(
                new RegistryReadIntValueCommand(RegistryBaseKey.CurrentUser, "SubKey", "IntValue", 0),
                new NullLogger());

            response.IsSuccess.Should().BeTrue();
            response.Result.Should().Be(123);
        }
        public static bool TryDeserializeFromValueSet(
            IDictionary <string, object> valueSet,
            out IServiceCommandResponse response,
            out IServiceCommandResponse errorResponse)
        {
            if (!BridgeMessageDeserializer.TryCreateFromValueSet(
                    valueSet,
                    out BridgeMessageDeserializer deserializer,
                    out errorResponse))
            {
                response = null;
                return(false);
            }

            return(TryDeserialize(deserializer, out response, out errorResponse));
        }
        public static bool TryDeserializeFromJsonString(
            string jsonString,
            out IServiceCommandResponse response,
            out IServiceCommandResponse errorResponse)
        {
            if (!BridgeMessageDeserializer.TryCreateFromJsonString(
                    jsonString,
                    out BridgeMessageDeserializer deserializer,
                    out errorResponse))
            {
                response = null;
                return(false);
            }

            return(TryDeserialize(deserializer, out response, out errorResponse));
        }
        public static bool TryCreateFromValueSet(
            IDictionary <string, object> valueSet,
            out BridgeMessageDeserializer deserializer,
            out IServiceCommandResponse errorResponse)
        {
            Param.VerifyNotNull(valueSet, nameof(valueSet));

            if (!TryGetCommandName(valueSet, out ServiceCommandName commandName, out errorResponse))
            {
                deserializer = null;
                return(false);
            }

            deserializer = new BridgeMessageDeserializer(valueSet, commandName);
            return(true);
        }
Example #12
0
        private static bool TryDeserialize(
            BridgeMessageDeserializer deserializer,
            out IServiceCommand command,
            out IServiceCommandResponse errorResponse)
        {
            switch (deserializer.CommandName)
            {
            case ServiceCommandName.ShutdownServer:
                command = new ShutdownServerCommand();
                break;

            case ServiceCommandName.Echo:
                command = new EchoCommand(deserializer);
                break;

            case ServiceCommandName.RegistryReadIntValue:
                command = new RegistryReadIntValueCommand(deserializer);
                break;

            case ServiceCommandName.RegistryReadStringValue:
                command = new RegistryReadStringValueCommand(deserializer);
                break;

            case ServiceCommandName.RegistryWriteIntValue:
                command = new RegistryWriteIntValueCommand(deserializer);
                break;

            case ServiceCommandName.RegistryWriteStringValue:
                command = new RegistryWriteStringValueCommand(deserializer);
                break;

            case ServiceCommandName.Unknown:
            default:
                throw new InvalidOperationException(
                          "This should be unreachable because the deserializer should have detected an invalid command name");
            }

            if (deserializer.HadError)
            {
                command = null;
            }
            errorResponse = deserializer.LastError;
            return(!deserializer.HadError);
        }
Example #13
0
        public void ExecuteRead_should_return_an_error_if_the_sub_key_is_not_present()
        {
            var mockKey = new Mock <IWin32RegistryKey>(MockBehavior.Strict);

            mockKey.Setup(key => key.OpenSubKey("SubKey", false)).Throws(new UnauthorizedAccessException("Test"));
            mockKey.Setup(key => key.Dispose());

            var mockRegistry = new Mock <IWin32Registry>(MockBehavior.Strict);

            mockRegistry.Setup(reg => reg.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64))
            .Returns(mockKey.Object);

            var executor = new RegistryCommandExecutor(mockRegistry.Object);
            IServiceCommandResponse response = executor.ExecuteRead(
                new RegistryReadIntValueCommand(RegistryBaseKey.CurrentUser, "SubKey", "IntValue", 0),
                new NullLogger());

            response.IsError.Should().BeTrue();
            response.ErrorCode.Should().Be(ServiceCommandErrorCode.RegistryReadError);
            response.ErrorMessage.Should()
            .Be(
                @"Error in reading registry value 'HKEY_CURRENT_USER\SubKey\IntValue': System.UnauthorizedAccessException: Test.");
        }