public IValidationContext Validate(string machineIdToValidate)
        {
            Argument.IsNotNullOrWhitespace(() => machineIdToValidate);

            var validationContext = new ValidationContext();

            Log.Debug("Retrieving machine id");

            var machineId = _identificationService.GetMachineId();

            Log.Debug("Validating machine id '{0}' against expected machine id '{1}'", machineId, machineIdToValidate);

            var machineSplitted  = machineId.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None);
            var expectedSplitter = machineIdToValidate.Split(new[] { LicenseElements.IdentificationSeparator }, StringSplitOptions.None);

            if (machineSplitted.Length != expectedSplitter.Length)
            {
                var error = "The number of items inside the license differ too much, assuming machine ids do not match";
                Log.Error(error);
                validationContext.Add(BusinessRuleValidationResult.CreateError(error));

                return(validationContext);
            }

            var invalidEntries = 0;

            for (var i = 0; i < machineSplitted.Length; i++)
            {
                if (!string.Equals(expectedSplitter[i], machineSplitted[i], StringComparison.OrdinalIgnoreCase))
                {
                    invalidEntries++;
                }
            }

            if (invalidEntries > Threshold)
            {
                var error = string.Format("{0} values are not equal, not accepting the machine id, maximum threshold is '{1}'", invalidEntries, Threshold);
                Log.Error(error);
                validationContext.Add(BusinessRuleValidationResult.CreateError(error));

                return(validationContext);
            }

            if (invalidEntries > 0)
            {
                var warning = string.Format("One of the values is not equal, but we have a threshold of {0} so accepting machine id", Threshold);
                Log.Warning(warning);

                validationContext.Add(BusinessRuleValidationResult.CreateWarning(warning));
            }

            return(validationContext);
        }
예제 #2
0
        public void CanAddValidationComponentsTest()
        {
            var vc = new ValidationContext(ImmediateScheduler.Instance);

            var invalidName = string.Empty;

            var vm = new TestViewModel {
                Name = "valid"
            };

            var v1 = new BasePropertyValidation <TestViewModel, string>(
                vm,
                v => v.Name,
                s => !string.IsNullOrEmpty(s),
                msg => $"{msg} isn't valid");

            vc.Add(v1);

            Assert.True(vc.IsValid);

            vm.Name = invalidName;

            Assert.False(v1.IsValid);
            Assert.False(vc.IsValid);

            Assert.Equal(1, vc.Text.Count);
        }
        public void ComponentChangesTriggersChangeTest()
        {
            var vc = new ValidationContext();

            var component = new TextValidationComponent();

            vc.Add(component);

            var stateChanges = new List <bool>();
            List <ValidationState> changes = new List <ValidationState>();

            vc.ValidationStatusChange.Subscribe(v => changes.Add(v));
            vc.Valid.Subscribe(v => stateChanges.Add(v));

            Assert.Equal(1, changes.Count);
            Assert.True(changes[0].IsValid);
            Assert.Equal(1, stateChanges.Count);

            var failedState = new ValidationState(false, ComponentValidationText1, component);

            component.PushState(failedState);

            Assert.Equal(2, changes.Count);
            Assert.Equal(2, stateChanges.Count);
            Assert.False(changes[1].IsValid);
            Assert.False(stateChanges[1]);
            Assert.Equal(ComponentValidationText1, changes[1].Text.ToSingleLine());
            Assert.Equal(ComponentValidationText1, vc.Text.ToSingleLine());
            Assert.False(vc.IsValid);
        }
        public void ShouldSupportBindingToValidationContextWrappedInValidationHelper()
        {
            const string nameValidationError = "Name should not be empty.";
            var          view = new TestView(new TestViewModel {
                Name = string.Empty
            });
            var outerContext = new ValidationContext(ImmediateScheduler.Instance);
            var validation   = new BasePropertyValidation <TestViewModel, string>(
                view.ViewModel,
                vm => vm.Name,
                name => !string.IsNullOrWhiteSpace(name),
                nameValidationError);

            outerContext.Add(validation);
            view.ViewModel.NameRule = new ValidationHelper(outerContext);

            view.Bind(view.ViewModel, vm => vm.Name, v => v.NameLabel);
            view.BindValidation(view.ViewModel, vm => vm.NameRule, v => v.NameErrorLabel);

            Assert.False(view.ViewModel.NameRule.IsValid);
            Assert.NotEmpty(view.NameErrorLabel);
            Assert.Equal(nameValidationError, view.NameErrorLabel);

            view.ViewModel.Name = "Jotaro";

            Assert.True(view.ViewModel.NameRule.IsValid);
            Assert.Empty(view.NameErrorLabel);
        }
예제 #5
0
        public void TwoValidationComponentsCorrectlyResultInContextTest()
        {
            const string validName   = "valid";
            var          invalidName = string.Empty;

            var vc = new ValidationContext(ImmediateScheduler.Instance);

            var vm = new TestViewModel {
                Name = validName, Name2 = validName
            };

            var firstValidation = new BasePropertyValidation <TestViewModel, string>(
                vm,
                v => v.Name,
                s => !string.IsNullOrEmpty(s),
                s => $"Name {s} isn't valid");

            var secondValidation = new BasePropertyValidation <TestViewModel, string>(
                vm,
                v => v.Name2,
                s => !string.IsNullOrEmpty(s),
                s => $"Name 2 {s} isn't valid");

            vc.Add(firstValidation);
            vc.Add(secondValidation);

            Assert.True(vc.IsValid);
            Assert.Equal(0, vc.Text.Count);

            vm.Name = invalidName;
            Assert.False(vc.IsValid);
            Assert.Equal(1, vc.Text.Count);
            Assert.Equal("Name " + invalidName + " isn't valid", vc.Text[0]);

            vm.Name2 = invalidName;
            Assert.False(vc.IsValid);
            Assert.Equal(2, vc.Text.Count);
            Assert.Equal("Name " + invalidName + " isn't valid", vc.Text[0]);
            Assert.Equal("Name 2 " + invalidName + " isn't valid", vc.Text[1]);

            vm.Name  = validName;
            vm.Name2 = validName;

            Assert.True(vc.IsValid);
            Assert.Equal(0, vc.Text.Count);
        }
        public void MultipleComponentsCorrectStateTest()
        {
            var vc = new ValidationContext();

            var component1 = new TextValidationComponent();

            vc.Add(component1);

            var component2 = new TextValidationComponent();

            vc.Add(component2);

            var failedState1  = new ValidationState(false, ComponentValidationText1, component1);
            var successState1 = new ValidationState(true, string.Empty, component1);

            var failedState2  = new ValidationState(false, ComponentValidationText2, component2);
            var successState2 = new ValidationState(true, string.Empty, component2);

            component1.PushState(failedState1);

            Assert.False(vc.IsValid);

            component1.PushState(successState1);
            Assert.True(vc.IsValid);

            component1.PushState(failedState1);
            component2.PushState(failedState2);

            Assert.False(vc.IsValid);

            var vt = vc.Text;

            Assert.Equal(2, vt.Count);

            component2.PushState(successState2);

            Assert.False(vc.IsValid);
            Assert.Equal(1, vc.Text.Count);

            component1.PushState(successState1);

            Assert.True(vc.IsValid);
            Assert.Equal(0, vc.Text.Count);
        }
        public void CanAddComponentTest()
        {
            var vc = new ValidationContext();

            var component = new TextValidationComponent();

            vc.Add(component);

            Assert.True(vc.IsValid);
        }
예제 #8
0
        protected override Task InitializeAsync()
        {
            var context = new ValidationContext();

            var result1 = BusinessRuleValidationResult.CreateErrorWithTag("Error1 message", "A");
            var result2 = BusinessRuleValidationResult.CreateWarningWithTag("Warning1 message", "B");
            var result3 = FieldValidationResult.CreateWarningWithTag("Property1", "Warning2 message", "C");

            var tag = new { Name = "A", Line = (int?)2 };

            var result4 = BusinessRuleValidationResult.CreateErrorWithTag("Error2 message with object tag", tag);
            var result5 = BusinessRuleValidationResult.CreateErrorWithTag("Error3 message", "B");

            var result6  = BusinessRuleValidationResult.CreateError("Error3 message");
            var result7  = BusinessRuleValidationResult.CreateError("Error4 message");
            var result8  = FieldValidationResult.CreateWarningWithTag("Property2", "Warning3 message", new { Name = "A", Line = 1 });
            var result9  = FieldValidationResult.CreateWarningWithTag("Property2", "Warning4 message", new { Name = "A", Line = 2 });
            var result10 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning5 message", new { Name = "A", Line = 3, ColumnName = "ColA" });
            var result11 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning6 message", new { Name = "A", Line = 20, ColumnIndex = 2 });
            var result12 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning7 message", new { Name = "A", Line = 12, ColumnName = "ColC", ColumnIndex = 3 });
            var result13 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning8 message", new { Name = "A", Line = 10 });
            var result14 = FieldValidationResult.CreateWarningWithTag("Property2", "Warning9 message", new { Name = "A", Line = 24 });

            context.Add(result1);
            context.Add(result2);
            context.Add(result3);
            context.Add(result4);
            context.Add(result5);
            context.Add(result6);
            context.Add(result7);
            context.Add(result8);
            context.Add(result9);
            context.Add(result10);
            context.Add(result11);
            context.Add(result12);
            context.Add(result13);
            context.Add(result14);

            ValidationContext = context;

            return(base.InitializeAsync());
        }
        /// <summary>
        /// Validates the license.
        /// </summary>
        /// <param name="license">The license key the user has given to be validated.</param>
        /// <returns>The validation context containing all the validation results.</returns>
        public IValidationContext ValidateLicense(string license)
        {
            Argument.IsNotNullOrWhitespace(() => license);

            var validationContext = new ValidationContext();

            Log.Info("Validating license");

            try
            {
                var licenseObject = License.Load(license);
                var failureList   = licenseObject.Validate()
                                    .Signature(_applicationIdService.ApplicationId)
                                    .AssertValidLicense().ToList();

                if (failureList.Count > 0)
                {
                    foreach (var failure in failureList)
                    {
                        var businessRuleValidationResult = BusinessRuleValidationResult.CreateErrorWithTag(failure.Message, failure.HowToResolve);
                        validationContext.Add(businessRuleValidationResult);
                    }
                }

                var licenseAttributes = licenseObject.AdditionalAttributes;
                if (licenseAttributes != null)
                {
                    foreach (var licenseAttribute in licenseAttributes.GetAll())
                    {
                        if (string.Equals(licenseAttribute.Key, LicenseElements.MachineId))
                        {
                            Log.Debug("Validating license using machine ID");

                            var machineLicenseValidationContext = _machineLicenseValidationService.Validate(licenseAttribute.Value);
                            validationContext.SynchronizeWithContext(machineLicenseValidationContext, true);

                            if (machineLicenseValidationContext.HasErrors)
                            {
                                Log.Error("The license can only run on machine with ID '{0}'", licenseAttribute.Value);
                            }
                        }

                        // TODO: add additional attribute checks here
                    }
                }

                // Also validate the xml, very important for expiration date and version
                var xmlValidationContext = ValidateXml(license);
                validationContext.SynchronizeWithContext(xmlValidationContext, true);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "An error occurred while loading the license");

                validationContext.Add(BusinessRuleValidationResult.CreateError("An unknown error occurred while loading the license, please contact support"));
            }
            finally
            {
                if (validationContext.GetErrors().Count > 0)
                {
                    Log.Warning("License is not valid:");
                    Log.Indent();

                    foreach (var error in validationContext.GetErrors())
                    {
                        Log.Warning("- {0}\n{1}", error.Message, error.Tag as string);
                    }

                    Log.Unindent();
                }
                else
                {
                    Log.Info("License is valid");
                }
            }

            return(validationContext);
        }
        /// <summary>
        /// Validates the XML
        /// </summary>
        /// <param name="license">The license.</param>
        /// <returns>The validation context containing all the validation results.</returns>
        /// <exception cref="ArgumentException">The <paramref name="license" /> is <c>null</c> or whitespace.</exception>
        /// <exception cref="XmlException">The license text is not valid XML.</exception>
        /// <exception cref="Exception">The root element is not License.</exception>
        /// <exception cref="Exception">There were no inner nodes found.</exception>
        public IValidationContext ValidateXml(string license)
        {
            var validationContext = new ValidationContext();

            if (string.IsNullOrWhiteSpace(license))
            {
                validationContext.Add(BusinessRuleValidationResult.CreateError("No license available"));
            }

            var xmlDataList = new List <XmlDataModel>();

            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(license);
                var xmlRoot = xmlDoc.DocumentElement;
                if (!string.Equals(xmlRoot.Name, "License"))
                {
                    validationContext.Add(BusinessRuleValidationResult.CreateError("Please make sure that you pasted the complete license, including the <License> tags"));
                }

                var xmlNodes = xmlRoot.ChildNodes;
                foreach (XmlNode node in xmlNodes)
                {
                    if (!string.Equals(node.Name, "ProductFeatures"))
                    {
                        xmlDataList.Add(new XmlDataModel
                        {
                            Name  = node.Name,
                            Value = node.InnerText
                        });
                    }
                    else
                    {
                        foreach (XmlNode featureNode in node.ChildNodes)
                        {
                            xmlDataList.Add(new XmlDataModel
                            {
                                Name  = featureNode.Attributes[0].Value,
                                Value = featureNode.InnerText
                            });
                        }
                    }
                }

                if (xmlDataList.Count == 0)
                {
                    validationContext.Add(BusinessRuleValidationResult.CreateError("License contains no valid data"));
                }

                var expData = xmlDataList.FirstOrDefault(x => string.Equals(x.Name, LicenseElements.Expiration));
                if (expData != null)
                {
                    DateTime expirationDateTime;
                    if (DateTime.TryParse(expData.Value, out expirationDateTime))
                    {
                        Log.Debug("Using expiration behavior '{0}'", _expirationBehavior.GetType().Name);

                        var portableLicense = License.Load(license);

                        if (_expirationBehavior.IsExpired(portableLicense, expirationDateTime, DateTime.Now))
                        {
                            validationContext.Add(BusinessRuleValidationResult.CreateError("The license has expired. Please delete the current license if you have a new one."));
                        }
                    }
                    else
                    {
                        validationContext.Add(BusinessRuleValidationResult.CreateError("The expiration date was not a valid date / tim value"));
                    }
                }

                var xmlDataVersion = xmlDataList.FirstOrDefault(x => string.Equals(x.Name, LicenseElements.Version));
                if (xmlDataVersion != null)
                {
                    Version licenseVersion;
                    if (Version.TryParse(xmlDataVersion.Value, out licenseVersion))
                    {
                        var productVersion = Assembly.GetExecutingAssembly().GetName().Version;
                        if (productVersion > licenseVersion)
                        {
                            validationContext.Add(BusinessRuleValidationResult.CreateError("Your license only supports versions up to '{0}' while the current version of this product is '{1}'", licenseVersion, productVersion));
                        }
                    }
                    else
                    {
                        validationContext.Add(BusinessRuleValidationResult.CreateError("The version was not a valid version value"));
                    }
                }
            }
            catch (XmlException xmlex)
            {
                Log.Debug(xmlex);

                validationContext.Add(BusinessRuleValidationResult.CreateError("The license data is not a license"));
            }
            catch (Exception ex)
            {
                Log.Debug(ex);

                //var innermessage = string.Empty;
                //if (ex.InnerException != null)
                //{
                //    innermessage = ex.InnerException.Message;
                //}

                validationContext.Add(BusinessRuleValidationResult.CreateError(ex.Message));
            }

            if (validationContext.HasErrors || validationContext.HasWarnings)
            {
                Log.Warning("The XML is invalid");
            }
            return(validationContext);
        }
예제 #11
0
        public IValidationContext Parse(List <string> commandLineArguments, IContext targetContext)
        {
            var validationContext = new ValidationContext();

            var quoteSplitCharacters = targetContext.QuoteSplitCharacters.ToArray();

            targetContext.OriginalCommandLine = string.Join(" ", commandLineArguments);

            var isHelp = commandLineArguments.Any(commandLineArgument => commandLineArgument.IsHelp(quoteSplitCharacters));

            if (isHelp)
            {
                targetContext.IsHelp = true;
                return(validationContext);
            }

            var optionDefinitions = _optionDefinitionService.GetOptionDefinitions(targetContext);

            var handledOptions = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            Log.Debug("Parsing command line");

            for (var i = 0; i < commandLineArguments.Count; i++)
            {
                var commandLineArgument = commandLineArguments[i];

                try
                {
                    // Allow the first one to be a non-switch
                    if (i == 0 && !commandLineArguments[i].IsSwitch(quoteSplitCharacters))
                    {
                        var emptyOptionDefinition = (from x in optionDefinitions
                                                     where !x.HasSwitch()
                                                     select x).FirstOrDefault();

                        if (emptyOptionDefinition == null)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoEmptySwitch"), commandLineArgument);
                            Log.Debug(message);
                            validationContext.Add(BusinessRuleValidationResult.CreateError(message));
                            continue;
                        }

                        UpdateContext(targetContext, emptyOptionDefinition, commandLineArgument);
                        handledOptions.Add(emptyOptionDefinition.ShortName);
                        continue;
                    }

                    if (!commandLineArgument.IsSwitch(quoteSplitCharacters))
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseNoSwitch"), commandLineArgument);
                        Log.Debug(message);
                        validationContext.Add(BusinessRuleValidationResult.CreateWarning(message));
                        continue;
                    }

                    var value = string.Empty;

                    var optionDefinition = (from x in optionDefinitions
                                            where x.IsSwitch(commandLineArgument, quoteSplitCharacters)
                                            select x).FirstOrDefault();
                    var isKnownDefinition = (optionDefinition != null);
                    if (!isKnownDefinition)
                    {
                        var message = string.Format(_languageService.GetString("CommandLine_CannotParseSwitchNotRecognized"), commandLineArgument);
                        Log.Debug(message);
                        validationContext.Add(BusinessRuleValidationResult.CreateWarning(message));

                        // Try to read the next value, but keep in mind that some options might
                        // not have a value passed into it
                        var potentialValue = (i < commandLineArguments.Count - 1) ? commandLineArguments[i + 1] : string.Empty;
                        if (!string.IsNullOrWhiteSpace(potentialValue) && potentialValue.IsSwitch(quoteSplitCharacters))
                        {
                            potentialValue = string.Empty;
                        }

                        value = potentialValue;
                    }

                    targetContext.RawValues[commandLineArgument.TrimSwitchPrefix()] = value;

                    if (!isKnownDefinition)
                    {
                        continue;
                    }

                    if (!optionDefinition.AcceptsValue)
                    {
                        // Assume boolean switch
                        value = "true";
                    }
                    else
                    {
                        if (commandLineArguments.Count <= i + 1)
                        {
                            var message = string.Format(_languageService.GetString("CommandLine_CannotParseValueMissing"), commandLineArgument);
                            Log.Info(message);
                            validationContext.Add(BusinessRuleValidationResult.CreateWarning(message));
                            continue;
                        }

                        value = commandLineArguments[++i];
                    }

                    UpdateContext(targetContext, optionDefinition, value);
                    handledOptions.Add(optionDefinition.ShortName);
                }
                catch (Exception ex)
                {
                    validationContext.Add(BusinessRuleValidationResult.CreateError(_languageService.GetString("CommandLine_CannotParseExceptionOccurred"), commandLineArgument, ex.Message));
                }
            }

            ValidateMandatorySwitches(validationContext, optionDefinitions, handledOptions);

            Log.Debug("Finishing the context");

            targetContext.Finish();

            return(validationContext);
        }
        private async Task <IProject> SyncedLoadProjectAsync(string location)
        {
            Argument.IsNotNullOrWhitespace("location", location);

            var project = Projects.FirstOrDefault(x => location.EqualsIgnoreCase(x.Location));

            if (project != null)
            {
                return(project);
            }

            var projectLocation = location;

            using (new DisposableToken(null, token => _loadingProjects.Add(projectLocation), token => _loadingProjects.Remove(projectLocation)))
            {
                Log.Debug($"Going to load project from '{location}', checking if an upgrade is required");

                if (await _projectUpgrader.RequiresUpgradeAsync(location))
                {
                    Log.Debug($"Upgrade is required for '{location}', upgrading...");

                    location = await _projectUpgrader.UpgradeAsync(location);

                    Log.Debug($"Upgraded project, final location is '{location}'");
                }

                Log.Debug($"Loading project from '{location}'");

                _projectStateSetter.SetProjectLoading(location, true);

                var cancelEventArgs = new ProjectCancelEventArgs(location);

                await ProjectLoadingAsync.SafeInvokeAsync(this, cancelEventArgs, false).ConfigureAwait(false);

                if (cancelEventArgs.Cancel)
                {
                    Log.Debug("Canceled loading of project from '{0}'", location);

                    _projectStateSetter.SetProjectLoading(location, false);

                    await ProjectLoadingCanceledAsync.SafeInvokeAsync(this, new ProjectEventArgs(location)).ConfigureAwait(false);

                    return(null);
                }

                Exception error = null;

                IValidationContext validationContext = null;

                try
                {
                    if (_projects.Count > 0 && ProjectManagementType == ProjectManagementType.SingleDocument)
                    {
                        throw Log.ErrorAndCreateException <SdiProjectManagementException>("Cannot load project '{0}', currently in SDI mode", location);
                    }

                    if (!await _projectValidator.CanStartLoadingProjectAsync(location))
                    {
                        validationContext = new ValidationContext();
                        validationContext.Add(BusinessRuleValidationResult.CreateError("Project validator informed that project could not be loaded"));

                        throw new ProjectException(location, $"Cannot load project from '{location}'");
                    }

                    validationContext = await _projectValidator.ValidateProjectBeforeLoadingAsync(location);

                    if (validationContext.HasErrors)
                    {
                        throw Log.ErrorAndCreateException <InvalidOperationException>($"Project could not be loaded from '{location}', validator returned errors");
                    }

                    project = await QuietlyLoadProjectAsync(location, true).ConfigureAwait(false);

                    validationContext = await _projectValidator.ValidateProjectAsync(project);

                    if (validationContext.HasErrors)
                    {
                        throw Log.ErrorAndCreateException <InvalidOperationException>($"Project data was loaded from '{location}', but the validator returned errors");
                    }

                    RegisterProject(project);
                }
                catch (Exception ex)
                {
                    error = ex;
                    Log.Error(ex, "Failed to load project from '{0}'", location);
                }

                if (error != null)
                {
                    _projectStateSetter.SetProjectLoading(location, false);

                    await ProjectLoadingFailedAsync.SafeInvokeAsync(this, new ProjectErrorEventArgs(location, error, validationContext)).ConfigureAwait(false);

                    return(null);
                }

                _projectStateSetter.SetProjectLoading(project?.Location, false);

                await ProjectLoadedAsync.SafeInvokeAsync(this, new ProjectEventArgs(project)).ConfigureAwait(false);

                Log.Info("Loaded project from '{0}'", location);
            }

            return(project);
        }