public void AllocateLocallyUniqueIdTest()
        {
            Luid expected = new Luid(100, 100);

            CopyStructToAddrMock<Luid> target = new CopyStructToAddrMock<Luid>();
            target.Result = expected;

            Mock<ISafeNativeMethods> mock = new Mock<ISafeNativeMethods>();
            SafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.AllocateLocallyUniqueIdImpl(It.IsAny<IntPtr>())).Callback((IntPtr pLuid) =>
            {
                target.Execute(pLuid);
            }).Returns(NativeMethods.SUCCESS);

            Luid actual = RasHelper.Instance.AllocateLocallyUniqueId();

            Assert.AreEqual(expected, actual);
        }
        public void GetConnectionStatusErrorCodeTest()
        {
            NativeMethods.RASCONNSTATUS expected = new NativeMethods.RASCONNSTATUS()
            {
                connectionState = RasConnectionState.Authenticate,
                deviceName = "WAN Miniport (PPTP)",
                deviceType = NativeMethods.RASDT_Vpn,
                errorCode = NativeMethods.ERROR_PROTOCOL_NOT_CONFIGURED,
                phoneNumber = IPAddress.Loopback.ToString()
            };

            CopyStructToAddrMock<NativeMethods.RASCONNSTATUS> target = new CopyStructToAddrMock<NativeMethods.RASCONNSTATUS>();
            target.Result = expected;

            Mock<ISafeNativeMethods> mock = new Mock<ISafeNativeMethods>();
            SafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetConnectStatus(It.IsAny<RasHandle>(), It.IsAny<IntPtr>())).Callback((RasHandle handle, IntPtr status) =>
                {
                    target.Execute(status);
                }).Returns(NativeMethods.SUCCESS);
            mock.Setup(o => o.GetErrorString(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<int>())).Callback((int errorCode, string result, int bufferSize) =>
                {
                    new SafeNativeMethods().GetErrorString(errorCode, result, bufferSize);
                }).Returns(NativeMethods.SUCCESS);

            RasConnectionStatus actual = RasHelper.Instance.GetConnectionStatus(new RasHandle(new IntPtr(1), false));

            Assert.AreEqual(expected.connectionState, actual.ConnectionState);
            Assert.AreEqual(expected.deviceName, actual.Device.Name);
            Assert.IsTrue(string.Equals(expected.deviceType, actual.Device.DeviceType.ToString(), StringComparison.CurrentCultureIgnoreCase));
            Assert.AreEqual(expected.errorCode, actual.ErrorCode);
            Assert.AreEqual(expected.phoneNumber, actual.PhoneNumber);
            Assert.IsTrue(!string.IsNullOrEmpty(actual.ErrorMessage));            
        }
        public void GetConnectionStatusTest()
        {
            NativeMethods.RASCONNSTATUS expected = new NativeMethods.RASCONNSTATUS()
            {
                connectionState = RasConnectionState.AllDevicesConnected,
                deviceName = "WAN Miniport (PPTP)",
                deviceType = NativeMethods.RASDT_Vpn,
                errorCode = NativeMethods.SUCCESS,
                phoneNumber = IPAddress.Loopback.ToString(),
#if (WIN7 || WIN8)
                localEndPoint = new NativeMethods.RASTUNNELENDPOINT() { addr = new byte[] { 192, 168, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4 },
                remoteEndPoint = new NativeMethods.RASTUNNELENDPOINT() { addr = new byte[] { 192, 168, 2, 254, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, type = NativeMethods.RASTUNNELENDPOINTTYPE.IPv4 },
                connectionSubState = RasConnectionSubState.None
#endif
            };

            CopyStructToAddrMock<NativeMethods.RASCONNSTATUS> target = new CopyStructToAddrMock<NativeMethods.RASCONNSTATUS>();
            target.Result = expected;

            Mock<ISafeNativeMethods> mock = new Mock<ISafeNativeMethods>();
            SafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetConnectStatus(It.IsAny<RasHandle>(), It.IsAny<IntPtr>())).Callback((RasHandle handle, IntPtr connectionStatus) =>
                {
                    target.Execute(connectionStatus);
                }).Returns(NativeMethods.SUCCESS);

            RasConnectionStatus actual = RasHelper.Instance.GetConnectionStatus(new RasHandle(new IntPtr(1), false));

            Assert.AreEqual(expected.connectionState, actual.ConnectionState);
            Assert.AreEqual(expected.deviceName, actual.Device.Name);
            Assert.IsTrue(string.Equals(expected.deviceType, actual.Device.DeviceType.ToString(), StringComparison.CurrentCultureIgnoreCase));
            Assert.AreEqual(expected.errorCode, actual.ErrorCode);
            Assert.AreEqual(null, actual.ErrorMessage);
            Assert.AreEqual(expected.phoneNumber, actual.PhoneNumber);
#if (WIN7 || WIN8)
            TestUtilities.AssertEndPoint(expected.localEndPoint, actual.LocalEndPoint);
            TestUtilities.AssertEndPoint(expected.remoteEndPoint, actual.RemoteEndPoint);
            Assert.IsTrue(actual.LocalEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
            Assert.IsTrue(actual.RemoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
#endif
        }
        public void GetCredentialsTest()
        {
            NativeMethods.RASCREDENTIALS expected = new NativeMethods.RASCREDENTIALS()
            {
                userName = "******",
                password = "******",
                domain = "Domain",
                options = NativeMethods.RASCM.UserName | NativeMethods.RASCM.Password | NativeMethods.RASCM.Domain
            };

            CopyStructToAddrMock<NativeMethods.RASCREDENTIALS> target = new CopyStructToAddrMock<NativeMethods.RASCREDENTIALS>();
            target.Result = expected;

            Mock<IUnsafeNativeMethods> mock = new Mock<IUnsafeNativeMethods>();
            UnsafeNativeMethods.Instance = mock.Object;

            mock.Setup(o => o.GetCredentials(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<IntPtr>())).Callback((string phoneBookPath, string entryName, IntPtr credentials) =>
                {
                    target.Execute(credentials);
                }).Returns(NativeMethods.SUCCESS);

            NetworkCredential actual = RasHelper.Instance.GetCredentials("C:\\Test.pbk", "Test Entry", NativeMethods.RASCM.UserName | NativeMethods.RASCM.Password | NativeMethods.RASCM.Domain);

            Assert.AreEqual(expected.userName, actual.UserName);
            Assert.AreEqual(expected.password, actual.Password);
            Assert.AreEqual(expected.domain, actual.Domain);
        }