Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ public void Audit_hostname_can_not_be_null_when_adding_audit_instance()

[TestCase("192.168.1.1")]
[TestCase("256.0.0.0")]
[TestCase("::1")]
[TestCase("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
public void Audit_hostname_can_be_an_ip_address_when_adding_audit_instance(string ipAddress)
{
var viewModel = new ServiceControlAddViewModel
Expand All @@ -337,6 +339,33 @@ public void Audit_hostname_can_be_an_ip_address_when_adding_audit_instance(strin
Assert.That(notifyErrorInfo.GetErrors(nameof(viewModel.AuditHostName)), Is.Empty);
}

[TestCase("hostname with spaces")]
[TestCase("bad@hostname")]
[TestCase("bad#hostname")]
[TestCase("badhostname...")]
[TestCase("badhostname[/")]
public void Audit_hostname_cannot_contain_invalid_characters_when_adding_audit_instance(string invalidHostname)
{
var viewModel = new ServiceControlAddViewModel
{
InstallAuditInstance = true,
SubmitAttempted = true,
AuditHostName = invalidHostname
};

viewModel.NotifyOfPropertyChange(nameof(viewModel.AuditHostName));

var notifyErrorInfo = GetNotifyErrorInfo(viewModel);
var errors = notifyErrorInfo.GetErrors(nameof(viewModel.AuditHostName));

Assert.Multiple(() =>
{
Assert.That(errors, Is.Not.Empty, "Hostname validation should exist and trigger for invalid hostnames");
Assert.That(errors.Cast<string>().Any(error => error.Contains("Hostname is not valid")), Is.True,
"Hostname validation should display the exact error message 'Hostname is not valid'");
});
}

#endregion

#region Portnumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ public void Error_hostname_can_not_be_null_when_adding_error_instance()

[TestCase("192.168.1.1")]
[TestCase("256.0.0.0")]
[TestCase("::1")]
[TestCase("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
public void Error_hostname_can_be_an_ip_address_when_adding_error_instance(string ipAddress)
{
var viewModel = new ServiceControlAddViewModel
Expand All @@ -336,6 +338,33 @@ public void Error_hostname_can_be_an_ip_address_when_adding_error_instance(strin
Assert.That(notifyErrorInfo.GetErrors(nameof(viewModel.ErrorHostName)), Is.Empty);
}

[TestCase("hostname with spaces")]
[TestCase("bad@hostname")]
[TestCase("bad#hostname")]
[TestCase("badhostname...")]
[TestCase("badhostname[/")]
public void Error_hostname_cannot_contain_invalid_characters_when_adding_error_instance(string invalidHostname)
{
var viewModel = new ServiceControlAddViewModel
{
InstallErrorInstance = true,
SubmitAttempted = true,
ErrorHostName = invalidHostname
};

viewModel.NotifyOfPropertyChange(nameof(viewModel.ErrorHostName));

var notifyErrorInfo = GetNotifyErrorInfo(viewModel);
var errors = notifyErrorInfo.GetErrors(nameof(viewModel.ErrorHostName));

Assert.Multiple(() =>
{
Assert.That(errors, Is.Not.Empty, "Hostname validation should exist and trigger for invalid hostnames");
Assert.That(errors.Cast<string>().Any(error => error.Contains("Hostname is not valid")), Is.True,
"Hostname validation should display the exact error message 'Hostname is not valid'");
});
}

#endregion

#region Portnumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public void Monitoring_hostname_cannot_be_null_when_adding_monitoring_instance()

[TestCase("192.168.1.1")]
[TestCase("256.0.0.0")]
[TestCase("::1")]
[TestCase("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
public void Monitoring_hostname_can_be_an_ip_address_when_adding_monitoring_instance(string ipAddress)
{
var viewModel = new MonitoringAddViewModel
Expand All @@ -142,6 +144,32 @@ public void Monitoring_hostname_can_be_an_ip_address_when_adding_monitoring_inst
Assert.That(notifyErrorInfo.GetErrors(nameof(viewModel.HostName)), Is.Empty);
}

[TestCase("hostname with spaces")]
[TestCase("bad@hostname")]
[TestCase("bad#hostname")]
[TestCase("badhostname...")]
[TestCase("badhostname[/")]
public void Monitoring_hostname_cannot_contain_invalid_characters_when_adding_monitoring_instance(string invalidHostname)
{
var viewModel = new MonitoringAddViewModel
{
SubmitAttempted = true,
HostName = invalidHostname
};

viewModel.NotifyOfPropertyChange(nameof(viewModel.HostName));

var notifyErrorInfo = GetNotifyErrorInfo(viewModel);
var errors = notifyErrorInfo.GetErrors(nameof(viewModel.HostName));

Assert.Multiple(() =>
{
Assert.That(errors, Is.Not.Empty, "Hostname validation should exist and trigger for invalid hostnames");
Assert.That(errors.Cast<string>().Any(error => error.Contains("Hostname is not valid")), Is.True,
"Hostname validation should display the exact error message 'Hostname is not valid'");
});
}

#endregion

#region Portnumber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ServiceControl.Config.Tests.Validation
{
using System.ComponentModel;
using System.Linq;
using NUnit.Framework;
using ServiceControlInstaller.Engine.Instances;
using UI.InstanceAdd;
Expand Down Expand Up @@ -127,7 +128,9 @@ public void Audit_hostname_can_not_be_null_when_editing_audit_instance()

[TestCase("192.168.1.1")]
[TestCase("256.0.0.0")]
public void Audi_hostname_can_be_an_ip_address_when_editing_an_audit_instance(string ipAddress)
[TestCase("::1")]
[TestCase("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
public void Audit_hostname_can_be_an_ip_address_when_editing_an_audit_instance(string ipAddress)
{
var viewModel = new ServiceControlAuditEditViewModel
{
Expand All @@ -142,6 +145,32 @@ public void Audi_hostname_can_be_an_ip_address_when_editing_an_audit_instance(st
Assert.That(notifyErrorInfo.GetErrors(nameof(viewModel.HostName)), Is.Empty);
}

[TestCase("hostname with spaces")]
[TestCase("bad@hostname")]
[TestCase("bad#hostname")]
[TestCase("badhostname...")]
[TestCase("badhostname[/")]
public void Audit_hostname_cannot_contain_invalid_characters_when_editing_audit_instance(string invalidHostname)
{
var viewModel = new ServiceControlAuditEditViewModel
{
SubmitAttempted = true,
HostName = invalidHostname
};

viewModel.NotifyOfPropertyChange(nameof(viewModel.HostName));

var notifyErrorInfo = GetNotifyErrorInfo(viewModel);
var errors = notifyErrorInfo.GetErrors(nameof(viewModel.HostName));

Assert.Multiple(() =>
{
Assert.That(errors, Is.Not.Empty, "Hostname validation should exist and trigger for invalid hostnames");
Assert.That(errors.Cast<string>().Any(error => error.Contains("Hostname is not valid")), Is.True,
"Hostname validation should display the exact error message 'Hostname is not valid'");
});
}

#endregion

#region Portnumber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ServiceControl.Config.Tests.Validation
{
using System.ComponentModel;
using System.Linq;
using NUnit.Framework;
using ServiceControlInstaller.Engine.Instances;
using UI.InstanceAdd;
Expand Down Expand Up @@ -120,6 +121,8 @@ public void Error_hostname_can_not_be_null_when_editing_error_instance()

[TestCase("192.168.1.1")]
[TestCase("256.0.0.0")]
[TestCase("::1")]
[TestCase("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
public void Error_hostname_can_be_an_ip_address_when_editing_an_error_instance(string ipAddress)
{
var viewModel = new ServiceControlEditViewModel
Expand All @@ -135,6 +138,32 @@ public void Error_hostname_can_be_an_ip_address_when_editing_an_error_instance(s
Assert.That(notifyErrorInfo.GetErrors(nameof(viewModel.HostName)), Is.Empty);
}

[TestCase("hostname with spaces")]
[TestCase("bad@hostname")]
[TestCase("bad#hostname")]
[TestCase("badhostname...")]
[TestCase("badhostname[/")]
public void Error_hostname_cannot_contain_invalid_characters_when_editing_error_instance(string invalidHostname)
{
var viewModel = new ServiceControlEditViewModel
{
SubmitAttempted = true,
HostName = invalidHostname
};

viewModel.NotifyOfPropertyChange(nameof(viewModel.HostName));

var notifyErrorInfo = GetNotifyErrorInfo(viewModel);
var errors = notifyErrorInfo.GetErrors(nameof(viewModel.HostName));

Assert.Multiple(() =>
{
Assert.That(errors, Is.Not.Empty, "Hostname validation should exist and trigger for invalid hostnames");
Assert.That(errors.Cast<string>().Any(error => error.Contains("Hostname is not valid")), Is.True,
"Hostname validation should display the exact error message 'Hostname is not valid'");
});
}

#endregion

#region Portnumber
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ServiceControl.Config.Tests.Validation
{
using System.ComponentModel;
using System.Linq;
using NUnit.Framework;
using UI.InstanceEdit;

Expand Down Expand Up @@ -42,6 +43,8 @@ public void Monitoring_hostname_cannot_be_null_when_editing_monitoring_instance(

[TestCase("192.168.1.1")]
[TestCase("256.0.0.0")]
[TestCase("::1")]
[TestCase("2001:0db8:85a3:0000:0000:8a2e:0370:7334")]
public void Monitoring_hostname_can_be_an_ip_address_when_editing_a_monitoring_instance(string ipAddress)
{
var viewModel = new MonitoringEditViewModel
Expand All @@ -56,6 +59,32 @@ public void Monitoring_hostname_can_be_an_ip_address_when_editing_a_monitoring_i

Assert.That(notifyErrorInfo.GetErrors(nameof(viewModel.HostName)), Is.Empty);
}

[TestCase("hostname with spaces")]
[TestCase("bad@hostname")]
[TestCase("bad#hostname")]
[TestCase("badhostname...")]
[TestCase("badhostname[/")]
public void Monitoring_hostname_cannot_contain_invalid_characters_when_editing_monitoring_instance(string invalidHostname)
{
var viewModel = new MonitoringEditViewModel
{
SubmitAttempted = true,
HostName = invalidHostname
};

viewModel.NotifyOfPropertyChange(nameof(viewModel.HostName));

var notifyErrorInfo = GetNotifyErrorInfo(viewModel);
var errors = notifyErrorInfo.GetErrors(nameof(viewModel.HostName));

Assert.Multiple(() =>
{
Assert.That(errors, Is.Not.Empty, "Hostname validation should exist and trigger for invalid hostnames");
Assert.That(errors.Cast<string>().Any(error => error.Contains("Hostname is not valid")), Is.True,
"Hostname validation should display the exact error message 'Hostname is not valid'");
});
}
#endregion

#region Portnumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public ServiceControlAddViewModelValidator()

RuleFor(viewModel => viewModel.ErrorHostName)
.NotEmpty()
.ValidHostname()
.When(viewModel => viewModel.InstallErrorInstance);

RuleFor(x => x.ErrorPortNumber)
Expand Down Expand Up @@ -155,6 +156,7 @@ public ServiceControlAddViewModelValidator()

RuleFor(viewModel => viewModel.AuditHostName)
.NotEmpty()
.ValidHostname()
.When(viewModel => viewModel.InstallAuditInstance);

RuleFor(x => x.AuditPortNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ public MonitoringEditViewModelValidator()
.NotEmpty()
.When(x => x.SubmitAttempted);

RuleFor(x => x.HostName)
.NotEmpty()
.When(x => x.SubmitAttempted);

RuleFor(x => x.PortNumber)
.NotEmpty()
.ValidPort()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public ServiceControlAuditEditViewModelValidator()

RuleFor(x => x.HostName)
.NotEmpty()
.ValidHostname()
.When(x => x.SubmitAttempted);

RuleFor(x => x.PortNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public ServiceControlEditViewModelValidator()

RuleFor(x => x.HostName)
.NotEmpty()
.ValidHostname()
.When(x => x.SubmitAttempted);

RuleFor(x => x.PortNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ protected SharedMonitoringEditorViewModelValidator()
{
RuleFor(x => x.HostName)
.NotEmpty()
.When(x => x.SubmitAttempted);
.ValidHostname();

RuleFor(x => x.LogPath)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public AddNewAuditInstanceViewModelValidator()
.NotEmpty()
.NotEqual(x => x.ServiceControlAudit.AuditQueueName).WithMessage(string.Format(Validation.Validations.MSG_UNIQUEQUEUENAME, "Audit"))
.When(x => x.SubmitAttempted && (x.ServiceControlAudit.AuditForwarding?.Value ?? false));

RuleFor(x => x.ServiceControlAudit.HostName)
.NotEmpty()
.ValidHostname()
.When(x => x.SubmitAttempted);
}
}
}
17 changes: 17 additions & 0 deletions src/ServiceControl.Config/Validation/Validations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ public static IRuleBuilderOptions<T, string> MustBeUniqueWindowsServiceName<T>(t
});
}

public static IRuleBuilderOptions<T, string> ValidHostname<T>(this IRuleBuilder<T, string> ruleBuilder)
{
return ruleBuilder.Must((t, hostname) =>
{
if (string.IsNullOrWhiteSpace(hostname))
{
return false;
}

var hostNameType = Uri.CheckHostName(hostname);
return hostNameType is UriHostNameType.Dns or UriHostNameType.IPv4 or UriHostNameType.IPv6;
})
.WithMessage(MSG_INVALID_HOSTNAME);
}

public const string MSG_EMAIL_NOT_VALID = "Not Valid.";

public const string MSG_THIS_TRANSPORT_REQUIRES_A_CONNECTION_STRING = "This transport requires a connection string.";
Expand Down Expand Up @@ -234,6 +249,8 @@ public static IRuleBuilderOptions<T, string> MustBeUniqueWindowsServiceName<T>(t

public const string MSG_ILLEGAL_PATH_CHAR = "Paths cannot contain characters {0}";

public const string MSG_INVALID_HOSTNAME = "Hostname is not valid.";

static char[] ILLEGAL_PATH_CHARS =
{
'*',
Expand Down