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

CSHARP-4255: Fix bug and some tests. #993

Merged
merged 6 commits into from Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -46,11 +46,24 @@ public MongoEncryptionCreateCollectionException(Exception innerException, BsonDo
protected MongoEncryptionCreateCollectionException(SerializationInfo info, StreamingContext context)
: base(info, context)
{
_encryptedFields = (BsonDocument)info.GetValue("_encryptedFields", typeof(BsonDocument));
}

/// <summary>
/// The encrypted fields.
/// </summary>
public BsonDocument EncryptedFields => _encryptedFields;

// public methods
/// <summary>
/// Gets the object data.
/// </summary>
/// <param name="info">The information.</param>
/// <param name="context">The context.</param>
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue("_encryptedFields", _encryptedFields);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is nameof applicable here?

Copy link
Author

Choose a reason for hiding this comment

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

done

}
}
}
@@ -0,0 +1,47 @@
/* 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.IO;
using System.Runtime.Serialization.Formatters.Binary;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Driver.Encryption;
using Xunit;

namespace MongoDB.Driver.Tests.Encryption
{
public class MongoEncryptionCreateCollectionExceptionTests
{
[Fact]
public void Serialization_should_work()
{
var subject = new MongoEncryptionCreateCollectionException(new Exception("inner"), new BsonDocument("value", 1));

var formatter = new BinaryFormatter();
using (var stream = new MemoryStream())
{
#pragma warning disable SYSLIB0011 // BinaryFormatter serialization is obsolete
formatter.Serialize(stream, subject);
stream.Position = 0;
var rehydrated = (MongoEncryptionCreateCollectionException)formatter.Deserialize(stream);
#pragma warning restore SYSLIB0011 // BinaryFormatter serialization is obsolete

rehydrated.InnerException.Message.Should().Be(subject.InnerException.Message);
rehydrated.EncryptedFields.Should().Be(subject.EncryptedFields).And.Should().NotBeNull();
}
}
}
}