Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak which tests get run on .NET 6 Android #1505

Merged
merged 3 commits into from
Apr 4, 2023
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
39 changes: 38 additions & 1 deletion src/Couchbase.Lite.Shared/Sync/WebSocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
using Couchbase.Lite.Internal.Logging;
using Couchbase.Lite.Support;
using Couchbase.Lite.Util;

using JetBrains.Annotations;

using LiteCore.Interop;
Expand Down Expand Up @@ -927,6 +926,30 @@ private bool ValidateServerCert(object sender, X509Certificate certificate, X509
#endif

if (!onlySelfSigned && sslPolicyErrors != SslPolicyErrors.None) {
#if NET6_0_OR_GREATER && __ANDROID__
// Workaround (part 1) for https://github.com/dotnet/runtime/issues/84202
WriteLog.To.Sync.I(Tag, "Checking system TrustManagerFactory");
var tmf = Javax.Net.Ssl.TrustManagerFactory.GetInstance(Javax.Net.Ssl.TrustManagerFactory.DefaultAlgorithm);
tmf.Init(default(Java.Security.KeyStore));
var cf = Java.Security.Cert.CertificateFactory.GetInstance("X.509");
foreach (var tm in tmf.GetTrustManagers()) {
if(tm is Javax.Net.Ssl.IX509TrustManager x509tm) {
var javaCert = cf.GenerateCertificate(new MemoryStream(cert2.GetRawCertData())) as Java.Security.Cert.X509Certificate;
try {
x509tm.CheckServerTrusted(new[] { javaCert }, "RSA");
} catch(Exception) {
WriteLog.To.Sync.W(Tag, "TrustManager does not trust this server cert, moving to next one...");
continue;
}

WriteLog.To.Sync.I(Tag, "TrustManager trusts this server cert");
return true;
}
}

WriteLog.To.Sync.W(Tag, "No more TrustManagers found, server cert is not trusted by system");
#endif

WriteLog.To.Sync.W(Tag, $"Error validating TLS chain: {sslPolicyErrors}");
if (chain.ChainElements != null) {
foreach(var element in chain.ChainElements) {
Expand Down Expand Up @@ -956,6 +979,20 @@ private bool ValidateServerCert(object sender, X509Certificate certificate, X509
}
}
} else if (onlySelfSigned) {
#if NET6_0_OR_GREATER && __ANDROID__
// Workaround (part 2) for https://github.com/dotnet/runtime/issues/84202
if (chain.ChainElements.Count == 0) {
WriteLog.To.Sync.I(Tag, "Working around weird behavior in .NET 6 Android (X509Chain empty...)");
var isSelfSigned = cert2.IssuerName.Name == cert2.SubjectName.Name;
if(!isSelfSigned) {
WriteLog.To.Sync.E(Tag, "ValidateServerCert failed due to received lone cert not being self signed");
_validationException = new TlsCertificateException("A non self-signed certificate was received in self-signed mode.",
C4NetworkErrorCode.TLSCertUnknownRoot, X509ChainStatusFlags.ExplicitDistrust);
}

return isSelfSigned;
}
#endif
if (chain.ChainElements.Count != 1) {
WriteLog.To.Sync.E(Tag, "ValidateServerCert failed due to cert chain ChainElements's Count != 1");
_validationException = new TlsCertificateException("A non self-signed certificate was received in self-signed mode.",
Expand Down
2 changes: 0 additions & 2 deletions src/Couchbase.Lite.Tests.Shared/TLSIdentityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public TLSIdentityTest()
}

#region TLSIdentity tests
#if !NET6_0_APPLE && !NET6_0_ANDROID
[Fact]
public void TestCreateGetDeleteServerIdentity() => CreateGetDeleteServerIdentity(true);

Expand Down Expand Up @@ -189,7 +188,6 @@ public void TestCertificateExpiration()
// Delete
TLSIdentity.DeleteIdentity(_store, ServerCertLabel, null);
}
#endif
#endregion

#region TLSIdentity tests helpers
Expand Down
29 changes: 24 additions & 5 deletions src/Couchbase.Lite.Tests.Shared/URLEndpointListenerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public URLEndpointListenerTest()


#endregion
#if !NET6_0_APPLE && !NET6_0_ANDROID
#region Public Methods


Expand Down Expand Up @@ -436,7 +435,10 @@ public void TestClientCertAuthenticatorRootCerts()
TLSIdentity.DeleteIdentity(_store, ClientCertLabel, null);
_listener.Stop();
}
#endif


#if !NET6_0_ANDROID
[Fact]
public void TestListenerWithImportIdentity()
{
Expand Down Expand Up @@ -536,7 +538,7 @@ public void TestAcceptOnlySelfSignedCertMode()
false,//accept only self signed server cert
null,
//TODO: Need to handle Linux throwing different error TLSCertUntrusted (5008)
(int)CouchbaseLiteError.TLSCertUnknownRoot, //maui android 5006
(int)CouchbaseLiteError.TLSCertUnknownRoot,
CouchbaseLiteErrorType.CouchbaseLite
);

Expand Down Expand Up @@ -812,14 +814,15 @@ public void TestMultipleReplicatorsOnReadOnlyListener()

[Fact]//hang maui android
public void TestDeleteWithActiveReplicationsAndURLEndpointListener() => WithActiveReplicationsAndURLEndpointListener(false);
#endif

[Fact]
public void TestCloseWithActiveReplicatorAndURLEndpointListeners() => WithActiveReplicatorAndURLEndpointListeners(true);

[Fact]
public void TestDeleteWithActiveReplicatorAndURLEndpointListeners() => WithActiveReplicatorAndURLEndpointListeners(false);

#endif

[Fact]
public void TestStopListener()
{
Expand Down Expand Up @@ -911,7 +914,6 @@ public void TestCreateListenerConfigWithEmptyCollection()
}

#endregion
#endif
#region Private Methods

private void CollectionsPushPullReplication(bool continuous)
Expand Down Expand Up @@ -968,7 +970,24 @@ private void CollectionsPushPullReplication(bool continuous)

private int GetEADDRINUSECode()
{
#if NET6_0_OR_GREATER || __MOBILE__
#if NET6_0_OR_GREATER
if (OperatingSystem.IsAndroid() || OperatingSystem.IsLinux())
{
return 98;
}

if (OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst() || OperatingSystem.IsMacOS())
{
return 48;
}

if (OperatingSystem.IsWindows())
{
return 100;
}

throw new PlatformNotSupportedException();
#elif __MOBILE__
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return 100;
Expand Down