public void TestSemaphoreReleaseOnExceptionalOperation() { CMDClient client = new CMDClient(null, "Some network"); var sem = mocks.DynamicMock<System.Threading.Semaphore>(); var stream = mocks.DynamicMock<System.IO.Stream>(); using (mocks.Record()) { Expect.Call(sem.WaitOne()).Return(true); Expect.Call(sem.Release()).Return(0); stream.Flush(); LastCall.On(stream).Throw(new Exception()); } typeof(CMDClient).GetField("networkStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(client, stream); typeof(CMDClient).GetField("semaphore", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(client, sem); Command command = new Command(CommandType.UserExit, IPAddress.Parse("127.0.0.1"), null); try { client.SendCommandToServerUnthreaded(command); } catch (Exception e) { } mocks.VerifyAll(); }
public void TestSemaphoreReleaseOnExceptionalOperation() { IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); Command command = new Command(CommandType.UserExit, ipaddress, null); System.IO.Stream fakeStream = mocks.DynamicMock<System.IO.Stream>(); System.Threading.Semaphore fakeSemaphore = mocks.DynamicMock<System.Threading.Semaphore>(); byte[] commandBytes = { 0, 0, 0, 0 }; byte[] ipLength = { 9, 0, 0, 0 }; byte[] ip = { 49, 50, 55, 46, 48, 46, 48, 46, 49 }; byte[] metaDataLength = { 2, 0, 0, 0 }; byte[] metaData = { 10, 0 }; using (mocks.Ordered()) { Expect.Call(fakeSemaphore.WaitOne()).Return(true); fakeStream.Write(commandBytes, 0, 4); fakeStream.Flush(); fakeStream.Write(ipLength, 0, 4); fakeStream.Flush(); fakeStream.Write(ip, 0, 9); fakeStream.Flush(); fakeStream.Write(metaDataLength, 0, 4); fakeStream.Flush(); fakeStream.Write(metaData, 0, 2); fakeStream.Flush(); LastCall.On(fakeStream).Throw(new Exception()); Expect.Call(fakeSemaphore.Release()).Return(1); } mocks.ReplayAll(); CMDClient client = new CMDClient(null, "Bogus network name"); // we need to set the private variable here typeof(CMDClient).GetField("networkStream", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(client, fakeStream); typeof(CMDClient).GetField("semaphore", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(client, fakeSemaphore); try { client.SendCommandToServerUnthreaded(command); } catch (Exception e) { Console.Write("Exception caught"); } mocks.VerifyAll(); }
public void TestSemaphoreReleaseOnNormalOperation() { CMDClient client = new CMDClient(null, "Some network"); var sem = mocks.DynamicMock<System.Threading.Semaphore>(); using (mocks.Record()) { Expect.Call(sem.WaitOne()).Return(true); Expect.Call(sem.Release()).Return(0); } using (MemoryStream ms = new MemoryStream()) { typeof(CMDClient).GetField("networkStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(client, ms); typeof(CMDClient).GetField("semaphore", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(client, sem); Command command = new Command(CommandType.UserExit, IPAddress.Parse("127.0.0.1"), null); client.SendCommandToServerUnthreaded(command); mocks.VerifyAll(); } }
public void TestUserExitCommand() { IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); Command command = new Command(CommandType.UserExit, ipaddress, null); System.IO.Stream fakeStream = mocks.DynamicMock<System.IO.Stream>(); byte[] commandBytes = { 0, 0, 0, 0 }; byte[] ipLength = { 9, 0, 0, 0 }; byte[] ip = { 49, 50, 55, 46, 48, 46, 48, 46, 49 }; byte[] metaDataLength = { 2, 0, 0, 0 }; byte[] metaData = { 10, 0 }; using (mocks.Ordered()) { fakeStream.Write(commandBytes, 0, 4); fakeStream.Flush(); fakeStream.Write(ipLength, 0, 4); fakeStream.Flush(); fakeStream.Write(ip, 0, 9); fakeStream.Flush(); fakeStream.Write(metaDataLength, 0, 4); fakeStream.Flush(); fakeStream.Write(metaData, 0, 2); fakeStream.Flush(); } mocks.ReplayAll(); CMDClient client = new CMDClient(null, "Bogus network name"); // we need to set the private variable here typeof(CMDClient).GetField("networkStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(client, fakeStream); client.SendCommandToServerUnthreaded(command); mocks.VerifyAll(); }
public void TestUserExitCommandWithoutMocks() { byte[] commandBytes = { 0, 0, 0, 0 }; byte[] ipLength = { 9, 0, 0, 0 }; byte[] ip = { 49, 50, 55, 46, 48, 46, 48, 46, 49 }; byte[] metaDataLength = { 2, 0, 0, 0 }; byte[] metaData = { 10, 0 }; using (MemoryStream expectedStream = new MemoryStream(), actualStream = new MemoryStream()) { expectedStream.Write(commandBytes, 0, 4); expectedStream.Write(ipLength, 0, 4); expectedStream.Write(ip, 0, 9); expectedStream.Write(metaDataLength, 0, 4); expectedStream.Write(metaData, 0, 2); CMDClient client = new CMDClient(null, "Bogus network name"); typeof(CMDClient).GetField("networkStream", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(client, actualStream); IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); Command command = new Command(CommandType.UserExit, ipaddress, null); client.SendCommandToServerUnthreaded(command); Assert.AreEqual(expectedStream.ToArray().Length, actualStream.ToArray().Length); expectedStream.Position = 0; actualStream.Position = 0; int buf; while ((buf = expectedStream.ReadByte()) >= 0) Assert.AreEqual(buf, actualStream.ReadByte()); } }
public void TestUserExitCommandWithoutMocks() { IPAddress ipaddress = IPAddress.Parse("127.0.0.1"); Command command = new Command(CommandType.UserExit, ipaddress, null); MemoryStream stream = new MemoryStream(); byte[] commandBytes = { 0, 0, 0, 0 }; byte[] ipLength = { 9, 0, 0, 0 }; byte[] ip = { 49, 50, 55, 46, 48, 46, 48, 46, 49 }; byte[] metaDataLength = { 2, 0, 0, 0 }; byte[] metaData = { 10, 0 }; stream.Write(commandBytes, 0, 4); stream.Write(ipLength, 0, 4); stream.Write(ip, 0, 9); stream.Write(metaDataLength, 0, 4); stream.Write(metaData, 0, 2); CMDClient client = new CMDClient(null, "Bogus network name"); // we need to set the private variable here typeof(CMDClient).GetField("networkStream", BindingFlags.NonPublic | BindingFlags.Instance) .SetValue(client, stream); client.SendCommandToServerUnthreaded(command); mocks.VerifyAll(); }