Skip to content
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
33 changes: 33 additions & 0 deletions evergreen/evergreen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1111,20 +1111,31 @@ tasks:
- func: configure-framework
vars:
FRAMEWORK: net472
DOTNET_SDK_VERSION: 10.0
- func: run-unit-tests

- name: unit-tests-netstandard21
commands:
- func: configure-framework
vars:
FRAMEWORK: netstandard2.1
DOTNET_SDK_VERSION: 10.0
- func: run-unit-tests

- name: unit-tests-net60
commands:
- func: configure-framework
vars:
FRAMEWORK: net6.0
DOTNET_SDK_VERSION: 10.0
- func: run-unit-tests

- name: unit-tests-net100
commands:
- func: configure-framework
vars:
FRAMEWORK: net10.0
DOTNET_SDK_VERSION: 10.0
- func: run-unit-tests

- name: test-net472
Expand Down Expand Up @@ -1654,6 +1665,7 @@ tasks:
- func: configure-framework
vars:
FRAMEWORK: net472
DOTNET_SDK_VERSION: 10.0
- func: bootstrap-mongo-orchestration
- func: run-smoke-tests

Expand All @@ -1665,6 +1677,7 @@ tasks:
- func: configure-framework
vars:
FRAMEWORK: netcoreapp3.1
DOTNET_SDK_VERSION: 10.0
- func: bootstrap-mongo-orchestration
- func: run-smoke-tests

Expand All @@ -1676,6 +1689,7 @@ tasks:
- func: configure-framework
vars:
FRAMEWORK: net5.0
DOTNET_SDK_VERSION: 10.0
- func: bootstrap-mongo-orchestration
- func: run-smoke-tests

Expand All @@ -1687,6 +1701,7 @@ tasks:
- func: configure-framework
vars:
FRAMEWORK: net6.0
DOTNET_SDK_VERSION: 10.0
- func: bootstrap-mongo-orchestration
- func: run-smoke-tests

Expand All @@ -1698,6 +1713,19 @@ tasks:
- func: configure-framework
vars:
FRAMEWORK: net8.0
DOTNET_SDK_VERSION: 10.0
- 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
DOTNET_SDK_VERSION: 10.0
- func: bootstrap-mongo-orchestration
- func: run-smoke-tests

Expand Down Expand Up @@ -2380,6 +2408,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
Expand All @@ -2389,6 +2418,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
Expand All @@ -2398,6 +2428,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
Expand All @@ -2406,6 +2437,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" }
Expand Down Expand Up @@ -2778,6 +2810,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:
Expand Down
2 changes: 2 additions & 0 deletions evergreen/install-dotnet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions evergreen/run-smoke-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions evergreen/run-unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
36 changes: 28 additions & 8 deletions src/MongoDB.Bson/BsonExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,41 @@ public static BsonDocument ToBsonDocument<TNominalType>(
/// <param name="configurator">The serialization context configurator.</param>
/// <param name="args">The serialization args.</param>
/// <returns>A BsonDocument.</returns>
/// <exception cref="System.ArgumentNullException">nominalType</exception>
/// <exception cref="System.ArgumentException">serializer</exception>
public static BsonDocument ToBsonDocument(
this object obj,
Type nominalType,
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> configurator = null,
BsonSerializationArgs args = default(BsonSerializationArgs)) =>
ToBsonDocument(obj, nominalType, BsonDocumentWriterSettings.Defaults, serializer, configurator, args);

internal static BsonDocument ToBsonDocument<TNominalType>(
this TNominalType obj,
BsonDocumentWriterSettings bsonDocumentWriterSettings,
IBsonSerializer serializer = null,
Action<BsonSerializationContext.Builder> 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<BsonSerializationContext.Builder> 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;
Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions tests/BuildProps/Tests.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(IsWindows)'=='true'">$(TargetFrameworks);net472</TargetFrameworks>
<TargetFrameworks Condition="'$(ADD_NET10_TFM)'=='1'">$(TargetFrameworks);net10.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\MongoDB.Driver.snk</AssemblyOriginatorKeyFile>
Expand Down
2 changes: 1 addition & 1 deletion tests/MongoDB.Bson.Tests/IO/BsonStreamAdapterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ public void Read_should_throw_when_subject_is_disposed()
var count = 2;
subject.Dispose();

Action action = () => subject.Read(buffer, offset, count);
Action action = () => _ = subject.Read(buffer, offset, count);

action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("BsonStreamAdapter");
}
Expand Down
8 changes: 4 additions & 4 deletions tests/MongoDB.Bson.Tests/IO/ByteBufferStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void Read_should_throw_when_buffer_is_null()
{
var subject = CreateSubject();

Action action = () => subject.Read(null, 0, 0);
Action action = () => _ = subject.Read(null!, 0, 0);

action.ShouldThrow<ArgumentNullException>().And.ParamName.Should().Be("buffer");
}
Expand All @@ -379,7 +379,7 @@ public void Read_should_throw_when_count_is_out_of_range(int destinationSize, in
var subject = CreateSubject();
var destination = new byte[destinationSize];

Action action = () => subject.Read(destination, offset, count);
Action action = () => _ = subject.Read(destination, offset, count);

action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("count");
}
Expand All @@ -394,7 +394,7 @@ public void Read_should_throw_when_offset_is_out_of_range(int destinationSize, i
var subject = CreateSubject();
var destination = new byte[destinationSize];

Action action = () => subject.Read(destination, offset, 0);
Action action = () => _ = subject.Read(destination, offset, 0);

action.ShouldThrow<ArgumentOutOfRangeException>().And.ParamName.Should().Be("offset");
}
Expand All @@ -405,7 +405,7 @@ public void Read_should_throw_when_subject_is_disposed()
var subject = CreateDisposedSubject();
var destination = new byte[1];

Action action = () => subject.Read(destination, 0, 1);
Action action = () => _ = subject.Read(destination, 0, 1);

action.ShouldThrow<ObjectDisposedException>().And.ObjectName.Should().Be("ByteBufferStream");
}
Expand Down
2 changes: 2 additions & 0 deletions tests/MongoDB.Bson.Tests/Serialization/BsonClassMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,9 @@ private BsonClassMap WithKnownTypes(BsonClassMap classMap, List<Type> value)

private BsonClassMap Clone(BsonClassMap classMap)
{
#pragma warning disable SYSLIB0050
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a ticket to look into the warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks: CSHARP-5794

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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

using System.IO;
using MongoDB.Bson;
using System.Runtime.InteropServices;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using Xunit;
Expand All @@ -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 };
Expand All @@ -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<BsonSerializationException>(() => c1.ToBson());
Assert.Throws<BsonSerializationException>(() => c1.ToBsonDocument());
Assert.Throws<BsonSerializationException>(() => c1.ToJson(writerSettings: new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }));
Assert.Throws<BsonSerializationException>(() => c1.ToBson(writerSettings: bsonWriterSettings));
Assert.Throws<BsonSerializationException>(() => c1.ToBsonDocument(bsonDocumentWriterSettings: bsonDocumentWriterSettings));
Assert.Throws<BsonSerializationException>(() => c1.ToJson(writerSettings: jsonWriterSettings));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1' OR '$(TargetFramework)' == 'net6.0'">
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1' OR '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net10.0'">
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.2.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
35 changes: 35 additions & 0 deletions tests/MongoDB.Driver.TestHelpers/X509CertificateLoader.cs
Original file line number Diff line number Diff line change
@@ -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<byte> certificateData) =>
new(certificateData.ToArray());

public static X509Certificate2 LoadCertificateFromFile(string certificateFilename) =>
new (certificateFilename);

public static X509Certificate2 LoadPkcs12FromFile(string certificateFilename, string password) =>
new(certificateFilename, password);
}

#endif
Loading