1
//! Configuration for the Arti command line application
2
//
3
// (This module is called `cfg` to avoid name clash with the `config` crate, which we use.)
4

            
5
use derive_deftly::Deftly;
6
use tor_config_path::CfgPath;
7

            
8
#[cfg(feature = "onion-service-service")]
9
use crate::onion_proxy::{
10
    OnionServiceProxyConfigBuilder, OnionServiceProxyConfigMap, OnionServiceProxyConfigMapBuilder,
11
};
12
#[cfg(feature = "rpc")]
13
semipublic_use! {
14
    use crate::rpc::{
15
        RpcConfig, RpcConfigBuilder,
16
        listener::{RpcListenerSetConfig, RpcListenerSetConfigBuilder},
17
    };
18
}
19
use arti_client::TorClientConfig;
20
#[cfg(feature = "onion-service-service")]
21
use tor_config::define_list_builder_accessors;
22
use tor_config::derive::prelude::*;
23
pub(crate) use tor_config::{ConfigBuildError, Listen};
24

            
25
use crate::{LoggingConfig, LoggingConfigBuilder};
26

            
27
/// Example file demonstrating our configuration and the default options.
28
///
29
/// The options in this example file are all commented out;
30
/// the actual defaults are done via builder attributes in all the Rust config structs.
31
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
32
pub(crate) const ARTI_EXAMPLE_CONFIG: &str = concat!(include_str!("./arti-example-config.toml"));
33

            
34
/// Test case file for the oldest version of the config we still support.
35
///
36
/// (When updating, copy `arti-example-config.toml` from the earliest version we want to
37
/// be compatible with.)
38
//
39
// Probably, in the long run, we will want to make this architecture more general: we'll want
40
// to have a larger number of examples to test, and we won't want to write a separate constant
41
// for each. Probably in that case, we'll want a directory of test examples, and we'll want to
42
// traverse the whole directory.
43
//
44
// Compare C tor, look at conf_examples and conf_failures - each of the subdirectories there is
45
// an example configuration situation that we wanted to validate.
46
//
47
// NB here in Arti the OLDEST_SUPPORTED_CONFIG and the ARTI_EXAMPLE_CONFIG are tested
48
// somewhat differently: we test that the current example is *exhaustive*, not just
49
// parsable.
50
#[cfg(test)]
51
const OLDEST_SUPPORTED_CONFIG: &str = concat!(include_str!("./oldest-supported-config.toml"),);
52

            
53
/// Replacement for rpc config when the rpc feature is disabled.
54
#[cfg(not(feature = "rpc"))]
55
type RpcConfig = ();
56

            
57
/// Replacement for onion service config when the onion service feature is disabled.
58
#[cfg(not(feature = "onion-service-service"))]
59
type OnionServiceProxyConfigMap = ();
60

            
61
/// Structure to hold our application configuration options
62
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
63
#[derive_deftly(TorConfig)]
64
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
65
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
66
pub(crate) struct ApplicationConfig {
67
    /// If true, we should watch our configuration files for changes, and reload
68
    /// our configuration when they change.
69
    ///
70
    /// Note that this feature may behave in unexpected ways if the path to the
71
    /// directory holding our configuration files changes its identity (because
72
    /// an intermediate symlink is changed, because the directory is removed and
73
    /// recreated, or for some other reason).
74
    #[deftly(tor_config(default))]
75
    pub(crate) watch_configuration: bool,
76

            
77
    /// If true, we should allow other applications not owned by the system
78
    /// administrator to monitor the Arti application and inspect its memory.
79
    ///
80
    /// Otherwise, we take various steps (including disabling core dumps) to
81
    /// make it harder for other programs to view our internal state.
82
    ///
83
    /// This option has no effect when arti is built without the `harden`
84
    /// feature.  When `harden` is not enabled, debugger attachment is permitted
85
    /// whether this option is set or not.
86
    #[deftly(tor_config(default))]
87
    pub(crate) permit_debugging: bool,
88

            
89
    /// If true, then we do not exit when we are running as `root`.
90
    ///
91
    /// This has no effect on Windows.
92
    #[deftly(tor_config(default))]
93
    pub(crate) allow_running_as_root: bool,
94
}
95

            
96
/// Configuration for one or more proxy listeners.
97
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
98
#[derive_deftly(TorConfig)]
99
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
100
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
101
pub(crate) struct ProxyConfig {
102
    /// Addresses to listen on for incoming SOCKS connections.
103
    //
104
    // TODO: Once http-connect is non-experimental, we should rename this option in a backward-compatible way.
105
    #[deftly(tor_config(default = "Listen::new_localhost(9150)"))]
106
    pub(crate) socks_listen: Listen,
107

            
108
    /// Addresses to listen on for incoming DNS connections.
109
    #[deftly(tor_config(default = "Listen::new_none()"))]
110
    pub(crate) dns_listen: Listen,
111
}
112

            
113
/// Configuration for arti-specific storage locations.
114
///
115
/// See also [`arti_client::config::StorageConfig`].
116
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
117
#[derive_deftly(TorConfig)]
118
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
119
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
120
pub(crate) struct ArtiStorageConfig {
121
    /// A file in which to write information about the ports we're listening on.
122
    #[deftly(tor_config(setter(into), default = "default_port_info_file()"))]
123
    pub(crate) port_info_file: CfgPath,
124
}
125

            
126
/// Return the default ports_info_file location.
127
380
fn default_port_info_file() -> CfgPath {
128
380
    CfgPath::new("${ARTI_LOCAL_DATA}/public/port_info.json".to_owned())
129
380
}
130

            
131
/// Configuration for system resources used by Tor.
132
///
133
/// You cannot change *these variables* in this section on a running Arti client.
134
///
135
/// Note that there are other settings in this section,
136
/// in [`arti_client::config::SystemConfig`].
137
//
138
// These two structs exist because:
139
//
140
//  1. Our doctrine is that configuration structs live with the code that uses the info.
141
//  2. tor-memquota's configuration is used by the MemoryQuotaTracker in TorClient
142
//  3. File descriptor limits are enforced here in arti because it's done process-global
143
//  4. Nevertheless, logically, these things want to be in the same section of the file.
144
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
145
#[derive_deftly(TorConfig)]
146
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
147
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
148
#[non_exhaustive]
149
pub(crate) struct SystemConfig {
150
    /// Maximum number of file descriptors we should launch with
151
    #[deftly(tor_config(setter(into), default = "default_max_files()"))]
152
    pub(crate) max_files: u64,
153
}
154

            
155
/// Return the default maximum number of file descriptors to launch with.
156
378
fn default_max_files() -> u64 {
157
378
    16384
158
378
}
159

            
160
/// Structure to hold Arti's configuration options, whether from a
161
/// configuration file or the command line.
162
//
163
/// These options are declared in a public crate outside of `arti` so that other
164
/// applications can parse and use them, if desired.  If you're only embedding
165
/// arti via `arti-client`, and you don't want to use Arti's configuration
166
/// format, use [`arti_client::TorClientConfig`] instead.
167
///
168
/// By default, Arti will run using the default Tor network, store state and
169
/// cache information to a per-user set of directories shared by all
170
/// that user's applications, and run a SOCKS client on a local port.
171
///
172
/// NOTE: These are NOT the final options or their final layout. Expect NO
173
/// stability here.
174
#[derive(Debug, Deftly, Clone, Eq, PartialEq)]
175
#[derive_deftly(TorConfig)]
176
#[deftly(tor_config(post_build = "Self::post_build"))]
177
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
178
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
179
pub(crate) struct ArtiConfig {
180
    /// Configuration for application behavior.
181
    #[deftly(tor_config(sub_builder))]
182
    application: ApplicationConfig,
183

            
184
    /// Configuration for proxy listeners
185
    #[deftly(tor_config(sub_builder))]
186
    proxy: ProxyConfig,
187

            
188
    /// Logging configuration
189
    #[deftly(tor_config(sub_builder))]
190
    logging: LoggingConfig,
191

            
192
    /// Metrics configuration
193
    #[deftly(tor_config(sub_builder))]
194
    pub(crate) metrics: MetricsConfig,
195

            
196
    /// Configuration for RPC subsystem
197
    #[deftly(tor_config(
198
        sub_builder,
199
        cfg = r#" feature = "rpc" "#,
200
        cfg_desc = "with RPC support"
201
    ))]
202
    pub(crate) rpc: RpcConfig,
203

            
204
    /// Information on system resources used by Arti.
205
    ///
206
    /// Note that there are other settings in this section,
207
    /// in [`arti_client::config::SystemConfig`] -
208
    /// these two structs overlay here.
209
    #[deftly(tor_config(sub_builder))]
210
    pub(crate) system: SystemConfig,
211

            
212
    /// Information on where things are stored by Arti.
213
    ///
214
    /// Note that [`TorClientConfig`] also has a storage configuration;
215
    /// our configuration logic should merge them correctly.
216
    #[deftly(tor_config(sub_builder))]
217
    pub(crate) storage: ArtiStorageConfig,
218

            
219
    /// Configured list of proxied onion services.
220
    ///
221
    /// Note that this field is present unconditionally, but when onion service
222
    /// support is disabled, it is replaced with a stub type from
223
    /// `onion_proxy_disabled`, and its setter functions are not implemented.
224
    /// The purpose of this stub type is to give an error if somebody tries to
225
    /// configure onion services when the `onion-service-service` feature is
226
    /// disabled.
227
    #[deftly(tor_config(
228
        setter(skip),
229
        sub_builder,
230
        cfg = r#" feature = "onion-service-service" "#,
231
        cfg_reject,
232
        cfg_desc = "with onion service support"
233
    ))]
234
    pub(crate) onion_services: OnionServiceProxyConfigMap,
235
}
236

            
237
impl ArtiConfigBuilder {
238
    /// validate the [`ArtiConfig`] after building.
239
    #[allow(clippy::unnecessary_wraps)]
240
378
    fn post_build(config: ArtiConfig) -> Result<ArtiConfig, ConfigBuildError> {
241
        #[cfg_attr(not(feature = "onion-service-service"), allow(unused_mut))]
242
378
        let mut config = config;
243
        #[cfg(feature = "onion-service-service")]
244
378
        for svc in config.onion_services.values_mut() {
245
170
            // Pass the application-level watch_configuration to each restricted discovery config.
246
170
            *svc.svc_cfg
247
170
                .restricted_discovery_mut()
248
170
                .watch_configuration_mut() = config.application.watch_configuration;
249
170
        }
250

            
251
378
        Ok(config)
252
378
    }
253
}
254

            
255
impl tor_config::load::TopLevel for ArtiConfig {
256
    type Builder = ArtiConfigBuilder;
257
    // Some config options such as "proxy.socks_port" are no longer
258
    // just "deprecated" and have since been completely removed from Arti,
259
    // but there's no harm in informing the user that the options are still deprecated.
260
    // For these removed options, Arti will ignore them like it does for all unknown options.
261
    const DEPRECATED_KEYS: &'static [&'static str] = &["proxy.socks_port", "proxy.dns_port"];
262
}
263

            
264
#[cfg(feature = "onion-service-service")]
265
define_list_builder_accessors! {
266
    struct ArtiConfigBuilder {
267
        pub(crate) onion_services: [OnionServiceProxyConfigBuilder],
268
    }
269
}
270

            
271
/// Convenience alias for the config for a whole `arti` program
272
///
273
/// Used primarily as a type parameter on calls to [`tor_config::resolve`]
274
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
275
pub(crate) type ArtiCombinedConfig = (ArtiConfig, TorClientConfig);
276

            
277
/// Configuration for exporting metrics (eg, perf data)
278
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
279
#[derive_deftly(TorConfig)]
280
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
281
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
282
pub(crate) struct MetricsConfig {
283
    /// Where to listen for incoming HTTP connections.
284
    #[deftly(tor_config(sub_builder))]
285
    pub(crate) prometheus: PrometheusConfig,
286
}
287

            
288
/// Configuration for one or more proxy listeners.
289
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
290
#[derive_deftly(TorConfig)]
291
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
292
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
293
pub(crate) struct PrometheusConfig {
294
    /// Port on which to establish a Prometheus scrape endpoint
295
    ///
296
    /// We listen here for incoming HTTP connections.
297
    ///
298
    /// If just a port is provided, we don't support IPv6.
299
    /// Alternatively, (only) a single address and port can be specified.
300
    /// These restrictions are due to upstream limitations:
301
    /// <https://github.com/metrics-rs/metrics/issues/567>.
302
    #[deftly(tor_config(default))]
303
    pub(crate) listen: Listen,
304
}
305

            
306
impl ArtiConfig {
307
    /// Return the [`ApplicationConfig`] for this configuration.
308
620
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
309
620
    pub(crate) fn application(&self) -> &ApplicationConfig {
310
620
        &self.application
311
620
    }
312

            
313
    /// Return the [`LoggingConfig`] for this configuration.
314
308
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
315
308
    pub(crate) fn logging(&self) -> &LoggingConfig {
316
308
        &self.logging
317
308
    }
318

            
319
    /// Return the [`ProxyConfig`] for this configuration.
320
2
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
321
2
    pub(crate) fn proxy(&self) -> &ProxyConfig {
322
2
        &self.proxy
323
2
    }
324

            
325
    /// Return the [`ArtiStorageConfig`] for this configuration.
326
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
327
    ///
328
    pub(crate) fn storage(&self) -> &ArtiStorageConfig {
329
        &self.storage
330
    }
331

            
332
    /// Return the [`RpcConfig`] for this configuration.
333
    #[cfg(feature = "rpc")]
334
4
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
335
4
    pub(crate) fn rpc(&self) -> &RpcConfig {
336
4
        &self.rpc
337
4
    }
338
}
339

            
340
#[cfg(test)]
341
mod test {
342
    // @@ begin test lint list maintained by maint/add_warning @@
343
    #![allow(clippy::bool_assert_comparison)]
344
    #![allow(clippy::clone_on_copy)]
345
    #![allow(clippy::dbg_macro)]
346
    #![allow(clippy::mixed_attributes_style)]
347
    #![allow(clippy::print_stderr)]
348
    #![allow(clippy::print_stdout)]
349
    #![allow(clippy::single_char_pattern)]
350
    #![allow(clippy::unwrap_used)]
351
    #![allow(clippy::unchecked_time_subtraction)]
352
    #![allow(clippy::useless_vec)]
353
    #![allow(clippy::needless_pass_by_value)]
354
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
355
    // TODO add this next lint to maint/add_warning, for all tests
356
    #![allow(clippy::iter_overeager_cloned)]
357
    // Saves adding many individual #[cfg], or a sub-module
358
    #![cfg_attr(not(feature = "pt-client"), allow(dead_code))]
359

            
360
    use arti_client::config::TorClientConfigBuilder;
361
    use arti_client::config::dir;
362
    use itertools::{EitherOrBoth, Itertools, chain};
363
    use regex::Regex;
364
    use std::collections::HashSet;
365
    use std::fmt::Write as _;
366
    use std::iter;
367
    use std::time::Duration;
368
    use tor_config::load::{ConfigResolveError, ResolutionResults};
369
    use tor_config_path::CfgPath;
370

            
371
    #[allow(unused_imports)] // depends on features
372
    use tor_error::ErrorReport as _;
373

            
374
    #[cfg(feature = "restricted-discovery")]
375
    use {
376
        arti_client::HsClientDescEncKey,
377
        std::str::FromStr as _,
378
        tor_hsservice::config::restricted_discovery::{
379
            DirectoryKeyProviderBuilder, HsClientNickname,
380
        },
381
    };
382

            
383
    use super::*;
384

            
385
    //---------- tests that rely on the provided example config file ----------
386
    //
387
    // These are quite complex.  They uncomment the file, parse bits of it,
388
    // and do tests via serde and via the normal config machinery,
389
    // to see that everything is documented as expected.
390

            
391
    fn uncomment_example_settings(template: &str) -> String {
392
        let re = Regex::new(r#"(?m)^\#([^ \n])"#).unwrap();
393
        re.replace_all(template, |cap: &regex::Captures<'_>| -> _ {
394
            cap.get(1).unwrap().as_str().to_string()
395
        })
396
        .into()
397
    }
398

            
399
    /// Is this key present or absent in the examples in one of the example files ?
400
    ///
401
    /// Depending on which variable this is in, it refers to presence in other the
402
    /// old or the new example file.
403
    ///
404
    /// This type is *not* used in declarations in `declared_config_exceptions`;
405
    /// it is used by the actual checking code.
406
    /// The declarations use types in that function.
407
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
408
    enum InExample {
409
        Absent,
410
        Present,
411
    }
412
    /// Which of the two example files?
413
    ///
414
    /// This type is *not* used in declarations in `declared_config_exceptions`;
415
    /// it is used by the actual checking code.
416
    /// The declarations use types in that function.
417
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
418
    enum WhichExample {
419
        Old,
420
        New,
421
    }
422
    /// An exception to the usual expectations about configuration example files
423
    ///
424
    /// This type is *not* used in declarations in `declared_config_exceptions`;
425
    /// it is used by the actual checking code.
426
    /// The declarations use types in that function.
427
    #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
428
    struct ConfigException {
429
        /// The actual config key
430
        key: String,
431
        /// Does it appear in the oldest supported example file?
432
        in_old_example: InExample,
433
        /// Does it appear in the current example file?
434
        in_new_example: InExample,
435
        /// Does our code recognise it ?  `None` means "don't know"
436
        in_code: Option<bool>,
437
    }
438
    impl ConfigException {
439
        fn in_example(&self, which: WhichExample) -> InExample {
440
            use WhichExample::*;
441
            match which {
442
                Old => self.in_old_example,
443
                New => self.in_new_example,
444
            }
445
        }
446
    }
447

            
448
    /// *every* feature that's listed as `InCode::FeatureDependent`
449
    const ALL_RELEVANT_FEATURES_ENABLED: bool = cfg!(all(
450
        feature = "bridge-client",
451
        feature = "pt-client",
452
        feature = "onion-service-client",
453
        feature = "rpc",
454
    ));
455

            
456
    /// Return the expected exceptions to the usual expectations about config and examples
457
    fn declared_config_exceptions() -> Vec<ConfigException> {
458
        /// Is this key recognised by the parsing code ?
459
        ///
460
        /// (This can be feature-dependent, so literal values of this type
461
        /// are often feature-qualified.)
462
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
463
        enum InCode {
464
            /// No configuration of this codebase knows about this option
465
            Ignored,
466
            /// *Some* configuration of this codebase know about this option
467
            ///
468
            /// This means:
469
            ///   - If *every* feature in `ALL_RELEVANT_FEATURES_ENABLED` is enabled,
470
            ///     the config key is expected to be `Recognised`
471
            ///   - Otherwise we're not sure (because cargo features are additive,
472
            ///     dependency crates' features might be *en*abled willy-nilly).
473
            FeatureDependent,
474
            /// All configurations of this codebase know about this option
475
            Recognized,
476
        }
477
        use InCode::*;
478

            
479
        /// Marker.  `Some(InOld)` means presence of this config key in the oldest-supported file
480
        struct InOld;
481
        /// Marker.  `Some(InNew)` means presence of this config key in the current example file
482
        struct InNew;
483

            
484
        let mut out = vec![];
485

            
486
        // Declare some keys which aren't "normal", eg they aren't documented in the usual
487
        // way, are configurable, aren't in the oldest supported file, etc.
488
        //
489
        // `in_old_example` and `in_new_example` are whether the key appears in
490
        // `arti-example-config.toml` and `oldest-supported-config.toml` respectively.
491
        // (in each case, only a line like `#example.key = ...` counts.)
492
        //
493
        // `whether_supported` tells is if the key is supposed to be
494
        // recognised by the code.
495
        //
496
        // `keys` is the list of keys.  Add a // comment at the start of the list
497
        // so that rustfmt retains the consistent formatting.
498
        let mut declare_exceptions = |in_old_example: Option<InOld>,
499
                                      in_new_example: Option<InNew>,
500
                                      in_code: InCode,
501
                                      keys: &[&str]| {
502
            let in_code = match in_code {
503
                Ignored => Some(false),
504
                Recognized => Some(true),
505
                FeatureDependent if ALL_RELEVANT_FEATURES_ENABLED => Some(true),
506
                FeatureDependent => None,
507
            };
508
            #[allow(clippy::needless_pass_by_value)] // pass by value defends against a->a b->a
509
            fn in_example<T>(spec: Option<T>) -> InExample {
510
                match spec {
511
                    None => InExample::Absent,
512
                    Some(_) => InExample::Present,
513
                }
514
            }
515
            let in_old_example = in_example(in_old_example);
516
            let in_new_example = in_example(in_new_example);
517
            out.extend(keys.iter().cloned().map(|key| ConfigException {
518
                key: key.to_owned(),
519
                in_old_example,
520
                in_new_example,
521
                in_code,
522
            }));
523
        };
524

            
525
        declare_exceptions(
526
            None,
527
            Some(InNew),
528
            Recognized,
529
            &[
530
                // Keys that are newer than the oldest-supported example, but otherwise normal.
531
                "application.allow_running_as_root",
532
                "bridges",
533
                "logging.time_granularity",
534
                "path_rules.long_lived_ports",
535
                "use_obsolete_software",
536
                "circuit_timing.disused_circuit_timeout",
537
                "storage.port_info_file",
538
            ],
539
        );
540

            
541
        declare_exceptions(
542
            None,
543
            None,
544
            Recognized,
545
            &[
546
                // Examples exist but are not auto-testable
547
                "tor_network.authorities",
548
                "tor_network.fallback_caches",
549
            ],
550
        );
551

            
552
        declare_exceptions(
553
            None,
554
            None,
555
            Recognized,
556
            &[
557
                // Examples exist but are not auto-testable
558
                "logging.opentelemetry",
559
            ],
560
        );
561

            
562
        declare_exceptions(
563
            Some(InOld),
564
            Some(InNew),
565
            if cfg!(target_family = "windows") {
566
                Ignored
567
            } else {
568
                Recognized
569
            },
570
            &[
571
                // Unix-only mistrust settings
572
                "storage.permissions.trust_group",
573
                "storage.permissions.trust_user",
574
            ],
575
        );
576

            
577
        declare_exceptions(
578
            None,
579
            None, // TODO: Make examples for bridges settings!
580
            FeatureDependent,
581
            &[
582
                // Settings only available with bridge support
583
                "bridges.transports", // we recognise this so we can reject it
584
            ],
585
        );
586

            
587
        declare_exceptions(
588
            None,
589
            Some(InNew),
590
            FeatureDependent,
591
            &[
592
                // Settings only available with experimental-api support
593
                "storage.keystore",
594
            ],
595
        );
596

            
597
        declare_exceptions(
598
            None,
599
            None, // it's there, but not formatted for auto-testing
600
            FeatureDependent,
601
            &[
602
                // Settings only available with tokio-console support
603
                "logging.tokio_console",
604
                "logging.tokio_console.enabled",
605
            ],
606
        );
607

            
608
        declare_exceptions(
609
            None,
610
            None, // it's there, but not formatted for auto-testing
611
            Recognized,
612
            &[
613
                // Memory quota, tested by fn memquota (below)
614
                "system.memory",
615
                "system.memory.max",
616
                "system.memory.low_water",
617
            ],
618
        );
619

            
620
        declare_exceptions(
621
            None,
622
            Some(InNew), // The top-level section is in the new file (only).
623
            Recognized,
624
            &["metrics"],
625
        );
626

            
627
        declare_exceptions(
628
            None,
629
            None, // The inner information is not formatted for auto-testing
630
            Recognized,
631
            &[
632
                // Prometheus metrics exporter, tested by fn metrics (below)
633
                "metrics.prometheus",
634
                "metrics.prometheus.listen",
635
            ],
636
        );
637

            
638
        declare_exceptions(
639
            None,
640
            Some(InNew),
641
            FeatureDependent,
642
            &[
643
                // PT-only settings
644
            ],
645
        );
646

            
647
        declare_exceptions(
648
            None,
649
            Some(InNew),
650
            FeatureDependent,
651
            &[
652
                // HS client settings
653
                "address_filter.allow_onion_addrs",
654
                "circuit_timing.hs_desc_fetch_attempts",
655
                "circuit_timing.hs_intro_rend_attempts",
656
            ],
657
        );
658

            
659
        declare_exceptions(
660
            None,
661
            None, // TODO RPC, these should actually appear in the example config
662
            FeatureDependent,
663
            &[
664
                // RPC-only settings
665
                "rpc",
666
                "rpc.rpc_listen",
667
            ],
668
        );
669

            
670
        // These are commented-out by default, and tested with test::onion_services().
671
        declare_exceptions(
672
            None,
673
            None,
674
            FeatureDependent,
675
            &[
676
                // onion-service only settings.
677
                "onion_services",
678
            ],
679
        );
680

            
681
        declare_exceptions(
682
            None,
683
            Some(InNew),
684
            FeatureDependent,
685
            &[
686
                // Vanguards-specific settings
687
                "vanguards",
688
                "vanguards.mode",
689
            ],
690
        );
691

            
692
        // These are commented-out by default
693
        declare_exceptions(
694
            None,
695
            None,
696
            FeatureDependent,
697
            &[
698
                "storage.keystore.ctor",
699
                "storage.keystore.ctor.services",
700
                "storage.keystore.ctor.clients",
701
            ],
702
        );
703

            
704
        out.sort();
705

            
706
        let dupes = out.iter().map(|exc| &exc.key).duplicates().collect_vec();
707
        assert!(
708
            dupes.is_empty(),
709
            "duplicate exceptions in configuration {dupes:?}"
710
        );
711

            
712
        eprintln!(
713
            "declared config exceptions for this configuration:\n{:#?}",
714
            &out
715
        );
716
        out
717
    }
718

            
719
    #[test]
720
    fn default_config() {
721
        use InExample::*;
722

            
723
        let empty_config = tor_config::ConfigurationSources::new_empty()
724
            .load()
725
            .unwrap();
726
        let empty_config: ArtiCombinedConfig = tor_config::resolve(empty_config).unwrap();
727

            
728
        let default = (ArtiConfig::default(), TorClientConfig::default());
729
        let exceptions = declared_config_exceptions();
730

            
731
        /// Helper to decide what to do about a possible discrepancy
732
        ///
733
        /// Provided with `EitherOrBoth` of:
734
        ///   - the config key that the config parser reported it found, but didn't recognise
735
        ///   - the declared exception entry
736
        ///     (for the same config key)
737
        ///
738
        /// Decides whether this is something that should fail the test.
739
        /// If so it returns `Err((key, error_message))`, otherwise `Ok`.
740
        #[allow(clippy::needless_pass_by_value)] // clippy is IMO wrong about eob
741
        fn analyse_joined_info(
742
            which: WhichExample,
743
            uncommented: bool,
744
            eob: EitherOrBoth<&String, &ConfigException>,
745
        ) -> Result<(), (String, String)> {
746
            use EitherOrBoth::*;
747
            let (key, err) = match eob {
748
                // Unrecognised entry, no exception
749
                Left(found) => (found, "found in example but not processed".into()),
750
                Both(found, exc) => {
751
                    let but = match (exc.in_example(which), exc.in_code, uncommented) {
752
                        (Absent, _, _) => "but exception entry expected key to be absent",
753
                        (_, _, false) => "when processing still-commented-out file!",
754
                        (_, Some(true), _) => {
755
                            "but an exception entry says it should have been recognised"
756
                        }
757
                        (Present, Some(false), true) => return Ok(()), // that's as expected
758
                        (Present, None, true) => return Ok(()), // that's could be as expected
759
                    };
760
                    (
761
                        found,
762
                        format!("parser reported unrecognised config key, {but}"),
763
                    )
764
                }
765
                Right(exc) => {
766
                    // An exception entry exists.  The actual situation is either
767
                    //   - not found in file (so no "unrecognised" report)
768
                    //   - processed successfully (found in file and in code)
769
                    // but we don't know which.
770
                    let trouble = match (exc.in_example(which), exc.in_code, uncommented) {
771
                        (Absent, _, _) => return Ok(()), // not in file, no report expected
772
                        (_, _, false) => return Ok(()),  // not uncommented, no report expected
773
                        (_, Some(true), _) => return Ok(()), // code likes it, no report expected
774
                        (Present, Some(false), true) => {
775
                            "expected an 'unknown config key' report but didn't see one"
776
                        }
777
                        (Present, None, true) => return Ok(()), // not sure, have to just allow it
778
                    };
779
                    (&exc.key, trouble.into())
780
                }
781
            };
782
            Err((key.clone(), err))
783
        }
784

            
785
        let parses_to_defaults = |example: &str, which: WhichExample, uncommented: bool| {
786
            let cfg = {
787
                let mut sources = tor_config::ConfigurationSources::new_empty();
788
                sources.push_source(
789
                    tor_config::ConfigurationSource::from_verbatim(example.to_string()),
790
                    tor_config::sources::MustRead::MustRead,
791
                );
792
                sources.load().unwrap()
793
            };
794

            
795
            // This tests that the example settings do not *contradict* the defaults.
796
            let results: ResolutionResults<ArtiCombinedConfig> =
797
                tor_config::resolve_return_results(cfg).unwrap();
798

            
799
            assert_eq!(&results.value, &default, "{which:?} {uncommented:?}");
800
            assert_eq!(&results.value, &empty_config, "{which:?} {uncommented:?}");
801

            
802
            // We serialize the DisfavouredKey entries to strings to compare them against
803
            // `known_unrecognized_options`.
804
            let unrecognized = results
805
                .unrecognized
806
                .iter()
807
                .map(|k| k.to_string())
808
                .collect_vec();
809

            
810
            eprintln!(
811
                "parsing of {which:?} uncommented={uncommented:?}, unrecognized={unrecognized:#?}"
812
            );
813

            
814
            let reports =
815
                Itertools::merge_join_by(unrecognized.iter(), exceptions.iter(), |u, e| {
816
                    u.as_str().cmp(&e.key)
817
                })
818
                .filter_map(|eob| analyse_joined_info(which, uncommented, eob).err())
819
                .collect_vec();
820

            
821
            if !reports.is_empty() {
822
                let reports = reports.iter().fold(String::new(), |mut out, (k, s)| {
823
                    writeln!(out, "  {}: {}", s, k).unwrap();
824
                    out
825
                });
826

            
827
                panic!(
828
                    r"
829
mismatch: results of parsing example files (& vs declared exceptions):
830
example config file {which:?}, uncommented={uncommented:?}
831
{reports}
832
"
833
                );
834
            }
835

            
836
            results.value
837
        };
838

            
839
        let _ = parses_to_defaults(ARTI_EXAMPLE_CONFIG, WhichExample::New, false);
840
        let _ = parses_to_defaults(OLDEST_SUPPORTED_CONFIG, WhichExample::Old, false);
841

            
842
        let built_default = (
843
            ArtiConfigBuilder::default().build().unwrap(),
844
            TorClientConfigBuilder::default().build().unwrap(),
845
        );
846

            
847
        let parsed = parses_to_defaults(
848
            &uncomment_example_settings(ARTI_EXAMPLE_CONFIG),
849
            WhichExample::New,
850
            true,
851
        );
852
        let parsed_old = parses_to_defaults(
853
            &uncomment_example_settings(OLDEST_SUPPORTED_CONFIG),
854
            WhichExample::Old,
855
            true,
856
        );
857

            
858
        assert_eq!(&parsed, &built_default);
859
        assert_eq!(&parsed_old, &built_default);
860

            
861
        assert_eq!(&default, &built_default);
862
    }
863

            
864
    /// Config file exhaustiveness and default checking
865
    ///
866
    /// `example_file` is a putative configuration file text.
867
    /// It is expected to contain "example lines",
868
    /// which are lines in start with `#` *not followed by whitespace*.
869
    ///
870
    /// This function checks that:
871
    ///
872
    /// Positive check on the example lines that are present.
873
    ///  * `example_file`, when example lines are uncommented, can be parsed.
874
    ///  * The example values are the same as the default values.
875
    ///
876
    /// Check for missing examples:
877
    ///  * Every key `in `TorClientConfig` or `ArtiConfig` has a corresponding example value.
878
    ///  * Except as declared in [`declared_config_exceptions`]
879
    ///  * And also, tolerating absence in the example files of `deprecated` keys
880
    ///
881
    /// It handles straightforward cases, where the example line is in a `[section]`
882
    /// and is something like `#key = value`.
883
    ///
884
    /// More complex keys, eg those which don't appear in "example lines" starting with just `#`,
885
    /// must be dealt with ad-hoc and mentioned in `declared_config_exceptions`.
886
    ///
887
    /// For complex config keys, it may not be sufficient to simply write the default value in
888
    /// the example files (along with perhaps some other information).  In that case,
889
    ///   1. Write a bespoke example (with lines starting `# `) in the config file.
890
    ///   2. Write a bespoke test, to test the parsing of the bespoke example.
891
    ///      This will probably involve using `ExampleSectionLines` and may be quite ad-hoc.
892
    ///      The test function bridges(), below, is a complex worked example.
893
    ///   3. Either add a trivial example for the affected key(s) (starting with just `#`)
894
    ///      or add the affected key(s) to `declared_config_exceptions`
895
    fn exhaustive_1(example_file: &str, which: WhichExample, deprecated: &[String]) {
896
        use InExample::*;
897
        use serde_json::Value as JsValue;
898
        use std::collections::BTreeSet;
899

            
900
        let example = uncomment_example_settings(example_file);
901
        let example: toml::Value = toml::from_str(&example).unwrap();
902
        // dbg!(&example);
903
        let example = serde_json::to_value(example).unwrap();
904
        // dbg!(&example);
905

            
906
        // "Exhaustive" taxonomy of the recognized configuration keys
907
        //
908
        // We use the JSON serialization of the default builders, because Rust's toml
909
        // implementation likes to omit more things, that we want to see.
910
        //
911
        // I'm not sure this is quite perfect but it is pretty good,
912
        // and has found a number of un-exampled config keys.
913
        let exhausts = [
914
            serde_json::to_value(TorClientConfig::builder()).unwrap(),
915
            serde_json::to_value(ArtiConfig::builder()).unwrap(),
916
        ];
917

            
918
        /// This code does *not* record a problem for keys *in* the example file
919
        /// that are unrecognized.  That is handled by the `default_config` test.
920
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, derive_more::Display)]
921
        enum ProblemKind {
922
            #[display("recognised by serialisation, but missing from example config file")]
923
            MissingFromExample,
924
            #[display("expected that example config file should contain have this as a table")]
925
            ExpectedTableInExample,
926
            #[display(
927
                "declared exception says this key should be recognised but not in file, but that doesn't seem to be the case"
928
            )]
929
            UnusedException,
930
        }
931

            
932
        #[derive(Default, Debug)]
933
        struct Walk {
934
            current_path: Vec<String>,
935
            problems: Vec<(String, ProblemKind)>,
936
        }
937

            
938
        impl Walk {
939
            /// Records a problem
940
            fn bad(&mut self, kind: ProblemKind) {
941
                self.problems.push((self.current_path.join("."), kind));
942
            }
943

            
944
            /// Recurses, looking for problems
945
            ///
946
            /// Visited for every node in either or both of the starting `exhausts`.
947
            ///
948
            /// `E` is the number of elements in `exhausts`, ie the number of different
949
            /// top-level config types that Arti uses.  Ie, 2.
950
            fn walk<const E: usize>(
951
                &mut self,
952
                example: Option<&JsValue>,
953
                exhausts: [Option<&JsValue>; E],
954
            ) {
955
                assert! { exhausts.into_iter().any(|e| e.is_some()) }
956

            
957
                let example = if let Some(e) = example {
958
                    e
959
                } else {
960
                    self.bad(ProblemKind::MissingFromExample);
961
                    return;
962
                };
963

            
964
                let tables = exhausts.map(|e| e?.as_object());
965

            
966
                // Union of the keys of both exhausts' tables (insofar as they *are* tables)
967
                let table_keys = tables
968
                    .iter()
969
                    .flat_map(|t| t.map(|t| t.keys().cloned()).into_iter().flatten())
970
                    .collect::<BTreeSet<String>>();
971

            
972
                for key in table_keys {
973
                    let example = if let Some(e) = example.as_object() {
974
                        e
975
                    } else {
976
                        // At least one of the exhausts was a nonempty table,
977
                        // but the corresponding example node isn't a table.
978
                        self.bad(ProblemKind::ExpectedTableInExample);
979
                        continue;
980
                    };
981

            
982
                    // Descend the same key in all the places.
983
                    self.current_path.push(key.clone());
984
                    self.walk(example.get(&key), tables.map(|t| t?.get(&key)));
985
                    self.current_path.pop().unwrap();
986
                }
987
            }
988
        }
989

            
990
        let exhausts = exhausts.iter().map(Some).collect_vec().try_into().unwrap();
991

            
992
        let mut walk = Walk::default();
993
        walk.walk::<2>(Some(&example), exhausts);
994
        let mut problems = walk.problems;
995

            
996
        /// Marker present in `expect_missing` to say we *definitely* expect it
997
        #[derive(Debug, Copy, Clone)]
998
        struct DefinitelyRecognized;
999

            
        let expect_missing = declared_config_exceptions()
            .iter()
            .filter_map(|exc| {
                let definitely = match (exc.in_example(which), exc.in_code) {
                    (Present, _) => return None, // in file, don't expect "non-exhaustive" notice
                    (_, Some(false)) => return None, // code hasn't heard of it, likewise
                    (Absent, Some(true)) => Some(DefinitelyRecognized),
                    (Absent, None) => None, // allow this exception but don't mind if not known
                };
                Some((exc.key.clone(), definitely))
            })
            .collect_vec();
        dbg!(&expect_missing);
        // Things might appear in expect_missing for different reasons, and sometimes
        // at different levels.  For example, `bridges.transports` is expected to be
        // missing because we document that a different way in the example; but
        // `bridges` is expected to be missing from the OLDEST_SUPPORTED_CONFIG,
        // because that config predates bridge support.
        //
        // When this happens, we need to remove `bridges.transports` in favour of
        // the over-arching `bridges`.
        let expect_missing: Vec<(String, Option<DefinitelyRecognized>)> = expect_missing
            .iter()
            .cloned()
            .filter({
                let original: HashSet<_> = expect_missing.iter().map(|(k, _)| k.clone()).collect();
                move |(found, _)| {
                    !found
                        .match_indices('.')
                        .any(|(doti, _)| original.contains(&found[0..doti]))
                }
            })
            .collect_vec();
        dbg!(&expect_missing);
        for (exp, definitely) in expect_missing {
            let was = problems.len();
            problems.retain(|(path, _)| path != &exp);
            if problems.len() == was && definitely.is_some() {
                problems.push((exp, ProblemKind::UnusedException));
            }
        }
        let problems = problems
            .into_iter()
            .filter(|(key, _kind)| !deprecated.iter().any(|dep| key == dep))
            .map(|(path, m)| format!("    config key {:?}: {}", path, m))
            .collect_vec();
        // If this assert fails, it might be because in `fn exhaustive`, below,
        // a newly-defined config item has not been added to the list for OLDEST_SUPPORTED_CONFIG.
        assert!(
            problems.is_empty(),
            "example config {which:?} exhaustiveness check failed: {}\n-----8<-----\n{}\n-----8<-----\n",
            problems.join("\n"),
            example_file,
        );
    }
    #[test]
    fn exhaustive() {
        let mut deprecated = vec![];
        <(ArtiConfig, TorClientConfig) as tor_config::load::Resolvable>::enumerate_deprecated_keys(
            &mut |l| {
                for k in l {
                    deprecated.push(k.to_string());
                }
            },
        );
        let deprecated = deprecated.iter().cloned().collect_vec();
        // Check that:
        //  - The primary example config file has good examples for everything
        //  - Except for deprecated config keys
        //  - (And, except for those that we never expect: CONFIG_KEYS_EXPECT_NO_EXAMPLE.)
        exhaustive_1(ARTI_EXAMPLE_CONFIG, WhichExample::New, &deprecated);
        // Check that:
        //  - That oldest supported example config file has good examples for everything
        //  - Except for keys that we have introduced since that file was written
        //  - (And, except for those that we never expect: CONFIG_KEYS_EXPECT_NO_EXAMPLE.)
        // We *tolerate* entries in this table that don't actually occur in the oldest-supported
        // example.  This avoids having to feature-annotate them.
        exhaustive_1(OLDEST_SUPPORTED_CONFIG, WhichExample::Old, &deprecated);
    }
    /// Check that the `Report` of `err` contains the string `exp`, and otherwise panic
    #[cfg_attr(feature = "pt-client", allow(dead_code))]
    fn expect_err_contains(err: ConfigResolveError, exp: &str) {
        use std::error::Error as StdError;
        let err: Box<dyn StdError> = Box::new(err);
        let err = tor_error::Report(err).to_string();
        assert!(
            err.contains(exp),
            "wrong message, got {:?}, exp {:?}",
            err,
            exp,
        );
    }
    #[test]
    fn bridges() {
        // We make assumptions about the contents of `arti-example-config.toml` !
        //
        // 1. There are nontrivial, non-default examples of `bridges.bridges`.
        // 2. These are in the `[bridges]` section, after a line `# For example:`
        // 3. There's precisely one ``` example, with conventional TOML formatting.
        // 4. There's precisely one [ ] example, with conventional TOML formatting.
        // 5. Both these examples specify the same set of bridges.
        // 6. There are three bridges.
        // 7. Lines starting with a digit or `[` are direct bridges; others are PT.
        //
        // Below, we annotate with `[1]` etc. where these assumptions are made.
        // Filter examples that we don't want to test in this configuration
        let filter_examples = |#[allow(unused_mut)] mut examples: ExampleSectionLines| -> _ {
            // [7], filter out the PTs
            if cfg!(all(feature = "bridge-client", not(feature = "pt-client"))) {
                let looks_like_addr =
                    |l: &str| l.starts_with(|c: char| c.is_ascii_digit() || c == '[');
                examples.lines.retain(|l| looks_like_addr(l));
            }
            examples
        };
        // Tests that one example parses, and returns what it parsed.
        // If bridge support is completely disabled, checks that this configuration
        // is rejected, as it should be, and returns a dummy value `((),)`
        // (so that the rest of the test has something to "compare that we parsed it the same").
        let resolve_examples = |examples: &ExampleSectionLines| {
            // [7], check that the PT bridge is properly rejected
            #[cfg(all(feature = "bridge-client", not(feature = "pt-client")))]
            {
                let err = examples.resolve::<TorClientConfig>().unwrap_err();
                expect_err_contains(err, "support disabled in cargo features");
            }
            let examples = filter_examples(examples.clone());
            #[cfg(feature = "bridge-client")]
            {
                examples.resolve::<TorClientConfig>().unwrap()
            }
            #[cfg(not(feature = "bridge-client"))]
            {
                let err = examples.resolve::<TorClientConfig>().unwrap_err();
                expect_err_contains(err, "support disabled in cargo features");
                // Use ((),) as the dummy unit value because () gives clippy conniptions
                ((),)
            }
        };
        // [1], [2], narrow to just the nontrivial, non-default, examples
        let mut examples = ExampleSectionLines::from_section("bridges");
        examples.narrow((r#"^# For example:"#, true), NARROW_NONE);
        let compare = {
            // [3], narrow to the multi-line string
            let mut examples = examples.clone();
            examples.narrow((r#"^#  bridges = '''"#, true), (r#"^#  '''"#, true));
            examples.uncomment();
            let parsed = resolve_examples(&examples);
            // Now we fish out the lines ourselves as a double-check
            // We must strip off the bridges = ''' and ''' lines.
            examples.lines.remove(0);
            examples.lines.remove(examples.lines.len() - 1);
            // [6], check we got the number of examples we expected
            examples.expect_lines(3);
            // If we have the bridge API, try parsing each line and using the API to insert it
            #[cfg(feature = "bridge-client")]
            {
                let examples = filter_examples(examples);
                let mut built = TorClientConfig::builder();
                for l in &examples.lines {
                    built.bridges().bridges().push(l.trim().parse().expect(l));
                }
                let built = built.build().unwrap();
                assert_eq!(&parsed, &built);
            }
            parsed
        };
        // [4], [5], narrow to the [ ] section, parse again, and compare
        {
            examples.narrow((r#"^#  bridges = \["#, true), (r#"^#  \]"#, true));
            examples.uncomment();
            let parsed = resolve_examples(&examples);
            assert_eq!(&parsed, &compare);
        }
    }
    #[test]
    fn transports() {
        // Extract and uncomment our transports lines.
        //
        // (They're everything from  `# An example managed pluggable transport`
        // through the start of the next
        // section.  They start with "#    ".)
        let mut file =
            ExampleSectionLines::from_markers("# An example managed pluggable transport", "[");
        file.lines.retain(|line| line.starts_with("#    "));
        file.uncomment();
        let result = file.resolve::<(TorClientConfig, ArtiConfig)>();
        let cfg_got = result.unwrap();
        #[cfg(feature = "pt-client")]
        {
            use arti_client::config::{BridgesConfig, pt::TransportConfig};
            use tor_config_path::CfgPath;
            let bridges_got: &BridgesConfig = cfg_got.0.as_ref();
            // Build the expected configuration.
            let mut bld = BridgesConfig::builder();
            {
                let mut b = TransportConfig::builder();
                b.protocols(vec!["obfs4".parse().unwrap(), "obfs5".parse().unwrap()]);
                b.path(CfgPath::new("/usr/bin/obfsproxy".to_string()));
                b.arguments(vec!["-obfs4".to_string(), "-obfs5".to_string()]);
                b.run_on_startup(true);
                bld.transports().push(b);
            }
            {
                let mut b = TransportConfig::builder();
                b.protocols(vec!["obfs4".parse().unwrap()]);
                b.proxy_addr("127.0.0.1:31337".parse().unwrap());
                bld.transports().push(b);
            }
            let bridges_expected = bld.build().unwrap();
            assert_eq!(&bridges_expected, bridges_got);
        }
    }
    #[test]
    fn memquota() {
        // Test that uncommenting the example generates a config
        // with tracking enabled, iff support is compiled in.
        let mut file = ExampleSectionLines::from_section("system");
        file.lines.retain(|line| line.starts_with("#    memory."));
        file.uncomment();
        let result = file.resolve_return_results::<(TorClientConfig, ArtiConfig)>();
        let result = result.unwrap();
        // Test that the example config doesn't have any unrecognised keys
        assert_eq!(result.unrecognized, []);
        assert_eq!(result.deprecated, []);
        let inner: &tor_memquota::testing::ConfigInner =
            result.value.0.system_memory().inner().unwrap();
        // Test that the example low_water is the default
        // value for the example max.
        let defaulted_low = tor_memquota::Config::builder()
            .max(*inner.max)
            .build()
            .unwrap();
        let inner_defaulted_low = defaulted_low.inner().unwrap();
        assert_eq!(inner, inner_defaulted_low);
    }
    #[test]
    fn metrics() {
        // Test that uncommenting the example generates a config with prometheus enabled.
        let mut file = ExampleSectionLines::from_section("metrics");
        file.lines
            .retain(|line| line.starts_with("#    prometheus."));
        file.uncomment();
        let result = file
            .resolve_return_results::<(TorClientConfig, ArtiConfig)>()
            .unwrap();
        // Test that the example config doesn't have any unrecognised keys
        assert_eq!(result.unrecognized, []);
        assert_eq!(result.deprecated, []);
        // Check that the example is as we expected
        assert_eq!(
            result
                .value
                .1
                .metrics
                .prometheus
                .listen
                .single_address_legacy()
                .unwrap(),
            Some("127.0.0.1:9035".parse().unwrap()),
        );
        // We don't test "compiled out but not used" here.
        // That case is handled in proxy.rs at startup time.
    }
    #[test]
    fn onion_services() {
        // Here we require that the onion services configuration is between a line labeled
        // with `##### ONION SERVICES` and a line labeled with `##### RPC`, and that each
        // line of _real_ configuration in that section begins with `#    `.
        let mut file = ExampleSectionLines::from_markers("##### ONION SERVICES", "##### RPC");
        file.lines.retain(|line| line.starts_with("#    "));
        file.uncomment();
        let result = file.resolve::<(TorClientConfig, ArtiConfig)>();
        #[cfg(feature = "onion-service-service")]
        {
            let svc_expected = {
                use tor_hsrproxy::config::*;
                let mut b = OnionServiceProxyConfigBuilder::default();
                b.service().nickname("allium-cepa".parse().unwrap());
                b.proxy().proxy_ports().push(ProxyRule::new(
                    ProxyPattern::one_port(80).unwrap(),
                    ProxyAction::Forward(
                        Encapsulation::Simple,
                        TargetAddr::Inet("127.0.0.1:10080".parse().unwrap()),
                    ),
                ));
                b.proxy().proxy_ports().push(ProxyRule::new(
                    ProxyPattern::one_port(22).unwrap(),
                    ProxyAction::DestroyCircuit,
                ));
                b.proxy().proxy_ports().push(ProxyRule::new(
                    ProxyPattern::one_port(265).unwrap(),
                    ProxyAction::IgnoreStream,
                ));
                /* TODO (#1246)
                b.proxy().proxy_ports().push(ProxyRule::new(
                    ProxyPattern::port_range(1, 1024).unwrap(),
                    ProxyAction::Forward(
                        Encapsulation::Simple,
                        TargetAddr::Unix("/var/run/allium-cepa/socket".into()),
                    ),
                ));
                */
                b.proxy().proxy_ports().push(ProxyRule::new(
                    ProxyPattern::one_port(443).unwrap(),
                    ProxyAction::RejectStream,
                ));
                b.proxy().proxy_ports().push(ProxyRule::new(
                    ProxyPattern::all_ports(),
                    ProxyAction::DestroyCircuit,
                ));
                #[cfg(feature = "restricted-discovery")]
                {
                    const ALICE_KEY: &str =
                        "descriptor:x25519:PU63REQUH4PP464E2Y7AVQ35HBB5DXDH5XEUVUNP3KCPNOXZGIBA";
                    const BOB_KEY: &str =
                        "descriptor:x25519:b5zqgtpermmuda6vc63lhjuf5ihpokjmuk26ly2xksf7vg52aesq";
                    for (nickname, key) in [("alice", ALICE_KEY), ("bob", BOB_KEY)] {
                        b.service()
                            .restricted_discovery()
                            .enabled(true)
                            .static_keys()
                            .access()
                            .push((
                                HsClientNickname::from_str(nickname).unwrap(),
                                HsClientDescEncKey::from_str(key).unwrap(),
                            ));
                    }
                    let mut dir = DirectoryKeyProviderBuilder::default();
                    dir.path(CfgPath::new(
                        "/var/lib/tor/hidden_service/authorized_clients".to_string(),
                    ));
                    b.service()
                        .restricted_discovery()
                        .key_dirs()
                        .access()
                        .push(dir);
                }
                b.build().unwrap()
            };
            cfg_if::cfg_if! {
                if #[cfg(feature = "restricted-discovery")] {
                    let cfg = result.unwrap();
                    let services = cfg.1.onion_services;
                    assert_eq!(services.len(), 1);
                    let svc = services.values().next().unwrap();
                    assert_eq!(svc, &svc_expected);
                } else {
                    expect_err_contains(
                        result.unwrap_err(),
                        "restricted_discovery.enabled=true, but restricted-discovery feature not enabled"
                    );
                }
            }
        }
        #[cfg(not(feature = "onion-service-service"))]
        {
            expect_err_contains(result.unwrap_err(), "not built with onion service support");
        }
    }
    #[cfg(feature = "rpc")]
    #[test]
    fn rpc_defaults() {
        let mut file = ExampleSectionLines::from_markers("##### RPC", "[");
        // This will get us all the RPC entries that correspond to our defaults.
        //
        // The examples that _aren't_ in our defaults have '#      ' at the start.
        file.lines
            .retain(|line| line.starts_with("#    ") && !line.starts_with("#      "));
        file.uncomment();
        let parsed = file
            .resolve_return_results::<(TorClientConfig, ArtiConfig)>()
            .unwrap();
        assert!(parsed.unrecognized.is_empty());
        assert!(parsed.deprecated.is_empty());
        let rpc_parsed: &RpcConfig = parsed.value.1.rpc();
        let rpc_default = RpcConfig::default();
        assert_eq!(rpc_parsed, &rpc_default);
    }
    #[cfg(feature = "rpc")]
    #[test]
    fn rpc_full() {
        use crate::rpc::listener::{ConnectPointOptionsBuilder, RpcListenerSetConfigBuilder};
        // This will get us all the RPC entries, including those that _don't_ correspond to our defaults.
        let mut file = ExampleSectionLines::from_markers("##### RPC", "[");
        // We skip the "file" item because it conflicts with "dir" and "file_options"
        file.lines
            .retain(|line| line.starts_with("#    ") && !line.contains("file ="));
        file.uncomment();
        let parsed = file
            .resolve_return_results::<(TorClientConfig, ArtiConfig)>()
            .unwrap();
        let rpc_parsed: &RpcConfig = parsed.value.1.rpc();
        let expected = {
            let mut bld_opts = ConnectPointOptionsBuilder::default();
            bld_opts.enable(false);
            let mut bld_set = RpcListenerSetConfigBuilder::default();
            bld_set.dir(CfgPath::new("${HOME}/.my_connect_files/".to_string()));
            bld_set.listener_options().enable(true);
            bld_set
                .file_options()
                .insert("bad_file.json".to_string(), bld_opts);
            let mut bld = RpcConfigBuilder::default();
            bld.listen().insert("label".to_string(), bld_set);
            bld.build().unwrap()
        };
        assert_eq!(&expected, rpc_parsed);
    }
    /// Helper for fishing out parts of the config file and uncommenting them.
    ///
    /// It represents a part of a configuration file.
    ///
    /// This can be used to find part of the config file by ad-hoc regexp matching,
    /// uncomment it, and parse it.  This is useful as part of a test to check
    /// that we can parse more complex config.
    #[derive(Debug, Clone)]
    struct ExampleSectionLines {
        /// The header for the section that we are parsing.  It is
        /// prepended to the lines before parsing them.
        section: String,
        /// The lines in the section.
        lines: Vec<String>,
    }
    /// A 2-tuple of a regular expression and a flag describing whether the line
    /// containing the expression should be included in the result of `narrow()`.
    type NarrowInstruction<'s> = (&'s str, bool);
    /// A NarrowInstruction that does not match anything.
    const NARROW_NONE: NarrowInstruction<'static> = ("?<none>", false);
    impl ExampleSectionLines {
        /// Construct a new `ExampleSectionLines` from `ARTI_EXAMPLE_CONFIG`, containing
        /// everything that starts with `[section]`, up to but not including the
        /// next line that begins with a `[`.
        fn from_section(section: &str) -> Self {
            Self::from_markers(format!("[{section}]"), "[")
        }
        /// Construct a new `ExampleSectionLines` from `ARTI_EXAMPLE_CONFIG`,
        /// containing everything that starts with `start`, up to but not
        /// including the next line that begins with `end`.
        ///
        /// If `start` is a configuration section header it will be put in the
        /// `section` field of the returned `ExampleSectionLines`, otherwise
        /// at the beginning of the `lines` field.
        ///
        /// `start` will be perceived as a configuration section header if it
        /// starts with `[` and ends with `]`.
        fn from_markers<S, E>(start: S, end: E) -> Self
        where
            S: AsRef<str>,
            E: AsRef<str>,
        {
            let (start, end) = (start.as_ref(), end.as_ref());
            let mut lines = ARTI_EXAMPLE_CONFIG
                .lines()
                .skip_while(|line| !line.starts_with(start))
                .peekable();
            let section = lines
                .next_if(|l0| l0.starts_with('['))
                .map(|section| section.to_owned())
                .unwrap_or_default();
            let lines = lines
                .take_while(|line| !line.starts_with(end))
                .map(|l| l.to_owned())
                .collect_vec();
            Self { section, lines }
        }
        /// Remove all lines from this section, except those between the (unique) line matching
        /// "start" and the next line matching "end" (or the end of the file).
        fn narrow(&mut self, start: NarrowInstruction, end: NarrowInstruction) {
            let find_index = |(re, include), start_pos, exactly_one: bool, adjust: [isize; 2]| {
                if (re, include) == NARROW_NONE {
                    return None;
                }
                let re = Regex::new(re).expect(re);
                let i = self
                    .lines
                    .iter()
                    .enumerate()
                    .skip(start_pos)
                    .filter(|(_, l)| re.is_match(l))
                    .map(|(i, _)| i);
                let i = if exactly_one {
                    i.clone().exactly_one().unwrap_or_else(|_| {
                        panic!("RE={:?} I={:#?} L={:#?}", re, i.collect_vec(), &self.lines)
                    })
                } else {
                    i.clone().next()?
                };
                let adjust = adjust[usize::from(include)];
                let i = (i as isize + adjust) as usize;
                Some(i)
            };
            eprint!("narrow {:?} {:?}: ", start, end);
            let start = find_index(start, 0, true, [1, 0]).unwrap_or(0);
            let end = find_index(end, start + 1, false, [0, 1]).unwrap_or(self.lines.len());
            eprintln!("{:?} {:?}", start, end);
            // don't tolerate empty
            assert!(start < end, "empty, from {:#?}", &self.lines);
            self.lines = self.lines.drain(..).take(end).skip(start).collect_vec();
        }
        /// Assert that this section contains exactly `n` lines.
        fn expect_lines(&self, n: usize) {
            assert_eq!(self.lines.len(), n);
        }
        /// Remove `#` from the start of every line that begins with it.
        fn uncomment(&mut self) {
            self.strip_prefix("#");
        }
        /// Remove `prefix` from the start of every line.
        ///
        /// If there are lines that *don't* start with `prefix`, crash.
        ///
        /// But, lines starting with `[` are left unchanged, in any case.
        /// (These are TOML section markers; changing them would change the TOML structure.)
        fn strip_prefix(&mut self, prefix: &str) {
            for l in &mut self.lines {
                if !l.starts_with('[') {
                    *l = l.strip_prefix(prefix).expect(l).to_string();
                }
            }
        }
        /// Join the parts of this object together into a single string.
        fn build_string(&self) -> String {
            chain!(iter::once(&self.section), self.lines.iter(),).join("\n")
        }
        /// Make a TOML document of this section and parse it as a complete configuration.
        /// Panic if the section cannot be parsed.
        fn parse(&self) -> tor_config::ConfigurationTree {
            let s = self.build_string();
            eprintln!("parsing\n  --\n{}\n  --", &s);
            let mut sources = tor_config::ConfigurationSources::new_empty();
            sources.push_source(
                tor_config::ConfigurationSource::from_verbatim(s.clone()),
                tor_config::sources::MustRead::MustRead,
            );
            sources.load().expect(&s)
        }
        fn resolve<R: tor_config::load::Resolvable>(&self) -> Result<R, ConfigResolveError> {
            tor_config::load::resolve(self.parse())
        }
        fn resolve_return_results<R: tor_config::load::Resolvable>(
            &self,
        ) -> Result<ResolutionResults<R>, ConfigResolveError> {
            tor_config::load::resolve_return_results(self.parse())
        }
    }
    // More normal config tests
    #[test]
    fn builder() {
        use tor_config_path::CfgPath;
        let sec = std::time::Duration::from_secs(1);
        let mut authorities = dir::AuthorityContacts::builder();
        authorities.v3idents().push([22; 20].into());
        let mut fallback = dir::FallbackDir::builder();
        fallback
            .rsa_identity([23; 20].into())
            .ed_identity([99; 32].into())
            .orports()
            .push("127.0.0.7:7".parse().unwrap());
        let mut bld = ArtiConfig::builder();
        let mut bld_tor = TorClientConfig::builder();
        bld.proxy().socks_listen(Listen::new_localhost(9999));
        bld.logging().console("warn");
        *bld_tor.tor_network().authorities() = authorities;
        bld_tor.tor_network().set_fallback_caches(vec![fallback]);
        bld_tor
            .storage()
            .cache_dir(CfgPath::new("/var/tmp/foo".to_owned()))
            .state_dir(CfgPath::new("/var/tmp/bar".to_owned()));
        bld_tor.download_schedule().retry_certs().attempts(10);
        bld_tor.download_schedule().retry_certs().initial_delay(sec);
        bld_tor.download_schedule().retry_certs().parallelism(3);
        bld_tor.download_schedule().retry_microdescs().attempts(30);
        bld_tor
            .download_schedule()
            .retry_microdescs()
            .initial_delay(10 * sec);
        bld_tor
            .download_schedule()
            .retry_microdescs()
            .parallelism(9);
        bld_tor
            .override_net_params()
            .insert("wombats-per-quokka".to_owned(), 7);
        bld_tor
            .path_rules()
            .ipv4_subnet_family_prefix(20)
            .ipv6_subnet_family_prefix(48);
        bld_tor.preemptive_circuits().disable_at_threshold(12);
        bld_tor
            .preemptive_circuits()
            .set_initial_predicted_ports(vec![80, 443]);
        bld_tor
            .preemptive_circuits()
            .prediction_lifetime(Duration::from_secs(3600))
            .min_exit_circs_for_port(2);
        bld_tor
            .circuit_timing()
            .max_dirtiness(90 * sec)
            .request_timeout(10 * sec)
            .request_max_retries(22)
            .request_loyalty(3600 * sec);
        bld_tor.address_filter().allow_local_addrs(true);
        let val = bld.build().unwrap();
        assert_ne!(val, ArtiConfig::default());
    }
    #[test]
    fn articonfig_application() {
        let config = ArtiConfig::default();
        let application = config.application();
        assert_eq!(&config.application, application);
    }
    #[test]
    fn articonfig_logging() {
        let config = ArtiConfig::default();
        let logging = config.logging();
        assert_eq!(&config.logging, logging);
    }
    #[test]
    fn articonfig_proxy() {
        let config = ArtiConfig::default();
        let proxy = config.proxy();
        assert_eq!(&config.proxy, proxy);
    }
    /// Comprehensive tests for `proxy.socks_listen` and `proxy.dns_listen`.
    ///
    /// The "this isn't set at all, just use the default" cases are tested elsewhere.
    fn ports_listen(
        f: &str,
        get_listen: &dyn Fn(&ArtiConfig) -> &Listen,
        bld_get_listen: &dyn Fn(&ArtiConfigBuilder) -> &Option<Listen>,
        setter_listen: &dyn Fn(&mut ArtiConfigBuilder, Listen) -> &mut ProxyConfigBuilder,
    ) {
        let from_toml = |s: &str| -> ArtiConfigBuilder {
            let cfg: toml::Value = toml::from_str(dbg!(s)).unwrap();
            let cfg: ArtiConfigBuilder = cfg.try_into().unwrap();
            cfg
        };
        let chk = |cfg: &ArtiConfigBuilder, expected: &Listen| {
            dbg!(bld_get_listen(cfg));
            let cfg = cfg.build().unwrap();
            assert_eq!(get_listen(&cfg), expected);
        };
        let check_setters = |port, expected: &_| {
            let cfg = ArtiConfig::builder();
            for listen in match port {
                None => vec![Listen::new_none(), Listen::new_localhost(0)],
                Some(port) => vec![Listen::new_localhost(port)],
            } {
                let mut cfg = cfg.clone();
                setter_listen(&mut cfg, dbg!(listen));
                chk(&cfg, expected);
            }
        };
        {
            let expected = Listen::new_localhost(100);
            let cfg = from_toml(&format!("proxy.{}_listen = 100", f));
            assert_eq!(bld_get_listen(&cfg), &Some(Listen::new_localhost(100)));
            chk(&cfg, &expected);
            check_setters(Some(100), &expected);
        }
        {
            let expected = Listen::new_none();
            let cfg = from_toml(&format!("proxy.{}_listen = 0", f));
            chk(&cfg, &expected);
            check_setters(None, &expected);
        }
    }
    #[test]
    fn ports_listen_socks() {
        ports_listen(
            "socks",
            &|cfg| &cfg.proxy.socks_listen,
            &|bld| &bld.proxy.socks_listen,
            &|bld, arg| bld.proxy.socks_listen(arg),
        );
    }
    #[test]
    fn ports_listen_dns() {
        ports_listen(
            "dns",
            &|cfg| &cfg.proxy.dns_listen,
            &|bld| &bld.proxy.dns_listen,
            &|bld, arg| bld.proxy.dns_listen(arg),
        );
    }
}