Skip to content

Commit

Permalink
Improve validation for AET, IP/Host
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Chang <vicchang@nvidia.com>
  • Loading branch information
mocsharp committed Nov 17, 2022
1 parent 9a0a705 commit f6c7832
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 8 deletions.
83 changes: 83 additions & 0 deletions src/Configuration/Test/ValidationExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

using System;
using System.Collections.Generic;
using FellowOakDicom;
using Monai.Deploy.InformaticsGateway.Api;
using Xunit;
Expand Down Expand Up @@ -202,5 +203,87 @@ public void SourceApplicationEntity_Valid()
}

#endregion SourceApplicationEntity.IsValid

#region IsAeTitleValid
[Theory]
[InlineData("123")]
[InlineData("MYAET")]
[InlineData("EAST-123-123")]
public void GivenAValidAETitle_WhenIIsAeTitleValid_ExpectToReturnTrue(string value)
{
var errors = new List<string>();
Assert.True(ValidationExtensions.IsAeTitleValid("test", value, errors));
Assert.Empty(errors);
}

[Theory]
[InlineData("")]
[InlineData(" ")]
[InlineData("AE\\")]
[InlineData("AE/")]
[InlineData("1$")]
[InlineData("A.E.T.")]
public void GivenAnInvalidAETitle_WhenIsAeTitleValid_ExpectToReturnFalse(string value)
{
var errors = new List<string>();
Assert.False(ValidationExtensions.IsAeTitleValid("test", value, errors));
Assert.NotEmpty(errors);
}

#endregion

#region IsValidHostNameIp
[Theory]
[InlineData("0.0.0.0")]
[InlineData("10.20.30.40")]
[InlineData("255.255.255.0")]
[InlineData("1.2.3.4")]
[InlineData("192.168.0.1")]
public void GivenAValidIpAddress_WhenIsValidHostNameIpIsCalled_ExpectToReturnTrue(string value)
{
var errors = new List<string>();
Assert.True(ValidationExtensions.IsValidHostNameIp("test", value, errors));
Assert.Empty(errors);
}

[Theory]
[InlineData("256.256.256.256")]
[InlineData("1.0")]
[InlineData("1")]
[InlineData("2.3.4")]
public void GivenAnInvalidIpAddress_WhenIsValidHostNameIpIsCalled_ExpectToReturnFalse(string value)
{
var errors = new List<string>();
Assert.False(ValidationExtensions.IsValidHostNameIp("test", value, errors));
Assert.NotEmpty(errors);
}

[Theory]
[InlineData("localhost")]
[InlineData("east-1")]
[InlineData("east-2.k8s.local")]
[InlineData("cloud.com")]
[InlineData("east.cloud.com")]
[InlineData("super.west.cloud.com")]
public void GivenAValidHostName_WhenIsValidHostNameIpIsCalled_ExpectToReturnTrue(string value)
{
var errors = new List<string>();
Assert.True(ValidationExtensions.IsValidHostNameIp("test", value, errors));
Assert.Empty(errors);
}

[Theory]
[InlineData("localhost!")]
[InlineData("cloud@com")]
[InlineData("east@cloud.com")]
[InlineData("super/west.cloud.com")]
public void GivenAnInvalidHostName_WhenIsValidHostNameIpIsCalled_ExpectToReturnFalse(string value)
{
var errors = new List<string>();
Assert.False(ValidationExtensions.IsValidHostNameIp("test", value, errors));
Assert.NotEmpty(errors);
}

#endregion
}
}
27 changes: 19 additions & 8 deletions src/Configuration/ValidationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Ardalis.GuardClauses;
using FellowOakDicom;
using Monai.Deploy.InformaticsGateway.Api;
Expand All @@ -29,7 +30,7 @@ public static class ValidationExtensions

public static bool IsValid(this MonaiApplicationEntity monaiApplicationEntity, out IList<string> validationErrors)
{
Guard.Against.Null(monaiApplicationEntity, nameof(monaiApplicationEntity));
Guard.Against.Null(monaiApplicationEntity);

validationErrors = new List<string>();

Expand All @@ -42,7 +43,7 @@ public static bool IsValid(this MonaiApplicationEntity monaiApplicationEntity, o

public static bool IsValid(this DestinationApplicationEntity destinationApplicationEntity, out IList<string> validationErrors)
{
Guard.Against.Null(destinationApplicationEntity, nameof(destinationApplicationEntity));
Guard.Against.Null(destinationApplicationEntity);

validationErrors = new List<string>();

Expand All @@ -57,7 +58,7 @@ public static bool IsValid(this DestinationApplicationEntity destinationApplicat

public static bool IsValid(this SourceApplicationEntity sourceApplicationEntity, out IList<string> validationErrors)
{
Guard.Against.Null(sourceApplicationEntity, nameof(sourceApplicationEntity));
Guard.Against.Null(sourceApplicationEntity);

validationErrors = new List<string>();

Expand All @@ -70,7 +71,7 @@ public static bool IsValid(this SourceApplicationEntity sourceApplicationEntity,

public static bool IsValidDicomTag(string source, string grouping, IList<string> validationErrors = null)
{
Guard.Against.NullOrWhiteSpace(source, nameof(source));
Guard.Against.NullOrWhiteSpace(source);

try
{
Expand All @@ -92,25 +93,35 @@ public static bool IsValidDicomTag(string source, string grouping, IList<string>

public static bool IsAeTitleValid(string source, string aeTitle, IList<string> validationErrors = null)
{
Guard.Against.NullOrWhiteSpace(source, nameof(source));
Guard.Against.NullOrWhiteSpace(source);

if (!string.IsNullOrWhiteSpace(aeTitle) && aeTitle.Length <= 15) return true;
if (!string.IsNullOrWhiteSpace(aeTitle) &&
aeTitle.Length <= 15 &&
Regex.IsMatch(aeTitle, @"^[a-zA-Z0-9_\-]+$"))
{
return true;
}

validationErrors?.Add($"'{aeTitle}' is not a valid AE Title (source: {source}).");
return false;
}

public static bool IsValidHostNameIp(string source, string hostIp, IList<string> validationErrors = null)
{
if (!string.IsNullOrWhiteSpace(hostIp)) return true;
if (!string.IsNullOrWhiteSpace(hostIp) &&
(Regex.IsMatch(hostIp, @"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$") || // IP address
Regex.IsMatch(hostIp, @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$"))) // Host/domain name
{
return true;
}

validationErrors?.Add($"Invalid host name/IP address '{hostIp}' specified for {source}.");
return false;
}

public static bool IsPortValid(string source, int port, IList<string> validationErrors = null)
{
Guard.Against.NullOrWhiteSpace(source, nameof(source));
Guard.Against.NullOrWhiteSpace(source);

if (port > 0 && port <= 65535) return true;

Expand Down

0 comments on commit f6c7832

Please sign in to comment.