зеркало из https://github.com/SteeltoeOSS/Common.git
comments and a couple tests for WindowsNetworkFileShare #9
This commit is contained in:
Родитель
760006c46f
Коммит
806489317d
12
Common.sln
12
Common.sln
|
@ -1,7 +1,7 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27004.2010
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.28705.295
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{D9798FDE-76F4-4848-8AE0-95249C0101F0}"
|
||||
EndProject
|
||||
|
@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||
nuget.config = nuget.config
|
||||
README.md = README.md
|
||||
stylecop.json = stylecop.json
|
||||
targetframework.props = targetframework.props
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Common", "src\Steeltoe.Common\Steeltoe.Common.csproj", "{4AEA9704-3B99-4317-A959-7D6E4CDD4811}"
|
||||
|
@ -41,6 +42,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Common.Security",
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Common.Security.Test", "test\Steeltoe.Common.Security.Test\Steeltoe.Common.Security.Test.csproj", "{6E2CBAC4-3FF1-4DC9-9B9B-D0ADFE80218D}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Steeltoe.Common.Net.Test", "test\Steeltoe.Common.Net.Test\Steeltoe.Common.Net.Test.csproj", "{1DE7FECF-6A0A-4221-A3C1-165B5DB83506}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
|
@ -83,6 +86,10 @@ Global
|
|||
{6E2CBAC4-3FF1-4DC9-9B9B-D0ADFE80218D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6E2CBAC4-3FF1-4DC9-9B9B-D0ADFE80218D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6E2CBAC4-3FF1-4DC9-9B9B-D0ADFE80218D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{1DE7FECF-6A0A-4221-A3C1-165B5DB83506}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1DE7FECF-6A0A-4221-A3C1-165B5DB83506}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1DE7FECF-6A0A-4221-A3C1-165B5DB83506}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1DE7FECF-6A0A-4221-A3C1-165B5DB83506}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -97,6 +104,7 @@ Global
|
|||
{5C2D53DB-2980-4393-BCA1-B4C51004E4F7} = {D9798FDE-76F4-4848-8AE0-95249C0101F0}
|
||||
{8D322AD5-ED49-4D40-952C-B38FD97D21EF} = {D9798FDE-76F4-4848-8AE0-95249C0101F0}
|
||||
{6E2CBAC4-3FF1-4DC9-9B9B-D0ADFE80218D} = {CC77ED1F-BC03-4B9D-A07A-186C9A13042B}
|
||||
{1DE7FECF-6A0A-4221-A3C1-165B5DB83506} = {CC77ED1F-BC03-4B9D-A07A-186C9A13042B}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {4A85F9DA-2C2D-48E9-A28C-9B35C473C150}
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright 2017 the original author or authors.
|
||||
//
|
||||
// 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.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Steeltoe.Common.Net.Test")]
|
|
@ -1,4 +1,4 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Import Project="..\..\versions.props" />
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
|||
<VersionPrefix>$(SteeltoeVersion)</VersionPrefix>
|
||||
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
|
||||
<Authors>Pivotal;dtillman</Authors>
|
||||
<TargetFrameworks>netstandard2.0</TargetFrameworks>
|
||||
<TargetFrameworks>net461</TargetFrameworks>
|
||||
<AssemblyName>Steeltoe.Common.Net</AssemblyName>
|
||||
<PackageId>Steeltoe.Common.Net</PackageId>
|
||||
<PackageTags>NET Core;NET Framework</PackageTags>
|
||||
|
|
|
@ -13,15 +13,19 @@
|
|||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace Steeltoe.Common.Net
|
||||
{
|
||||
/// <summary>
|
||||
/// For interacting with SMB network file shares on Windows
|
||||
/// </summary>
|
||||
public class WindowsNetworkFileShare : IDisposable
|
||||
{
|
||||
private const int NO_ERROR = 0;
|
||||
// private const int NO_ERROR = 0;
|
||||
private const int ERROR_ACCESS_DENIED = 5;
|
||||
private const int ERROR_ALREADY_ASSIGNED = 85;
|
||||
private const int ERROR_PATH_NOT_FOUND = 53;
|
||||
|
@ -47,7 +51,7 @@ namespace Steeltoe.Common.Net
|
|||
|
||||
// Created with excel formula:
|
||||
// ="new ErrorClass("&A1&", """&PROPER(SUBSTITUTE(MID(A1,7,LEN(A1)-6), "_", " "))&"""), "
|
||||
private static ErrorClass[] error_list = new ErrorClass[]
|
||||
private static readonly ErrorClass[] Error_list = new ErrorClass[]
|
||||
{
|
||||
new ErrorClass(ERROR_ACCESS_DENIED, "Error: Access Denied"),
|
||||
new ErrorClass(ERROR_ALREADY_ASSIGNED, "Error: Already Assigned"),
|
||||
|
@ -59,6 +63,7 @@ namespace Steeltoe.Common.Net
|
|||
new ErrorClass(ERROR_INVALID_ADDRESS, "Error: Invalid Address"),
|
||||
new ErrorClass(ERROR_INVALID_PARAMETER, "Error: Invalid Parameter"),
|
||||
new ErrorClass(ERROR_INVALID_PASSWORD, "Error: Invalid Password"),
|
||||
new ErrorClass(ERROR_INVALID_PASSWORDNAME, "Error: Invalid Password Format"),
|
||||
new ErrorClass(ERROR_MORE_DATA, "Error: More Data"),
|
||||
new ErrorClass(ERROR_NO_MORE_ITEMS, "Error: No More Items"),
|
||||
new ErrorClass(ERROR_NO_NET_OR_BAD_PATH, "Error: No Net Or Bad Path"),
|
||||
|
@ -75,8 +80,18 @@ namespace Steeltoe.Common.Net
|
|||
|
||||
private readonly string _networkName;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WindowsNetworkFileShare"/> class.
|
||||
/// </summary>
|
||||
/// <param name="networkName">Address of the file share</param>
|
||||
/// <param name="credentials">Username and password for accessing the file share</param>
|
||||
public WindowsNetworkFileShare(string networkName, NetworkCredential credentials)
|
||||
{
|
||||
if (!Platform.IsWindows)
|
||||
{
|
||||
throw new PlatformNotSupportedException("WindowsNetworkFileShare only works on Windows");
|
||||
}
|
||||
|
||||
_networkName = networkName;
|
||||
|
||||
var netResource = new NetResource
|
||||
|
@ -99,11 +114,17 @@ namespace Steeltoe.Common.Net
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finalizes an instance of the <see cref="WindowsNetworkFileShare"/> class.
|
||||
/// </summary>
|
||||
~WindowsNetworkFileShare()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scope of the file share
|
||||
/// </summary>
|
||||
public enum ResourceScope : int
|
||||
{
|
||||
Connected = 1,
|
||||
|
@ -113,6 +134,9 @@ namespace Steeltoe.Common.Net
|
|||
Context
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Type of network resource
|
||||
/// </summary>
|
||||
public enum ResourceType : int
|
||||
{
|
||||
Any = 0,
|
||||
|
@ -121,6 +145,9 @@ namespace Steeltoe.Common.Net
|
|||
Reserved = 8,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The display options for the network object in a network browsing user interface
|
||||
/// </summary>
|
||||
public enum ResourceDisplaytype : int
|
||||
{
|
||||
Generic = 0x0,
|
||||
|
@ -137,6 +164,16 @@ namespace Steeltoe.Common.Net
|
|||
Ndscontainer = 0x0b
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the most recent extended error code set by a WNet function
|
||||
/// <para/>P/Invoke call to mpr.dll - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetgetlasterrora"/>
|
||||
/// </summary>
|
||||
/// <param name="error">The error code reported by the network provider.</param>
|
||||
/// <param name="errorBuf">String variable to receive the description of the error</param>
|
||||
/// <param name="errorBufSize">Size of error buffer</param>
|
||||
/// <param name="nameBuf">String variable to receive the network provider raising the error</param>
|
||||
/// <param name="nameBufSize">Size of name buffer</param>
|
||||
/// <returns>If the function succeeds, and it obtains the last error that the network provider reported, the return value is NO_ERROR.<para/>If the caller supplies an invalid buffer, the return value is ERROR_INVALID_ADDRESS.</returns>
|
||||
[DllImport("mpr.dll", CharSet = CharSet.Auto)]
|
||||
public static extern int WNetGetLastError(
|
||||
out int error,
|
||||
|
@ -145,17 +182,48 @@ namespace Steeltoe.Common.Net
|
|||
out StringBuilder nameBuf,
|
||||
int nameBufSize);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a description for an error returned by a P/Invoke call
|
||||
/// </summary>
|
||||
/// <param name="errNum">Error code</param>
|
||||
/// <returns>An error message</returns>
|
||||
internal static string GetErrorForNumber(int errNum)
|
||||
{
|
||||
if (!Error_list.Any(e => e.Num == errNum))
|
||||
{
|
||||
return "Error: Unknown, " + errNum;
|
||||
}
|
||||
else
|
||||
{
|
||||
return Error_list.First(e => e.Num == errNum).Message;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the object, cancels connection with file share
|
||||
/// </summary>
|
||||
/// <param name="disposing">Not used</param>
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
WNetCancelConnection2(_networkName, 0, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes a connection to a network resource and can redirect a local device to the network resource.
|
||||
/// <para/>P/Invoke call to mpr.dll - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetaddconnection2a"/>
|
||||
/// </summary>
|
||||
/// <param name="netResource">Network resource to interact with</param>
|
||||
/// <param name="password">Password for making the network connection</param>
|
||||
/// <param name="username">Username for making the network connection</param>
|
||||
/// <param name="flags">A set of connection options - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetaddconnection2a#parameters"/></param>
|
||||
/// <returns>An integer representing the result - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetaddconnection2a#return-value"/></returns>
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetAddConnection2(
|
||||
NetResource netResource,
|
||||
|
@ -163,12 +231,36 @@ namespace Steeltoe.Common.Net
|
|||
string username,
|
||||
int flags);
|
||||
|
||||
/// <summary>
|
||||
/// Cancels an existing network connection, removes remembered network connections that are not currently connected.
|
||||
/// <para/>P/Invoke call to mpr.dll - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetcancelconnection2a"/>
|
||||
/// </summary>
|
||||
/// <param name="name">
|
||||
/// Pointer to a constant null-terminated string that specifies the name of either the redirected local device or the remote network resource to disconnect from.<para/>
|
||||
/// If this parameter specifies a redirected local device, the function cancels only the specified device redirection. If the parameter specifies a remote network resource, all connections without devices are canceled.
|
||||
/// </param>
|
||||
/// <param name="flags">Connection type - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetcancelconnection2a#parameters"/></param>
|
||||
/// <param name="force">Specifies whether the disconnection should occur if there are open files or jobs on the connection. If this parameter is FALSE, the function fails if there are open files or jobs.</param>
|
||||
/// <returns>An integer representing the result - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetcancelconnection2a#return-value"/></returns>
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetCancelConnection2(
|
||||
string name,
|
||||
int flags,
|
||||
bool force);
|
||||
|
||||
/// <summary>
|
||||
/// Makes a connection to a network resource. Can redirect a local device to a network resource.
|
||||
/// <para/>P/Invoke call to mpr.dll - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetuseconnectiona"/>
|
||||
/// </summary>
|
||||
/// <param name="hwndOwner">Handle to a window that the provider of network resources can use as an owner window for dialog boxes</param>
|
||||
/// <param name="netResource">Network resource to interact with</param>
|
||||
/// <param name="password">A null-terminated string that specifies a password to be used in making the network connection</param>
|
||||
/// <param name="username">A null-terminated string that specifies a user name for making the connection</param>
|
||||
/// <param name="flags">Set of bit flags describing the connection</param>
|
||||
/// <param name="lpAccessName">Pointer to a buffer that receives system requests on the connection</param>
|
||||
/// <param name="lpBufferSize">Pointer to a variable that specifies the size of the lpAccessName buffer, in characters.<para />If the call fails because the buffer is not large enough, the function returns the required buffer size in this location</param>
|
||||
/// <param name="lpResult">Pointer to a variable that receives additional information about the connection</param>
|
||||
/// <returns>An integer representing the result - <seealso href="https://docs.microsoft.com/en-us/windows/desktop/api/winnetwk/nf-winnetwk-wnetuseconnectiona#return-value"/></returns>
|
||||
[DllImport("mpr.dll")]
|
||||
private static extern int WNetUseConnection(
|
||||
IntPtr hwndOwner,
|
||||
|
@ -180,28 +272,6 @@ namespace Steeltoe.Common.Net
|
|||
string lpBufferSize,
|
||||
string lpResult);
|
||||
|
||||
private static string GetErrorForNumber(int errNum)
|
||||
{
|
||||
foreach (ErrorClass er in error_list)
|
||||
{
|
||||
if (er.Num == errNum)
|
||||
{
|
||||
return er.Message;
|
||||
}
|
||||
}
|
||||
|
||||
return "Error: Unknown, " + errNum;
|
||||
}
|
||||
|
||||
private static string GetLastError(int result)
|
||||
{
|
||||
StringBuilder sbErrorBuf = new StringBuilder(500);
|
||||
StringBuilder sbNameBuf = new StringBuilder(500);
|
||||
int resultref = result;
|
||||
int res = WNetGetLastError(out resultref, out sbErrorBuf, sbErrorBuf.Capacity, out sbNameBuf, sbNameBuf.Capacity);
|
||||
return sbErrorBuf.ToString();
|
||||
}
|
||||
|
||||
private struct ErrorClass
|
||||
{
|
||||
public int Num;
|
||||
|
@ -209,11 +279,15 @@ namespace Steeltoe.Common.Net
|
|||
|
||||
public ErrorClass(int num, string message)
|
||||
{
|
||||
this.Num = num;
|
||||
this.Message = message;
|
||||
Num = num;
|
||||
Message = message;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The NETRESOURCE structure contains information about a network resource.
|
||||
/// More info on NetResource: <seealso href="https://msdn.microsoft.com/en-us/c53d078e-188a-4371-bdb9-fc023bc0c1ba"/>
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public class NetResource
|
||||
{
|
||||
|
|
|
@ -27,6 +27,8 @@ namespace Steeltoe.Common
|
|||
|
||||
public static bool IsNetCore => RuntimeInformation.FrameworkDescription.StartsWith(NET_CORE);
|
||||
|
||||
public static bool IsWindows => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
|
||||
|
||||
public static bool IsCloudFoundry => Environment.GetEnvironmentVariable(VCAP_APPLICATION) != null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Import Project="..\..\versions.props" />
|
||||
<Import Project="..\..\targetframework.props" />
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net461</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup>
|
||||
<NoWarn>SA1101;SA1124;SA1201;SA1309;SA1310;SA1401;SA1600;SA1652;1591</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Steeltoe.Common.Net\Steeltoe.Common.Net.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="xunit.runner.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
|
||||
<PackageReference Include="xunit" Version="$(XunitVersion)" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitStudioVersion)" />
|
||||
<PackageReference Include="StyleCop.Analyzers" Version="$(StyleCopVersion)">
|
||||
<PrivateAssets>All</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AdditionalFiles Include="..\..\stylecop.json">
|
||||
<Link>stylecop.json</Link>
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</AdditionalFiles>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,34 @@
|
|||
// Copyright 2017 the original author or authors.
|
||||
//
|
||||
// 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 Xunit;
|
||||
|
||||
namespace Steeltoe.Common.Net.Test
|
||||
{
|
||||
public class WindowsNetworkFileShareTest
|
||||
{
|
||||
[Fact]
|
||||
public void GetErrorForKnownNumber_ReturnsKnownError()
|
||||
{
|
||||
Assert.Equal("Error: Access Denied", WindowsNetworkFileShare.GetErrorForNumber(5));
|
||||
Assert.Equal("Error: No Network", WindowsNetworkFileShare.GetErrorForNumber(1222));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetErrorForUnknownNumber_ReturnsUnKnownError()
|
||||
{
|
||||
Assert.Equal("Error: Unknown, 9999", WindowsNetworkFileShare.GetErrorForNumber(9999));
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче