| 1 |
// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 |
|
| 3 |
#[cfg(unix)] |
| 4 |
#[test] |
| 5 |
fn test_gdbus_peer_connection() { |
| 6 |
use gio::{ |
| 7 |
DBusConnection, DBusConnectionFlags, DBusNodeInfo, Socket, |
| 8 |
glib::{self, VariantTy}, |
| 9 |
prelude::*, |
| 10 |
}; |
| 11 |
use std::os::unix::net::UnixStream; |
| 12 |
|
| 13 |
const EXAMPLE_XML: &str = r#" |
| 14 |
<node> |
| 15 |
<interface name='com.github.gtk_rs'> |
| 16 |
<property type='i' name='Number' access='readwrite' /> |
| 17 |
<method name='Hello'> |
| 18 |
<arg type='s' name='name' direction='in'/> |
| 19 |
<arg type='s' name='greet' direction='out'/> |
| 20 |
</method> |
| 21 |
</interface> |
| 22 |
</node> |
| 23 |
"#; |
| 24 |
|
| 25 |
pub async fn spawn_server(fd: UnixStream) -> DBusConnection { |
| 26 |
let socket = Socket::from_fd(fd.into()).unwrap(); |
| 27 |
let socket_connection = socket.connection_factory_create_connection(); |
| 28 |
|
| 29 |
let guid = gio::dbus_generate_guid(); |
| 30 |
|
| 31 |
dbg!("server connecting"); |
| 32 |
|
| 33 |
let connection = DBusConnection::new_future( |
| 34 |
&socket_connection, |
| 35 |
Some(&guid), |
| 36 |
DBusConnectionFlags::AUTHENTICATION_SERVER |
| 37 |
.union(DBusConnectionFlags::DELAY_MESSAGE_PROCESSING), |
| 38 |
None, |
| 39 |
) |
| 40 |
.await |
| 41 |
.unwrap(); |
| 42 |
|
| 43 |
dbg!("server connected"); |
| 44 |
|
| 45 |
let interface_info = DBusNodeInfo::for_xml(EXAMPLE_XML) |
| 46 |
.unwrap() |
| 47 |
.lookup_interface("com.github.gtk_rs") |
| 48 |
.unwrap(); |
| 49 |
|
| 50 |
let _id = connection |
| 51 |
.register_object("/com/github/gtk_rs", &interface_info) |
| 52 |
.method_call( |
| 53 |
|_connection, |
| 54 |
_sender, |
| 55 |
_object_path, |
| 56 |
_interface_name, |
| 57 |
_method_name, |
| 58 |
parameters, |
| 59 |
invocation| { |
| 60 |
dbg!( |
| 61 |
"method_call", |
| 62 |
_sender, |
| 63 |
_object_path, |
| 64 |
_interface_name, |
| 65 |
_method_name, |
| 66 |
¶meters, |
| 67 |
&invocation |
| 68 |
); |
| 69 |
|
| 70 |
let name = parameters.child_get::<String>(0); |
| 71 |
invocation.return_value(Some(&(format!("Hello {name}!"),).to_variant())); |
| 72 |
}, |
| 73 |
) |
| 74 |
.property({ |
| 75 |
|_connection, _sender, _object_path, _interface_name, _property_name| { |
| 76 |
dbg!( |
| 77 |
"get_property", |
| 78 |
_sender, |
| 79 |
_object_path, |
| 80 |
_interface_name, |
| 81 |
_property_name |
| 82 |
); |
| 83 |
assert_eq!(_property_name, "Number"); |
| 84 |
123.to_variant() |
| 85 |
} |
| 86 |
}) |
| 87 |
.set_property({ |
| 88 |
|_connection, _sender, _object_path, _interface_name, _property_name, _value| { |
| 89 |
dbg!( |
| 90 |
"set_property", |
| 91 |
_sender, |
| 92 |
_object_path, |
| 93 |
_interface_name, |
| 94 |
_property_name, |
| 95 |
&_value |
| 96 |
); |
| 97 |
assert_eq!(_property_name, "Number"); |
| 98 |
assert_eq!(_value, 456.to_variant()); |
| 99 |
true |
| 100 |
} |
| 101 |
}) |
| 102 |
.build() |
| 103 |
.unwrap(); |
| 104 |
|
| 105 |
dbg!("server starts message processing"); |
| 106 |
|
| 107 |
connection.start_message_processing(); |
| 108 |
|
| 109 |
dbg!("server awaiting calls"); |
| 110 |
|
| 111 |
connection |
| 112 |
} |
| 113 |
|
| 114 |
pub async fn spawn_client(fd: UnixStream) -> DBusConnection { |
| 115 |
let socket_client = Socket::from_fd(fd.into()).unwrap(); |
| 116 |
let socket_connection_client = socket_client.connection_factory_create_connection(); |
| 117 |
|
| 118 |
dbg!("client connecting"); |
| 119 |
|
| 120 |
let connection = DBusConnection::new_future( |
| 121 |
&socket_connection_client, |
| 122 |
None, |
| 123 |
DBusConnectionFlags::AUTHENTICATION_CLIENT, |
| 124 |
None, |
| 125 |
) |
| 126 |
.await |
| 127 |
.unwrap(); |
| 128 |
|
| 129 |
dbg!("client connected"); |
| 130 |
|
| 131 |
connection |
| 132 |
} |
| 133 |
|
| 134 |
let ctx = glib::MainContext::default(); |
| 135 |
|
| 136 |
let (x, y) = std::os::unix::net::UnixStream::pair().unwrap(); |
| 137 |
|
| 138 |
x.set_nonblocking(true).unwrap(); |
| 139 |
y.set_nonblocking(true).unwrap(); |
| 140 |
|
| 141 |
ctx.block_on(async move { |
| 142 |
let ctx = glib::MainContext::default(); |
| 143 |
|
| 144 |
let server = ctx.spawn_local(spawn_server(x)); |
| 145 |
let client = ctx.spawn_local(spawn_client(y)); |
| 146 |
|
| 147 |
let server = server.await.unwrap(); |
| 148 |
let client = client.await.unwrap(); |
| 149 |
|
| 150 |
dbg!("calling method"); |
| 151 |
|
| 152 |
let result = client |
| 153 |
.call_future( |
| 154 |
None, |
| 155 |
"/com/github/gtk_rs", |
| 156 |
"com.github.gtk_rs", |
| 157 |
"Hello", |
| 158 |
Some(&("World",).into()), |
| 159 |
Some(VariantTy::new("(s)").unwrap()), |
| 160 |
gio::DBusCallFlags::NONE, |
| 161 |
10000, |
| 162 |
) |
| 163 |
.await |
| 164 |
.unwrap(); |
| 165 |
|
| 166 |
dbg!("method called"); |
| 167 |
|
| 168 |
dbg!(&result); |
| 169 |
|
| 170 |
dbg!("getting property"); |
| 171 |
|
| 172 |
let getresult = client |
| 173 |
.call_future( |
| 174 |
None, |
| 175 |
"/com/github/gtk_rs", |
| 176 |
"org.freedesktop.DBus.Properties", |
| 177 |
"Get", |
| 178 |
Some(&("com.github.gtk_rs", "Number").to_variant()), |
| 179 |
Some(VariantTy::new("(v)").unwrap()), |
| 180 |
gio::DBusCallFlags::NONE, |
| 181 |
10000, |
| 182 |
) |
| 183 |
.await |
| 184 |
.unwrap(); |
| 185 |
|
| 186 |
assert_eq!(getresult, (123.to_variant(),).to_variant()); |
| 187 |
|
| 188 |
dbg!("setting property"); |
| 189 |
|
| 190 |
let setresult = client |
| 191 |
.call_future( |
| 192 |
None, |
| 193 |
"/com/github/gtk_rs", |
| 194 |
"org.freedesktop.DBus.Properties", |
| 195 |
"Set", |
| 196 |
Some(&("com.github.gtk_rs", "Number", 456.to_variant()).to_variant()), |
| 197 |
None, |
| 198 |
gio::DBusCallFlags::NONE, |
| 199 |
10000, |
| 200 |
) |
| 201 |
.await |
| 202 |
.unwrap(); |
| 203 |
|
| 204 |
assert_eq!(setresult, ().to_variant()); |
| 205 |
|
| 206 |
dbg!("closing client"); |
| 207 |
client.close_future().await.unwrap(); |
| 208 |
dbg!("closed client, closing server"); |
| 209 |
server.close_future().await.unwrap(); |
| 210 |
dbg!("closed server"); |
| 211 |
|
| 212 |
drop(client); |
| 213 |
drop(server); |
| 214 |
|
| 215 |
assert_eq!(result.child_get::<String>(0), "Hello World!"); |
| 216 |
|
| 217 |
glib::timeout_future_with_priority( |
| 218 |
glib::Priority::LOW, |
| 219 |
std::time::Duration::from_millis(50), |
| 220 |
) |
| 221 |
.await; |
| 222 |
}); |
| 223 |
} |