Skip to content

Commit 90ef04b

Browse files
committed
CSHARP-1563: Build and test MongoDB.Driver against .NET Core.
1 parent 5db562c commit 90ef04b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+5593
-68
lines changed

CSharpDriver.NetCore.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MongoDB.Driver.Core.Tests.N
2626
EndProject
2727
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MongoDB.Driver.NetCore", "src\MongoDB.Driver.NetCore\MongoDB.Driver.NetCore.xproj", "{98B177EC-39FA-4854-BB6A-55CB837767EE}"
2828
EndProject
29+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MongoDB.Driver.TestHelpers.NetCore", "tests\MongoDB.Driver.TestHelpers.NetCore\MongoDB.Driver.TestHelpers.NetCore.xproj", "{4D0C8048-E009-4CE5-9C7F-40A6E5CD8C7F}"
30+
EndProject
31+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "MongoDB.Driver.Tests.NetCore", "tests\MongoDB.Driver.Tests.NetCore\MongoDB.Driver.Tests.NetCore.xproj", "{0478B209-1898-4AD3-A09D-4275E6B20974}"
32+
EndProject
2933
Global
3034
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3135
Debug|Any CPU = Debug|Any CPU
@@ -60,6 +64,14 @@ Global
6064
{98B177EC-39FA-4854-BB6A-55CB837767EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
6165
{98B177EC-39FA-4854-BB6A-55CB837767EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
6266
{98B177EC-39FA-4854-BB6A-55CB837767EE}.Release|Any CPU.Build.0 = Release|Any CPU
67+
{4D0C8048-E009-4CE5-9C7F-40A6E5CD8C7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
68+
{4D0C8048-E009-4CE5-9C7F-40A6E5CD8C7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
69+
{4D0C8048-E009-4CE5-9C7F-40A6E5CD8C7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
70+
{4D0C8048-E009-4CE5-9C7F-40A6E5CD8C7F}.Release|Any CPU.Build.0 = Release|Any CPU
71+
{0478B209-1898-4AD3-A09D-4275E6B20974}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
72+
{0478B209-1898-4AD3-A09D-4275E6B20974}.Debug|Any CPU.Build.0 = Debug|Any CPU
73+
{0478B209-1898-4AD3-A09D-4275E6B20974}.Release|Any CPU.ActiveCfg = Release|Any CPU
74+
{0478B209-1898-4AD3-A09D-4275E6B20974}.Release|Any CPU.Build.0 = Release|Any CPU
6375
EndGlobalSection
6476
GlobalSection(SolutionProperties) = preSolution
6577
HideSolutionNode = FALSE
@@ -72,5 +84,7 @@ Global
7284
{E345CE1D-7876-49BF-BBD3-CD78707B715B} = {6D163A80-981C-491A-B519-465342D76568}
7385
{264D9363-60CF-4B7A-BCCB-6FE8D883B36B} = {6D163A80-981C-491A-B519-465342D76568}
7486
{98B177EC-39FA-4854-BB6A-55CB837767EE} = {CB619CDE-385F-4D7D-8601-A6482943975C}
87+
{4D0C8048-E009-4CE5-9C7F-40A6E5CD8C7F} = {6D163A80-981C-491A-B519-465342D76568}
88+
{0478B209-1898-4AD3-A09D-4275E6B20974} = {6D163A80-981C-491A-B519-465342D76568}
7589
EndGlobalSection
7690
EndGlobal

src/MongoDB.Driver/Linq/MethodHelper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ public static MethodInfo GetMethodDefinition(MethodInfo methodInfo)
8989
}
9090

9191
var declaringTypeDefinition = methodInfo.DeclaringType.GetGenericTypeDefinition();
92-
#if !NETCORE
93-
return (MethodInfo)MethodBase.GetMethodFromHandle(methodInfo.MethodHandle, declaringTypeDefinition.TypeHandle);
94-
#else
92+
#if NETCORE
9593
var bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public;
9694
var parameterTypes = methodInfo.GetParameters().Select(p => p.ParameterType).ToArray();
9795
return declaringTypeDefinition.GetTypeInfo().GetMethods(bindingFlags)
98-
.Where(m => m.Name == methodInfo.Name && m.GetParameters().Select(p => p.ParameterType).SequenceEqual(parameterTypes))
96+
.Where(m => m.Name == methodInfo.Name && m.GetParameters().Count() == parameterTypes.Length) // TODO: need better matching
9997
.Single();
98+
#else
99+
return (MethodInfo)MethodBase.GetMethodFromHandle(methodInfo.MethodHandle, declaringTypeDefinition.TypeHandle);
100100
#endif
101101
}
102102
}

src/MongoDB.Driver/MongoCredential.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ public string Password
9797
var passwordEvidence = _evidence as PasswordEvidence;
9898
if (passwordEvidence != null)
9999
{
100-
#if !NETCORE
101-
return MongoUtils.ToInsecureString(passwordEvidence.SecurePassword);
102-
#else
100+
#if NETCORE
103101
return passwordEvidence.Password;
102+
#else
103+
return MongoUtils.ToInsecureString(passwordEvidence.SecurePassword);
104104
#endif
105105
}
106106

@@ -391,10 +391,10 @@ internal IAuthenticator ToAuthenticator()
391391
var passwordEvidence = _evidence as PasswordEvidence;
392392
if (passwordEvidence != null)
393393
{
394-
#if !NETCORE
395-
var insecurePassword = MongoUtils.ToInsecureString(passwordEvidence.SecurePassword);
396-
#else
394+
#if NETCORE
397395
var insecurePassword = passwordEvidence.Password;
396+
#else
397+
var insecurePassword = MongoUtils.ToInsecureString(passwordEvidence.SecurePassword);
398398
#endif
399399
var credential = new UsernamePasswordCredential(
400400
_identity.Source,

src/MongoDB.Driver/PasswordEvidence.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,24 @@ namespace MongoDB.Driver
3131
public sealed class PasswordEvidence : MongoIdentityEvidence
3232
{
3333
// private fields
34-
#if !NETCORE
34+
#if NETCORE
35+
private readonly string _password;
36+
#else
3537
private readonly SecureString _securePassword;
3638
private readonly string _digest; // used to implement Equals without referring to the SecureString
37-
#else
38-
private readonly string _password;
3939
#endif
4040

4141
// constructors
42-
#if !NETCORE
42+
#if NETCORE
43+
/// <summary>
44+
/// Initializes a new instance of the <see cref="PasswordEvidence" /> class.
45+
/// </summary>
46+
/// <param name="password">The password.</param>
47+
public PasswordEvidence(string password)
48+
{
49+
_password = password;
50+
}
51+
#else
4352
/// <summary>
4453
/// Initializes a new instance of the <see cref="PasswordEvidence" /> class.
4554
/// </summary>
@@ -58,33 +67,24 @@ public PasswordEvidence(SecureString password)
5867
public PasswordEvidence(string password)
5968
: this(CreateSecureString(password))
6069
{ }
61-
#else
62-
/// <summary>
63-
/// Initializes a new instance of the <see cref="PasswordEvidence" /> class.
64-
/// </summary>
65-
/// <param name="password">The password.</param>
66-
public PasswordEvidence(string password)
67-
{
68-
_password = password;
69-
}
7070
#endif
7171

7272
// public properties
73-
#if !NETCORE
73+
#if NETCORE
7474
/// <summary>
7575
/// Gets the password.
7676
/// </summary>
77-
public SecureString SecurePassword
77+
public string Password
7878
{
79-
get { return _securePassword; }
79+
get { return _password; }
8080
}
8181
#else
8282
/// <summary>
8383
/// Gets the password.
8484
/// </summary>
85-
public string Password
85+
public SecureString SecurePassword
8686
{
87-
get { return _password; }
87+
get { return _securePassword; }
8888
}
8989
#endif
9090

@@ -100,10 +100,10 @@ public override bool Equals(object rhs)
100100
{
101101
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
102102

103-
#if !NETCORE
104-
return _digest == ((PasswordEvidence)rhs)._digest;
105-
#else
103+
#if NETCORE
106104
return _password == ((PasswordEvidence)rhs)._password;
105+
#else
106+
return _digest == ((PasswordEvidence)rhs)._digest;
107107
#endif
108108
}
109109

@@ -115,10 +115,10 @@ public override bool Equals(object rhs)
115115
/// </returns>
116116
public override int GetHashCode()
117117
{
118-
#if !NETCORE
119-
return _digest.GetHashCode();
120-
#else
118+
#if NETCORE
121119
return _password.GetHashCode();
120+
#else
121+
return _digest.GetHashCode();
122122
#endif
123123
}
124124

@@ -134,10 +134,10 @@ internal string ComputeMongoCRPasswordDigest(string username)
134134
{
135135
var encoding = Utf8Encodings.Strict;
136136
var prefixBytes = encoding.GetBytes(username + ":mongo:");
137-
#if !NETCORE
138-
var hash = ComputeHash(md5, prefixBytes, _securePassword);
139-
#else
137+
#if NETCORE
140138
var hash = ComputeHash(md5, prefixBytes, _password);
139+
#else
140+
var hash = ComputeHash(md5, prefixBytes, _securePassword);
141141
#endif
142142
return BsonUtils.ToHexString(hash);
143143
}
@@ -176,7 +176,18 @@ private static string GenerateDigest(SecureString secureString)
176176
}
177177
#endif
178178

179-
#if !NETCORE
179+
#if NETCORE
180+
private static byte[] ComputeHash(HashAlgorithm algorithm, byte[] prefixBytes, string password)
181+
{
182+
var encoding = Utf8Encodings.Strict;
183+
var passwordBytes = new byte[password.Length * 3]; // worst case for UTF16 to UTF8 encoding
184+
var passwordUtf8Length = encoding.GetBytes(password, 0, password.Length, passwordBytes, 0);
185+
var buffer = new byte[prefixBytes.Length + passwordUtf8Length];
186+
Buffer.BlockCopy(prefixBytes, 0, buffer, 0, prefixBytes.Length);
187+
Buffer.BlockCopy(passwordBytes, 0, buffer, prefixBytes.Length, passwordUtf8Length);
188+
return algorithm.ComputeHash(buffer);
189+
}
190+
#else
180191
private static byte[] ComputeHash(HashAlgorithm algorithm, byte[] prefixBytes, SecureString secureString)
181192
{
182193
var bstr = Marshal.SecureStringToBSTR(secureString);
@@ -217,16 +228,6 @@ private static byte[] ComputeHash(HashAlgorithm algorithm, byte[] prefixBytes, S
217228
{
218229
Marshal.ZeroFreeBSTR(bstr);
219230
}
220-
#else
221-
private static byte[] ComputeHash(HashAlgorithm algorithm, byte[] prefixBytes, string password)
222-
{
223-
var encoding = Utf8Encodings.Strict;
224-
var passwordBytes = new byte[password.Length * 3]; // worst case for UTF16 to UTF8 encoding
225-
var passwordUtf8Length = encoding.GetBytes(password, 0, password.Length, passwordBytes, 0);
226-
var buffer = new byte[prefixBytes.Length + passwordUtf8Length];
227-
Buffer.BlockCopy(prefixBytes, 0, buffer, 0, prefixBytes.Length);
228-
Buffer.BlockCopy(passwordBytes, 0, buffer, prefixBytes.Length, passwordUtf8Length);
229-
return algorithm.ComputeHash(buffer);
230231
}
231232
#endif
232233
}

src/MongoDB.Driver/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2010-2015 MongoDB Inc.
1+
/* Copyright 2010-2016 MongoDB Inc.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -34,4 +34,5 @@
3434
[assembly: InternalsVisibleTo("MongoDB.Driver.Legacy.TestHelpers")]
3535
[assembly: InternalsVisibleTo("MongoDB.Driver.Legacy.Tests")]
3636
[assembly: InternalsVisibleTo("MongoDB.Driver.Tests")]
37+
[assembly: InternalsVisibleTo("MongoDB.Driver.Tests.NetCore")]
3738
[assembly: InternalsVisibleTo("MongoDB.Driver.TestHelpers")]

src/MongoDB.Driver/SslSettings.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,18 @@ private X509Certificate CloneCertificate(X509Certificate certificate)
261261
var certificate2 = certificate as X509Certificate2;
262262
if (certificate2 != null)
263263
{
264-
#if !NETCORE
265-
return new X509Certificate2(certificate2);
266-
#else
264+
#if NETCORE
267265
return new X509Certificate2(certificate2.RawData);
266+
#else
267+
return new X509Certificate2(certificate2);
268268
#endif
269269
}
270270
else
271271
{
272-
#if !NETCORE
273-
return new X509Certificate(certificate);
274-
#else
272+
#if NETCORE
275273
return new X509Certificate(certificate.Export(X509ContentType.Cert));
274+
#else
275+
return new X509Certificate(certificate);
276276
#endif
277277
}
278278
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>4d0c8048-e009-4ce5-9c7f-40a6e5cd8c7f</ProjectGuid>
11+
<RootNamespace>MongoDB.Driver.TestHelpers.NetCore</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"version": "1.0.0-*",
3+
4+
"dependencies": {
5+
"MongoDB.Bson.NetCore": "1.0.0-*",
6+
"MongoDB.Driver.Core.NetCore": "1.0.0-*",
7+
"MongoDB.Driver.Core.TestHelpers.NetCore": "1.0.0-*",
8+
"MongoDB.Driver.NetCore": "1.0.0-*",
9+
"NETStandard.Library": "1.6.0"
10+
},
11+
12+
"frameworks": {
13+
"netstandard1.6": {
14+
"imports": "dnxcore50",
15+
"buildOptions": {
16+
"compile": {
17+
"include": [ "../MongoDB.Driver.TestHelpers/**/*.cs" ]
18+
},
19+
"define": [ "DEBUG", "TRACE", "NETCORE" ]
20+
}
21+
}
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>0478b209-1898-4ad3-a09d-4275e6b20974</ProjectGuid>
10+
<RootNamespace>MongoDB.Driver.Tests.NetCore</RootNamespace>
11+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
12+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
13+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
14+
</PropertyGroup>
15+
<PropertyGroup>
16+
<SchemaVersion>2.0</SchemaVersion>
17+
</PropertyGroup>
18+
<ItemGroup>
19+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
20+
</ItemGroup>
21+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
22+
</Project>

0 commit comments

Comments
 (0)