Skip to content

Commit

Permalink
Fix VS2022 build (#1566)
Browse files Browse the repository at this point in the history
- add build targets for net6.0
- fix new warnings in VS2022 compiler
  • Loading branch information
mregen committed Nov 2, 2021
1 parent 96eb6c8 commit 49b7f93
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 34 deletions.
5 changes: 4 additions & 1 deletion Libraries/Opc.Ua.Gds.Client.Common/CertificateWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,10 @@ public int KeySize
{
if (Certificate != null)
{
return Certificate.PublicKey.Key.KeySize;
#if ECC_SUPPORT
// TODO
#endif
return X509Utils.GetRSAPublicKeySize(Certificate);
}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<DefineConstants>$(DefineConstants);ECC_SUPPORT</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net472'">
<DefineConstants>$(DefineConstants);ECC_SUPPORT</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net5.0'">
<DefineConstants>$(DefineConstants);ECC_SUPPORT</DefineConstants>
</PropertyGroup>


<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<DefineConstants>$(DefineConstants);ECC_SUPPORT</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<PackageId>$(PackageId).Debug</PackageId>
</PropertyGroup>
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Opc.Ua.Security.Certificates/PEM/PEMWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ private static byte[] EncodeAsPEM(byte[] content, string contentType)
textWriter.WriteLine("-----BEGIN {0}-----", contentType);
while (base64.Length > LineLength)
{
textWriter.WriteLine(base64.Substring(0, LineLength));
textWriter.WriteLine(base64.AsSpan(0, LineLength));
base64 = base64.Substring(LineLength);
}
textWriter.WriteLine(base64);
textWriter.WriteLine("-----END {0}-----", contentType);
return Encoding.ASCII.GetBytes(textWriter.ToString());
}
}
#endregion
#endregion
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public async Task SendAsync(HttpContext context)

if (NodeId.IsNull(input.RequestHeader.AuthenticationToken) && input.TypeId != DataTypeIds.CreateSessionRequest)
{
if (context.Request.Headers.Keys.Contains("Authorization"))
if (context.Request.Headers.ContainsKey("Authorization"))
{
foreach (string value in context.Request.Headers["Authorization"])
{
Expand Down Expand Up @@ -372,7 +372,7 @@ public async Task SendAsync(HttpContext context)
context.Response.ContentLength = response.Length;
context.Response.ContentType = context.Request.ContentType;
context.Response.StatusCode = (int)HttpStatusCode.OK;
#if NETSTANDARD2_1_OR_GREATER || NET5_0
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
await context.Response.Body.WriteAsync(response.AsMemory(0, response.Length)).ConfigureAwait(false);
#else
await context.Response.Body.WriteAsync(response, 0, response.Length).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ DateTime nextUpdate

if (domainNames != null && domainNames.Count > 0)
{
if (!subjectName.Contains("DC=") && !subjectName.Contains("="))
if (!subjectName.Contains("DC=") && !subjectName.Contains('='))
{
subjectName += Utils.Format(", DC={0}", domainNames[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public X509Certificate2 LoadPrivateKey(string thumbprint, string subjectName, st
{
if (!X509Utils.CompareDistinguishedName(subjectName, certificate.Subject))
{
if (subjectName.Contains("="))
if (subjectName.Contains('='))
{
continue;
}
Expand Down
5 changes: 4 additions & 1 deletion Stack/Opc.Ua.Core/Security/Certificates/X509Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public static IList<string> GetDomainsFromCertficate(X509Certificate2 certificat
{
builder.Append('.');
}

#if NET5_0_OR_GREATER || NETSTANDARD2_1
builder.Append(fields[ii].AsSpan(3));
#else
builder.Append(fields[ii].Substring(3));
#endif
}
}

Expand Down
2 changes: 1 addition & 1 deletion Stack/Opc.Ua.Core/Stack/Server/ServerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,7 @@ private void OnProcessRequestQueue(object state)
{
m_totalThreadCount--;

Utils.Trace(Utils.TraceMasks.Error, "Thread ended: " + Thread.CurrentThread.ManagedThreadId + ". Current thread count: " + m_totalThreadCount + ". Active thread count" + m_activeThreadCount);
Utils.Trace(Utils.TraceMasks.Error, "Thread ended: " + Environment.CurrentManagedThreadId + ". Current thread count: " + m_totalThreadCount + ". Active thread count" + m_activeThreadCount);

return;
}
Expand Down
25 changes: 17 additions & 8 deletions Stack/Opc.Ua.Core/Types/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,13 +1020,13 @@ public static int GetTimeout(TimeSpan timeSpan)
return (int)timeSpan.TotalMilliseconds;
}

/// <inheritdoc cref="Dns.GetHostAddressesAsync"/>
/// <inheritdoc cref="Dns.GetHostAddressesAsync(string)"/>
public static Task<IPAddress[]> GetHostAddressesAsync(string hostNameOrAddress)
{
return Dns.GetHostAddressesAsync(hostNameOrAddress);
}

/// <inheritdoc cref="Dns.GetHostAddresses"/>
/// <inheritdoc cref="Dns.GetHostAddresses(string)"/>
public static IPAddress[] GetHostAddresses(string hostNameOrAddress)
{
return Dns.GetHostAddresses(hostNameOrAddress);
Expand Down Expand Up @@ -1104,11 +1104,16 @@ public static string ReplaceLocalhost(string uri, string hostname = null)
}

// construct new uri.
StringBuilder buffer = new StringBuilder();
var buffer = new StringBuilder();
#if NET5_0_OR_GREATER || NETSTANDARD2_1
buffer.Append(uri.AsSpan(0, index))
.Append(hostname ?? GetHostName())
.Append(uri.AsSpan(index + localhost.Length));
#else
buffer.Append(uri.Substring(0, index))
.Append(hostname == null ? GetHostName() : hostname)
.Append(hostname ?? GetHostName())
.Append(uri.Substring(index + localhost.Length));

#endif
return buffer.ToString();
}

Expand Down Expand Up @@ -1140,12 +1145,16 @@ public static string ReplaceDCLocalhost(string subjectName, string hostname = nu
}

// construct new uri.
StringBuilder buffer = new StringBuilder();

var buffer = new StringBuilder();
#if NET5_0_OR_GREATER || NETSTANDARD2_1
buffer.Append(subjectName.AsSpan(0, index + 3))
.Append(hostname == null ? GetHostName() : hostname)
.Append(subjectName.AsSpan(index + dclocalhost.Length));
#else
buffer.Append(subjectName.Substring(0, index + 3))
.Append(hostname == null ? GetHostName() : hostname)
.Append(subjectName.Substring(index + dclocalhost.Length));

#endif
return buffer.ToString();
}

Expand Down
42 changes: 27 additions & 15 deletions Tests/Opc.Ua.Gds.Tests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public void QueryServersByName()
string searchName = application.ApplicationRecord.ApplicationNames[0].Text.Trim();
if (searchName.Length > searchPatternLength)
{
searchName = searchName.Substring(0, searchPatternLength) + "%";
searchName = string.Concat(searchName.Substring(0, searchPatternLength), "%");
}
atLeastOneServer = m_gdsClient.GDSClient.QueryServers(1, searchName, "", "", null);
Assert.IsNotNull(atLeastOneServer);
Expand Down Expand Up @@ -553,7 +553,7 @@ public void QueryServersByAppUri()
string searchName = application.ApplicationRecord.ApplicationUri;
if (searchName.Length > searchPatternLength)
{
searchName = searchName.Substring(0, searchPatternLength) + "%";
searchName = string.Concat(searchName.Substring(0, searchPatternLength), "%");
}
atLeastOneServer = m_gdsClient.GDSClient.QueryServers(1, null, searchName, null, null);
Assert.IsNotNull(atLeastOneServer);
Expand Down Expand Up @@ -585,7 +585,7 @@ public void QueryServersByProductUri()
string searchName = application.ApplicationRecord.ProductUri;
if (searchName.Length > searchPatternLength)
{
searchName = searchName.Substring(0, searchPatternLength) + "%";
searchName = string.Concat(searchName.Substring(0, searchPatternLength), "%");
}
atLeastOneServer = m_gdsClient.GDSClient.QueryServers(1, null, null, searchName, null);
Assert.IsNotNull(atLeastOneServer);
Expand Down Expand Up @@ -654,7 +654,8 @@ public void StartInvalidNewKeyPairRequests()
foreach (var application in m_invalidApplicationTestSet)
{
Assert.Null(application.CertificateRequestId);
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.StartNewKeyPairRequest(
application.ApplicationRecord.ApplicationId,
application.CertificateGroupId,
Expand Down Expand Up @@ -742,7 +743,8 @@ public void FinishInvalidNewKeyPairRequests()
ConnectGDS(true);
foreach (var application in m_invalidApplicationTestSet)
{
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.FinishRequest(
application.ApplicationRecord.ApplicationId,
new NodeId(Guid.NewGuid()),
Expand Down Expand Up @@ -861,7 +863,8 @@ public void StartGoodSigningRequestWithInvalidAppURI()
// load csr with invalid app URI
var testCSR = Utils.GetAbsoluteFilePath("test.csr", true, true, false);
byte[] certificateRequest = File.ReadAllBytes(testCSR);
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.StartSigningRequest(
application.ApplicationRecord.ApplicationId,
application.CertificateGroupId,
Expand All @@ -877,21 +880,24 @@ public void GetGoodCertificateGroupsNullTests()
AssertIgnoreTestWithoutGoodRegistration();
ConnectGDS(true);

Assert.That(() => {
Assert.That(() =>
{
m_gdsClient.GDSClient.GetCertificateGroups(null);
}, Throws.Exception);

foreach (var application in m_goodApplicationTestSet)
{
var trustListId = m_gdsClient.GDSClient.GetTrustList(application.ApplicationRecord.ApplicationId, null);
var trustList = m_gdsClient.GDSClient.ReadTrustList(trustListId);
Assert.That(() => {
Assert.That(() =>
{
m_gdsClient.GDSClient.ReadTrustList(null);
}, Throws.Exception);
var certificateGroups = m_gdsClient.GDSClient.GetCertificateGroups(application.ApplicationRecord.ApplicationId);
foreach (var certificateGroup in certificateGroups)
{
Assert.That(() => {
Assert.That(() =>
{
m_gdsClient.GDSClient.GetTrustList(null, certificateGroup);
}, Throws.Exception);
}
Expand All @@ -903,22 +909,27 @@ public void GetInvalidCertificateGroupsNullTests()
{
AssertIgnoreTestWithoutInvalidRegistration();
ConnectGDS(true);
Assert.That(() => {
Assert.That(() =>
{
m_gdsClient.GDSClient.GetCertificateGroups(null);
}, Throws.Exception);
Assert.That(() => {
Assert.That(() =>
{
m_gdsClient.GDSClient.GetCertificateGroups(new NodeId(Guid.NewGuid()));
}, Throws.Exception);

foreach (var application in m_invalidApplicationTestSet)
{
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.GetTrustList(application.ApplicationRecord.ApplicationId, null);
}, Throws.Exception);
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.GetTrustList(application.ApplicationRecord.ApplicationId, new NodeId(Guid.NewGuid()));
}, Throws.Exception);
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.GetCertificateGroups(application.ApplicationRecord.ApplicationId);
}, Throws.Exception);
}
Expand Down Expand Up @@ -962,7 +973,8 @@ public void GetInvalidCertificateStatus()
ConnectGDS(true);
foreach (var application in m_invalidApplicationTestSet)
{
Assert.That(() => {
Assert.That(() =>
{
_ = m_gdsClient.GDSClient.GetCertificateStatus(application.ApplicationRecord.ApplicationId, null, null);
}, Throws.Exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<DefineConstants>$(DefineConstants);ECC_SUPPORT</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<DefineConstants>$(DefineConstants);ECC_SUPPORT</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
Expand All @@ -43,6 +47,14 @@
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Formats.Asn1" Version="5.0.0" />
</ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions targets.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<Project>
<Choose>
<!-- Visual Studio 2022 -->
<When Condition="'$(VisualStudioVersion)' == '17.0'">
<PropertyGroup>
<AppTargetFrameworks>net462;netcoreapp3.1;net6.0</AppTargetFrameworks>
<AppTargetFramework>net6.0</AppTargetFramework>
<TestsTargetFrameworks>net462;netcoreapp2.1;netcoreapp3.1;net6.0</TestsTargetFrameworks>
<LibTargetFrameworks>net462;netstandard2.0;netstandard2.1;net6.0</LibTargetFrameworks>
<LibxTargetFrameworks>net462;netcoreapp2.1;netstandard2.1;net6.0</LibxTargetFrameworks>
</PropertyGroup>
</When>
<!-- Visual Studio 2019 -->
<When Condition="'$(VisualStudioVersion)' == '16.0'">
<PropertyGroup>
<AppTargetFrameworks>net462;netcoreapp3.1</AppTargetFrameworks>
Expand Down

0 comments on commit 49b7f93

Please sign in to comment.