fritz-sim 0.1.1

Stateful fake TR-064 server built from recorded FRITZ!Box fixtures, for tests (unofficial, not affiliated with FRITZ! GmbH)
Documentation
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Stateful fake TR-064 server, built from recorded fixtures. Used by tests
//! only: every write test runs against it, never against a real device.
//!
//! [`Sim::start`] serves a [`Fixture`] on the loopback interface: the device
//! description and the service descriptions without authentication, and
//! the action calls with HTTP Digest authentication.
//!
//! - Calls are checked against the service description: an unknown action
//!   fails with UPnP error 401, missing or unknown arguments with 402, and
//!   values that don't fit the data type or the allowed values with 600.
//!   Like the devices, it doesn't check the allowed range.
//! - Getters answer as recorded. Getters with an index answer the recorded
//!   entry, or 713 if there is none.
//! - Every argument refers to a state variable. Calls of other actions, like
//!   `SetEnable`, write their input arguments into these variables, and the
//!   getters that read the same variables answer with the new values.
//! - Paths to list documents, like the one `X_AVM-DE_GetHostListPath`
//!   returns, hold a session ID, and the documents are served for it.
//! - The web interface has a session login, `/login_sid.lua`, with a
//!   PBKDF2 challenge, and serves the documents of the REST API, like
//!   `/api/v0/smarthome/overview`, for the open session.
//! - Tests can [inject](Sim::inject) UPnP errors, delays, discarded writes
//!   and a locked login, and read the [requests](Sim::requests) the server
//!   got.
//!
//! ```no_run
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! use fritz_sim::{Fixture, Injection, Sim};
//!
//! let sim = Sim::start(Fixture::bundled("fritzbox-7690/8.25")?).await?;
//! let client = sim.client().build()?;
//! let description = client.description().await?;
//! let wlan = description.service("WLANConfiguration1")?;
//! client.call(wlan, "SetEnable", &[("NewEnable", "0")]).await?;
//! assert_eq!(sim.state("WLANConfiguration1", "Enable").as_deref(), Some("0"));
//!
//! sim.inject(Injection::fault("WLANConfiguration1", "SetEnable", 866));
//! assert!(client.call(wlan, "SetEnable", &[("NewEnable", "1")]).await.is_err());
//! # Ok(())
//! # }
//! ```

use std::future::IntoFuture as _;
use std::io;
use std::net::{Ipv4Addr, SocketAddr};
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::Duration;

use axum::Router;
use axum::extract::State;
use axum::http::header::{AUTHORIZATION, CONTENT_TYPE, WWW_AUTHENTICATE};
use axum::http::{HeaderMap, Method, StatusCode, Uri};
use axum::response::{IntoResponse, Response};
use tokio::task::JoinHandle;
use tr064::{ClientBuilder, Fingerprint, SecretString, Service, Url};

mod auth;
mod device;
mod fixture;
mod soap;
mod tls;
mod web;

use device::{Device, Fault, Hooks, short_id};
pub use fixture::{Answer, Error, Fixture};
use tls::TlsListener;
use web::Web;

/// The user name the simulator accepts by default.
pub const USERNAME: &str = "fritz-sim";
/// The password the simulator accepts by default.
pub const PASSWORD: &str = "fritz-sim-password";

const XML: &str = r#"text/xml; charset="utf-8""#;

/// How the simulator listens and whom it lets in.
#[derive(Debug, Clone)]
pub struct Options {
    /// Port 0 picks a free port.
    pub address: SocketAddr,
    pub username: String,
    pub password: String,
    /// Serve HTTPS with a new self-signed certificate, see
    /// [`Sim::fingerprint`].
    pub https: bool,
}

impl Default for Options {
    /// A free port on the loopback interface, plain HTTP, with [`USERNAME`]
    /// and [`PASSWORD`].
    fn default() -> Self {
        Self {
            address: SocketAddr::from((Ipv4Addr::LOCALHOST, 0)),
            username: USERNAME.to_owned(),
            password: PASSWORD.to_owned(),
            https: false,
        }
    }
}

/// A change to how the simulator behaves, until
/// [cleared](Sim::clear_injections).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Injection {
    /// Calls of an action fail with a UPnP error once their arguments are
    /// valid. `service` is any reference [`Service::matches`] accepts, like
    /// `WLANConfiguration1`.
    Fault {
        service: String,
        action: String,
        code: u16,
    },
    /// Every response is sent after a delay, to make clients time out.
    Delay(Duration),
    /// Writes succeed, but change nothing.
    DiscardWrites,
    /// The account is locked: every login fails, even with the right
    /// password.
    LockLogin,
}

impl Injection {
    /// Calls of `action` of `service` fail with the UPnP error `code`.
    pub fn fault(service: impl Into<String>, action: impl Into<String>, code: u16) -> Self {
        Self::Fault {
            service: service.into(),
            action: action.into(),
            code,
        }
    }
}

/// A request the simulator got.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Request {
    pub method: String,
    pub path: String,
    /// The action, for action calls.
    pub action: Option<String>,
    /// The input arguments, for action calls.
    pub arguments: Vec<(String, String)>,
    /// Whether the request carried valid credentials.
    pub authorized: bool,
}

/// A running simulator. It stops when dropped.
#[derive(Debug)]
pub struct Sim {
    url: Url,
    fingerprint: Option<Fingerprint>,
    shared: Arc<Shared>,
    server: JoinHandle<io::Result<()>>,
}

impl Sim {
    /// Serves `fixture` on a free port of the loopback interface, with
    /// [`USERNAME`] and [`PASSWORD`].
    ///
    /// # Errors
    ///
    /// The errors of binding the port.
    pub async fn start(fixture: Fixture) -> io::Result<Self> {
        Self::start_with(fixture, Options::default()).await
    }

    /// Serves `fixture` as configured.
    ///
    /// # Errors
    ///
    /// The errors of binding the address.
    pub async fn start_with(fixture: Fixture, options: Options) -> io::Result<Self> {
        let listener = tokio::net::TcpListener::bind(options.address).await?;
        let scheme = if options.https { "https" } else { "http" };
        let url = Url::parse(&format!("{scheme}://{}", listener.local_addr()?))
            .map_err(io::Error::other)?;
        let https = options.https;
        let shared = Arc::new(Shared {
            options,
            inner: Mutex::new(Inner {
                device: Device::new(fixture),
                web: Web::default(),
                injections: Vec::new(),
                requests: Vec::new(),
            }),
        });
        let app = Router::new()
            .fallback(handle)
            .with_state(Arc::clone(&shared));
        let (server, fingerprint) = if https {
            let (listener, fingerprint) = TlsListener::new(listener);
            let server = tokio::spawn(axum::serve(listener, app).into_future());
            (server, Some(fingerprint))
        } else {
            (tokio::spawn(axum::serve(listener, app).into_future()), None)
        };
        Ok(Self {
            url,
            fingerprint,
            shared,
            server,
        })
    }

    /// The base URL, like `http://127.0.0.1:49000`.
    pub fn url(&self) -> &Url {
        &self.url
    }

    /// The fingerprint of the certificate, if the simulator serves HTTPS.
    pub fn fingerprint(&self) -> Option<Fingerprint> {
        self.fingerprint
    }

    /// A client builder for the simulator, with its credentials and, for
    /// HTTPS, its certificate pinned.
    pub fn client(&self) -> ClientBuilder {
        let options = &self.shared.options;
        let builder = tr064::Client::builder(self.url.clone()).credentials(
            options.username.clone(),
            SecretString::from(options.password.clone()),
        );
        match self.fingerprint {
            Some(fingerprint) => builder.pin(fingerprint),
            None => builder,
        }
    }

    /// Changes how the simulator behaves from the next request on.
    pub fn inject(&self, injection: Injection) {
        self.shared.lock().injections.push(injection);
    }

    /// Removes all injections.
    pub fn clear_injections(&self) {
        self.shared.lock().injections.clear();
    }

    /// The requests the simulator got so far, oldest first.
    pub fn requests(&self) -> Vec<Request> {
        self.shared.lock().requests.clone()
    }

    /// The value of a state variable of a service, like `Enable` of
    /// `WLANConfiguration1`. `service` is any reference
    /// [`Service::matches`] accepts.
    pub fn state(&self, service: &str, variable: &str) -> Option<String> {
        let inner = self.shared.lock();
        let service = inner.device.fixture.description.service(service).ok()?;
        inner
            .device
            .state(short_id(service), variable)
            .map(str::to_owned)
    }
}

impl Drop for Sim {
    fn drop(&mut self) {
        self.server.abort();
    }
}

#[derive(Debug)]
struct Shared {
    options: Options,
    inner: Mutex<Inner>,
}

#[derive(Debug)]
struct Inner {
    device: Device,
    web: Web,
    injections: Vec<Injection>,
    requests: Vec<Request>,
}

impl Shared {
    fn lock(&self) -> MutexGuard<'_, Inner> {
        self.inner.lock().unwrap_or_else(PoisonError::into_inner)
    }

    /// Answers a request, and the delay before the answer.
    fn respond(
        &self,
        method: &Method,
        uri: &Uri,
        headers: &HeaderMap,
        body: &str,
    ) -> (Response, Duration) {
        let mut inner = self.lock();
        let path = uri.path();
        let call = (method == Method::POST)
            .then(|| soap::parse(body))
            .flatten();
        let locked = inner.injections.contains(&Injection::LockLogin);
        let authorized = !locked
            && auth::verify(
                headers.get(AUTHORIZATION).and_then(|v| v.to_str().ok()),
                &auth::Expected {
                    username: &self.options.username,
                    password: &self.options.password,
                    nonce: NONCE,
                    method: method.as_str(),
                    uri: uri.path_and_query().map_or(path, |p| p.as_str()),
                },
            );
        inner.requests.push(Request {
            method: method.to_string(),
            path: path.to_owned(),
            action: call.as_ref().map(|c| c.action.clone()),
            arguments: call.as_ref().map_or_default(|c| c.arguments.clone()),
            authorized,
        });
        let delay = inner
            .injections
            .iter()
            .map(|i| match i {
                Injection::Delay(delay) => *delay,
                _ => Duration::ZERO,
            })
            .sum();
        let response = match *method {
            _ if path == web::LOGIN => inner.web.login(method, body, &self.options, locked),
            _ if path.starts_with(web::API) => {
                let Inner { web, device, .. } = &*inner;
                web.api(&device.fixture.api, method, path, headers)
            }
            Method::GET => inner.document(path, uri.query()),
            Method::POST if !authorized => (
                StatusCode::UNAUTHORIZED,
                [(WWW_AUTHENTICATE, auth::challenge(NONCE))],
            )
                .into_response(),
            Method::POST => inner.control(path, headers, call),
            _ => StatusCode::METHOD_NOT_ALLOWED.into_response(),
        };
        (response, delay)
    }
}

/// The nonce of every challenge. Clients may reuse it as long as they
/// like.
const NONCE: &str = "F1A2B3C4D5E6F708";

/// The session ID in the paths of list documents, in place of the `***` the
/// fixture recorded.
const SESSION: &str = "5e55104d0c0ffee5";

impl Inner {
    /// The device description, a service description, or a list document
    /// for the [session](SESSION).
    fn document(&self, path: &str, query: Option<&str>) -> Response {
        let fixture = &self.device.fixture;
        let name = path.trim_start_matches('/');
        if let Some(list) = name
            .strip_suffix(".lua")
            .and_then(|stem| fixture.lists.get(stem))
        {
            let session = query
                .into_iter()
                .flat_map(|q| q.split('&'))
                .any(|p| p == format!("sid={SESSION}"));
            return if session {
                list.clone().into_response()
            } else {
                StatusCode::FORBIDDEN.into_response()
            };
        }
        let xml = match name {
            "tr64desc.xml" => Some(&fixture.description_xml),
            name => fixture.scpds.get(name).map(|(xml, _)| xml),
        };
        match xml {
            Some(xml) => ([(CONTENT_TYPE, XML)], xml.clone()).into_response(),
            None => StatusCode::NOT_FOUND.into_response(),
        }
    }

    /// Answers an authorized action call.
    fn control(&mut self, path: &str, headers: &HeaderMap, call: Option<soap::Call>) -> Response {
        let service = self
            .device
            .fixture
            .description
            .services()
            .find(|s| s.control_url == path)
            .cloned();
        let Some(service) = service else {
            return StatusCode::NOT_FOUND.into_response();
        };
        let Some(call) = call else {
            return StatusCode::BAD_REQUEST.into_response();
        };
        let soap_action = headers
            .get("SOAPAction")
            .and_then(|v| v.to_str().ok())
            .unwrap_or_default()
            .trim_matches('"');
        let expected = format!("{}#{}", service.service_type, call.action);
        let result = if soap_action == expected && call.service_type == service.service_type {
            let hooks = self.hooks(&service, &call.action);
            self.device
                .call(&service, &call.action, &call.arguments, &hooks)
        } else {
            Err(Fault::new(401))
        };
        match result {
            Ok(mut out) => {
                for (_, value) in &mut out {
                    *value = value.replace("sid=***", &format!("sid={SESSION}"));
                }
                let xml = soap::response(&service.service_type, &call.action, &out);
                ([(CONTENT_TYPE, XML)], xml).into_response()
            }
            Err(fault) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                [(CONTENT_TYPE, XML)],
                soap::fault(&fault),
            )
                .into_response(),
        }
    }

    fn hooks(&self, service: &Service, action: &str) -> Hooks {
        let fault = self.injections.iter().find_map(|i| match i {
            Injection::Fault {
                service: reference,
                action: name,
                code,
            } if name == action && service.matches(reference) => Some(Fault::new(*code)),
            _ => None,
        });
        Hooks {
            fault,
            discard_writes: self.injections.contains(&Injection::DiscardWrites),
        }
    }
}

async fn handle(
    State(shared): State<Arc<Shared>>,
    method: Method,
    uri: Uri,
    headers: HeaderMap,
    body: String,
) -> Response {
    let (response, delay) = shared.respond(&method, &uri, &headers, &body);
    tokio::time::sleep(delay).await;
    response
}

#[cfg(test)]
mod tests {
    use super::*;

    const FIXTURE: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/fixtures/fritzbox-7690/8.25");
    const WLAN: &str = "urn:dslforum-org:service:WLANConfiguration:1";
    const CONTROL: &str = "/upnp/control/wlanconfig1";

    fn inner() -> Inner {
        Inner {
            device: Device::new(Fixture::load(FIXTURE).unwrap()),
            web: Web::default(),
            injections: Vec::new(),
            requests: Vec::new(),
        }
    }

    /// A call of `GetInfo`.
    fn call(service_type: &str) -> soap::Call {
        soap::Call {
            service_type: service_type.to_owned(),
            action: "GetInfo".to_owned(),
            arguments: Vec::new(),
        }
    }

    fn soap_action(value: &str) -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert("SOAPAction", value.parse().unwrap());
        headers
    }

    #[test]
    fn answers_calls_that_match_the_service() {
        let headers = soap_action(&format!("\"{WLAN}#GetInfo\""));
        let response = inner().control(CONTROL, &headers, Some(call(WLAN)));
        assert_eq!(response.status(), StatusCode::OK);
    }

    #[test]
    fn rejects_calls_that_do_not_match_the_service() {
        let mut inner = inner();
        let right = soap_action(&format!("{WLAN}#GetInfo"));
        let wrong = soap_action(&format!("{WLAN}#SetEnable"));
        for (headers, service_type) in [
            (&wrong, WLAN),
            (&HeaderMap::new(), WLAN),
            (&right, "urn:other"),
        ] {
            let response = inner.control(CONTROL, headers, Some(call(service_type)));
            assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
        }
        let response = inner.control("/upnp/control/missing", &right, Some(call(WLAN)));
        assert_eq!(response.status(), StatusCode::NOT_FOUND);
        let response = inner.control(CONTROL, &right, None);
        assert_eq!(response.status(), StatusCode::BAD_REQUEST);
    }

    #[test]
    fn allows_only_get_and_post() {
        let shared = Shared {
            options: Options::default(),
            inner: Mutex::new(inner()),
        };
        let uri = Uri::from_static("/tr64desc.xml");
        let (response, delay) = shared.respond(&Method::PUT, &uri, &HeaderMap::new(), "");
        assert_eq!(response.status(), StatusCode::METHOD_NOT_ALLOWED);
        assert_eq!(delay, Duration::ZERO);
        assert_eq!(shared.lock().requests[0].method, "PUT");
    }
}