File: dbus.rs

package info (click to toggle)
phrog 0.50.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 716 kB
  • sloc: makefile: 35; sh: 28; xml: 25
file content (149 lines) | stat: -rw-r--r-- 4,373 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use crate::common::SupervisedChild;
use anyhow::Context;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::{Duration, Instant};
use zbus::zvariant::ObjectPath;

pub fn dbus_daemon(kind: &str, tmpdir: &Path) -> SupervisedChild {
    let config_path = tmpdir.join(format!("{}-dbus.xml", kind));
    let sock_path = tmpdir.join(format!("{}.sock", kind));
    let dbus_path = format!("unix:path={}", sock_path.display());
    std::fs::write(
        &config_path,
        format!(
            r#"
    <!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
     "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
    <busconfig>
      <type>{}</type>
      <keep_umask/>
      <listen>{}</listen>
      <policy context="default">
        <allow send_destination="*" eavesdrop="true"/>
        <allow eavesdrop="true"/>
        <allow own="*"/>
      </policy>
    </busconfig>
    "#,
            kind, dbus_path
        ),
    )
    .expect("failed to write dbus config");

    std::env::set_var(
        format!("DBUS_{}_BUS_ADDRESS", kind.to_uppercase()),
        dbus_path,
    );
    let child = std::process::Command::new("dbus-daemon")
        .arg(format!("--config-file={}", config_path.to_str().unwrap()))
        .stdout(Stdio::null())
        .stdin(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
        .expect("failed to launch dbus-daemon");

    let start = Instant::now();
    while !sock_path.exists() {
        if start.elapsed() > Duration::from_secs(5) {
            panic!("dbus-daemon failed to launch");
        }
        std::thread::sleep(Duration::from_millis(50));
    }

    SupervisedChild::new("dbus-daemon", child)
}

struct AccountsFixture {
    num_users: Option<u32>,
}
struct UserFixture {
    name: String,
    username: String,
    icon_file: String,
}

#[zbus::interface(name = "org.freedesktop.Accounts")]
impl AccountsFixture {
    async fn list_cached_users(&self) -> Vec<ObjectPath> {
        let mut users = vec![
            ObjectPath::from_static_str_unchecked("/org/freedesktop/Accounts/phoshi"),
            ObjectPath::from_static_str_unchecked("/org/freedesktop/Accounts/agx"),
            ObjectPath::from_static_str_unchecked("/org/freedesktop/Accounts/sam"),
        ];
        if let Some(num_users) = self.num_users {
            users.truncate(num_users as _);
        }
        users
    }
}

impl UserFixture {
    fn new(name: &str, username: &str, icon_file: &str) -> Self {
        Self {
            name: name.into(),
            username: username.into(),
            icon_file: PathBuf::from(env!("CARGO_MANIFEST_DIR"))
                .join("tests/fixtures/")
                .join(icon_file)
                .display()
                .to_string(),
        }
    }
}

#[zbus::interface(name = "org.freedesktop.Accounts.User")]
impl UserFixture {
    #[zbus(property)]
    async fn real_name(&self) -> &str {
        &self.name
    }
    #[zbus(property)]
    async fn user_name(&self) -> &str {
        &self.username
    }
    #[zbus(property)]
    async fn icon_file(&self) -> &str {
        &self.icon_file
    }
}

pub async fn run_accounts_fixture(
    connection: zbus::Connection,
    num_users: Option<u32>,
) -> anyhow::Result<()> {
    connection
        .object_server()
        .at("/org/freedesktop/Accounts", AccountsFixture { num_users })
        .await
        .context("failed to serve org.freedesktop.Accounts")?;
    connection
        .object_server()
        .at(
            "/org/freedesktop/Accounts/agx",
            UserFixture::new("Guido", "agx", "guido.png"),
        )
        .await
        .context("failed to serve org.freedesktop.Accounts.User")?;
    connection
        .object_server()
        .at(
            "/org/freedesktop/Accounts/phoshi",
            UserFixture::new("Phoshi", "phoshi", "phoshi.png"),
        )
        .await
        .context("failed to serve org.freedesktop.Accounts.User")?;
    connection
        .object_server()
        .at(
            "/org/freedesktop/Accounts/sam",
            UserFixture::new("Sam", "samcday", "samcday.jpeg"),
        )
        .await
        .context("failed to serve org.freedesktop.Accounts.User")?;
    connection
        .request_name("org.freedesktop.Accounts")
        .await
        .context("failed to request name")?;
    Ok(())
}