コード例 #1
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        public void GetActiveConnectionsTest()
        {
            NativeMethods.RASCONN[] expected = new NativeMethods.RASCONN[]
            {
                new NativeMethods.RASCONN()
                {
                    deviceName = "WAN Miniport (PPTP)",
                    deviceType = NativeMethods.RASDT_Vpn,
                    entryId = Guid.NewGuid(),
                    entryName = "Test VPN Entry",
                    handle = new IntPtr(1),
                    phoneBook = "C:\\Test.pbk",
                    subEntryId = 0,
#if (WINXP || WIN2K8 || WIN7 || WIN8)
                    sessionId = Luid.Empty,
#endif
#if (WIN2K8 || WIN7 || WIN8)
                    correlationId = Guid.NewGuid()
#endif
                },
                new NativeMethods.RASCONN()
                {
                    deviceName = "Standard POTS Modem",
                    deviceType = NativeMethods.RASDT_Modem,
                    entryId = Guid.NewGuid(),
                    entryName = "Test Modem Entry",
                    handle = new IntPtr(2),
                    phoneBook = "C:\\Test.pbk",
                    subEntryId = 0,
#if (WINXP || WIN2K8 || WIN7 || WIN8)
                    sessionId = Luid.Empty,
#endif
#if (WIN2K8 || WIN7 || WIN8)
                    correlationId = Guid.NewGuid()
#endif
                }
            };

            IntPtr expectedSize = new IntPtr(Marshal.SizeOf(typeof(NativeMethods.RASCONN)) * expected.Length);

            StructBufferedPInvokeMock<StructBufferedPInvokeParams, NativeMethods.RASCONN> target = new StructBufferedPInvokeMock<StructBufferedPInvokeParams, NativeMethods.RASCONN>();
            target.Result = expected;

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

            mock.Setup(o => o.EnumConnections(It.Is<StructBufferedPInvokeParams>(i => i.BufferSize != expectedSize))).Callback((StructBufferedPInvokeParams info) =>
                {
                    target.Execute(info);
                }).Returns(NativeMethods.ERROR_BUFFER_TOO_SMALL);
            mock.Setup(o => o.EnumConnections(It.Is<StructBufferedPInvokeParams>(i => i.BufferSize == expectedSize))).Callback((StructBufferedPInvokeParams info) =>
                {
                    target.Execute(info);
                }).Returns(NativeMethods.SUCCESS);

            ReadOnlyCollection<RasConnection> actual = RasHelper.Instance.GetActiveConnections();

            Assert.AreEqual(expected.Length, actual.Count);

            for (int index = 0; index < expected.Length; index++)
            {
                NativeMethods.RASCONN objA = expected[index];
                RasConnection objB = actual[index];

                Assert.AreEqual(objA.deviceName, objB.Device.Name);
                Assert.IsTrue(string.Equals(objA.deviceType, objB.Device.DeviceType.ToString(), StringComparison.CurrentCultureIgnoreCase));
                Assert.AreEqual(objA.entryId, objB.EntryId);
                Assert.AreEqual(objA.entryName, objB.EntryName);
                Assert.AreEqual(objA.handle, objB.Handle.DangerousGetHandle());
                Assert.AreEqual(objA.phoneBook, objB.PhoneBookPath);
                Assert.AreEqual(objA.subEntryId, objB.SubEntryId);

#if (WINXP || WIN2K8 || WIN7 || WIN8)
                Assert.AreEqual(objA.sessionId, objB.SessionId);
#endif
#if (WIN2K8 || WIN7 || WIN8)
                Assert.AreEqual(objA.correlationId, objB.CorrelationId);
#endif
            }
        }
コード例 #2
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        public void GetAutoDialAddressTest()
        {
            NativeMethods.RASAUTODIALENTRY[] expected = new NativeMethods.RASAUTODIALENTRY[]
            {
                new NativeMethods.RASAUTODIALENTRY()
                {
                    dialingLocation = 0,
                    entryName = "Test Entry 1",
                    options = 0
                },
                new NativeMethods.RASAUTODIALENTRY()
                {
                    dialingLocation = 1,
                    entryName = "Test Entry 2",
                    options = 1
                }
            };

            int expectedSize = Marshal.SizeOf(typeof(NativeMethods.RASAUTODIALENTRY)) * expected.Length;

            StructBufferedPInvokeMock<RasGetAutodialAddressParams, NativeMethods.RASAUTODIALENTRY> target = new StructBufferedPInvokeMock<RasGetAutodialAddressParams, NativeMethods.RASAUTODIALENTRY>();
            target.Result = expected;

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

            mock.Setup(o => o.GetAutodialAddress(It.Is<RasGetAutodialAddressParams>(i => i.BufferSize.ToInt32() != expectedSize))).Callback((RasGetAutodialAddressParams value) =>
                {
                    target.Execute(value);
                }).Returns(NativeMethods.ERROR_BUFFER_TOO_SMALL);
            mock.Setup(o => o.GetAutodialAddress(It.Is<RasGetAutodialAddressParams>(i => i.BufferSize.ToInt32() == expectedSize))).Callback((RasGetAutodialAddressParams value) =>
                {
                    target.Execute(value);
                }).Returns(NativeMethods.SUCCESS);

            string address = "Test";
            RasAutoDialAddress actual = RasHelper.Instance.GetAutoDialAddress(address);

            Assert.AreEqual(address, actual.Address);
            Assert.IsNotNull(actual.Entries);
            Assert.AreEqual(expected.Length, actual.Entries.Count);

            for (int index = 0; index < expected.Length; index++)
            {
                Assert.AreEqual(expected[index].dialingLocation, actual.Entries[index].DialingLocation);
                Assert.AreEqual(expected[index].entryName, actual.Entries[index].EntryName);
            }
        }
コード例 #3
0
ファイル: RasHelperTest.cs プロジェクト: RSchwoerer/Terminals
        public void GetDevicesTest()
        {
            NativeMethods.RASDEVINFO[] expected = new NativeMethods.RASDEVINFO[]
            {
                new NativeMethods.RASDEVINFO()
                {
                    name = "WAN Miniport (PPTP)",
                    type = NativeMethods.RASDT_Vpn
                },
                new NativeMethods.RASDEVINFO()
                {
                    name = "WAN Miniport (L2TP)",
                    type = NativeMethods.RASDT_Vpn
                },
                new NativeMethods.RASDEVINFO() 
                {
                    name = "POTS Modem",
                    type = NativeMethods.RASDT_Modem
                }
            };

            IntPtr expectedSize = new IntPtr(Marshal.SizeOf(typeof(NativeMethods.RASDEVINFO)) * expected.Length);

            StructBufferedPInvokeMock<StructBufferedPInvokeParams, NativeMethods.RASDEVINFO> target = new StructBufferedPInvokeMock<StructBufferedPInvokeParams, NativeMethods.RASDEVINFO>();
            target.Result = expected;

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

            mock.Setup(o => o.EnumDevices(It.Is<StructBufferedPInvokeParams>(i => i.BufferSize != expectedSize))).Callback((StructBufferedPInvokeParams value) =>
                {
                    target.Execute(value);
                }).Returns(NativeMethods.ERROR_BUFFER_TOO_SMALL);
            mock.Setup(o => o.EnumDevices(It.Is<StructBufferedPInvokeParams>(i => i.BufferSize == expectedSize))).Callback((StructBufferedPInvokeParams value) =>
                {
                    target.Execute(value);
                }).Returns(NativeMethods.SUCCESS);

            ReadOnlyCollection<RasDevice> actual = RasHelper.Instance.GetDevices();

            Assert.AreEqual(expected.Length, actual.Count);

            for (int index = 0; index < expected.Length; index++)
            {
                Assert.AreEqual(expected[index].name, actual[index].Name);
                Assert.IsTrue(string.Equals(expected[index].type, actual[index].DeviceType.ToString(), StringComparison.CurrentCultureIgnoreCase));
            }
        }