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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Supporting types for the `simctl privacy` subcommand.

use super::{Device, Result, Validate};

/// Refers to a specific service that an app needs to have permission for to
/// access.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PrivacyService {
    /// Wildcard that includes all services.
    All,

    /// Grants access to a user's calendar.
    Calendar,

    /// Grants limited access to a user's contacts.
    ContactsLimited,

    /// Grants access to a user's contacts.
    Contacts,

    /// Grants access to a user's location when an app is active.
    Location,

    /// Grants access to a user's location even if the app is in background.
    LocationAlways,

    /// Grants access to adding photos to the user's photo library.
    PhotosAdd,

    /// Grants access to reading photos from the user's photo library.
    Photos,

    /// Grants access to the user's media library (i.e. music and videos).
    MediaLibrary,

    /// Grants access to the user's microphone (which will most likely be the
    /// microphone of the Mac that the simulator runs on).
    Microphone,

    /// Grants access to the user's motion sensors.
    Motion,

    /// Grants access to the user's reminders.
    Reminders,

    /// Grants access to Siri.
    Siri,
}

impl ToString for PrivacyService {
    fn to_string(&self) -> String {
        match self {
            PrivacyService::All => "all",
            PrivacyService::Calendar => "calendar",
            PrivacyService::ContactsLimited => "contacts-limited",
            PrivacyService::Contacts => "contacts",
            PrivacyService::Location => "location",
            PrivacyService::LocationAlways => "location-always",
            PrivacyService::PhotosAdd => "photos-add",
            PrivacyService::Photos => "photos",
            PrivacyService::MediaLibrary => "media-library",
            PrivacyService::Microphone => "microphone",
            PrivacyService::Motion => "motion",
            PrivacyService::Reminders => "reminders",
            PrivacyService::Siri => "siri",
        }
        .to_owned()
    }
}

/// Wrapper around the `simctl privacy` subcommand.
pub struct Privacy {
    device: Device,
}

impl Device {
    /// Returns a wrapper around the `simctl privacy` subcommand.
    pub fn privacy(&self) -> Privacy {
        Privacy {
            device: self.clone(),
        }
    }
}

impl Privacy {
    /// Grants access to the given service to an application with the given
    /// bundle ID.
    pub fn grant(&self, service: PrivacyService, bundle_id: &str) -> Result<()> {
        self.device
            .simctl()
            .command("privacy")
            .arg(&self.device.udid)
            .arg("grant")
            .arg(service.to_string())
            .arg(bundle_id)
            .output()?
            .validate()
    }

    /// Revokes access to the given service from an application with the given
    /// bundle ID.
    pub fn revoke(&self, service: PrivacyService, bundle_id: &str) -> Result<()> {
        self.device
            .simctl()
            .command("privacy")
            .arg(&self.device.udid)
            .arg("revoke")
            .arg(service.to_string())
            .arg(bundle_id)
            .output()?
            .validate()
    }

    /// Resets access to the given service from an application with the given
    /// bundle ID. This will cause the OS to ask again when this app requests
    /// permission to use the given service.
    pub fn reset(&self, service: PrivacyService, bundle_id: &str) -> Result<()> {
        self.device
            .simctl()
            .command("privacy")
            .arg(&self.device.udid)
            .arg("reset")
            .arg(service.to_string())
            .arg(bundle_id)
            .output()?
            .validate()
    }

    /// Resets access to the given service from all applications running on the
    /// device.
    pub fn reset_all(&self, service: PrivacyService) -> Result<()> {
        self.device
            .simctl()
            .command("privacy")
            .arg(&self.device.udid)
            .arg("reset")
            .arg(service.to_string())
            .output()?
            .validate()
    }
}

#[cfg(test)]
mod tests {
    use serial_test::serial;

    use super::*;
    use crate::mock;

    #[test]
    #[serial]
    fn test_privacy_grant() -> Result<()> {
        mock::device()?.boot()?;
        mock::device()?
            .privacy()
            .grant(PrivacyService::Location, "com.apple.Maps")?;
        mock::device()?.shutdown()?;

        Ok(())
    }

    #[test]
    #[serial]
    fn test_privacy_revoke() -> Result<()> {
        mock::device()?.boot()?;
        mock::device()?
            .privacy()
            .revoke(PrivacyService::Location, "com.apple.Maps")?;
        mock::device()?.shutdown()?;

        Ok(())
    }

    #[test]
    #[serial]
    fn test_privacy_reset() -> Result<()> {
        mock::device()?.boot()?;
        mock::device()?
            .privacy()
            .grant(PrivacyService::Location, "com.apple.Maps")?;
        mock::device()?
            .privacy()
            .reset(PrivacyService::Location, "com.apple.Maps")?;
        mock::device()?.shutdown()?;

        Ok(())
    }

    #[test]
    #[serial]
    fn test_privacy_reset_all() -> Result<()> {
        mock::device()?.boot()?;
        mock::device()?
            .privacy()
            .grant(PrivacyService::Location, "com.apple.Maps")?;
        mock::device()?
            .privacy()
            .reset_all(PrivacyService::Location)?;
        mock::device()?.shutdown()?;

        Ok(())
    }
}