Esempio n. 1
0
        public async Task ConnectionFunction()
        {
            var tokenTcs = new TaskCompletionSource <object>();
            var token    = new object();

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(new MyConnectionOptions {
                    ConnectFunction = () => Task.FromResult(
                        new ClientSetupResult
                    {
                        ConnectionAddress = address,
                        TeardownToken     = token
                    }),
                    DisposeAction = tokenTcs.SetResult
                });

                await conn1.ConnectAsync();
            }

            var disposeToken = await tokenTcs.Task;

            Assert.Equal(token, disposeToken);
        }
Esempio n. 2
0
        public async Task Method()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var conn2     = new Connection(address);
                var conn2Info = await conn2.ConnectAsync();

                var conn2Name = conn2Info.LocalName;
                var path      = StringOperations.Path;
                var proxy     = conn1.CreateProxy <IStringOperations>(conn2Name, path);

                await conn2.RegisterObjectAsync(new StringOperations());

                var reply = await proxy.ConcatAsync("hello ", "world");

                Assert.Equal("hello world", reply);
            }
        }
Esempio n. 3
0
        public async Task ResolveService()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                IConnection conn1     = new Connection(address);
                var         conn1Info = await conn1.ConnectAsync();

                IConnection conn2 = new Connection(address);
                await conn2.ConnectAsync();

                var owner = await conn2.ResolveServiceOwnerAsync(serviceName);

                Assert.Equal(null, owner);

                await conn1.RegisterServiceAsync(serviceName);

                owner = await conn2.ResolveServiceOwnerAsync(serviceName);

                Assert.Equal(conn1Info.LocalName, owner);

                await conn1.UnregisterServiceAsync(serviceName);

                owner = await conn2.ResolveServiceOwnerAsync(serviceName);

                Assert.Equal(null, owner);
            }
        }
Esempio n. 4
0
        public async Task Signal()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var conn2     = new Connection(address);
                var conn2Info = await conn2.ConnectAsync();

                await conn2.RegisterObjectAsync(new PingPong());

                var conn2Name = conn2Info.LocalName;
                var path      = PingPong.Path;
                var proxy     = conn1.CreateProxy <IPingPong>(conn2Name, path);
                var tcs       = new TaskCompletionSource <string>();
                await proxy.WatchPongAsync(message => tcs.SetResult(message));

                await proxy.PingAsync("hello world");

                var reply = await tcs.Task;
                Assert.Equal("hello world", reply);

                conn1.Dispose();
                conn2.Dispose();
            }
        }
Esempio n. 5
0
        public async Task DaemonDisconnect()
        {
            var dbusDaemon = new DBusDaemon();
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                IConnection conn2     = new Connection(address);
                var         conn2Info = await conn2.ConnectAsync();

                await conn2.RegisterObjectAsync(new Slow());

                IConnection conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var proxy = conn1.CreateProxy <ISlow>(conn2Info.LocalName, Slow.Path);

                var pending = proxy.SlowAsync();

                dbusDaemon.Dispose();

                await Assert.ThrowsAsync <DisconnectedException>(() => pending);
            }
        }
Esempio n. 6
0
        public async Task AutoConnect()
        {
            string socketPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            string address    = $"unix:path={socketPath}";

            var connection = new Connection(new ClientConnectionOptions(address)
            {
                AutoConnect = true
            });

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(DBusDaemonProtocol.Unix, socketPath);

                var reply = await connection.ListServicesAsync();
            }

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(DBusDaemonProtocol.Unix, socketPath);

                var reply = await connection.ListServicesAsync();
            }

            var exception = await Assert.ThrowsAsync <ConnectException>(() => connection.ListServicesAsync());
        }
Esempio n. 7
0
        public async Task BusOperations()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                IConnection conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.ListActivatableServicesAsync();

                var exception = await Assert.ThrowsAsync <DBusException>(() => conn1.ActivateServiceAsync("com.some.service"));

                Assert.Equal("org.freedesktop.DBus.Error.ServiceUnknown", exception.ErrorName);

                var isRunning = await conn1.IsServiceActiveAsync("com.some.service");

                Assert.Equal(false, isRunning);
            }
        }
Esempio n. 8
0
        public async Task ReplaceRegistered()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.AllowReplacement);

                var conn2 = new Connection(address);
                await conn2.ConnectAsync();

                await conn2.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.ReplaceExisting);
            }
        }
Esempio n. 9
0
        public async Task NameAlreadyRegisteredOnOtherConnection()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.None);

                var conn2 = new Connection(address);
                await conn2.ConnectAsync();

                await Assert.ThrowsAsync <InvalidOperationException>(() => conn2.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.None));
            }
        }
Esempio n. 10
0
        public async Task WatchResolveService(string resolvedService, bool filterEvents)
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                IConnection conn1     = new Connection(address);
                var         conn1Info = await conn1.ConnectAsync();

                IConnection conn2     = new Connection(address);
                var         conn2Info = await conn2.ConnectAsync();

                var changeEvents = new BlockingCollection <ServiceOwnerChangedEventArgs>(new ConcurrentQueue <ServiceOwnerChangedEventArgs>());
                Action <ServiceOwnerChangedEventArgs> onChange =
                    change => { if (!filterEvents || (change.ServiceName == serviceName))
                                {
                                    changeEvents.Add(change);
                                }
                };
                var resolver = await conn2.ResolveServiceOwnerAsync(resolvedService, onChange);

                await conn1.RegisterServiceAsync(serviceName);

                var e = await changeEvents.TakeAsync();

                Assert.Equal(serviceName, e.ServiceName);
                Assert.Equal(null, e.OldOwner);
                Assert.Equal(conn1Info.LocalName, e.NewOwner);

                await conn1.UnregisterServiceAsync(serviceName);

                e = await changeEvents.TakeAsync();

                Assert.Equal(serviceName, e.ServiceName);
                Assert.Equal(conn1Info.LocalName, e.OldOwner);
                Assert.Equal(null, e.NewOwner);

                resolver.Dispose();
                await conn1.RegisterServiceAsync(serviceName);

                resolver = await conn2.ResolveServiceOwnerAsync(resolvedService, onChange);

                e = await changeEvents.TakeAsync();

                Assert.Equal(serviceName, e.ServiceName);
                Assert.Equal(null, e.OldOwner);
                Assert.Equal(conn1Info.LocalName, e.NewOwner);
            }
        }
Esempio n. 11
0
        public async Task UnixFd()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(DBusDaemonProtocol.Unix);

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var conn2     = new Connection(address);
                var conn2Info = await conn2.ConnectAsync();

                var conn2Name = conn2Info.LocalName;
                var path      = FdOperations.Path;
                var proxy     = conn1.CreateProxy <IFdOperations>(conn2Name, path);

                await conn2.RegisterObjectAsync(new FdOperations());

                var fileName = Path.GetTempFileName();
                var expected = "content";
                File.WriteAllText(fileName, expected);
                try
                {
                    SafeFileHandle receivedHandle;
                    using (var fileStream = File.OpenRead(fileName))
                    {
                        var handle = fileStream.SafeFileHandle;
                        Assert.False(handle.IsClosed);
                        receivedHandle = await proxy.PassAsync(handle);

                        Assert.True(handle.IsClosed);
                    }
                    using (var reader = new StreamReader(new FileStream(receivedHandle, FileAccess.Read)))
                    {
                        var text = reader.ReadToEnd();
                        Assert.Equal(expected, text);
                    }
                }
                finally
                {
                    File.Delete(fileName);
                }
            }
        }
Esempio n. 12
0
        public async Task UnixFd_Unsupported()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            if (DBusDaemon.IsSELinux)
            {
                throw new SkipTestException("Cannot provide SELinux context to DBus daemon over TCP");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(DBusDaemonProtocol.Tcp);

                var address = dbusDaemon.Address;

                var conn1     = new Connection(address);
                var conn1Info = await conn1.ConnectAsync();

                var conn2     = new Connection(address);
                var conn2Info = await conn2.ConnectAsync();

                var conn2Name = conn2Info.LocalName;
                var path      = FdOperations.Path;
                var proxy     = conn1.CreateProxy <IFdOperations>(conn2Name, path);

                await conn2.RegisterObjectAsync(new FdOperations());

                var fileName = Path.GetTempFileName();
                var expected = "content";
                File.WriteAllText(fileName, expected);
                try
                {
                    using (var fileStream = File.OpenRead(fileName))
                    {
                        var handle = fileStream.SafeFileHandle;
                        Assert.False(handle.IsClosed);
                        SafeFileHandle receivedHandle = await proxy.PassAsync(handle);

                        Assert.True(handle.IsClosed);
                        Assert.True(receivedHandle.IsInvalid);
                    }
                }
                finally
                {
                    File.Delete(fileName);
                }
            }
        }
Esempio n. 13
0
        public async Task Properties()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var conn2     = new Connection(address);
                var conn2Info = await conn2.ConnectAsync();

                var dictionary = new Dictionary <string, object> {
                    { "key1", 1 }, { "key2", 2 }
                };
                await conn2.RegisterObjectAsync(new PropertyObject(dictionary));

                var proxy = conn1.CreateProxy <IPropertyObject>(conn2Info.LocalName, PropertyObject.Path);

                var properties = await proxy.GetAllAsync();

                Assert.Equal(dictionary, properties);
                var val1 = await proxy.GetAsync("key1");

                Assert.Equal(1, val1);

                var tcs = new TaskCompletionSource <PropertyChanges>();
                await proxy.WatchPropertiesAsync(_ => tcs.SetResult(_));

                await proxy.SetAsync("key1", "changed");

                var val1Changed = await proxy.GetAsync("key1");

                Assert.Equal("changed", val1Changed);

                var changes = await tcs.Task;
                Assert.Equal(1, changes.Changed.Length);
                Assert.Equal("key1", changes.Changed.First().Key);
                Assert.Equal("changed", changes.Changed.First().Value);
                Assert.Equal(0, changes.Invalidated.Length);
            }
        }
Esempio n. 14
0
        public async Task TryMultipleAddresses()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                string address = "unix:path=/does/not/exist;"
                                 + dbusDaemon.Address;

                var connection     = new Connection(address);
                var connectionInfo = await connection.ConnectAsync();

                Assert.StartsWith(":", connectionInfo.LocalName);
                Assert.Equal(true, connectionInfo.RemoteIsBus);
            }
        }
Esempio n. 15
0
        public async Task Transport(DBusDaemonProtocol protocol)
        {
            if (DBusDaemon.IsSELinux && protocol == DBusDaemonProtocol.Tcp)
            {
                throw new SkipTestException("Cannot provide SELinux context to DBus daemon over TCP");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(protocol);

                var connection     = new Connection(dbusDaemon.Address);
                var connectionInfo = await connection.ConnectAsync();

                Assert.StartsWith(":", connectionInfo.LocalName);
                Assert.Equal(true, connectionInfo.RemoteIsBus);
            }
        }
Esempio n. 16
0
        public async Task NameAlreadyRegisteredOnSameConnection()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.None);

                await Assert.ThrowsAsync <InvalidOperationException>(() => conn1.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.None));
            }
        }
Esempio n. 17
0
        public async Task ClientDisconnect()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                IConnection conn2     = new Connection(address);
                var         conn2Info = await conn2.ConnectAsync();

                await conn2.RegisterObjectAsync(new Slow());

                // connection
                IConnection conn1 = new Connection(address);
                await conn1.ConnectAsync();

                // resolve
                var resolverTcs = new TaskCompletionSource <object>();
                await conn1.ResolveServiceOwnerAsync("some.service", _ => {}, e => resolverTcs.SetException(e));

                var proxy = conn1.CreateProxy <ISlow>(conn2Info.LocalName, Slow.Path);
                // method
                var pendingMethod = proxy.SlowAsync();
                // signal
                var signalTcs = new TaskCompletionSource <object>();
                await proxy.WatchSomethingErrorAsync(() => { }, e => signalTcs.SetException(e));

                conn1.Dispose();

                // method
                await Assert.ThrowsAsync <ObjectDisposedException>(() => pendingMethod);

                // signal
                await Assert.ThrowsAsync <ObjectDisposedException>(() => signalTcs.Task);

                // resolve
                await Assert.ThrowsAsync <ObjectDisposedException>(() => resolverTcs.Task);
            }
        }
Esempio n. 18
0
        public async Task Register()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.RegisterServiceAsync(serviceName);

                bool released = await conn1.UnregisterServiceAsync(serviceName);

                Assert.Equal(true, released);
            }
        }
Esempio n. 19
0
        public async Task EmitAquired()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var conn1OnLost    = new ObservableAction();
                var conn1OnAquired = new ObservableAction();
                await conn1.QueueServiceRegistrationAsync(serviceName, conn1OnAquired.Action, conn1OnLost.Action, ServiceRegistrationOptions.AllowReplacement);

                await conn1OnAquired.AssertNumberOfCallsAsync(1);

                await conn1OnLost.AssertNumberOfCallsAsync(0);

                var conn2 = new Connection(address);
                await conn2.ConnectAsync();

                var conn2OnLost    = new ObservableAction();
                var conn2OnAquired = new ObservableAction();
                await conn2.QueueServiceRegistrationAsync(serviceName, conn2OnAquired.Action, conn2OnLost.Action);

                await conn1OnAquired.AssertNumberOfCallsAsync(1);

                await conn1OnLost.AssertNumberOfCallsAsync(1);

                await conn2OnAquired.AssertNumberOfCallsAsync(1);

                await conn2OnLost.AssertNumberOfCallsAsync(0);
            }
        }
Esempio n. 20
0
        public async Task ReplaceRegistered()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.AllowReplacement);

                var conn2 = new Connection(address);
                await conn2.ConnectAsync();

                await conn2.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.ReplaceExisting);
            }
        }
Esempio n. 21
0
        public async Task TryMultipleAddresses()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync();

                string address = "unix:path=/does/not/exist;"
                                 + dbusDaemon.Address;

                var connection = new Connection(address);
                await connection.ConnectAsync();

                Assert.StartsWith(":", connection.LocalName);
                Assert.Equal(true, connection.RemoteIsBus);
            }
        }
Esempio n. 22
0
        public async Task EmitLost()
        {
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                var onLost = new ObservableAction();
                await conn1.RegisterServiceAsync(serviceName, onLost.Action, ServiceRegistrationOptions.AllowReplacement);

                var conn2 = new Connection(address);
                await conn2.ConnectAsync();

                await conn2.RegisterServiceAsync(serviceName, ServiceRegistrationOptions.ReplaceExisting);

                await onLost.AssertNumberOfCallsAsync(1);
            }
        }
Esempio n. 23
0
        public async Task Register()
        {
            if (!File.Exists("dbus-daemon"))
            {
                throw new SkipTestException("dbus-daemon not present");
            }
            using (var dbusDaemon = new DBusDaemon())
            {
                string serviceName = "tmds.dbus.test";

                await dbusDaemon.StartAsync();

                var address = dbusDaemon.Address;

                var conn1 = new Connection(address);
                await conn1.ConnectAsync();

                await conn1.RegisterServiceAsync(serviceName);

                bool released = await conn1.UnregisterServiceAsync(serviceName);

                Assert.Equal(true, released);
            }
        }
Esempio n. 24
0
        public async Task StateChanged()
        {
            string socketPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            string address    = $"unix:path={socketPath}";

            var connection = new Connection(new ClientConnectionOptions(address)
            {
                AutoConnect = true
            });
            var changeEvents = new BlockingCollection <ConnectionStateChangedEventArgs>(new ConcurrentQueue <ConnectionStateChangedEventArgs>());

            connection.StateChanged += (o, change) => changeEvents.Add(change);
            ConnectionStateChangedEventArgs e;

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(DBusDaemonProtocol.Unix, socketPath);

                var reply = await connection.ListServicesAsync();

                // Created -> Connecting
                e = await changeEvents.TakeAsync();

                Assert.Equal(ConnectionState.Connecting, e.State);

                // Connecting -> Connected
                e = await changeEvents.TakeAsync();

                Assert.Equal(ConnectionState.Connected, e.State);
                Assert.True(e.ConnectionInfo.RemoteIsBus);
                Assert.NotNull(e.ConnectionInfo.LocalName);
            }

            // Connected -> Disconnecting
            e = await changeEvents.TakeAsync();

            Assert.Equal(ConnectionState.Disconnecting, e.State);
            Assert.NotNull(e.DisconnectReason);
            Assert.IsType <DisconnectedException>(e.DisconnectReason);

            // Disconnecting -> Disconnected
            e = await changeEvents.TakeAsync();

            Assert.Equal(ConnectionState.Disconnected, e.State);
            Assert.NotNull(e.DisconnectReason);
            Assert.IsType <DisconnectedException>(e.DisconnectReason);

            var exception = await Assert.ThrowsAsync <ConnectException>(() => connection.ListServicesAsync());

            // Disconnected -> Connecting
            e = await changeEvents.TakeAsync();

            Assert.Equal(ConnectionState.Connecting, e.State);
            Assert.NotNull(e.DisconnectReason);
            Assert.IsType <DisconnectedException>(e.DisconnectReason);

            // Connecting -> Disconnecting
            e = await changeEvents.TakeAsync();

            Assert.Equal(ConnectionState.Disconnecting, e.State);
            Assert.NotNull(e.DisconnectReason);
            Assert.IsType <ConnectException>(e.DisconnectReason);

            // Disconnecting -> Disconnected
            e = await changeEvents.TakeAsync();

            Assert.Equal(ConnectionState.Disconnected, e.State);
            Assert.NotNull(e.DisconnectReason);
            Assert.IsType <ConnectException>(e.DisconnectReason);

            using (var dbusDaemon = new DBusDaemon())
            {
                await dbusDaemon.StartAsync(DBusDaemonProtocol.Unix, socketPath);

                var reply = await connection.ListServicesAsync();

                // Created -> Connecting
                e = await changeEvents.TakeAsync();

                Assert.Equal(ConnectionState.Connecting, e.State);
                Assert.NotNull(e.DisconnectReason);
                Assert.IsType <ConnectException>(e.DisconnectReason);

                // Connecting -> Connected
                e = await changeEvents.TakeAsync();

                Assert.Equal(ConnectionState.Connected, e.State);
                Assert.True(e.ConnectionInfo.RemoteIsBus);
                Assert.NotNull(e.ConnectionInfo.LocalName);
                Assert.Null(e.DisconnectReason);
            }
        }