TcpClientCas.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // TcpClientCas.cs - CAS unit tests for System.Net.Sockets.TcpClient class
  3. //
  4. // Author:
  5. // Sebastien Pouliot <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.IO;
  12. using System.Net;
  13. using System.Net.Sockets;
  14. using System.Security;
  15. using System.Security.Permissions;
  16. using System.Threading;
  17. using MonoTests.System.Net.Sockets;
  18. namespace MonoCasTests.System.Net.Sockets {
  19. [TestFixture]
  20. [Category ("CAS")]
  21. public class TcpClientCas {
  22. private const int timeout = 30000;
  23. static ManualResetEvent reset;
  24. private string message;
  25. [TestFixtureSetUp]
  26. public void FixtureSetUp ()
  27. {
  28. reset = new ManualResetEvent (false);
  29. }
  30. [TestFixtureTearDown]
  31. public void FixtureTearDown ()
  32. {
  33. reset.Close ();
  34. }
  35. [SetUp]
  36. public void SetUp ()
  37. {
  38. if (!SecurityManager.SecurityEnabled)
  39. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  40. }
  41. // async tests (for stack propagation)
  42. private void ConnectCallback (IAsyncResult ar)
  43. {
  44. TcpClient c = (TcpClient)ar.AsyncState;
  45. c.EndConnect (ar);
  46. try {
  47. // can we do something bad here ?
  48. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  49. message = "Expected a SecurityException";
  50. }
  51. catch (SecurityException) {
  52. message = null;
  53. reset.Set ();
  54. }
  55. catch (Exception e) {
  56. message = e.ToString ();
  57. }
  58. }
  59. [Test]
  60. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  61. [Category ("InetAccess")]
  62. public void AsyncConnect_StringIntAsyncCallbackObject ()
  63. {
  64. TcpClient s = new TcpClient ();
  65. message = "AsyncConnect";
  66. reset.Reset ();
  67. IAsyncResult r = s.BeginConnect ("www.google.com", 80, new AsyncCallback (ConnectCallback), s);
  68. Assert.IsNotNull (r, "IAsyncResult");
  69. if (!reset.WaitOne (timeout, true))
  70. Assert.Ignore ("Timeout");
  71. Assert.IsNull (message, message);
  72. }
  73. [Test]
  74. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  75. [Category ("InetAccess")]
  76. public void AsyncConnect_IPAddressIntAsyncCallbackObject ()
  77. {
  78. IPHostEntry host = Dns.Resolve ("www.google.com");
  79. TcpClient s = new TcpClient ();
  80. message = "AsyncConnect";
  81. reset.Reset ();
  82. IAsyncResult r = s.BeginConnect (host.AddressList[0], 80, new AsyncCallback (ConnectCallback), s);
  83. Assert.IsNotNull (r, "IAsyncResult");
  84. if (!reset.WaitOne (timeout, true))
  85. Assert.Ignore ("Timeout");
  86. Assert.IsNull (message, message);
  87. }
  88. [Test]
  89. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  90. [Category ("InetAccess")]
  91. public void AsyncConnect_IPAddressArrayIntAsyncCallbackObject ()
  92. {
  93. IPHostEntry host = Dns.Resolve ("www.google.com");
  94. TcpClient s = new TcpClient ();
  95. message = "AsyncConnect";
  96. reset.Reset ();
  97. IAsyncResult r = s.BeginConnect (host.AddressList, 80, new AsyncCallback (ConnectCallback), s);
  98. Assert.IsNotNull (r, "IAsyncResult");
  99. if (!reset.WaitOne (timeout, true))
  100. Assert.Ignore ("Timeout");
  101. Assert.IsNull (message, message);
  102. }
  103. }
  104. }