Ejemplo n.º 1
0
        public void SockaddrInTest()
        {
            var address1 = new SockaddrIn {
                sin_family = UnixAddressFamily.AF_INET,
                sin_port   = Syscall.htons(5678),
                sin_addr   = NativeConvert.ToInAddr(IPAddress.Loopback),
            };

            var storage  = address1.ToSockaddrStorage();
            var address2 = SockaddrIn.FromSockaddrStorage(storage);

            Assert.AreEqual(address1, address2);

            var sockaddr = Sockaddr.FromSockaddrStorage(storage);

            Assert.AreEqual(sockaddr.sa_family, address1.sa_family);

            var storage2 = storage.ToSockaddrStorage();

            Assert.AreEqual(storage, storage2);

            var storage3 = new SockaddrStorage(123);

            storage2.CopyTo(storage3);
            Assert.AreEqual(storage, storage3);

            Assert.AreEqual("{sin_family=AF_INET, sin_port=htons(5678), sin_addr=127.0.0.1}", address1.ToString());
        }
Ejemplo n.º 2
0
        public void SockaddrIn6Test()
        {
            if (!Socket.OSSupportsIPv6)
            {
                Assert.Ignore("OS does not support IPv6.");
            }

            var address1 = new SockaddrIn6 {
                sin6_family   = UnixAddressFamily.AF_INET6,
                sin6_port     = Syscall.htons(1234),
                sin6_flowinfo = 2,
                sin6_addr     = NativeConvert.ToIn6Addr(IPAddress.IPv6Loopback),
                sin6_scope_id = 3
            };

            var storage  = address1.ToSockaddrStorage();
            var address2 = SockaddrIn6.FromSockaddrStorage(storage);

            Assert.AreEqual(address1, address2);

            var sockaddr = Sockaddr.FromSockaddrStorage(storage);

            Assert.AreEqual(sockaddr.sa_family, address1.sa_family);

            Assert.AreEqual("{sin6_family=AF_INET6, sin6_port=htons(1234), sin6_flowinfo=2, sin6_addr=::1, sin6_scope_id=3}", address1.ToString());
        }
Ejemplo n.º 3
0
        public void SockaddrUnTest()
        {
            var address1 = new SockaddrUn("/tmp/foo");

            Assert.AreEqual(address1.Path, "/tmp/foo");
            Assert.IsFalse(address1.IsLinuxAbstractNamespace);

            var storage  = address1.ToSockaddrStorage();
            var address2 = SockaddrUn.FromSockaddrStorage(storage);

            Assert.AreEqual(address1, address2);

            var sockaddr = Sockaddr.FromSockaddrStorage(storage);

            Assert.AreEqual(sockaddr.sa_family, address1.sa_family);

            var address3 = new SockaddrUn("/tmp/bar", linuxAbstractNamespace: true);

            Assert.AreEqual(address3.Path, "/tmp/bar");
            Assert.IsTrue(address3.IsLinuxAbstractNamespace);

            var address4 = new SockaddrUn(new string ('X', 9000));

            Assert.AreEqual(address4.Path, new string ('X', 9000));
            Assert.IsFalse(address4.IsLinuxAbstractNamespace);
            var storage2 = address4.ToSockaddrStorage();
            var address5 = SockaddrUn.FromSockaddrStorage(storage2);

            Assert.AreEqual(address4, address5);
            // Test the malloc() path for long SockaddrUn adresses (the syscalls will fail because the fd is invalid and because the path is too long)
            Syscall.bind(-1, address4);
            Syscall.getsockname(-1, address4);

            Assert.AreEqual("{sa_family=AF_UNIX, sun_path=\"/tmp/foo\"}", address1.ToString());
            Assert.AreEqual("{sa_family=AF_UNIX, sun_path=\"\\0/tmp/bar\"}", address3.ToString());
        }