From 8154a51dbe0450428af3a7c544310c712ebca9ad Mon Sep 17 00:00:00 2001 From: BorisDog Date: Mon, 24 Nov 2025 15:20:18 -0800 Subject: [PATCH 1/3] CSHARP-5782: Add .NET10 to unittests and smoke tests --- evergreen/evergreen.yml | 23 +++++++++++ evergreen/install-dotnet.sh | 4 +- tests/BuildProps/Tests.Build.props | 2 +- .../IO/BsonStreamAdapterTests.cs | 2 + .../IO/ByteBufferStreamTests.cs | 8 ++++ .../Serialization/BsonClassMapTests.cs | 2 + .../Serialization/BsonMemberMapTests.cs | 2 + .../MongoDB.Driver.Examples.csproj | 2 +- .../Core/CoreTestConfiguration.cs | 4 +- .../X509CertificateLoader.cs | 35 +++++++++++++++++ .../AuthenticationTests.cs | 3 +- .../Configuration/SslStreamSettingsTests.cs | 39 ++++++++++++++++--- .../GridFSSeekableDownloadStreamTests.cs | 6 +++ .../EncryptionTestHelper.cs | 6 +-- .../MongoDB.Driver.Tests/SslSettingsTests.cs | 7 ++-- tests/MongoDB.Driver.Tests/X509Tests.cs | 5 ++- .../MongoDB.Driver.SmokeTests.Sdk.csproj | 10 ++++- 17 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 tests/MongoDB.Driver.TestHelpers/X509CertificateLoader.cs diff --git a/evergreen/evergreen.yml b/evergreen/evergreen.yml index b807869d465..7546ce1f1ab 100644 --- a/evergreen/evergreen.yml +++ b/evergreen/evergreen.yml @@ -1127,6 +1127,13 @@ tasks: FRAMEWORK: net6.0 - func: run-unit-tests + - name: unit-tests-net100 + commands: + - func: configure-framework + vars: + FRAMEWORK: net10.0 + - func: run-unit-tests + - name: test-net472 commands: - func: setup-csfle-secrets @@ -1701,6 +1708,17 @@ tasks: - func: bootstrap-mongo-orchestration - func: run-smoke-tests + - name: test-smoke-tests-net100 + depends_on: + - name: push-packages-myget + variant: ".push-packages-myget" + commands: + - func: configure-framework + vars: + FRAMEWORK: net10.0 + - func: bootstrap-mongo-orchestration + - func: run-smoke-tests + - name: test-csharp-spec-benchmarks commands: - func: bootstrap-mongo-orchestration @@ -2380,6 +2398,7 @@ buildvariants: - name: unit-tests-net472 - name: unit-tests-netstandard21 - name: unit-tests-net60 + - name: unit-tests-net100 - name: unit-tests-ubuntu display_name: Unit Tests on Ubuntu @@ -2389,6 +2408,7 @@ buildvariants: tasks: - name: unit-tests-netstandard21 - name: unit-tests-net60 + - name: unit-tests-net100 - name: unit-tests-macos display_name: Unit Tests on MacOs @@ -2398,6 +2418,7 @@ buildvariants: tasks: - name: unit-tests-netstandard21 - name: unit-tests-net60 + - name: unit-tests-net100 - name: unit-tests-macos-arm display_name: Unit Tests on MacOs Arm @@ -2406,6 +2427,7 @@ buildvariants: OS: "macos-14-arm64" tasks: - name: unit-tests-net60 + - name: unit-tests-net100 - matrix_name: stable-api-tests matrix_spec: { version: ["5.0", "6.0", "7.0", "8.0", "rapid", "latest"], topology: ["standalone", "sharded-cluster"], auth: "auth", ssl: "nossl", os: "windows-64" } @@ -2778,6 +2800,7 @@ buildvariants: - name: test-smoke-tests-net50 - name: test-smoke-tests-net60 - name: test-smoke-tests-net80 + - name: test-smoke-tests-net100 rules: - if: { version: "*", topology: "*", ssl: "*", os: ["ubuntu-2004", "macos-14"] } then: diff --git a/evergreen/install-dotnet.sh b/evergreen/install-dotnet.sh index d9c1b67b043..e10e1110332 100644 --- a/evergreen/install-dotnet.sh +++ b/evergreen/install-dotnet.sh @@ -2,7 +2,7 @@ set -o errexit # Exit the script with error if any of the commands fail DOTNET_ROOT="${DOTNET_ROOT:-./.dotnet}" -DOTNET_SDK_VERSION="${DOTNET_SDK_VERSION:-8.0}" +DOTNET_SDK_VERSION="${DOTNET_SDK_VERSION:-10.0}" echo "runtime: $FRAMEWORK" @@ -13,6 +13,8 @@ if [ -n "$FRAMEWORK" ]; then RUNTIME_VERSION="6.0" elif [ "$FRAMEWORK" = "net8.0" ]; then RUNTIME_VERSION="8.0" + elif [ "$FRAMEWORK" = "net10.0" ]; then + RUNTIME_VERSION="10.0" elif [ "$FRAMEWORK" = "netstandard2.1" ]; then RUNTIME_VERSION="3.1" elif [ "$FRAMEWORK" = "netcoreapp3.1" ]; then diff --git a/tests/BuildProps/Tests.Build.props b/tests/BuildProps/Tests.Build.props index 20c57ed0aba..899132f35aa 100644 --- a/tests/BuildProps/Tests.Build.props +++ b/tests/BuildProps/Tests.Build.props @@ -6,7 +6,7 @@ - netcoreapp3.1;net6.0 + netcoreapp3.1;net6.0;net10.0 $(TargetFrameworks);net472 false true diff --git a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs index 81eb5b1dc4d..2617ca1887b 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs @@ -599,7 +599,9 @@ public void Read_should_throw_when_subject_is_disposed() var count = 2; subject.Dispose(); +#pragma warning disable CA2022 Action action = () => subject.Read(buffer, offset, count); +#pragma warning restore CA2022 action.ShouldThrow().And.ObjectName.Should().Be("BsonStreamAdapter"); } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs index d6e1c6bb793..1338552c23b 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs @@ -362,7 +362,9 @@ public void Read_should_throw_when_buffer_is_null() { var subject = CreateSubject(); +#pragma warning disable CA2022 Action action = () => subject.Read(null, 0, 0); +#pragma warning restore CA2022 action.ShouldThrow().And.ParamName.Should().Be("buffer"); } @@ -379,7 +381,9 @@ public void Read_should_throw_when_count_is_out_of_range(int destinationSize, in var subject = CreateSubject(); var destination = new byte[destinationSize]; +#pragma warning disable CA2022 Action action = () => subject.Read(destination, offset, count); +#pragma warning restore CA2022 action.ShouldThrow().And.ParamName.Should().Be("count"); } @@ -394,7 +398,9 @@ public void Read_should_throw_when_offset_is_out_of_range(int destinationSize, i var subject = CreateSubject(); var destination = new byte[destinationSize]; +#pragma warning disable CA2022 Action action = () => subject.Read(destination, offset, 0); +#pragma warning restore CA2022 action.ShouldThrow().And.ParamName.Should().Be("offset"); } @@ -405,7 +411,9 @@ public void Read_should_throw_when_subject_is_disposed() var subject = CreateDisposedSubject(); var destination = new byte[1]; +#pragma warning disable CA2022 Action action = () => subject.Read(destination, 0, 1); +#pragma warning restore CA2022 action.ShouldThrow().And.ObjectName.Should().Be("ByteBufferStream"); } diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs index 98d2ae8272a..337aae069c6 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs @@ -681,7 +681,9 @@ private BsonClassMap WithKnownTypes(BsonClassMap classMap, List value) private BsonClassMap Clone(BsonClassMap classMap) { +#pragma warning disable SYSLIB0050 var clone = (BsonClassMap)FormatterServices.GetUninitializedObject(classMap.GetType()); +#pragma warning restore SYSLIB0050 Reflector.SetFieldValue(clone, "_baseClassMap", Reflector.GetFieldValue(classMap, "_baseClassMap")); Reflector.SetFieldValue(clone, "_classType", Reflector.GetFieldValue(classMap, "_classType")); Reflector.SetFieldValue(clone, "_creator", Reflector.GetFieldValue(classMap, "_creator")); diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs index 1ecd01f138a..a85f12d7ef9 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonMemberMapTests.cs @@ -411,7 +411,9 @@ private BsonMemberMap CreateBsonMemberMap() private BsonMemberMap Clone(BsonMemberMap memberMap) { +#pragma warning disable SYSLIB0050 var clone = (BsonMemberMap)FormatterServices.GetUninitializedObject(memberMap.GetType()); +#pragma warning restore SYSLIB0050 Reflector.SetFieldValue(clone, "_classMap", Reflector.GetFieldValue(memberMap, "_classMap")); Reflector.SetFieldValue(clone, "_defaultValue", Reflector.GetFieldValue(memberMap, "_defaultValue")); Reflector.SetFieldValue(clone, "_defaultValueCreator", Reflector.GetFieldValue(memberMap, "_defaultValueCreator")); diff --git a/tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj b/tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj index c993cf89187..8916a97ab04 100644 --- a/tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj +++ b/tests/MongoDB.Driver.Examples/MongoDB.Driver.Examples.csproj @@ -16,7 +16,7 @@ - + diff --git a/tests/MongoDB.Driver.TestHelpers/Core/CoreTestConfiguration.cs b/tests/MongoDB.Driver.TestHelpers/Core/CoreTestConfiguration.cs index 8919c1d79c9..9ced9397892 100644 --- a/tests/MongoDB.Driver.TestHelpers/Core/CoreTestConfiguration.cs +++ b/tests/MongoDB.Driver.TestHelpers/Core/CoreTestConfiguration.cs @@ -151,11 +151,11 @@ public static ClusterBuilder ConfigureCluster(ClusterBuilder builder) X509Certificate cert; if (password == null) { - cert = new X509Certificate2(certificateFilename); + cert = X509CertificateLoader.LoadCertificateFromFile(certificateFilename); } else { - cert = new X509Certificate2(certificateFilename, password); + cert = X509CertificateLoader.LoadPkcs12FromFile(certificateFilename, password); } return ssl.With( clientCertificates: new[] { cert }); diff --git a/tests/MongoDB.Driver.TestHelpers/X509CertificateLoader.cs b/tests/MongoDB.Driver.TestHelpers/X509CertificateLoader.cs new file mode 100644 index 00000000000..d07e5190659 --- /dev/null +++ b/tests/MongoDB.Driver.TestHelpers/X509CertificateLoader.cs @@ -0,0 +1,35 @@ +/* Copyright 2010-present MongoDB Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Security.Cryptography.X509Certificates; + +namespace MongoDB.Driver.TestHelpers; + +#if !NET8_0_OR_GREATER + +public static class X509CertificateLoader +{ + public static X509Certificate2 LoadCertificate(ReadOnlySpan certificateData) => + new(certificateData.ToArray()); + + public static X509Certificate2 LoadCertificateFromFile(string certificateFilename) => + new (certificateFilename); + + public static X509Certificate2 LoadPkcs12FromFile(string certificateFilename, string password) => + new(certificateFilename, password); +} + +#endif diff --git a/tests/MongoDB.Driver.Tests/AuthenticationTests.cs b/tests/MongoDB.Driver.Tests/AuthenticationTests.cs index 56e644f6f1a..ae96102e662 100644 --- a/tests/MongoDB.Driver.Tests/AuthenticationTests.cs +++ b/tests/MongoDB.Driver.Tests/AuthenticationTests.cs @@ -21,6 +21,7 @@ using MongoDB.Driver.Core.Clusters.ServerSelectors; using MongoDB.Driver.Core.Misc; using MongoDB.Driver.Core.TestHelpers.XunitExtensions; +using MongoDB.Driver.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Xunit; @@ -300,7 +301,7 @@ public void Authentication_succeeds_with_MONGODB_X509_mechanism( var pathToClientCertificate = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PATH"); var password = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PASSWORD"); - var clientCertificate = new X509Certificate2(pathToClientCertificate, password); + var clientCertificate = X509CertificateLoader.LoadPkcs12FromFile(pathToClientCertificate, password); var userName = GetRfc2253FormattedUsernameFromX509ClientCertificate(clientCertificate); DropDatabaseUser(DriverTestConfiguration.Client, database: "$external", userName); diff --git a/tests/MongoDB.Driver.Tests/Core/Configuration/SslStreamSettingsTests.cs b/tests/MongoDB.Driver.Tests/Core/Configuration/SslStreamSettingsTests.cs index 9a17f55e9fb..4b9f395c25a 100644 --- a/tests/MongoDB.Driver.Tests/Core/Configuration/SslStreamSettingsTests.cs +++ b/tests/MongoDB.Driver.Tests/Core/Configuration/SslStreamSettingsTests.cs @@ -18,6 +18,7 @@ using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; using FluentAssertions; +using MongoDB.Driver.TestHelpers; using Xunit; namespace MongoDB.Driver.Core.Configuration @@ -63,8 +64,7 @@ public void constructor_with_checkCertificateRevocation_should_initialize_instan [Fact] public void constructor_with_clientCertificates_should_initialize_instance() { - var certificate = Array.Empty(); - var clientCertificates = new[] { new X509Certificate(certificate) }; + var clientCertificates = new [] { X509CertificateLoader.LoadCertificate(__testCert) }; var subject = new SslStreamSettings(clientCertificates: clientCertificates); @@ -136,10 +136,8 @@ public void With_checkCertificateRevocation_should_return_expected_result() [Fact] public void With_clientCertificates_should_return_expected_result() { - var oldCertificate = Array.Empty(); - var newCertificate = Array.Empty(); - var oldClientCertificates = new[] { new X509Certificate(oldCertificate) }; - var newClientCertificates = new[] { new X509Certificate(newCertificate) }; + var oldClientCertificates = new[] { X509CertificateLoader.LoadCertificate(__testCert) }; + var newClientCertificates = new[] { X509CertificateLoader.LoadCertificate(__testCert) }; var subject = new SslStreamSettings(clientCertificates: oldClientCertificates); var result = subject.With(clientCertificates: newClientCertificates); @@ -198,5 +196,34 @@ public void With_serverCertificateValidationCallback_should_return_expected_resu result.EnabledSslProtocols.Should().Be(subject.EnabledSslProtocols); result.ServerCertificateValidationCallback.Should().Be(newServerCertificateValidationCallback); } + + private static readonly byte[] __testCert = + [ + 48, 130, 2, 120, 48, 130, 1, 225, 160, 3, 2, 1, 2, 2, 9, 0, 206, 136, 148, 86, 218, 120, 139, 228, 48, 13, + 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 5, 5, 0, 48, 85, 49, 11, 48, 9, 6, 3, 85, 4, 6, 19, 2, 85, 83, 49, + 16, 48, 14, 6, 3, 85, 4, 8, 12, 7, 71, 101, 111, 114, 103, 105, 97, 49, 16, 48, 14, 6, 3, 85, 4, 7, 12, 7, + 65, 116, 108, 97, 110, 116, 97, 49, 17, 48, 15, 6, 3, 85, 4, 10, 12, 8, 84, 101, 115, 116, 32, 73, 110, 99, + 49, 15, 48, 13, 6, 3, 85, 4, 3, 12, 6, 84, 101, 115, 116, 101, 114, 48, 30, 23, 13, 49, 51, 48, 49, 50, 52, + 50, 50, 51, 49, 53, 55, 90, 23, 13, 52, 48, 48, 54, 49, 48, 50, 50, 51, 49, 53, 55, 90, 48, 85, 49, 11, 48, + 9, 6, 3, 85, 4, 6, 19, 2, 85, 83, 49, 16, 48, 14, 6, 3, 85, 4, 8, 12, 7, 71, 101, 111, 114, 103, 105, 97, + 49, 16, 48, 14, 6, 3, 85, 4, 7, 12, 7, 65, 116, 108, 97, 110, 116, 97, 49, 17, 48, 15, 6, 3, 85, 4, 10, 12, + 8, 84, 101, 115, 116, 32, 73, 110, 99, 49, 15, 48, 13, 6, 3, 85, 4, 3, 12, 6, 84, 101, 115, 116, 101, 114, + 48, 129, 159, 48, 13, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 1, 5, 0, 3, 129, 141, 0, 48, 129, 137, 2, 129, + 129, 0, 232, 50, 71, 90, 149, 7, 66, 154, 146, 17, 101, 153, 240, 201, 205, 17, 59, 156, 61, 172, 41, 163, + 80, 81, 177, 1, 14, 50, 152, 220, 19, 52, 114, 60, 93, 140, 66, 234, 182, 65, 56, 206, 53, 40, 67, 46, 69, + 120, 51, 245, 144, 87, 56, 115, 177, 152, 173, 157, 2, 44, 91, 53, 32, 128, 97, 145, 37, 68, 109, 122, 31, + 161, 19, 141, 73, 202, 231, 201, 251, 237, 201, 100, 104, 200, 174, 94, 50, 176, 101, 223, 70, 34, 22, 172, + 46, 171, 254, 90, 63, 56, 242, 75, 66, 31, 208, 99, 48, 144, 47, 118, 205, 76, 100, 230, 44, 28, 240, 2, + 149, 8, 21, 34, 221, 130, 204, 31, 64, 115, 2, 3, 1, 0, 1, 163, 80, 48, 78, 48, 29, 6, 3, 85, 29, 14, 4, 22, + 4, 20, 176, 240, 6, 4, 223, 189, 160, 18, 104, 18, 37, 81, 177, 25, 156, 225, 223, 154, 251, 188, 48, 31, 6, + 3, 85, 29, 35, 4, 24, 48, 22, 128, 20, 176, 240, 6, 4, 223, 189, 160, 18, 104, 18, 37, 81, 177, 25, 156, + 225, 223, 154, 251, 188, 48, 12, 6, 3, 85, 29, 19, 4, 5, 48, 3, 1, 1, 255, 48, 13, 6, 9, 42, 134, 72, 134, + 247, 13, 1, 1, 5, 5, 0, 3, 129, 129, 0, 125, 197, 226, 141, 46, 105, 97, 45, 124, 3, 78, 240, 183, 242, 135, + 46, 163, 108, 116, 43, 13, 140, 99, 162, 16, 163, 139, 110, 46, 46, 210, 140, 243, 52, 11, 37, 221, 96, 97, + 210, 147, 235, 98, 212, 72, 62, 195, 67, 209, 144, 74, 31, 187, 93, 102, 214, 132, 153, 150, 206, 32, 157, + 233, 124, 210, 12, 248, 64, 62, 65, 32, 18, 111, 211, 78, 51, 231, 117, 205, 93, 80, 41, 8, 190, 22, 236, + 50, 245, 140, 56, 54, 17, 12, 58, 56, 78, 33, 102, 200, 32, 134, 70, 223, 253, 226, 161, 221, 125, 203, 177, + 119, 225, 144, 250, 197, 202, 165, 142, 200, 144, 209, 170, 84, 179, 15, 56, 10, 194 + ]; } } diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs index f291adf375e..5a43649a6eb 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs @@ -139,7 +139,9 @@ public void Read_should_throw_when_buffer_is_null( { var subject = CreateSubject(); +#pragma warning disable CA2022 Action action = () => subject.Read(null, 0, 0); +#pragma warning restore CA2022 action.ShouldThrow().And.ParamName.Should().Be("buffer"); } @@ -171,7 +173,9 @@ public void Read_should_throw_when_count_is_invalid(int bufferLength, int offset } else { +#pragma warning disable CA2022 action = () => subject.Read(buffer, offset, count); +#pragma warning restore CA2022 } action.ShouldThrow().And.ParamName.Should().Be("count"); @@ -196,7 +200,9 @@ public void Read_should_throw_when_offset_is_invalid(int bufferLength, int offse } else { +#pragma warning disable CA2022 action = () => subject.Read(buffer, offset, 0); +#pragma warning restore CA2022 } action.ShouldThrow().And.ParamName.Should().Be("offset"); diff --git a/tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/EncryptionTestHelper.cs b/tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/EncryptionTestHelper.cs index 5225efd1429..3f9beef7540 100644 --- a/tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/EncryptionTestHelper.cs +++ b/tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/EncryptionTestHelper.cs @@ -281,9 +281,9 @@ public static SslSettings CreateTlsOptionsIfAllowed( { var certificateFilename = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PATH"); var password = Environment.GetEnvironmentVariable("MONGO_X509_CLIENT_CERTIFICATE_PASSWORD"); - var clientCertificate = new X509Certificate2(Ensure.IsNotNull(certificateFilename, nameof(certificateFilename)), Ensure.IsNotNull(password, nameof(password))); - var effectiveClientCertificates = clientCertificate != null ? new[] { clientCertificate } : Enumerable.Empty(); - return new SslSettings { ClientCertificates = effectiveClientCertificates }; + var clientCertificate = X509CertificateLoader.LoadPkcs12FromFile(Ensure.IsNotNull(certificateFilename, nameof(certificateFilename)), Ensure.IsNotNull(password, nameof(password))); + + return new SslSettings { ClientCertificates = [ clientCertificate ] }; } return null; diff --git a/tests/MongoDB.Driver.Tests/SslSettingsTests.cs b/tests/MongoDB.Driver.Tests/SslSettingsTests.cs index 3b9fb35f223..c610964578b 100644 --- a/tests/MongoDB.Driver.Tests/SslSettingsTests.cs +++ b/tests/MongoDB.Driver.Tests/SslSettingsTests.cs @@ -22,6 +22,7 @@ using System.Security.Cryptography.X509Certificates; using FluentAssertions; using MongoDB.Driver.Core.Configuration; +using MongoDB.Driver.TestHelpers; using Xunit; namespace MongoDB.Driver.Tests @@ -69,7 +70,7 @@ public void TestClientCertificates() Assert.Equal(null, settings.ClientCertificates); var certificateFileName = GetTestCertificateFileName(); - var clientCertificates = new[] { new X509Certificate2(certificateFileName, "password"), new X509Certificate2(certificateFileName, "password") }; + var clientCertificates = new[] { X509CertificateLoader.LoadPkcs12FromFile(certificateFileName, "password"), X509CertificateLoader.LoadPkcs12FromFile(certificateFileName, "password") }; settings.ClientCertificates = clientCertificates; Assert.True(clientCertificates.SequenceEqual(settings.ClientCertificates)); @@ -102,7 +103,7 @@ public void TestClone() var settings = new SslSettings { CheckCertificateRevocation = false, - ClientCertificates = new[] { new X509Certificate2(certificateFileName, "password") }, + ClientCertificates = [ X509CertificateLoader.LoadPkcs12FromFile(certificateFileName, "password") ], ClientCertificateSelectionCallback = ClientCertificateSelectionCallback, EnabledSslProtocols = SslProtocols.Tls12, ServerCertificateValidationCallback = ServerCertificateValidationCallback @@ -136,7 +137,7 @@ public void TestEquals() clone = settings.Clone(); var certificateFileName = GetTestCertificateFileName(); - clone.ClientCertificates = new[] { new X509Certificate2(certificateFileName, "password") }; + clone.ClientCertificates = [ X509CertificateLoader.LoadPkcs12FromFile(certificateFileName, "password") ]; Assert.NotEqual(settings, clone); Assert.NotEqual(clone, settings); diff --git a/tests/MongoDB.Driver.Tests/X509Tests.cs b/tests/MongoDB.Driver.Tests/X509Tests.cs index a948204ae6f..276ab94abc6 100644 --- a/tests/MongoDB.Driver.Tests/X509Tests.cs +++ b/tests/MongoDB.Driver.Tests/X509Tests.cs @@ -17,6 +17,7 @@ using System.Security.Cryptography.X509Certificates; using FluentAssertions; using MongoDB.Driver.Core.TestHelpers.XunitExtensions; +using MongoDB.Driver.TestHelpers; using MongoDB.TestHelpers.XunitExtensions; using Xunit; @@ -120,6 +121,6 @@ private X509Certificate2 GetClientCertificate(CertificateType certificateType) var path = Environment.GetEnvironmentVariable(pathVariable); var password = Environment.GetEnvironmentVariable(passwordVariable); - return new X509Certificate2(path, password); + return X509CertificateLoader.LoadPkcs12FromFile(path, password); } -} \ No newline at end of file +} diff --git a/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj b/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj index 0e18cd037c0..5e80e8979ec 100644 --- a/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj +++ b/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj @@ -4,8 +4,7 @@ - netcoreapp3.1;net472;net5.0;net6.0 - $(TargetFrameworks);net8.0 + netcoreapp3.1;net472;net5.0;net6.0;net8.0;net10.0 9 ..\..\..\MongoDBTest.ruleset @@ -61,6 +60,13 @@ + + + + + + + Always From 23b6bd29f83ab51e2bd5df569f9fdee243211359 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Mon, 24 Nov 2025 16:11:28 -0800 Subject: [PATCH 2/3] -Add temporary ADD_NET10_TFM flag --- evergreen/evergreen.yml | 10 ++++++++++ evergreen/install-dotnet.sh | 2 +- evergreen/run-smoke-tests.sh | 1 + evergreen/run-unit-tests.sh | 1 + tests/BuildProps/Tests.Build.props | 3 ++- .../MongoDB.Driver.SmokeTests.Sdk.csproj | 3 ++- 6 files changed, 17 insertions(+), 3 deletions(-) diff --git a/evergreen/evergreen.yml b/evergreen/evergreen.yml index 7546ce1f1ab..65ec2c7517c 100644 --- a/evergreen/evergreen.yml +++ b/evergreen/evergreen.yml @@ -1111,6 +1111,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net472 + DOTNET_SDK_VERSION: 10.0 - func: run-unit-tests - name: unit-tests-netstandard21 @@ -1118,6 +1119,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: netstandard2.1 + DOTNET_SDK_VERSION: 10.0 - func: run-unit-tests - name: unit-tests-net60 @@ -1125,6 +1127,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net6.0 + DOTNET_SDK_VERSION: 10.0 - func: run-unit-tests - name: unit-tests-net100 @@ -1132,6 +1135,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net10.0 + DOTNET_SDK_VERSION: 10.0 - func: run-unit-tests - name: test-net472 @@ -1661,6 +1665,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net472 + DOTNET_SDK_VERSION: 10.0 - func: bootstrap-mongo-orchestration - func: run-smoke-tests @@ -1672,6 +1677,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: netcoreapp3.1 + DOTNET_SDK_VERSION: 10.0 - func: bootstrap-mongo-orchestration - func: run-smoke-tests @@ -1683,6 +1689,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net5.0 + DOTNET_SDK_VERSION: 10.0 - func: bootstrap-mongo-orchestration - func: run-smoke-tests @@ -1694,6 +1701,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net6.0 + DOTNET_SDK_VERSION: 10.0 - func: bootstrap-mongo-orchestration - func: run-smoke-tests @@ -1705,6 +1713,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net8.0 + DOTNET_SDK_VERSION: 10.0 - func: bootstrap-mongo-orchestration - func: run-smoke-tests @@ -1716,6 +1725,7 @@ tasks: - func: configure-framework vars: FRAMEWORK: net10.0 + DOTNET_SDK_VERSION: 10.0 - func: bootstrap-mongo-orchestration - func: run-smoke-tests diff --git a/evergreen/install-dotnet.sh b/evergreen/install-dotnet.sh index e10e1110332..661ba5e6bc6 100644 --- a/evergreen/install-dotnet.sh +++ b/evergreen/install-dotnet.sh @@ -2,7 +2,7 @@ set -o errexit # Exit the script with error if any of the commands fail DOTNET_ROOT="${DOTNET_ROOT:-./.dotnet}" -DOTNET_SDK_VERSION="${DOTNET_SDK_VERSION:-10.0}" +DOTNET_SDK_VERSION="${DOTNET_SDK_VERSION:-8.0}" echo "runtime: $FRAMEWORK" diff --git a/evergreen/run-smoke-tests.sh b/evergreen/run-smoke-tests.sh index 3e96a4cc415..950d79a94e7 100644 --- a/evergreen/run-smoke-tests.sh +++ b/evergreen/run-smoke-tests.sh @@ -10,6 +10,7 @@ fi . ./evergreen/append-myget-package-source.sh +export ADD_NET10_TFM="1" # Remove after cake removal export DRIVER_PACKAGE_VERSION="${DRIVER_PACKAGE_VERSION}" ./evergreen/compile-sources.sh "$SMOKE_TESTS_PROJECT" diff --git a/evergreen/run-unit-tests.sh b/evergreen/run-unit-tests.sh index dd5cf66df17..6b408dad623 100644 --- a/evergreen/run-unit-tests.sh +++ b/evergreen/run-unit-tests.sh @@ -7,5 +7,6 @@ if [ "$FRAMEWORK" = "netstandard2.1" ]; then FRAMEWORK="netcoreapp3.1" fi +export ADD_NET10_TFM="1" # Remove after cake removal ./evergreen/compile-sources.sh dotnet test -c Release --no-build --filter "Category!=Integration" -f "$FRAMEWORK" --results-directory ./build/test-results --logger "junit;verbosity=detailed;LogFileName=TEST-{assembly}.xml;FailureBodyFormat=Verbose" --logger "console;verbosity=detailed" diff --git a/tests/BuildProps/Tests.Build.props b/tests/BuildProps/Tests.Build.props index 899132f35aa..84d97359f43 100644 --- a/tests/BuildProps/Tests.Build.props +++ b/tests/BuildProps/Tests.Build.props @@ -6,8 +6,9 @@ - netcoreapp3.1;net6.0;net10.0 + netcoreapp3.1;net6.0 $(TargetFrameworks);net472 + $(TargetFrameworks);net10.0 false true ..\..\MongoDB.Driver.snk diff --git a/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj b/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj index 5e80e8979ec..3a29de4cd74 100644 --- a/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj +++ b/tests/SmokeTests/MongoDB.Driver.SmokeTests.Sdk/MongoDB.Driver.SmokeTests.Sdk.csproj @@ -4,7 +4,8 @@ - netcoreapp3.1;net472;net5.0;net6.0;net8.0;net10.0 + netcoreapp3.1;net472;net5.0;net6.0;net8.0 + $(TargetFrameworks);net10.0 9 ..\..\..\MongoDBTest.ruleset From c623169102f58f8504daa5158fe5f5d98ead68b5 Mon Sep 17 00:00:00 2001 From: BorisDog Date: Tue, 25 Nov 2025 17:18:56 -0800 Subject: [PATCH 3/3] - PR comments + tests fixes --- src/MongoDB.Bson/BsonExtensionMethods.cs | 36 ++++++++++++++----- .../IO/BsonStreamAdapterTests.cs | 4 +-- .../IO/ByteBufferStreamTests.cs | 16 +++------ .../Serializers/CircularReferencesTests.cs | 22 +++++++++--- .../GridFSSeekableDownloadStreamTests.cs | 12 ++----- 5 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/MongoDB.Bson/BsonExtensionMethods.cs b/src/MongoDB.Bson/BsonExtensionMethods.cs index bcb41a8271f..5092efafa28 100644 --- a/src/MongoDB.Bson/BsonExtensionMethods.cs +++ b/src/MongoDB.Bson/BsonExtensionMethods.cs @@ -131,21 +131,41 @@ public static BsonDocument ToBsonDocument( /// The serialization context configurator. /// The serialization args. /// A BsonDocument. - /// nominalType - /// serializer public static BsonDocument ToBsonDocument( this object obj, Type nominalType, IBsonSerializer serializer = null, Action configurator = null, + BsonSerializationArgs args = default(BsonSerializationArgs)) => + ToBsonDocument(obj, nominalType, BsonDocumentWriterSettings.Defaults, serializer, configurator, args); + + internal static BsonDocument ToBsonDocument( + this TNominalType obj, + BsonDocumentWriterSettings bsonDocumentWriterSettings, + IBsonSerializer serializer = null, + Action configurator = null, + BsonSerializationArgs args = default) => + ToBsonDocument(obj, typeof(TNominalType), bsonDocumentWriterSettings, serializer, configurator, args); + + internal static BsonDocument ToBsonDocument( + this object obj, + Type nominalType, + BsonDocumentWriterSettings bsonDocumentWriterSettings, + IBsonSerializer serializer = null, + Action configurator = null, BsonSerializationArgs args = default(BsonSerializationArgs)) { if (nominalType == null) { - throw new ArgumentNullException("nominalType"); + throw new ArgumentNullException(nameof(nominalType)); } args.SetOrValidateNominalType(nominalType, "nominalType"); + if (bsonDocumentWriterSettings == null) + { + throw new ArgumentNullException(nameof(bsonDocumentWriterSettings)); + } + if (obj == null) { return null; @@ -175,11 +195,11 @@ public static BsonDocument ToBsonDocument( // otherwise serialize into a new BsonDocument var document = new BsonDocument(); - using (var bsonWriter = new BsonDocumentWriter(document)) - { - var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); - serializer.Serialize(context, args, obj); - } + using var bsonWriter = new BsonDocumentWriter(document, bsonDocumentWriterSettings); + + var context = BsonSerializationContext.CreateRoot(bsonWriter, configurator); + serializer.Serialize(context, args, obj); + return document; } diff --git a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs index 2617ca1887b..23fb1a74b5c 100644 --- a/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs @@ -599,9 +599,7 @@ public void Read_should_throw_when_subject_is_disposed() var count = 2; subject.Dispose(); -#pragma warning disable CA2022 - Action action = () => subject.Read(buffer, offset, count); -#pragma warning restore CA2022 + Action action = () => _ = subject.Read(buffer, offset, count); action.ShouldThrow().And.ObjectName.Should().Be("BsonStreamAdapter"); } diff --git a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs index 1338552c23b..5da0781ef36 100644 --- a/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs +++ b/tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs @@ -362,9 +362,7 @@ public void Read_should_throw_when_buffer_is_null() { var subject = CreateSubject(); -#pragma warning disable CA2022 - Action action = () => subject.Read(null, 0, 0); -#pragma warning restore CA2022 + Action action = () => _ = subject.Read(null!, 0, 0); action.ShouldThrow().And.ParamName.Should().Be("buffer"); } @@ -381,9 +379,7 @@ public void Read_should_throw_when_count_is_out_of_range(int destinationSize, in var subject = CreateSubject(); var destination = new byte[destinationSize]; -#pragma warning disable CA2022 - Action action = () => subject.Read(destination, offset, count); -#pragma warning restore CA2022 + Action action = () => _ = subject.Read(destination, offset, count); action.ShouldThrow().And.ParamName.Should().Be("count"); } @@ -398,9 +394,7 @@ public void Read_should_throw_when_offset_is_out_of_range(int destinationSize, i var subject = CreateSubject(); var destination = new byte[destinationSize]; -#pragma warning disable CA2022 - Action action = () => subject.Read(destination, offset, 0); -#pragma warning restore CA2022 + Action action = () => _ = subject.Read(destination, offset, 0); action.ShouldThrow().And.ParamName.Should().Be("offset"); } @@ -411,9 +405,7 @@ public void Read_should_throw_when_subject_is_disposed() var subject = CreateDisposedSubject(); var destination = new byte[1]; -#pragma warning disable CA2022 - Action action = () => subject.Read(destination, 0, 1); -#pragma warning restore CA2022 + Action action = () => _ = subject.Read(destination, 0, 1); action.ShouldThrow().And.ObjectName.Should().Be("ByteBufferStream"); } diff --git a/tests/MongoDB.Bson.Tests/Serialization/Serializers/CircularReferencesTests.cs b/tests/MongoDB.Bson.Tests/Serialization/Serializers/CircularReferencesTests.cs index 188da103f53..97c213da3aa 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/Serializers/CircularReferencesTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/Serializers/CircularReferencesTests.cs @@ -14,7 +14,7 @@ */ using System.IO; -using MongoDB.Bson; +using System.Runtime.InteropServices; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using Xunit; @@ -34,7 +34,7 @@ public class C public void TestCircularBsonArray() { // note: setting a breakpoint in this method will crash the debugger if the locals window is open - // because it tries to display the value of array (presumably it's getting an internal stack overflow) + // because it tries to display the value of an array (presumably it's getting an internal stack overflow) var array = new BsonArray(); array.Add(array); var c1 = new C { X = 1, BsonArray = array }; @@ -46,11 +46,23 @@ public void TestCircularBsonArray() [Fact] public void TestCircularDocument() { + var bsonWriterSettings = new BsonBinaryWriterSettings(); + var bsonDocumentWriterSettings = new BsonDocumentWriterSettings(); + var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; + + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + // Stack size is smaller on OS, observed on NET10 + bsonWriterSettings.MaxSerializationDepth = 50; + bsonDocumentWriterSettings.MaxSerializationDepth = 50; + jsonWriterSettings.MaxSerializationDepth = 50; + } + var c1 = new C { X = 1 }; c1.NestedDocument = c1; - Assert.Throws(() => c1.ToBson()); - Assert.Throws(() => c1.ToBsonDocument()); - Assert.Throws(() => c1.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell })); + Assert.Throws(() => c1.ToBson(writerSettings: bsonWriterSettings)); + Assert.Throws(() => c1.ToBsonDocument(bsonDocumentWriterSettings: bsonDocumentWriterSettings)); + Assert.Throws(() => c1.ToJson(writerSettings: jsonWriterSettings)); } [Fact] diff --git a/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs b/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs index 5a43649a6eb..1f32d07b4ee 100644 --- a/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs +++ b/tests/MongoDB.Driver.Tests/GridFS/GridFSSeekableDownloadStreamTests.cs @@ -139,9 +139,7 @@ public void Read_should_throw_when_buffer_is_null( { var subject = CreateSubject(); -#pragma warning disable CA2022 - Action action = () => subject.Read(null, 0, 0); -#pragma warning restore CA2022 + Action action = () => _ = subject.Read(null!, 0, 0); action.ShouldThrow().And.ParamName.Should().Be("buffer"); } @@ -173,9 +171,7 @@ public void Read_should_throw_when_count_is_invalid(int bufferLength, int offset } else { -#pragma warning disable CA2022 - action = () => subject.Read(buffer, offset, count); -#pragma warning restore CA2022 + action = () => _ = subject.Read(buffer, offset, count); } action.ShouldThrow().And.ParamName.Should().Be("count"); @@ -200,9 +196,7 @@ public void Read_should_throw_when_offset_is_invalid(int bufferLength, int offse } else { -#pragma warning disable CA2022 - action = () => subject.Read(buffer, offset, 0); -#pragma warning restore CA2022 + action = () => _ = subject.Read(buffer, offset, 0); } action.ShouldThrow().And.ParamName.Should().Be("offset");