summaryrefslogtreecommitdiff
path: root/tests/controller_acceptance.rs
blob: 0d83cd1fcd3d3c92f348d7b20f0921f43b31072c (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use async_trait::async_trait;
use roentgen::controller::{Caller, Controller, Operation};
use roentgen::runtime::OfflineRuntime;
use roentgen::subscription::SubscriptionSource;

struct FixtureSource(String);
impl FixtureSource {
    fn new(value: &str) -> Self {
        Self(value.into())
    }
}
#[async_trait]
impl SubscriptionSource for FixtureSource {
    async fn fetch(&self, _: &str) -> anyhow::Result<Vec<u8>> {
        Ok(self.0.as_bytes().to_vec())
    }
}
use tempfile::TempDir;

#[tokio::test]
async fn importing_requires_explicit_selection_and_enabling() {
    let home = TempDir::new().unwrap();
    let mut controller = Controller::open(
        home.path(),
        Box::new(OfflineRuntime),
        Box::new(FixtureSource::new(
            "trojan://secret@example.com:443?security=tls&type=tcp#First\n",
        )),
    )
    .unwrap();
    let root = Caller::root();
    controller
        .execute(
            &root,
            Operation::AddSubscription {
                name: "Personal".into(),
                url: "https://provider.example/token".into(),
            },
        )
        .await
        .unwrap();
    assert!(
        controller
            .status()
            .unwrap()
            .selected_configuration
            .is_none()
    );
    assert!(!controller.status().unwrap().requested_enabled);
    let entries = controller.configurations(None).unwrap();
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].name, "First");
    assert_eq!(entries[0].status, "supported");
    assert!(controller.execute(&root, Operation::Enable).await.is_err());
    assert!(!controller.status().unwrap().requested_enabled);
}

#[derive(Clone)]
struct MutableSource(std::sync::Arc<std::sync::Mutex<Option<String>>>);
#[async_trait]
impl SubscriptionSource for MutableSource {
    async fn fetch(&self, _: &str) -> anyhow::Result<Vec<u8>> {
        self.0
            .lock()
            .unwrap()
            .as_ref()
            .map(|v| v.as_bytes().to_vec())
            .ok_or_else(|| anyhow::anyhow!("Fixture fetch failed"))
    }
}

struct GuardedRuntime {
    armed: bool,
    routing: &'static str,
}
#[async_trait]
impl roentgen::runtime::Runtime for GuardedRuntime {
    async fn arm(&mut self) -> anyhow::Result<()> {
        self.armed = true;
        self.routing = "blocked";
        Ok(())
    }
    async fn apply(
        &mut self,
        connection: Option<&roentgen::model::Trojan>,
        _: u64,
    ) -> anyhow::Result<()> {
        anyhow::ensure!(self.armed, "Attempted unprotected activation");
        self.routing = if connection.is_some() {
            "tun"
        } else {
            "blocked"
        };
        Ok(())
    }
    async fn disable(&mut self) -> anyhow::Result<()> {
        self.armed = false;
        self.routing = "direct";
        Ok(())
    }
    fn routing(&self) -> &str {
        self.routing
    }
}

async fn fixture() -> (TempDir, Controller, MutableSource) {
    let home = TempDir::new().unwrap();
    let source = MutableSource(std::sync::Arc::new(std::sync::Mutex::new(Some(
        "trojan://secret@example.com:443?security=tls#First\n".into(),
    ))));
    let mut controller = Controller::open(
        home.path(),
        Box::new(GuardedRuntime {
            armed: false,
            routing: "direct",
        }),
        Box::new(source.clone()),
    )
    .unwrap();
    controller
        .execute(
            &Caller::root(),
            Operation::AddSubscription {
                name: "Personal".into(),
                url: "https://provider.example/token".into(),
            },
        )
        .await
        .unwrap();
    (home, controller, source)
}

#[tokio::test]
async fn losing_a_selection_keeps_enabled_intent_and_blocks_without_replacement() {
    let (_home, mut controller, source) = fixture().await;
    let id = controller.configurations(None).unwrap()[0].id.clone();
    controller
        .execute(&Caller::root(), Operation::Select { id })
        .await
        .unwrap();
    controller
        .execute(&Caller::root(), Operation::Enable)
        .await
        .unwrap();
    *source.0.lock().unwrap() = Some("trojan://another@other.example:443#Replacement\n".into());
    controller
        .execute(&Caller::root(), Operation::Refresh { name: None })
        .await
        .unwrap();
    let status = controller.status().unwrap();
    assert!(status.requested_enabled);
    assert!(status.selected_configuration.is_none());
    assert_eq!(status.routing, "blocked");
}

#[tokio::test]
async fn failed_fetch_preserves_selection_but_an_empty_received_list_clears_it() {
    let (_home, mut controller, source) = fixture().await;
    let id = controller.configurations(None).unwrap()[0].id.clone();
    controller
        .execute(&Caller::root(), Operation::Select { id: id.clone() })
        .await
        .unwrap();
    *source.0.lock().unwrap() = None;
    assert!(
        controller
            .execute(&Caller::root(), Operation::Refresh { name: None })
            .await
            .is_err()
    );
    assert_eq!(
        controller
            .status()
            .unwrap()
            .selected_configuration
            .as_deref(),
        Some(id.as_str())
    );
    *source.0.lock().unwrap() = Some(String::new());
    controller
        .execute(&Caller::root(), Operation::Refresh { name: None })
        .await
        .unwrap();
    assert!(
        controller
            .status()
            .unwrap()
            .selected_configuration
            .is_none()
    );
    assert!(controller.configurations(None).unwrap().is_empty());
}

#[tokio::test]
async fn mixed_base64_import_keeps_all_rows_and_prohibits_invalid_selection() {
    use base64::Engine;
    let (_home, mut controller, source) = fixture().await;
    let body = "trojan://secret@example.com:443#Good\nvless://other@example.com:443#Unsupported\ntrojan://missing-port@host#Broken\n";
    *source.0.lock().unwrap() = Some(base64::engine::general_purpose::STANDARD.encode(body));
    controller
        .execute(&Caller::root(), Operation::Refresh { name: None })
        .await
        .unwrap();
    let entries = controller.configurations(None).unwrap();
    assert_eq!(
        entries
            .iter()
            .map(|e| e.status.as_str())
            .collect::<Vec<_>>(),
        ["supported", "unsupported", "invalid"]
    );
    for entry in entries.iter().skip(1) {
        assert!(
            controller
                .execute(
                    &Caller::root(),
                    Operation::Select {
                        id: entry.id.clone()
                    }
                )
                .await
                .is_err()
        );
    }
    let public = serde_json::to_string(&entries).unwrap();
    assert!(!public.contains("secret"));
    assert!(!public.contains("missing-port"));
}

#[tokio::test]
async fn denied_operations_leave_durable_state_unchanged() {
    let (_home, mut controller, _source) = fixture().await;
    let before = serde_json::to_string(&controller.status().unwrap()).unwrap();
    assert!(
        controller
            .execute(
                &Caller::denied(),
                Operation::DeleteSubscription {
                    name: "Personal".into()
                }
            )
            .await
            .is_err()
    );
    assert_eq!(
        serde_json::to_string(&controller.status().unwrap()).unwrap(),
        before
    );
    assert_eq!(controller.subscriptions().len(), 1);
}

#[tokio::test]
async fn explicit_unset_and_deletion_never_disable_or_select_another_entry() {
    let (_home, mut controller, _source) = fixture().await;
    let id = controller.configurations(None).unwrap()[0].id.clone();
    controller
        .execute(&Caller::root(), Operation::Select { id: id.clone() })
        .await
        .unwrap();
    controller
        .execute(&Caller::root(), Operation::Enable)
        .await
        .unwrap();
    controller
        .execute(&Caller::root(), Operation::Unset)
        .await
        .unwrap();
    assert!(controller.status().unwrap().requested_enabled);
    assert_eq!(controller.status().unwrap().routing, "blocked");
    assert_eq!(controller.configurations(None).unwrap().len(), 1);
    controller
        .execute(&Caller::root(), Operation::Select { id })
        .await
        .unwrap();
    controller
        .execute(
            &Caller::root(),
            Operation::DeleteSubscription {
                name: "Personal".into(),
            },
        )
        .await
        .unwrap();
    assert!(controller.status().unwrap().requested_enabled);
    assert!(
        controller
            .status()
            .unwrap()
            .selected_configuration
            .is_none()
    );
    assert_eq!(controller.status().unwrap().routing, "blocked");
}

#[tokio::test]
async fn persisted_intent_restores_under_protection_after_controller_restart() {
    let (home, mut controller, source) = fixture().await;
    let id = controller.configurations(None).unwrap()[0].id.clone();
    controller
        .execute(&Caller::root(), Operation::Select { id: id.clone() })
        .await
        .unwrap();
    controller
        .execute(&Caller::root(), Operation::Enable)
        .await
        .unwrap();
    drop(controller);
    let mut restored = Controller::open(
        home.path(),
        Box::new(GuardedRuntime {
            armed: false,
            routing: "unknown",
        }),
        Box::new(source),
    )
    .unwrap();
    restored.restore().await.unwrap();
    assert_eq!(restored.status().unwrap().selected_configuration, Some(id));
    assert!(restored.status().unwrap().requested_enabled);
    assert_eq!(restored.status().unwrap().routing, "tun");
}

#[tokio::test]
async fn label_changes_keep_identity_but_duplicate_candidates_clear_selection() {
    let (_home, mut controller, source) = fixture().await;
    let id = controller.configurations(None).unwrap()[0].id.clone();
    controller
        .execute(&Caller::root(), Operation::Select { id: id.clone() })
        .await
        .unwrap();
    *source.0.lock().unwrap() = Some("trojan://secret@example.com:443#Renamed\n".into());
    controller
        .execute(&Caller::root(), Operation::Refresh { name: None })
        .await
        .unwrap();
    assert_eq!(
        controller.status().unwrap().selected_configuration,
        Some(id)
    );
    *source.0.lock().unwrap() = Some(
        "trojan://secret@example.com:443#Duplicate1\ntrojan://secret@example.com:443#Duplicate2\n"
            .into(),
    );
    controller
        .execute(&Caller::root(), Operation::Refresh { name: None })
        .await
        .unwrap();
    assert!(
        controller
            .status()
            .unwrap()
            .selected_configuration
            .is_none()
    );
}