public void ReturnsNoConnectionAsExpected() { var api = new Mock <IRasApi32>(); api.Setup(o => o.RasEnumConnections(It.IsAny <RASCONN[]>(), ref It.Ref <int> .IsAny, ref It.Ref <int> .IsAny)).Returns(new RasEnumConnectionsCallback( (RASCONN[] o1, ref int o2, ref int o3) => { o2 = 0; o3 = 0; return(SUCCESS); })); var deviceTypeFactory = new Mock <IDeviceTypeFactory>(); var exceptionPolicy = new Mock <IExceptionPolicy>(); var structFactory = new Mock <IStructArrayFactory>(); structFactory.Setup(o => o.CreateArray <RASCONN>(1, out It.Ref <int> .IsAny)).Returns(new RASCONN[1]); var serviceLocator = new Mock <IServiceProvider>(); var target = new RasEnumConnectionsService(api.Object, deviceTypeFactory.Object, exceptionPolicy.Object, structFactory.Object, serviceLocator.Object); var result = target.EnumerateConnections().ToArray(); CollectionAssert.IsEmpty(result); }
public void ReturnsOneConnectionAsExpected() { var entryName = "Test"; var phoneBookPath = @"C:\Test.pbk"; var deviceName = "WAN"; var api = new Mock <IRasApi32>(); api.Setup(o => o.RasEnumConnections(It.IsAny <RASCONN[]>(), ref It.Ref <int> .IsAny, ref It.Ref <int> .IsAny)).Returns(new RasEnumConnectionsCallback( (RASCONN[] o1, ref int o2, ref int o3) => { o1[0].hrasconn = new IntPtr(1); o1[0].szDeviceName = deviceName; o1[0].szDeviceType = RASDT_Vpn; o1[0].szEntryName = entryName; o1[0].szPhonebook = phoneBookPath; o2 = 1; o3 = 1; return(SUCCESS); })); var deviceTypeFactory = new Mock <IDeviceTypeFactory>(); deviceTypeFactory.Setup(o => o.Create(deviceName, RASDT_Vpn)).Returns(new Vpn(deviceName)); var exceptionPolicy = new Mock <IExceptionPolicy>(); var structFactory = new Mock <IStructArrayFactory>(); structFactory.Setup(o => o.CreateArray <RASCONN>(1, out It.Ref <int> .IsAny)).Returns(new RASCONN[1]); var getConnectStatus = new Mock <IRasGetConnectStatus>(); var getConnectionStatistics = new Mock <IRasGetConnectionStatistics>(); var hangUp = new Mock <IRasHangUp>(); var clearConnectionStatistics = new Mock <IRasClearConnectionStatistics>(); var serviceLocator = new Mock <IServiceProvider>(); serviceLocator.Setup(o => o.GetService(typeof(IRasGetConnectStatus))).Returns(getConnectStatus.Object); serviceLocator.Setup(o => o.GetService(typeof(IRasGetConnectionStatistics))).Returns(getConnectionStatistics.Object); serviceLocator.Setup(o => o.GetService(typeof(IRasHangUp))).Returns(hangUp.Object); serviceLocator.Setup(o => o.GetService(typeof(IRasClearConnectionStatistics))).Returns(clearConnectionStatistics.Object); var target = new RasEnumConnectionsService(api.Object, deviceTypeFactory.Object, exceptionPolicy.Object, structFactory.Object, serviceLocator.Object); var result = target.EnumerateConnections().Single(); Assert.IsNotNull(result); }
public void ThrowsAnExceptionWhenTheApiReturnsNonZero() { var api = new Mock <IRasApi32>(); api.Setup(o => o.RasEnumConnections(It.IsAny <RASCONN[]>(), ref It.Ref <int> .IsAny, ref It.Ref <int> .IsAny)).Returns(ERROR_INVALID_PARAMETER); var deviceTypeFactory = new Mock <IDeviceTypeFactory>(); var exceptionPolicy = new Mock <IExceptionPolicy>(); exceptionPolicy.Setup(o => o.Create(ERROR_INVALID_PARAMETER)).Returns(new TestException()); var structFactory = new Mock <IStructArrayFactory>(); structFactory.Setup(o => o.CreateArray <RASCONN>(1, out It.Ref <int> .IsAny)).Returns(new RASCONN[1]); var serviceLocator = new Mock <IServiceProvider>(); var target = new RasEnumConnectionsService(api.Object, deviceTypeFactory.Object, exceptionPolicy.Object, structFactory.Object, serviceLocator.Object); Assert.Throws <TestException>(() => target.EnumerateConnections().ToArray()); }
public void ThrowsAnExceptionWhenTheDeviceIsNull() { var entryName = "Test"; var phoneBookPath = @"C:\Test.pbk"; var deviceName = "WAN"; var api = new Mock <IRasApi32>(); api.Setup(o => o.RasEnumConnections(It.IsAny <RASCONN[]>(), ref It.Ref <int> .IsAny, ref It.Ref <int> .IsAny)).Returns(new RasEnumConnectionsCallback( (RASCONN[] o1, ref int o2, ref int o3) => { o1[0].hrasconn = new IntPtr(1); o1[0].szDeviceName = deviceName; o1[0].szDeviceType = RASDT_Vpn; o1[0].szEntryName = entryName; o1[0].szPhonebook = phoneBookPath; o2 = 1; o3 = 1; return(SUCCESS); })); var deviceTypeFactory = new Mock <IDeviceTypeFactory>(); deviceTypeFactory.Setup(o => o.Create(deviceName, RASDT_Vpn)).Returns((RasDevice)null); var exceptionPolicy = new Mock <IExceptionPolicy>(); var structFactory = new Mock <IStructArrayFactory>(); structFactory.Setup(o => o.CreateArray <RASCONN>(1, out It.Ref <int> .IsAny)).Returns(new RASCONN[1]); var serviceLocator = new Mock <IServiceProvider>(); var target = new RasEnumConnectionsService(api.Object, deviceTypeFactory.Object, exceptionPolicy.Object, structFactory.Object, serviceLocator.Object); var ex = Assert.Throws <InvalidOperationException>(() => target.EnumerateConnections().Single()); Assert.AreEqual("The device was not created.", ex.Message); }
public void ReturnsMultipleConnectionAsExpected() { var entryName = "Test"; var phoneBookPath = @"C:\Test.pbk"; var deviceName = "WAN"; var api = new Mock <IRasApi32>(); api.Setup(o => o.RasEnumConnections(It.IsAny <RASCONN[]>(), ref It.Ref <int> .IsAny, ref It.Ref <int> .IsAny)).Returns(new RasEnumConnectionsCallback( (RASCONN[] o1, ref int lpCb, ref int count) => { if (count == 1) { count = 2; return(ERROR_BUFFER_TOO_SMALL); } if (count == 2) { o1[0].hrasconn = new IntPtr(1); o1[0].szDeviceName = deviceName; o1[0].szDeviceType = RASDT_Vpn; o1[0].szEntryName = entryName; o1[0].szPhonebook = phoneBookPath; o1[1].hrasconn = new IntPtr(2); o1[1].szDeviceName = deviceName; o1[1].szDeviceType = RASDT_Vpn; o1[1].szEntryName = entryName; o1[1].szPhonebook = phoneBookPath; return(SUCCESS); } return(-1); })); var deviceTypeFactory = new Mock <IDeviceTypeFactory>(); deviceTypeFactory.Setup(o => o.Create(deviceName, RASDT_Vpn)).Returns(new Vpn(deviceName)); var exceptionPolicy = new Mock <IExceptionPolicy>(); var structFactory = new StructFactory(new MarshallingService()); var getConnectStatus = new Mock <IRasGetConnectStatus>(); var getConnectionStatistics = new Mock <IRasGetConnectionStatistics>(); var hangUp = new Mock <IRasHangUp>(); var clearConnectionStatistics = new Mock <IRasClearConnectionStatistics>(); var serviceLocator = new Mock <IServiceProvider>(); serviceLocator.Setup(o => o.GetService(typeof(IRasGetConnectStatus))).Returns(getConnectStatus.Object); serviceLocator.Setup(o => o.GetService(typeof(IRasGetConnectionStatistics))).Returns(getConnectionStatistics.Object); serviceLocator.Setup(o => o.GetService(typeof(IRasHangUp))).Returns(hangUp.Object); serviceLocator.Setup(o => o.GetService(typeof(IRasClearConnectionStatistics))).Returns(clearConnectionStatistics.Object); var target = new RasEnumConnectionsService(api.Object, deviceTypeFactory.Object, exceptionPolicy.Object, structFactory, serviceLocator.Object); var result = target.EnumerateConnections().ToArray(); Assert.AreEqual(2, result.Length); }