TcpClientCas.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. private string uri = "http://www.google.com";
  26. [TestFixtureSetUp]
  27. public void FixtureSetUp ()
  28. {
  29. reset = new ManualResetEvent (false);
  30. }
  31. [TestFixtureTearDown]
  32. public void FixtureTearDown ()
  33. {
  34. reset.Close ();
  35. }
  36. [SetUp]
  37. public void SetUp ()
  38. {
  39. if (!SecurityManager.SecurityEnabled)
  40. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  41. }
  42. // async tests (for stack propagation)
  43. #if NET_2_0
  44. /* Oops - not yet implemented in Mono
  45. private void ConnectCallback (IAsyncResult ar)
  46. {
  47. TcpClient c = (TcpClient)ar.AsyncState;
  48. c.EndConnect (ar);
  49. try {
  50. // can we do something bad here ?
  51. Assert.IsNotNull (Environment.GetEnvironmentVariable ("USERNAME"));
  52. message = "Expected a SecurityException";
  53. }
  54. catch (SecurityException) {
  55. message = null;
  56. reset.Set ();
  57. }
  58. catch (Exception e) {
  59. message = e.ToString ();
  60. }
  61. }
  62. [Test]
  63. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  64. [Category ("InetAccess")]
  65. public void AsyncConnect_StringIntAsyncCallbackObject ()
  66. {
  67. TcpClient s = new TcpClient ();
  68. message = "AsyncConnect";
  69. reset.Reset ();
  70. IAsyncResult r = s.BeginConnect ("www.google.com", 80, new AsyncCallback (ConnectCallback), s);
  71. Assert.IsNotNull (r, "IAsyncResult");
  72. if (!reset.WaitOne (timeout, true))
  73. Assert.Ignore ("Timeout");
  74. Assert.IsNull (message, message);
  75. }
  76. [Test]
  77. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  78. [Category ("InetAccess")]
  79. public void AsyncConnect_IPAddressIntAsyncCallbackObject ()
  80. {
  81. IPHostEntry host = Dns.Resolve ("www.google.com");
  82. TcpClient s = new TcpClient ();
  83. message = "AsyncConnect";
  84. reset.Reset ();
  85. IAsyncResult r = s.BeginConnect (host.AddressList[0], 80, new AsyncCallback (ConnectCallback), s);
  86. Assert.IsNotNull (r, "IAsyncResult");
  87. if (!reset.WaitOne (timeout, true))
  88. Assert.Ignore ("Timeout");
  89. Assert.IsNull (message, message);
  90. }
  91. [Test]
  92. [EnvironmentPermission (SecurityAction.Deny, Read = "USERNAME")]
  93. [Category ("InetAccess")]
  94. public void AsyncConnect_IPAddressArrayIntAsyncCallbackObject ()
  95. {
  96. IPHostEntry host = Dns.Resolve ("www.google.com");
  97. TcpClient s = new TcpClient ();
  98. message = "AsyncConnect";
  99. reset.Reset ();
  100. IAsyncResult r = s.BeginConnect (host.AddressList, 80, new AsyncCallback (ConnectCallback), s);
  101. Assert.IsNotNull (r, "IAsyncResult");
  102. if (!reset.WaitOne (timeout, true))
  103. Assert.Ignore ("Timeout");
  104. Assert.IsNull (message, message);
  105. }
  106. */
  107. #endif
  108. }
  109. }