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 paste::paste;
6

            
7
use derive_builder::Builder;
8
use serde::{Deserialize, Serialize};
9

            
10
#[cfg(feature = "onion-service-service")]
11
use crate::onion_proxy::{
12
    OnionServiceProxyConfigBuilder, OnionServiceProxyConfigMap, OnionServiceProxyConfigMapBuilder,
13
};
14
#[cfg(not(feature = "onion-service-service"))]
15
use crate::onion_proxy_disabled::{OnionServiceProxyConfigMap, OnionServiceProxyConfigMapBuilder};
16
#[cfg(feature = "rpc")]
17
#[cfg_attr(docsrs, doc(cfg(feature = "rpc")))]
18
pub use crate::rpc::{RpcConfig, RpcConfigBuilder};
19
use arti_client::TorClientConfig;
20
#[cfg(feature = "onion-service-service")]
21
use tor_config::define_list_builder_accessors;
22
use tor_config::resolve_alternative_specs;
23
pub(crate) use tor_config::{impl_standard_builder, 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
pub const ARTI_EXAMPLE_CONFIG: &str = concat!(include_str!("./arti-example-config.toml"));
32

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

            
52
/// Structure to hold our application configuration options
53
228
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
54
#[builder(build_fn(error = "ConfigBuildError"))]
55
#[builder(derive(Debug, Serialize, Deserialize))]
56
pub struct ApplicationConfig {
57
    /// If true, we should watch our configuration files for changes, and reload
58
    /// our configuration when they change.
59
    ///
60
    /// Note that this feature may behave in unexpected ways if the path to the
61
    /// directory holding our configuration files changes its identity (because
62
    /// an intermediate symlink is changed, because the directory is removed and
63
    /// recreated, or for some other reason).
64
    #[builder(default)]
65
    pub(crate) watch_configuration: bool,
66

            
67
    /// If true, we should allow other applications not owned by the system
68
    /// administrator to monitor the Arti application and inspect its memory.
69
    ///
70
    /// Otherwise, we take various steps (including disabling core dumps) to
71
    /// make it harder for other programs to view our internal state.
72
    ///
73
    /// This option has no effect when arti is built without the `harden`
74
    /// feature.  When `harden` is not enabled, debugger attachment is permitted
75
    /// whether this option is set or not.
76
    #[builder(default)]
77
    pub(crate) permit_debugging: bool,
78

            
79
    /// If true, then we do not exit when we are running as `root`.
80
    ///
81
    /// This has no effect on Windows.
82
    #[builder(default)]
83
    pub(crate) allow_running_as_root: bool,
84
}
85
impl_standard_builder! { ApplicationConfig }
86

            
87
/// Resolves values from `$field_listen` and `$field_port` (compat) into a `Listen`
88
///
89
/// For `dns` and `proxy`.
90
///
91
/// Handles defaulting, and normalization, using `resolve_alternative_specs`
92
/// and `Listen::new_localhost_option`.
93
///
94
/// Broken out into a macro so as to avoid having to state the field name four times,
95
/// which is a recipe for programming slips.
96
///
97
/// NOTE: Don't use this for new ports options!
98
/// We only have to use it where we do because of the legacy `port` options.
99
/// For new ports, provide a listener only.
100
#[deprecated = "This macro is only for supporting old _port options! Don't use it for new options."]
101
macro_rules! resolve_listen_port {
102
    { $self:expr, $field:ident, $def_port:expr } => { paste!{
103
        resolve_alternative_specs(
104
            [
105
                (
106
                    concat!(stringify!($field), "_listen"),
107
                    $self.[<$field _listen>].clone(),
108
                ),
109
                (
110
                    concat!(stringify!($field), "_port"),
111
                    $self.[<$field _port>].map(Listen::new_localhost_optional),
112
                ),
113
            ],
114
304
            || Listen::new_localhost($def_port),
115
        )?
116
    } }
117
}
118

            
119
/// Configuration for one or more proxy listeners.
120
310
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
121
#[builder(build_fn(error = "ConfigBuildError"))]
122
#[builder(derive(Debug, Serialize, Deserialize))]
123
#[allow(clippy::option_option)] // Builder port fields: Some(None) = specified to disable
124
pub struct ProxyConfig {
125
    /// Addresses to listen on for incoming SOCKS connections.
126
    #[builder(field(build = r#"#[allow(deprecated)]
127
                   // We use this deprecated macro to instantiate the legacy socks_port option.
128
                   { resolve_listen_port!(self, socks, 9150) }
129
                 "#))]
130
    pub(crate) socks_listen: Listen,
131

            
132
    /// Port to listen on (at localhost) for incoming SOCKS connections.
133
    ///
134
    /// This field is deprecated, and will, eventually, be removed.
135
    /// Use `socks_listen` instead, which accepts the same values,
136
    /// but which will also be able to support more flexible listening in the future.
137
    #[builder(
138
        setter(strip_option),
139
        field(type = "Option<Option<u16>>", build = "()")
140
    )]
141
    #[builder_setter_attr(deprecated)]
142
    pub(crate) socks_port: (),
143

            
144
    /// Addresses to listen on for incoming DNS connections.
145
    #[builder(field(build = r#"#[allow(deprecated)]
146
                   // We use this deprecated macro to instantiate the legacy dns_port option.
147
                   { resolve_listen_port!(self, dns, 0) }
148
                 "#))]
149
    pub(crate) dns_listen: Listen,
150

            
151
    /// Port to listen on (at localhost) for incoming DNS connections.
152
    ///
153
    /// This field is deprecated, and will, eventually, be removed.
154
    /// Use `dns_listen` instead, which accepts the same values,
155
    /// but which will also be able to support more flexible listening in the future.
156
    #[builder(
157
        setter(strip_option),
158
        field(type = "Option<Option<u16>>", build = "()")
159
    )]
160
    #[builder_setter_attr(deprecated)]
161
    pub(crate) dns_port: (),
162
}
163
impl_standard_builder! { ProxyConfig }
164

            
165
/// Configuration for system resources used by Tor.
166
///
167
/// You cannot change *these variables* in this section on a running Arti client.
168
///
169
/// Note that there are other settings in this section,
170
/// in [`arti_client::config::SystemConfig`].
171
//
172
// These two structs exist because:
173
//
174
//  1. Our doctrine is that configuration structs live with the code that uses the info.
175
//  2. tor-memquota's configuration is used by the MemoryQuotaTracker in TorClient
176
//  3. File descriptor limits are enforced here in arti because it's done process-global
177
//  4. Nevertheless, logically, these things want to be in the same section of the file.
178
332
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
179
#[builder(build_fn(error = "ConfigBuildError"))]
180
#[builder(derive(Debug, Serialize, Deserialize))]
181
#[non_exhaustive]
182
pub struct SystemConfig {
183
    /// Maximum number of file descriptors we should launch with
184
    #[builder(setter(into), default = "default_max_files()")]
185
    pub(crate) max_files: u64,
186
}
187
impl_standard_builder! { SystemConfig }
188

            
189
/// Return the default maximum number of file descriptors to launch with.
190
174
fn default_max_files() -> u64 {
191
174
    16384
192
174
}
193

            
194
/// Structure to hold Arti's configuration options, whether from a
195
/// configuration file or the command line.
196
//
197
/// These options are declared in a public crate outside of `arti` so that other
198
/// applications can parse and use them, if desired.  If you're only embedding
199
/// arti via `arti-client`, and you don't want to use Arti's configuration
200
/// format, use [`arti_client::TorClientConfig`] instead.
201
///
202
/// By default, Arti will run using the default Tor network, store state and
203
/// cache information to a per-user set of directories shared by all
204
/// that user's applications, and run a SOCKS client on a local port.
205
///
206
/// NOTE: These are NOT the final options or their final layout. Expect NO
207
/// stability here.
208
86
#[derive(Debug, Builder, Clone, Eq, PartialEq)]
209
#[builder(derive(Serialize, Deserialize, Debug))]
210
12
#[builder(build_fn(private, name = "build_unvalidated", error = "ConfigBuildError"))]
211
pub struct ArtiConfig {
212
    /// Configuration for application behavior.
213
    #[builder(sub_builder(fn_name = "build"))]
214
    #[builder_field_attr(serde(default))]
215
    application: ApplicationConfig,
216

            
217
    /// Configuration for proxy listeners
218
    #[builder(sub_builder(fn_name = "build"))]
219
    #[builder_field_attr(serde(default))]
220
    proxy: ProxyConfig,
221

            
222
    /// Logging configuration
223
    #[builder(sub_builder(fn_name = "build"))]
224
    #[builder_field_attr(serde(default))]
225
    logging: LoggingConfig,
226

            
227
    /// Metrics configuration
228
    #[builder(sub_builder(fn_name = "build"))]
229
    #[builder_field_attr(serde(default))]
230
    pub(crate) metrics: MetricsConfig,
231

            
232
    /// Configuration for RPC subsystem
233
    #[cfg(feature = "rpc")]
234
    #[builder(sub_builder(fn_name = "build"))]
235
    #[builder_field_attr(serde(default))]
236
    pub(crate) rpc: RpcConfig,
237

            
238
    /// Configuration for the RPC subsystem (disabled)
239
    //
240
    // This set of options allows us to detect and warn
241
    // when anything is set under "rpc" in the config.
242
    //
243
    // The incantations are a bit subtle: we use an Option<toml::Value> in the builder,
244
    // to ensure that our configuration will continue to round-trip thorough serde.
245
    // We use () in the configuration type, since toml::Value isn't Eq,
246
    // and since we don't want to expose whatever spurious options were in the config.
247
    // We use builder(private), since using builder(setter(skip))
248
    // would (apparently) override the type of the field in builder and make it a PhantomData.
249
    #[cfg(not(feature = "rpc"))]
250
    #[builder_field_attr(serde(default))]
251
    #[builder(field(type = "Option<toml::Value>", build = "()"), private)]
252
    rpc: (),
253

            
254
    /// Information on system resources used by Arti.
255
    ///
256
    /// Note that there are other settings in this section,
257
    /// in [`arti_client::config::SystemConfig`] -
258
    /// these two structs overlay here.
259
    #[builder(sub_builder(fn_name = "build"))]
260
    #[builder_field_attr(serde(default))]
261
    pub(crate) system: SystemConfig,
262

            
263
    /// Configured list of proxied onion services.
264
    ///
265
    /// Note that this field is present unconditionally, but when onion service
266
    /// support is disabled, it is replaced with a stub type from
267
    /// `onion_proxy_disabled`, and its setter functions are not implemented.
268
    /// The purpose of this stub type is to give an error if somebody tries to
269
    /// configure onion services when the `onion-service-service` feature is
270
    /// disabled.
271
    #[builder(sub_builder(fn_name = "build"), setter(custom))]
272
    #[builder_field_attr(serde(default))]
273
    pub(crate) onion_services: OnionServiceProxyConfigMap,
274
}
275

            
276
impl_standard_builder! { ArtiConfig }
277

            
278
impl ArtiConfigBuilder {
279
    /// Build the [`ArtiConfig`].
280
198
    pub fn build(&self) -> Result<ArtiConfig, ConfigBuildError> {
281
        #[cfg_attr(not(feature = "onion-service-service"), allow(unused_mut))]
282
198
        let mut config = self.build_unvalidated()?;
283
        #[cfg(feature = "onion-service-service")]
284
186
        for svc in config.onion_services.values_mut() {
285
38
            // Pass the application-level watch_configuration to each restricted discovery config.
286
38
            *svc.svc_cfg
287
38
                .restricted_discovery_mut()
288
38
                .watch_configuration_mut() = config.application.watch_configuration;
289
38
        }
290

            
291
        #[cfg(not(feature = "rpc"))]
292
        if self.rpc.is_some() {
293
            tracing::warn!("rpc options were set, but Arti was built without support for rpc.");
294
        }
295

            
296
186
        Ok(config)
297
198
    }
298
}
299

            
300
impl tor_config::load::TopLevel for ArtiConfig {
301
    type Builder = ArtiConfigBuilder;
302
    const DEPRECATED_KEYS: &'static [&'static str] = &["proxy.socks_port", "proxy.dns_port"];
303
}
304

            
305
#[cfg(feature = "onion-service-service")]
306
define_list_builder_accessors! {
307
    struct ArtiConfigBuilder {
308
        pub(crate) onion_services: [OnionServiceProxyConfigBuilder],
309
    }
310
}
311

            
312
/// Convenience alias for the config for a whole `arti` program
313
///
314
/// Used primarily as a type parameter on calls to [`tor_config::resolve`]
315
pub type ArtiCombinedConfig = (ArtiConfig, TorClientConfig);
316

            
317
/// Configuration for exporting metrics (eg, perf data)
318
336
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
319
#[builder(build_fn(error = "ConfigBuildError"))]
320
#[builder(derive(Debug, Serialize, Deserialize))]
321
pub struct MetricsConfig {
322
    /// Where to listen for incoming HTTP connections.
323
    #[builder(sub_builder(fn_name = "build"))]
324
    #[builder_field_attr(serde(default))]
325
    pub(crate) prometheus: PrometheusConfig,
326
}
327
impl_standard_builder! { MetricsConfig }
328

            
329
/// Configuration for one or more proxy listeners.
330
352
#[derive(Debug, Clone, Builder, Eq, PartialEq)]
331
#[builder(build_fn(error = "ConfigBuildError"))]
332
#[builder(derive(Debug, Serialize, Deserialize))]
333
#[allow(clippy::option_option)] // Builder port fields: Some(None) = specified to disable
334
pub struct PrometheusConfig {
335
    /// Port on which to establish a Prometheus scrape endpoint
336
    ///
337
    /// We listen here for incoming HTTP connections.
338
    ///
339
    /// If just a port is provided, we don't support IPv6.
340
    /// Alternatively, (only) a single address and port can be specified.
341
    /// These restrictions are due to upstream limitations:
342
    /// <https://github.com/metrics-rs/metrics/issues/567>.
343
    #[builder(default)]
344
    #[builder_field_attr(serde(default))]
345
    pub(crate) listen: Listen,
346
}
347
impl_standard_builder! { PrometheusConfig }
348

            
349
impl ArtiConfig {
350
    /// Return the [`ApplicationConfig`] for this configuration.
351
120
    pub fn application(&self) -> &ApplicationConfig {
352
120
        &self.application
353
120
    }
354

            
355
    /// Return the [`LoggingConfig`] for this configuration.
356
56
    pub fn logging(&self) -> &LoggingConfig {
357
56
        &self.logging
358
56
    }
359

            
360
    /// Return the [`ProxyConfig`] for this configuration.
361
2
    pub fn proxy(&self) -> &ProxyConfig {
362
2
        &self.proxy
363
2
    }
364

            
365
    /// Return the [`RpcConfig`] for this configuration.
366
    #[cfg(feature = "rpc")]
367
4
    pub fn rpc(&self) -> &RpcConfig {
368
4
        &self.rpc
369
4
    }
370
}
371

            
372
#[cfg(test)]
373
mod test {
374
    // @@ begin test lint list maintained by maint/add_warning @@
375
    #![allow(clippy::bool_assert_comparison)]
376
    #![allow(clippy::clone_on_copy)]
377
    #![allow(clippy::dbg_macro)]
378
    #![allow(clippy::mixed_attributes_style)]
379
    #![allow(clippy::print_stderr)]
380
    #![allow(clippy::print_stdout)]
381
    #![allow(clippy::single_char_pattern)]
382
    #![allow(clippy::unwrap_used)]
383
    #![allow(clippy::unchecked_duration_subtraction)]
384
    #![allow(clippy::useless_vec)]
385
    #![allow(clippy::needless_pass_by_value)]
386
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
387
    // TODO add this next lint to maint/add_warning, for all tests
388
    #![allow(clippy::iter_overeager_cloned)]
389
    // Saves adding many individual #[cfg], or a sub-module
390
    #![cfg_attr(not(feature = "pt-client"), allow(dead_code))]
391

            
392
    use arti_client::config::dir;
393
    use arti_client::config::TorClientConfigBuilder;
394
    use itertools::{chain, EitherOrBoth, Itertools};
395
    use regex::Regex;
396
    use std::collections::HashSet;
397
    use std::fmt::Write as _;
398
    use std::iter;
399
    use std::time::Duration;
400
    use tor_config::load::{ConfigResolveError, ResolutionResults};
401
    use tor_config_path::CfgPath;
402

            
403
    #[allow(unused_imports)] // depends on features
404
    use tor_error::ErrorReport as _;
405

            
406
    #[cfg(feature = "restricted-discovery")]
407
    use {
408
        arti_client::HsClientDescEncKey,
409
        std::str::FromStr as _,
410
        tor_hsservice::config::restricted_discovery::{
411
            DirectoryKeyProviderBuilder, HsClientNickname,
412
        },
413
    };
414

            
415
    use super::*;
416

            
417
    //---------- tests that rely on the provided example config file ----------
418
    //
419
    // These are quite complex.  They uncomment the file, parse bits of it,
420
    // and do tests via serde and via the normal config machinery,
421
    // to see that everything is documented as expected.
422

            
423
    fn uncomment_example_settings(template: &str) -> String {
424
        let re = Regex::new(r#"(?m)^\#([^ \n])"#).unwrap();
425
        re.replace_all(template, |cap: &regex::Captures<'_>| -> _ {
426
            cap.get(1).unwrap().as_str().to_string()
427
        })
428
        .into()
429
    }
430

            
431
    /// Is this key present or absent in the examples in one of the example files ?
432
    ///
433
    /// Depending on which variable this is in, it refers to presence in other the
434
    /// old or the new example file.
435
    ///
436
    /// This type is *not* used in declarations in `declared_config_exceptions`;
437
    /// it is used by the actual checking code.
438
    /// The declarations use types in that function.
439
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
440
    enum InExample {
441
        Absent,
442
        Present,
443
    }
444
    /// Which of the two example files?
445
    ///
446
    /// This type is *not* used in declarations in `declared_config_exceptions`;
447
    /// it is used by the actual checking code.
448
    /// The declarations use types in that function.
449
    #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
450
    enum WhichExample {
451
        Old,
452
        New,
453
    }
454
    /// An exception to the usual expectations about configuration example files
455
    ///
456
    /// This type is *not* used in declarations in `declared_config_exceptions`;
457
    /// it is used by the actual checking code.
458
    /// The declarations use types in that function.
459
    #[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
460
    struct ConfigException {
461
        /// The actual config key
462
        key: String,
463
        /// Does it appear in the oldest supported example file?
464
        in_old_example: InExample,
465
        /// Does it appear in the current example file?
466
        in_new_example: InExample,
467
        /// Does our code recognise it ?  `None` means "don't know"
468
        in_code: Option<bool>,
469
    }
470
    impl ConfigException {
471
        fn in_example(&self, which: WhichExample) -> InExample {
472
            use WhichExample::*;
473
            match which {
474
                Old => self.in_old_example,
475
                New => self.in_new_example,
476
            }
477
        }
478
    }
479

            
480
    /// *every* feature that's listed as `InCode::FeatureDependent`
481
    const ALL_RELEVANT_FEATURES_ENABLED: bool = cfg!(all(
482
        feature = "bridge-client",
483
        feature = "pt-client",
484
        feature = "onion-service-client",
485
        feature = "rpc",
486
    ));
487

            
488
    /// Return the expected exceptions to the usual expectations about config and examples
489
    fn declared_config_exceptions() -> Vec<ConfigException> {
490
        /// Is this key recognised by the parsing code ?
491
        ///
492
        /// (This can be feature-dependent, so literal values of this type
493
        /// are often feature-qualified.)
494
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
495
        enum InCode {
496
            /// No configuration of this codebase knows about this option
497
            Ignored,
498
            /// *Some* configuration of this codebase know about this option
499
            ///
500
            /// This means:
501
            ///   - If *every* feature in `ALL_RELEVANT_FEATURES_ENABLED` is enabled,
502
            ///     the config key is expected to be `Recognised`
503
            ///   - Otherwise we're not sure (because cargo features are additive,
504
            ///     dependency crates' features might be *en*abled willy-nilly).
505
            FeatureDependent,
506
            /// All configurations of this codebase know about this option
507
            Recognized,
508
        }
509
        use InCode::*;
510

            
511
        /// Marker.  `Some(InOld)` means presence of this config key in the oldest-supported file
512
        struct InOld;
513
        /// Marker.  `Some(InNew)` means presence of this config key in the current example file
514
        struct InNew;
515

            
516
        let mut out = vec![];
517

            
518
        // Declare some keys which aren't "normal", eg they aren't documented in the usual
519
        // way, are configurable, aren't in the oldest supported file, etc.
520
        //
521
        // `in_old_example` and `in_new_example` are whether the key appears in
522
        // `arti-example-config.toml` and `oldest-supported-config.toml` respectively.
523
        // (in each case, only a line like `#example.key = ...` counts.)
524
        //
525
        // `whether_supported` tells is if the key is supposed to be
526
        // recognised by the code.
527
        //
528
        // `keys` is the list of keys.  Add a // comment at the start of the list
529
        // so that rustfmt retains the consistent formatting.
530
        let mut declare_exceptions = |in_old_example: Option<InOld>,
531
                                      in_new_example: Option<InNew>,
532
                                      in_code: InCode,
533
                                      keys: &[&str]| {
534
            let in_code = match in_code {
535
                Ignored => Some(false),
536
                Recognized => Some(true),
537
                FeatureDependent if ALL_RELEVANT_FEATURES_ENABLED => Some(true),
538
                FeatureDependent => None,
539
            };
540
            #[allow(clippy::needless_pass_by_value)] // pass by value defends against a->a b->a
541
            fn in_example<T>(spec: Option<T>) -> InExample {
542
                match spec {
543
                    None => InExample::Absent,
544
                    Some(_) => InExample::Present,
545
                }
546
            }
547
            let in_old_example = in_example(in_old_example);
548
            let in_new_example = in_example(in_new_example);
549
            out.extend(keys.iter().cloned().map(|key| ConfigException {
550
                key: key.to_owned(),
551
                in_old_example,
552
                in_new_example,
553
                in_code,
554
            }));
555
        };
556

            
557
        declare_exceptions(
558
            None,
559
            Some(InNew),
560
            Recognized,
561
            &[
562
                // Keys that are newer than the oldest-supported example, but otherwise normal.
563
                "application.allow_running_as_root",
564
                "bridges",
565
                "logging.time_granularity",
566
                "path_rules.long_lived_ports",
567
                "proxy.socks_listen",
568
                "proxy.dns_listen",
569
                "use_obsolete_software",
570
            ],
571
        );
572

            
573
        declare_exceptions(
574
            None,
575
            None,
576
            Recognized,
577
            &[
578
                // Examples exist but are not auto-testable
579
                "tor_network.authorities",
580
                "tor_network.fallback_caches",
581
            ],
582
        );
583

            
584
        declare_exceptions(
585
            Some(InOld),
586
            Some(InNew),
587
            if cfg!(target_family = "windows") {
588
                Ignored
589
            } else {
590
                Recognized
591
            },
592
            &[
593
                // Unix-only mistrust settings
594
                "storage.permissions.trust_group",
595
                "storage.permissions.trust_user",
596
            ],
597
        );
598

            
599
        declare_exceptions(
600
            None,
601
            None, // TODO: Make examples for bridges settings!
602
            FeatureDependent,
603
            &[
604
                // Settings only available with bridge support
605
                "bridges.transports", // we recognise this so we can reject it
606
            ],
607
        );
608

            
609
        declare_exceptions(
610
            None,
611
            Some(InNew),
612
            FeatureDependent,
613
            &[
614
                // Settings only available with experimental-api support
615
                "storage.keystore",
616
            ],
617
        );
618

            
619
        declare_exceptions(
620
            None,
621
            None, // it's there, but not formatted for auto-testing
622
            Recognized,
623
            &[
624
                // Memory quota, tested by fn memquota (below)
625
                "system.memory",
626
                "system.memory.max",
627
                "system.memory.low_water",
628
            ],
629
        );
630

            
631
        declare_exceptions(
632
            None,
633
            Some(InNew), // The top-level section is in the new file (only).
634
            Recognized,
635
            &["metrics"],
636
        );
637

            
638
        declare_exceptions(
639
            None,
640
            None, // The inner information is not formatted for auto-testing
641
            Recognized,
642
            &[
643
                // Prometheus metrics exporter, tested by fn metrics (below)
644
                "metrics.prometheus",
645
                "metrics.prometheus.listen",
646
            ],
647
        );
648

            
649
        declare_exceptions(
650
            None,
651
            Some(InNew),
652
            FeatureDependent,
653
            &[
654
                // PT-only settings
655
            ],
656
        );
657

            
658
        declare_exceptions(
659
            None,
660
            Some(InNew),
661
            FeatureDependent,
662
            &[
663
                // HS client settings
664
                "address_filter.allow_onion_addrs",
665
                "circuit_timing.hs_desc_fetch_attempts",
666
                "circuit_timing.hs_intro_rend_attempts",
667
            ],
668
        );
669

            
670
        declare_exceptions(
671
            None,
672
            None, // TODO RPC, these should actually appear in the example config
673
            FeatureDependent,
674
            &[
675
                // RPC-only settings
676
                "rpc",
677
                "rpc.rpc_listen",
678
            ],
679
        );
680

            
681
        // These are commented-out by default, and tested with test::onion_services().
682
        declare_exceptions(
683
            None,
684
            None,
685
            FeatureDependent,
686
            &[
687
                // onion-service only settings.
688
                "onion_services",
689
            ],
690
        );
691

            
692
        declare_exceptions(
693
            None,
694
            Some(InNew),
695
            FeatureDependent,
696
            &[
697
                // Vanguards-specific settings
698
                "vanguards",
699
                "vanguards.mode",
700
            ],
701
        );
702

            
703
        // These are commented-out by default
704
        declare_exceptions(
705
            None,
706
            None,
707
            FeatureDependent,
708
            &[
709
                "storage.keystore.ctor",
710
                "storage.keystore.ctor.services",
711
                "storage.keystore.ctor.clients",
712
            ],
713
        );
714

            
715
        out.sort();
716

            
717
        let dupes = out.iter().map(|exc| &exc.key).duplicates().collect_vec();
718
        assert!(
719
            dupes.is_empty(),
720
            "duplicate exceptions in configuration {dupes:?}"
721
        );
722

            
723
        eprintln!(
724
            "declared config exceptions for this configuration:\n{:#?}",
725
            &out
726
        );
727
        out
728
    }
729

            
730
    #[test]
731
    fn default_config() {
732
        use InExample::*;
733

            
734
        let empty_config = tor_config::ConfigurationSources::new_empty()
735
            .load()
736
            .unwrap();
737
        let empty_config: ArtiCombinedConfig = tor_config::resolve(empty_config).unwrap();
738

            
739
        let default = (ArtiConfig::default(), TorClientConfig::default());
740
        let exceptions = declared_config_exceptions();
741

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

            
796
        let parses_to_defaults = |example: &str, which: WhichExample, uncommented: bool| {
797
            let cfg = {
798
                let mut sources = tor_config::ConfigurationSources::new_empty();
799
                sources.push_source(
800
                    tor_config::ConfigurationSource::from_verbatim(example.to_string()),
801
                    tor_config::sources::MustRead::MustRead,
802
                );
803
                sources.load().unwrap()
804
            };
805

            
806
            // This tests that the example settings do not *contradict* the defaults.
807
            let results: ResolutionResults<ArtiCombinedConfig> =
808
                tor_config::resolve_return_results(cfg).unwrap();
809

            
810
            assert_eq!(&results.value, &default, "{which:?} {uncommented:?}");
811
            assert_eq!(&results.value, &empty_config, "{which:?} {uncommented:?}");
812

            
813
            // We serialize the DisfavouredKey entries to strings to compare them against
814
            // `known_unrecognized_options`.
815
            let unrecognized = results
816
                .unrecognized
817
                .iter()
818
                .map(|k| k.to_string())
819
                .collect_vec();
820

            
821
            eprintln!(
822
                "parsing of {which:?} uncommented={uncommented:?}, unrecognized={unrecognized:#?}"
823
            );
824

            
825
            let reports =
826
                Itertools::merge_join_by(unrecognized.iter(), exceptions.iter(), |u, e| {
827
                    u.as_str().cmp(&e.key)
828
                })
829
                .filter_map(|eob| analyse_joined_info(which, uncommented, eob).err())
830
                .collect_vec();
831

            
832
            if !reports.is_empty() {
833
                let reports = reports.iter().fold(String::new(), |mut out, (k, s)| {
834
                    writeln!(out, "  {}: {}", s, k).unwrap();
835
                    out
836
                });
837

            
838
                panic!(
839
                    r"
840
mismatch: results of parsing example files (& vs declared exceptions):
841
example config file {which:?}, uncommented={uncommented:?}
842
{reports}
843
"
844
                );
845
            }
846

            
847
            results.value
848
        };
849

            
850
        let _ = parses_to_defaults(ARTI_EXAMPLE_CONFIG, WhichExample::New, false);
851
        let _ = parses_to_defaults(OLDEST_SUPPORTED_CONFIG, WhichExample::Old, false);
852

            
853
        let built_default = (
854
            ArtiConfigBuilder::default().build().unwrap(),
855
            TorClientConfigBuilder::default().build().unwrap(),
856
        );
857

            
858
        let parsed = parses_to_defaults(
859
            &uncomment_example_settings(ARTI_EXAMPLE_CONFIG),
860
            WhichExample::New,
861
            true,
862
        );
863
        let parsed_old = parses_to_defaults(
864
            &uncomment_example_settings(OLDEST_SUPPORTED_CONFIG),
865
            WhichExample::Old,
866
            true,
867
        );
868

            
869
        assert_eq!(&parsed, &built_default);
870
        assert_eq!(&parsed_old, &built_default);
871

            
872
        assert_eq!(&default, &built_default);
873
    }
874

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

            
911
        let example = uncomment_example_settings(example_file);
912
        let example: toml::Value = toml::from_str(&example).unwrap();
913
        // dbg!(&example);
914
        let example = serde_json::to_value(example).unwrap();
915
        // dbg!(&example);
916

            
917
        // "Exhaustive" taxonomy of the recognized configuration keys
918
        //
919
        // We use the JSON serialization of the default builders, because Rust's toml
920
        // implementation likes to omit more things, that we want to see.
921
        //
922
        // I'm not sure this is quite perfect but it is pretty good,
923
        // and has found a number of un-exampled config keys.
924
        let exhausts = [
925
            serde_json::to_value(TorClientConfig::builder()).unwrap(),
926
            serde_json::to_value(ArtiConfig::builder()).unwrap(),
927
        ];
928

            
929
        /// This code does *not* record a problem for keys *in* the example file
930
        /// that are unrecognized.  That is handled by the `default_config` test.
931
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, derive_more::Display)]
932
        enum ProblemKind {
933
            #[display("recognised by serialisation, but missing from example config file")]
934
            MissingFromExample,
935
            #[display("expected that example config file should contain have this as a table")]
936
            ExpectedTableInExample,
937
            #[display(
938
                "declared exception says this key should be recognised but not in file, but that doesn't seem to be the case"
939
            )]
940
            UnusedException,
941
        }
942

            
943
        #[derive(Default, Debug)]
944
        struct Walk {
945
            current_path: Vec<String>,
946
            problems: Vec<(String, ProblemKind)>,
947
        }
948

            
949
        impl Walk {
950
            /// Records a problem
951
            fn bad(&mut self, kind: ProblemKind) {
952
                self.problems.push((self.current_path.join("."), kind));
953
            }
954

            
955
            /// Recurses, looking for problems
956
            ///
957
            /// Visited for every node in either or both of the starting `exhausts`.
958
            ///
959
            /// `E` is the number of elements in `exhausts`, ie the number of different
960
            /// top-level config types that Arti uses.  Ie, 2.
961
            fn walk<const E: usize>(
962
                &mut self,
963
                example: Option<&JsValue>,
964
                exhausts: [Option<&JsValue>; E],
965
            ) {
966
                assert! { exhausts.into_iter().any(|e| e.is_some()) }
967

            
968
                let example = if let Some(e) = example {
969
                    e
970
                } else {
971
                    self.bad(ProblemKind::MissingFromExample);
972
                    return;
973
                };
974

            
975
                let tables = exhausts.map(|e| e?.as_object());
976

            
977
                // Union of the keys of both exhausts' tables (insofar as they *are* tables)
978
                let table_keys = tables
979
                    .iter()
980
                    .flat_map(|t| t.map(|t| t.keys().cloned()).into_iter().flatten())
981
                    .collect::<BTreeSet<String>>();
982

            
983
                for key in table_keys {
984
                    let example = if let Some(e) = example.as_object() {
985
                        e
986
                    } else {
987
                        // At least one of the exhausts was a nonempty table,
988
                        // but the corresponding example node isn't a table.
989
                        self.bad(ProblemKind::ExpectedTableInExample);
990
                        continue;
991
                    };
992

            
993
                    // Descend the same key in all the places.
994
                    self.current_path.push(key.clone());
995
                    self.walk(example.get(&key), tables.map(|t| t?.get(&key)));
996
                    self.current_path.pop().unwrap();
997
                }
998
            }
999
        }
        let exhausts = exhausts.iter().map(Some).collect_vec().try_into().unwrap();
        let mut walk = Walk::default();
        walk.walk::<2>(Some(&example), exhausts);
        let mut problems = walk.problems;
        /// Marker present in `expect_missing` to say we *definitely* expect it
        #[derive(Debug, Copy, Clone)]
        struct DefinitelyRecognized;
        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::{pt::TransportConfig, BridgesConfig};
            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)>();
        cfg_if::cfg_if! {
            if #[cfg(feature = "memquota")] {
                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);
            } else if #[cfg(arti_features_precise)] {
                // Test that requesting memory quota tracking generates a config error
                // if support is compiled out.
                let m = result.unwrap_err().report().to_string();
                assert!(m.contains("cargo feature `memquota` disabled"), "{m:?}");
            } else {
                // The `tor-memquota/memquota` feature is enabled by default in tor-memquota,
                // but the corresponding `memquota` feature is but not enabled here in `arti`.
                // so cargo --workspace enables it in a way we can't tell.  See arti/build.rs.
                println!("not testing memquota config, cannot figure out if it's enabled");
            }
        }
    }
    #[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(), "no support for running onion services");
        }
    }
    #[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.to_string()),
                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 auth = dir::Authority::builder()
            .name("Fred")
            .v3ident([22; 20].into())
            .clone();
        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().set_authorities(vec![auth]);
        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 the various `socks_port` and `dns_port`
    ///
    /// The "this isn't set at all, just use the default" cases are tested elsewhere.
    fn compat_ports_listen(
        f: &str,
        get_listen: &dyn Fn(&ArtiConfig) -> &Listen,
        bld_get_port: &dyn Fn(&ArtiConfigBuilder) -> &Option<Option<u16>>,
        bld_get_listen: &dyn Fn(&ArtiConfigBuilder) -> &Option<Listen>,
        setter_port: &dyn Fn(&mut ArtiConfigBuilder, Option<u16>) -> &mut ProxyConfigBuilder,
        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 conflicting_cfgs = [
            format!("proxy.{}_port = 0 \n proxy.{}_listen = 200", f, f),
            format!("proxy.{}_port = 100 \n proxy.{}_listen = 0", f, f),
            format!("proxy.{}_port = 100 \n proxy.{}_listen = 200", f, f),
        ];
        let chk = |cfg: &ArtiConfigBuilder, expected: &Listen| {
            dbg!(bld_get_listen(cfg), bld_get_port(cfg));
            let cfg = cfg.build().unwrap();
            assert_eq!(get_listen(&cfg), expected);
        };
        let check_setters = |port, expected: &_| {
            for cfg in chain!(
                iter::once(ArtiConfig::builder()),
                conflicting_cfgs.iter().map(|cfg| from_toml(cfg)),
            ) {
                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_port(&mut cfg, dbg!(port));
                    setter_listen(&mut cfg, dbg!(listen));
                    chk(&cfg, expected);
                }
            }
        };
        {
            let expected = Listen::new_localhost(100);
            let cfg = from_toml(&format!("proxy.{}_port = 100", f));
            assert_eq!(bld_get_port(&cfg), &Some(Some(100)));
            chk(&cfg, &expected);
            let cfg = from_toml(&format!("proxy.{}_listen = 100", f));
            assert_eq!(bld_get_listen(&cfg), &Some(Listen::new_localhost(100)));
            chk(&cfg, &expected);
            let cfg = from_toml(&format!(
                "proxy.{}_port = 100\n proxy.{}_listen = 100",
                f, f
            ));
            chk(&cfg, &expected);
            check_setters(Some(100), &expected);
        }
        {
            let expected = Listen::new_none();
            let cfg = from_toml(&format!("proxy.{}_port = 0", f));
            chk(&cfg, &expected);
            let cfg = from_toml(&format!("proxy.{}_listen = 0", f));
            chk(&cfg, &expected);
            let cfg = from_toml(&format!("proxy.{}_port = 0 \n proxy.{}_listen = 0", f, f));
            chk(&cfg, &expected);
            check_setters(None, &expected);
        }
        for cfg in &conflicting_cfgs {
            let cfg = from_toml(cfg);
            let err = dbg!(cfg.build()).unwrap_err();
            assert!(err.to_string().contains("specifying different values"));
        }
    }
    #[test]
    #[allow(deprecated)]
    fn ports_listen_socks() {
        compat_ports_listen(
            "socks",
            &|cfg| &cfg.proxy.socks_listen,
            &|bld| &bld.proxy.socks_port,
            &|bld| &bld.proxy.socks_listen,
            &|bld, arg| bld.proxy.socks_port(arg),
            &|bld, arg| bld.proxy.socks_listen(arg),
        );
    }
    #[test]
    #[allow(deprecated)]
    fn compat_ports_listen_dns() {
        compat_ports_listen(
            "dns",
            &|cfg| &cfg.proxy.dns_listen,
            &|bld| &bld.proxy.dns_port,
            &|bld| &bld.proxy.dns_listen,
            &|bld, arg| bld.proxy.dns_port(arg),
            &|bld, arg| bld.proxy.dns_listen(arg),
        );
    }
}