Ejemplo n.º 1
0
        public void Env()
        {
            string myPid = Process.GetCurrentProcess().Handle.ToString();

            // not set
            using (var fds = new FdSequence(3))
            {
                ServiceManager.Reset(fds[0]);
                Socket[] sockets = fds.GetListenSockets();
                Assert.Equal(0, sockets.Length);
            }

            // set
            using (var fds = new FdSequence(3))
            {
                ServiceManager.Reset(fds[0]);
                Environment.SetEnvironmentVariable(LISTEN_FDS, fds.Count.ToString());
                Environment.SetEnvironmentVariable(LISTEN_PID, myPid);

                // Retrieve Sockets
                Socket[] sockets = fds.GetListenSockets();
                Assert.Equal(3, sockets.Length);

                // Second call returns empty
                sockets = ServiceManager.GetListenSockets();
                Assert.Equal(0, sockets.Length);
            }

            ServiceManager.Reset(SD_LISTEN_FDS_START);
        }
Ejemplo n.º 2
0
        public void ListenSocketCanAccept(AddressFamily addressFamily)
        {
            // Travis doesn't have '::1' configured.
            if (addressFamily == AddressFamily.InterNetworkV6 &&
                Environment.GetEnvironmentVariable("TRAVIS") == "true")
            {
                return;
            }

            using (var fds = new FdSequence(3, addressFamily))
            {
                Socket[] sockets = fds.GetListenSockets(fds[0], fds.Count);
                foreach (Socket server in sockets)
                {
                    Assert.Equal(server.SocketType, SocketType.Stream);
                    Assert.Equal(server.AddressFamily, addressFamily);
                    Assert.Equal(server.ProtocolType, ProtocolType.Tcp);
                    using (var client = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp))
                    {
                        client.Connect(server.LocalEndPoint);
                        using (var acceptedSocket = server.Accept())
                        { }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void Cloexec()
        {
            const int TimeOutSec = 10;
            const int SleepMs    = 10;

            using (var fds = new FdSequence(1))
            {
                Socket[] sockets = fds.GetListenSockets(fds[0], fds.Count);
                var      socket  = sockets[0];
                using (var process = Process.Start("sleep", "10"))
                {
                    string fdPath     = $"/proc/{process.Handle}/fd/{GetFd(sockets[0])}";
                    bool   fileExists = true;
                    for (int i = 0; fileExists && i < TimeOutSec * 1000 / SleepMs; i++)
                    {
                        fileExists = File.Exists(fdPath);
                        if (fileExists)
                        {
                            System.Threading.Thread.Sleep(SleepMs);
                        }
                    }
                    Assert.False(fileExists);
                    process.Kill();
                }
            }
        }
Ejemplo n.º 4
0
 public void PidMismatch()
 {
     using (var fds = new FdSequence(3))
     {
         string   myPid     = "1";
         string   listenPid = "2";
         Socket[] sockets   = fds.GetListenSockets(myPid, listenPid, fds[0], fds.Count);
         Assert.Equal(0, sockets.Length);
     }
 }
Ejemplo n.º 5
0
 public void CorrectHandle()
 {
     using (var fds = new FdSequence(3))
     {
         Socket[] sockets = fds.GetListenSockets(fds[0], fds.Count);
         Assert.Equal(3, fds.Count);
         for (int i = 0; i < fds.Count; i++)
         {
             Assert.Equal(fds[i], GetFd(sockets[i]));
         }
     }
 }
Ejemplo n.º 6
0
 public void Cloexec()
 {
     using (var fds = new FdSequence(1))
     {
         Socket[] sockets = fds.GetListenSockets(fds[0], fds.Count);
         var      socket  = sockets[0];
         using (var process = Process.Start("sleep", "10"))
         {
             string fdPath = $"/proc/{process.Handle}/fd/{GetFd(sockets[0])}";
             Assert.False(File.Exists(fdPath));
             process.Kill();
         }
     }
 }
Ejemplo n.º 7
0
 public void ListenSocketCanAccept()
 {
     using (var fds = new FdSequence(3))
     {
         Socket[] sockets = fds.GetListenSockets(fds[0], fds.Count);
         foreach (Socket server in sockets)
         {
             Assert.Equal(server.SocketType, SocketType.Stream);
             Assert.Equal(server.AddressFamily, AddressFamily.InterNetwork);
             Assert.Equal(server.ProtocolType, ProtocolType.Tcp);
             using (var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
             {
                 client.Connect(server.LocalEndPoint);
                 using (var acceptedSocket = server.Accept())
                 { }
             }
         }
     }
 }