[System*] Throw a PlatformNotSupported exception when using the networking stack...
[mono.git] / mcs / class / System / Test / System.Net.Sockets / SocketAsyncTest.cs
1 using System;
2 using System.Collections;
3 using System.Threading;
4 using System.Net;
5 using System.Net.Sockets;
6 using NUnit.Framework;
7
8 namespace MonoTests.System.Net.Sockets
9 {
10         [TestFixture]
11         public class SocketAsyncTest
12         {
13                 Socket serverSocket;
14                 Socket clientSocket;
15                 ManualResetEvent readyEvent;
16                 ManualResetEvent mainEvent;
17                 Exception error;
18
19                 void SetUp ()
20                 {
21                         readyEvent = new ManualResetEvent (false);
22                         mainEvent = new ManualResetEvent (false);
23
24                         ThreadPool.QueueUserWorkItem (_ => DoWork ());
25                         readyEvent.WaitOne ();
26
27                         if (error != null)
28                                 throw error;
29
30                         clientSocket = new Socket (
31                                 AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
32                         clientSocket.Connect (serverSocket.LocalEndPoint);
33                         clientSocket.NoDelay = true;
34                 }
35
36                 [TestFixtureTearDown]
37                 public void TearDown ()
38                 {
39                         if (serverSocket != null)
40                                 serverSocket.Close ();
41                         readyEvent.Close ();
42                         mainEvent.Close ();
43                 }
44
45                 void DoWork ()
46                 {
47                         try {
48                                 serverSocket = new Socket (
49                                         AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
50                                 serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
51                                 serverSocket.Listen (1);
52
53                                 var async = new SocketAsyncEventArgs ();
54                                 async.Completed += (s,e) => OnAccepted (e);
55
56                                 if (!serverSocket.AcceptAsync (async))
57                                         OnAccepted (async);
58                         } catch (Exception e) {
59                                 error = e;
60                         } finally {
61                                 readyEvent.Set ();
62                         }
63                 }
64
65                 void OnAccepted (SocketAsyncEventArgs e)
66                 {
67                         var acceptSocket = e.AcceptSocket;
68
69                         try {
70                                 var header = new byte [4];
71                                 acceptSocket.Receive (header);
72                                 if ((header [0] != 0x12) || (header [1] != 0x34) ||
73                                     (header [2] != 0x56) || (header [3] != 0x78))
74                                         throw new InvalidOperationException ();
75                         } catch (Exception ex) {
76                                 error = ex;
77                                 return;
78                         }
79
80                         var recvAsync = new SocketAsyncEventArgs ();
81                         recvAsync.Completed += (sender, args) => OnReceived (args);
82                         recvAsync.SetBuffer (new byte [4], 0, 4);
83                         if (!acceptSocket.ReceiveAsync (recvAsync))
84                                 OnReceived (recvAsync);
85
86                         mainEvent.Set ();
87                 }
88
89                 void OnReceived (SocketAsyncEventArgs e)
90                 {
91                         if (e.SocketError != SocketError.Success)
92                                 error = new SocketException ((int) e.SocketError);
93                         else if (e.Buffer [0] != 0x9a)
94                                 error = new InvalidOperationException ();
95
96                         mainEvent.Set ();
97                 }
98
99                 [Test]
100                 [Category("Test")]
101 #if FEATURE_NO_BSD_SOCKETS
102                 [ExpectedException (typeof (PlatformNotSupportedException))]
103 #endif
104                 public void SendAsync ()
105                 {
106                         SetUp ();
107                         var buffer = new byte [] { 0x12, 0x34, 0x56, 0x78 };
108                         var m = new ManualResetEvent (false);
109                         var e = new SocketAsyncEventArgs ();
110                         e.SetBuffer (buffer, 0, buffer.Length);
111                         e.Completed += (s,o) => {
112                                 if (o.SocketError != SocketError.Success)
113                                         error = new SocketException ((int)o.SocketError);
114                                 m.Set ();
115                         };
116                         bool res = clientSocket.SendAsync (e);
117                         if (res) {
118                                 if (!m.WaitOne (1500))
119                                         Assert.Fail ("Timeout #1");
120                         }
121
122                         if (!mainEvent.WaitOne (1500))
123                                 Assert.Fail ("Timeout #2");
124                         if (error != null)
125                                 throw error;
126
127                         m.Reset ();
128                         mainEvent.Reset ();
129
130                         buffer [0] = 0x9a;
131                         buffer [1] = 0xbc;
132                         buffer [2] = 0xde;
133                         buffer [3] = 0xff;
134                         res = clientSocket.SendAsync (e);
135                         if (res) {
136                                 if (!m.WaitOne (1500))
137                                         Assert.Fail ("Timeout #3");
138                         }
139
140                         if (!mainEvent.WaitOne (1500))
141                                 Assert.Fail ("Timeout #4");
142                         if (error != null)
143                                 throw error;
144                 }
145         }
146 }