-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from atc-net/feature/validation_helpers
Feature/validation helpers
- Loading branch information
Showing
11 changed files
with
192 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
namespace Atc.Network.Helpers; | ||
|
||
public static class OpcUaAddressHelper | ||
{ | ||
private const int MinPortNumber = 1; | ||
private const int MaxPortNumber = ushort.MaxValue; | ||
|
||
public static bool IsValid( | ||
string url, | ||
bool restrictToIp4Address = false) | ||
{ | ||
ArgumentNullException.ThrowIfNull(url); | ||
|
||
return Uri.TryCreate(url, UriKind.Absolute, out var uriResult) && | ||
IsValid(uriResult, restrictToIp4Address); | ||
} | ||
|
||
public static bool IsValid( | ||
Uri uri, | ||
bool restrictToIp4Address = false) | ||
{ | ||
ArgumentNullException.ThrowIfNull(uri); | ||
|
||
if (!string.Equals(uri.Scheme, "opc.tcp", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (restrictToIp4Address) | ||
{ | ||
if (!IPv4AddressHelper.IsValid(uri.Host)) | ||
{ | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
if (string.IsNullOrEmpty(uri.Host)) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return uri.Port is >= MinPortNumber and <= MaxPortNumber; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// ReSharper disable StringLiteralTypo | ||
namespace Atc.Network.Test.Helpers; | ||
|
||
[SuppressMessage("Design", "CA1054:URI parameters should not be strings", Justification = "OK")] | ||
[SuppressMessage("Usage", "CA2234:Pass system uri objects instead of strings", Justification = "OK")] | ||
public class OpcUaAddressHelperTests | ||
{ | ||
[Theory] | ||
[InlineData(true, "opc.tcp://milo.digitalpetri.com:62541", false)] | ||
[InlineData(true, "opc.tcp://milo.digitalpetri.com:62541/", false)] | ||
[InlineData(true, "opc.tcp://milo.digitalpetri.com:62541/milo", false)] | ||
[InlineData(true, "opc.tcp://192.168.1.1:62541", true)] | ||
[InlineData(true, "opc.tcp://192.168.1.1:62541/", true)] | ||
[InlineData(true, "opc.tcp://192.168.1.1:62541/milo", true)] | ||
[InlineData(false, "", false)] | ||
[InlineData(false, "milo.digitalpetri.com:62541/milo", false)] | ||
[InlineData(false, "opc.tcp://62541/milo", false)] | ||
[InlineData(false, "opc.tcp://milo.digitalpetri.com:62541/milo", true)] | ||
[InlineData(false, "opc.tcp://192.168.1:62541", true)] | ||
[InlineData(false, "opc.tcp://192.168.1:62541/", true)] | ||
[InlineData(false, "opc.tcp://192.168.1:62541/milo", true)] | ||
public void IsValid( | ||
bool expected, | ||
string url, | ||
bool restrictToIp4Address) | ||
{ | ||
// Act | ||
var isValid = OpcUaAddressHelper.IsValid(url, restrictToIp4Address); | ||
|
||
// Assert | ||
Assert.Equal(expected, isValid); | ||
} | ||
|
||
[Theory] | ||
[InlineData(true, "opc.tcp://milo.digitalpetri.com:62541", false)] | ||
[InlineData(true, "opc.tcp://milo.digitalpetri.com:62541/", false)] | ||
[InlineData(true, "opc.tcp://milo.digitalpetri.com:62541/milo", false)] | ||
[InlineData(true, "opc.tcp://192.168.1.1:62541", true)] | ||
[InlineData(true, "opc.tcp://192.168.1.1:62541/", true)] | ||
[InlineData(true, "opc.tcp://192.168.1.1:62541/milo", true)] | ||
[InlineData(false, "milo.digitalpetri.com:62541/milo", false)] | ||
[InlineData(false, "opc.tcp://62541/milo", false)] | ||
[InlineData(false, "opc.tcp://milo.digitalpetri.com:62541/milo", true)] | ||
[InlineData(false, "opc.tcp://192.168.1:62541", true)] | ||
[InlineData(false, "opc.tcp://192.168.1:62541/", true)] | ||
[InlineData(false, "opc.tcp://192.168.1:62541/milo", true)] | ||
|
||
public void IsValid_AsUri( | ||
bool expected, | ||
string url, | ||
bool restrictToIp4Address) | ||
{ | ||
// Arrange | ||
var uri = new Uri(url); | ||
|
||
// Act | ||
var isValid = OpcUaAddressHelper.IsValid(uri, restrictToIp4Address); | ||
|
||
// Assert | ||
Assert.Equal(expected, isValid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters