public void GetNapStatusTest() { var expected = new RasNapStatus(RasIsolationState.Unknown, DateTime.MinValue); var mock = new Mock <IRasHelper>(); RasHelper.Instance = mock.Object; mock.Setup(o => o.GetNapStatus(It.IsAny <RasHandle>())).Returns(expected); var target = new RasConnection(); var actual = target.GetNapStatus(); Assert.AreSame(expected, actual); }
/// <summary> /// Retrieves the network access protection (NAP) status for a remote access connection. /// </summary> /// <param name="handle">The handle of the connection.</param> /// <returns>A <see cref="DotRas.RasNapStatus"/> object containing the NAP status.</returns> public RasNapStatus GetNapStatus(RasHandle handle) { if (Utilities.IsHandleInvalidOrClosed(handle)) { ThrowHelper.ThrowArgumentException("handle", Resources.Argument_InvalidHandle); } RasNapStatus retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASNAPSTATE)); NativeMethods.RASNAPSTATE napState = new NativeMethods.RASNAPSTATE(); napState.size = size; IntPtr pNapState = IntPtr.Zero; try { pNapState = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(napState, pNapState, true); int ret = SafeNativeMethods.Instance.GetNapStatus(handle, pNapState); if (ret == NativeMethods.SUCCESS) { napState = Utilities.PtrToStructure<NativeMethods.RASNAPSTATE>(pNapState); long fileTime = napState.probationTime.dwHighDateTime << 0x20 | napState.probationTime.dwLowDateTime; retval = new RasNapStatus( napState.isolationState, DateTime.FromFileTime(fileTime)); } else if (ret == NativeMethods.ERROR_INVALID_HANDLE) { ThrowHelper.ThrowInvalidHandleException(handle, "handle", Resources.Argument_InvalidHandle); } else if (ret == NativeMethods.ERROR_NOT_NAP_CAPABLE) { ThrowHelper.ThrowInvalidOperationException(Resources.Exception_HandleNotNapCapable); } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } finally { if (pNapState != IntPtr.Zero) { Marshal.FreeHGlobal(pNapState); } } return retval; }