2 // socket-related test cases
5 // Steffen Kiess (s-kiess@web.de)
7 // Copyright (C) 2015 Steffen Kiess
13 using System.Net.Sockets;
14 using System.Runtime.InteropServices;
17 using Mono.Unix.Native;
19 using NUnit.Framework;
21 namespace MonoTests.Mono.Unix.Native
23 [TestFixture, Category ("NotDotNet")]
24 public class SocketTest {
31 TempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
33 if (Directory.Exists (TempFolder))
34 Directory.Delete (TempFolder, true);
36 Directory.CreateDirectory (TempFolder);
40 public void TearDown()
42 if (Directory.Exists (TempFolder))
43 Directory.Delete (TempFolder, true);
46 // Set a timeout on all sockets to make sure that if a test fails it
47 // won't cause the program to hang
48 void SetTimeout (int socket)
50 var timeout = new Timeval {
54 if (Syscall.setsockopt (socket, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_RCVTIMEO, timeout) < 0 ||
55 Syscall.setsockopt (socket, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_SNDTIMEO, timeout) < 0)
56 UnixMarshal.ThrowExceptionForLastError ();
59 void WithSocketPair (Action<int, int> f)
62 if (Syscall.socketpair (UnixAddressFamily.AF_UNIX, UnixSocketType.SOCK_STREAM, 0, out socket1, out socket2) < 0)
63 UnixMarshal.ThrowExceptionForLastError ();
70 int r0 = Syscall.close (socket1);
71 int r1 = Syscall.close (socket2);
73 UnixMarshal.ThrowExceptionForLastError ();
77 void WithSockets (UnixAddressFamily af, UnixSocketType type, UnixSocketProtocol protocol, Action<int, int> f)
80 if ((so1 = Syscall.socket (af, type, protocol)) < 0)
81 UnixMarshal.ThrowExceptionForLastError ();
83 if ((so2 = Syscall.socket (af, type, protocol)) < 0)
84 UnixMarshal.ThrowExceptionForLastError ();
91 if (Syscall.close (so2) < 0)
92 UnixMarshal.ThrowExceptionForLastError ();
95 if (Syscall.close (so1) < 0)
96 UnixMarshal.ThrowExceptionForLastError ();
101 public void Socket ()
104 if ((socket = Syscall.socket (UnixAddressFamily.AF_UNIX, UnixSocketType.SOCK_STREAM, 0)) < 0)
105 UnixMarshal.ThrowExceptionForLastError ();
107 if (Syscall.close (socket) < 0)
108 UnixMarshal.ThrowExceptionForLastError ();
112 public void SocketPair ()
114 int socket1, socket2;
115 if (Syscall.socketpair (UnixAddressFamily.AF_UNIX, UnixSocketType.SOCK_STREAM, 0, out socket1, out socket2) < 0)
116 UnixMarshal.ThrowExceptionForLastError ();
118 int r0 = Syscall.close (socket1);
119 int r1 = Syscall.close (socket2);
120 if (r0 < 0 || r1 < 0)
121 UnixMarshal.ThrowExceptionForLastError ();
125 public void SendRecv ()
127 WithSocketPair ((so1, so2) => {
129 var buffer1 = new byte[] { 42, 43, 44 };
130 ret = Syscall.send (so1, buffer1, (ulong) buffer1.Length, 0);
132 UnixMarshal.ThrowExceptionForLastError ();
134 var buffer2 = new byte[1024];
135 ret = Syscall.recv (so2, buffer2, (ulong) buffer2.Length, 0);
137 UnixMarshal.ThrowExceptionForLastError ();
139 Assert.AreEqual (buffer1.Length, ret);
140 for (int i = 0; i < buffer1.Length; i++)
141 Assert.AreEqual (buffer1[i], buffer2[i]);
146 public void SockOpt ()
148 WithSockets (UnixAddressFamily.AF_UNIX, UnixSocketType.SOCK_STREAM, 0, (so1, so2) => {
149 // Set SO_REUSEADDR to 1
150 if (Syscall.setsockopt (so1, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_REUSEADDR, 1) < 0)
151 UnixMarshal.ThrowExceptionForLastError ();
153 // Get and check SO_REUSEADDR
155 if (Syscall.getsockopt (so1, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_REUSEADDR, out value) < 0)
156 UnixMarshal.ThrowExceptionForLastError ();
157 Assert.AreEqual (value, 1);
159 // Set SO_REUSEADDR to 0
160 if (Syscall.setsockopt (so1, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_REUSEADDR, new byte[10], 4) < 0)
161 UnixMarshal.ThrowExceptionForLastError ();
163 // Get and check SO_REUSEADDR
164 var buffer = new byte[15];
166 if (Syscall.getsockopt (so1, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_REUSEADDR, buffer, ref size) < 0)
167 UnixMarshal.ThrowExceptionForLastError ();
168 Assert.AreEqual (size, 4);
169 for (int i = 0; i < size; i++)
170 Assert.AreEqual (buffer[i], 0);
175 public void SockOptLinger ()
177 WithSockets (UnixAddressFamily.AF_INET, UnixSocketType.SOCK_STREAM, UnixSocketProtocol.IPPROTO_TCP, (so1, so2) => {
178 Linger linger = new Linger {
183 if (Syscall.setsockopt (so1, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_LINGER, linger) < 0)
184 UnixMarshal.ThrowExceptionForLastError ();
186 // Get and check SO_LINGER
188 if (Syscall.getsockopt (so1, UnixSocketProtocol.SOL_SOCKET, UnixSocketOptionName.SO_LINGER, out value) < 0)
189 UnixMarshal.ThrowExceptionForLastError ();
190 Assert.AreEqual (linger, value);
195 public void Shutdown ()
197 WithSocketPair ((so1, so2) => {
198 if (Syscall.shutdown (so1, ShutdownOption.SHUT_WR) < 0)
199 UnixMarshal.ThrowExceptionForLastError ();
201 var buffer2 = new byte[1024];
202 long ret = Syscall.recv (so2, buffer2, (ulong) buffer2.Length, 0);
204 UnixMarshal.ThrowExceptionForLastError ();
206 Assert.AreEqual (ret, 0);
216 // indent-tabs-mode: t