Merge pull request #2543 from ermshiperete/Xamarin-31021
[mono.git] / mcs / class / Mono.Posix / Test / Mono.Unix / UnixEndPointTest.cs
1 // UnixEndPointTest.cs: Unit tests for Mono.Unix.UnixListener
2 //
3 // Authors:
4 //  David Lechner (david@lechnology.com)
5 //
6 // (c) 2015 David Lechner
7 //
8
9 using System;
10 using System.IO;
11 using System.Net;
12 using System.Net.Sockets;
13 using System.Text;
14
15 using NUnit.Framework;
16 using Mono.Unix;
17
18 namespace MonoTests.Mono.Unix {
19
20     [TestFixture]
21     public class UnixEndPointTest {
22
23         // Regression test for https://bugzilla.xamarin.com/show_bug.cgi?id=35004
24         [Test]
25         public void TestCreate ()
26         {
27             const string socketFile = "test";
28             // mangledSocketFile simulates the socket file name with a null
29             // terminator and junk after the null terminator. This can be present
30             // in a SocketAddress when marshaled from native code.
31             const string mangledSocketFile = socketFile + "\0junk";
32
33             var bytes = Encoding.Default.GetBytes (mangledSocketFile);
34             var socketAddress = new SocketAddress (AddressFamily.Unix, bytes.Length + 2);
35             for (int i = 0; i < bytes.Length; i++) {
36                 socketAddress [i + 2] = bytes [i];
37             }
38             var dummyEndPoint = new UnixEndPoint (socketFile);
39
40             // testing that the Create() method strips off the null terminator and the junk
41             var endPoint = (UnixEndPoint)dummyEndPoint.Create (socketAddress);
42             Assert.AreEqual (socketFile, endPoint.Filename, "#A01");
43         }
44     }
45 }