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
    /// If true, and the `http-connect` feature is enabled,
113
    /// all members of `socks_listen` also support HTTP CONNECT.
114
    //
115
    // TODO:
116
    // At some point in the future we might want per-port configuration, like Tor has.
117
    #[deftly(tor_config(
118
        cfg = r#" feature="http-connect" "#,
119
        cfg_desc = "with HTTP CONNECT support"
120
    ))]
121
    #[deftly(tor_config(default = "true"))]
122
    pub(crate) enable_http_connect: bool,
123
}
124

            
125
impl ProxyConfig {
126
    /// Return the stream proxy protocols we support according to this configuration.
127
    pub(crate) fn protocols(&self) -> crate::proxy::ListenProtocols {
128
        use crate::proxy::ListenProtocols::*;
129
        #[cfg(feature = "http-connect")]
130
        if self.enable_http_connect {
131
            return SocksAndHttpConnect;
132
        }
133

            
134
        SocksOnly
135
    }
136
}
137

            
138
/// Configuration for arti-specific storage locations.
139
///
140
/// See also [`arti_client::config::StorageConfig`].
141
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
142
#[derive_deftly(TorConfig)]
143
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
144
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
145
pub(crate) struct ArtiStorageConfig {
146
    /// A file in which to write information about the ports we're listening on.
147
    #[deftly(tor_config(setter(into), default = "default_port_info_file()"))]
148
    pub(crate) port_info_file: CfgPath,
149
}
150

            
151
/// Return the default ports_info_file location.
152
380
fn default_port_info_file() -> CfgPath {
153
380
    CfgPath::new("${ARTI_LOCAL_DATA}/public/port_info.json".to_owned())
154
380
}
155

            
156
/// Configuration for system resources used by Tor.
157
///
158
/// You cannot change *these variables* in this section on a running Arti client.
159
///
160
/// Note that there are other settings in this section,
161
/// in [`arti_client::config::SystemConfig`].
162
//
163
// These two structs exist because:
164
//
165
//  1. Our doctrine is that configuration structs live with the code that uses the info.
166
//  2. tor-memquota's configuration is used by the MemoryQuotaTracker in TorClient
167
//  3. File descriptor limits are enforced here in arti because it's done process-global
168
//  4. Nevertheless, logically, these things want to be in the same section of the file.
169
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
170
#[derive_deftly(TorConfig)]
171
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
172
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
173
#[non_exhaustive]
174
pub(crate) struct SystemConfig {
175
    /// Maximum number of file descriptors we should launch with
176
    #[deftly(tor_config(setter(into), default = "default_max_files()"))]
177
    pub(crate) max_files: u64,
178
}
179

            
180
/// Return the default maximum number of file descriptors to launch with.
181
378
fn default_max_files() -> u64 {
182
378
    16384
183
378
}
184

            
185
/// Structure to hold Arti's configuration options, whether from a
186
/// configuration file or the command line.
187
//
188
/// These options are declared in a public crate outside of `arti` so that other
189
/// applications can parse and use them, if desired.  If you're only embedding
190
/// arti via `arti-client`, and you don't want to use Arti's configuration
191
/// format, use [`arti_client::TorClientConfig`] instead.
192
///
193
/// By default, Arti will run using the default Tor network, store state and
194
/// cache information to a per-user set of directories shared by all
195
/// that user's applications, and run a SOCKS client on a local port.
196
///
197
/// NOTE: These are NOT the final options or their final layout. Expect NO
198
/// stability here.
199
#[derive(Debug, Deftly, Clone, Eq, PartialEq)]
200
#[derive_deftly(TorConfig)]
201
#[deftly(tor_config(post_build = "Self::post_build"))]
202
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
203
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
204
pub(crate) struct ArtiConfig {
205
    /// Configuration for application behavior.
206
    #[deftly(tor_config(sub_builder))]
207
    application: ApplicationConfig,
208

            
209
    /// Configuration for proxy listeners
210
    #[deftly(tor_config(sub_builder))]
211
    proxy: ProxyConfig,
212

            
213
    /// Logging configuration
214
    #[deftly(tor_config(sub_builder))]
215
    logging: LoggingConfig,
216

            
217
    /// Metrics configuration
218
    #[deftly(tor_config(sub_builder))]
219
    pub(crate) metrics: MetricsConfig,
220

            
221
    /// Configuration for RPC subsystem
222
    #[deftly(tor_config(
223
        sub_builder,
224
        cfg = r#" feature = "rpc" "#,
225
        cfg_desc = "with RPC support"
226
    ))]
227
    pub(crate) rpc: RpcConfig,
228

            
229
    /// Information on system resources used by Arti.
230
    ///
231
    /// Note that there are other settings in this section,
232
    /// in [`arti_client::config::SystemConfig`] -
233
    /// these two structs overlay here.
234
    #[deftly(tor_config(sub_builder))]
235
    pub(crate) system: SystemConfig,
236

            
237
    /// Information on where things are stored by Arti.
238
    ///
239
    /// Note that [`TorClientConfig`] also has a storage configuration;
240
    /// our configuration logic should merge them correctly.
241
    #[deftly(tor_config(sub_builder))]
242
    pub(crate) storage: ArtiStorageConfig,
243

            
244
    /// Configured list of proxied onion services.
245
    ///
246
    /// Note that this field is present unconditionally, but when onion service
247
    /// support is disabled, it is replaced with a stub type from
248
    /// `onion_proxy_disabled`, and its setter functions are not implemented.
249
    /// The purpose of this stub type is to give an error if somebody tries to
250
    /// configure onion services when the `onion-service-service` feature is
251
    /// disabled.
252
    #[deftly(tor_config(
253
        setter(skip),
254
        sub_builder,
255
        cfg = r#" feature = "onion-service-service" "#,
256
        cfg_reject,
257
        cfg_desc = "with onion service support"
258
    ))]
259
    pub(crate) onion_services: OnionServiceProxyConfigMap,
260
}
261

            
262
impl ArtiConfigBuilder {
263
    /// validate the [`ArtiConfig`] after building.
264
    #[allow(clippy::unnecessary_wraps)]
265
378
    fn post_build(config: ArtiConfig) -> Result<ArtiConfig, ConfigBuildError> {
266
        #[cfg_attr(not(feature = "onion-service-service"), allow(unused_mut))]
267
378
        let mut config = config;
268
        #[cfg(feature = "onion-service-service")]
269
378
        for svc in config.onion_services.values_mut() {
270
170
            // Pass the application-level watch_configuration to each restricted discovery config.
271
170
            *svc.svc_cfg
272
170
                .restricted_discovery_mut()
273
170
                .watch_configuration_mut() = config.application.watch_configuration;
274
170
        }
275

            
276
378
        Ok(config)
277
378
    }
278
}
279

            
280
impl tor_config::load::TopLevel for ArtiConfig {
281
    type Builder = ArtiConfigBuilder;
282
    // Some config options such as "proxy.socks_port" are no longer
283
    // just "deprecated" and have since been completely removed from Arti,
284
    // but there's no harm in informing the user that the options are still deprecated.
285
    // For these removed options, Arti will ignore them like it does for all unknown options.
286
    const DEPRECATED_KEYS: &'static [&'static str] = &["proxy.socks_port", "proxy.dns_port"];
287
}
288

            
289
#[cfg(feature = "onion-service-service")]
290
define_list_builder_accessors! {
291
    struct ArtiConfigBuilder {
292
        pub(crate) onion_services: [OnionServiceProxyConfigBuilder],
293
    }
294
}
295

            
296
/// Convenience alias for the config for a whole `arti` program
297
///
298
/// Used primarily as a type parameter on calls to [`tor_config::resolve`]
299
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
300
pub(crate) type ArtiCombinedConfig = (ArtiConfig, TorClientConfig);
301

            
302
/// Configuration for exporting metrics (eg, perf data)
303
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
304
#[derive_deftly(TorConfig)]
305
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
306
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
307
pub(crate) struct MetricsConfig {
308
    /// Where to listen for incoming HTTP connections.
309
    #[deftly(tor_config(sub_builder))]
310
    pub(crate) prometheus: PrometheusConfig,
311
}
312

            
313
/// Configuration for one or more proxy listeners.
314
#[derive(Debug, Clone, Deftly, Eq, PartialEq)]
315
#[derive_deftly(TorConfig)]
316
#[cfg_attr(feature = "experimental-api", visibility::make(pub))]
317
#[cfg_attr(feature = "experimental-api", deftly(tor_config(vis = "pub")))]
318
pub(crate) struct PrometheusConfig {
319
    /// Port on which to establish a Prometheus scrape endpoint
320
    ///
321
    /// We listen here for incoming HTTP connections.
322
    ///
323
    /// If just a port is provided, we don't support IPv6.
324
    /// Alternatively, (only) a single address and port can be specified.
325
    /// These restrictions are due to upstream limitations:
326
    /// <https://github.com/metrics-rs/metrics/issues/567>.
327
    #[deftly(tor_config(default))]
328
    pub(crate) listen: Listen,
329
}
330

            
331
impl ArtiConfig {
332
    /// Return the [`ApplicationConfig`] for this configuration.
333
620
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
334
620
    pub(crate) fn application(&self) -> &ApplicationConfig {
335
620
        &self.application
336
620
    }
337

            
338
    /// Return the [`LoggingConfig`] for this configuration.
339
308
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
340
308
    pub(crate) fn logging(&self) -> &LoggingConfig {
341
308
        &self.logging
342
308
    }
343

            
344
    /// Return the [`ProxyConfig`] for this configuration.
345
2
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
346
2
    pub(crate) fn proxy(&self) -> &ProxyConfig {
347
2
        &self.proxy
348
2
    }
349

            
350
    /// Return the [`ArtiStorageConfig`] for this configuration.
351
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
352
    ///
353
    pub(crate) fn storage(&self) -> &ArtiStorageConfig {
354
        &self.storage
355
    }
356

            
357
    /// Return the [`RpcConfig`] for this configuration.
358
    #[cfg(feature = "rpc")]
359
4
    #[cfg_attr(feature = "experimental-api", visibility::make(pub))]
360
4
    pub(crate) fn rpc(&self) -> &RpcConfig {
361
4
        &self.rpc
362
4
    }
363
}
364

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

            
385
    use arti_client::config::TorClientConfigBuilder;
386
    use arti_client::config::dir;
387
    use itertools::{EitherOrBoth, Itertools, chain};
388
    use regex::Regex;
389
    use std::collections::HashSet;
390
    use std::fmt::Write as _;
391
    use std::iter;
392
    use std::time::Duration;
393
    use tor_config::load::{ConfigResolveError, ResolutionResults};
394
    use tor_config_path::CfgPath;
395

            
396
    #[allow(unused_imports)] // depends on features
397
    use tor_error::ErrorReport as _;
398

            
399
    #[cfg(feature = "restricted-discovery")]
400
    use {
401
        arti_client::HsClientDescEncKey,
402
        std::str::FromStr as _,
403
        tor_hsservice::config::restricted_discovery::{
404
            DirectoryKeyProviderBuilder, HsClientNickname,
405
        },
406
    };
407

            
408
    use super::*;
409

            
410
    //---------- tests that rely on the provided example config file ----------
411
    //
412
    // These are quite complex.  They uncomment the file, parse bits of it,
413
    // and do tests via serde and via the normal config machinery,
414
    // to see that everything is documented as expected.
415

            
416
    fn uncomment_example_settings(template: &str) -> String {
417
        let re = Regex::new(r#"(?m)^\#([^ \n])"#).unwrap();
418
        re.replace_all(template, |cap: &regex::Captures<'_>| -> _ {
419
            cap.get(1).unwrap().as_str().to_string()
420
        })
421
        .into()
422
    }
423

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

            
473
    /// *every* feature that's listed as `InCode::FeatureDependent`
474
    const ALL_RELEVANT_FEATURES_ENABLED: bool = cfg!(all(
475
        feature = "bridge-client",
476
        feature = "pt-client",
477
        feature = "onion-service-client",
478
        feature = "rpc",
479
    ));
480

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

            
504
        /// Marker.  `Some(InOld)` means presence of this config key in the oldest-supported file
505
        struct InOld;
506
        /// Marker.  `Some(InNew)` means presence of this config key in the current example file
507
        struct InNew;
508

            
509
        let mut out = vec![];
510

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

            
550
        declare_exceptions(
551
            None,
552
            Some(InNew),
553
            Recognized,
554
            &[
555
                // Keys that are newer than the oldest-supported example, but otherwise normal.
556
                "application.allow_running_as_root",
557
                "bridges",
558
                "logging.time_granularity",
559
                "path_rules.long_lived_ports",
560
                "use_obsolete_software",
561
                "circuit_timing.disused_circuit_timeout",
562
                "storage.port_info_file",
563
            ],
564
        );
565

            
566
        declare_exceptions(
567
            None,
568
            None,
569
            Recognized,
570
            &[
571
                // Examples exist but are not auto-testable
572
                "tor_network.authorities",
573
                "tor_network.fallback_caches",
574
            ],
575
        );
576

            
577
        declare_exceptions(
578
            None,
579
            None,
580
            Recognized,
581
            &[
582
                // Examples exist but are not auto-testable
583
                "logging.opentelemetry",
584
            ],
585
        );
586

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

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

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

            
622
        declare_exceptions(
623
            None,
624
            None, // it's there, but not formatted for auto-testing
625
            FeatureDependent,
626
            &[
627
                // Settings only available with tokio-console support
628
                "logging.tokio_console",
629
                "logging.tokio_console.enabled",
630
            ],
631
        );
632

            
633
        declare_exceptions(
634
            None,
635
            None, // it's there, but not formatted for auto-testing
636
            Recognized,
637
            &[
638
                // Memory quota, tested by fn memquota (below)
639
                "system.memory",
640
                "system.memory.max",
641
                "system.memory.low_water",
642
            ],
643
        );
644

            
645
        declare_exceptions(
646
            None,
647
            Some(InNew), // The top-level section is in the new file (only).
648
            Recognized,
649
            &["metrics"],
650
        );
651

            
652
        declare_exceptions(
653
            None,
654
            None, // The inner information is not formatted for auto-testing
655
            Recognized,
656
            &[
657
                // Prometheus metrics exporter, tested by fn metrics (below)
658
                "metrics.prometheus",
659
                "metrics.prometheus.listen",
660
            ],
661
        );
662

            
663
        declare_exceptions(
664
            None,
665
            Some(InNew),
666
            FeatureDependent,
667
            &[
668
                // PT-only settings
669
            ],
670
        );
671

            
672
        declare_exceptions(
673
            None,
674
            Some(InNew),
675
            FeatureDependent,
676
            &[
677
                // HS client settings
678
                "address_filter.allow_onion_addrs",
679
                "circuit_timing.hs_desc_fetch_attempts",
680
                "circuit_timing.hs_intro_rend_attempts",
681
            ],
682
        );
683

            
684
        declare_exceptions(
685
            None,
686
            Some(InNew),
687
            FeatureDependent,
688
            &[
689
                // HTTP Connect settings
690
                "proxy.enable_http_connect",
691
            ],
692
        );
693

            
694
        declare_exceptions(
695
            None,
696
            None, // TODO RPC, these should actually appear in the example config
697
            FeatureDependent,
698
            &[
699
                // RPC-only settings
700
                "rpc",
701
                "rpc.rpc_listen",
702
            ],
703
        );
704

            
705
        // These are commented-out by default, and tested with test::onion_services().
706
        declare_exceptions(
707
            None,
708
            None,
709
            FeatureDependent,
710
            &[
711
                // onion-service only settings.
712
                "onion_services",
713
            ],
714
        );
715

            
716
        declare_exceptions(
717
            None,
718
            Some(InNew),
719
            FeatureDependent,
720
            &[
721
                // Vanguards-specific settings
722
                "vanguards",
723
                "vanguards.mode",
724
            ],
725
        );
726

            
727
        // These are commented-out by default
728
        declare_exceptions(
729
            None,
730
            None,
731
            FeatureDependent,
732
            &[
733
                "storage.keystore.ctor",
734
                "storage.keystore.ctor.services",
735
                "storage.keystore.ctor.clients",
736
            ],
737
        );
738

            
739
        out.sort();
740

            
741
        let dupes = out.iter().map(|exc| &exc.key).duplicates().collect_vec();
742
        assert!(
743
            dupes.is_empty(),
744
            "duplicate exceptions in configuration {dupes:?}"
745
        );
746

            
747
        eprintln!(
748
            "declared config exceptions for this configuration:\n{:#?}",
749
            &out
750
        );
751
        out
752
    }
753

            
754
    #[test]
755
    fn default_config() {
756
        use InExample::*;
757

            
758
        let empty_config = tor_config::ConfigurationSources::new_empty()
759
            .load()
760
            .unwrap();
761
        let empty_config: ArtiCombinedConfig = tor_config::resolve(empty_config).unwrap();
762

            
763
        let default = (ArtiConfig::default(), TorClientConfig::default());
764
        let exceptions = declared_config_exceptions();
765

            
766
        /// Helper to decide what to do about a possible discrepancy
767
        ///
768
        /// Provided with `EitherOrBoth` of:
769
        ///   - the config key that the config parser reported it found, but didn't recognise
770
        ///   - the declared exception entry
771
        ///     (for the same config key)
772
        ///
773
        /// Decides whether this is something that should fail the test.
774
        /// If so it returns `Err((key, error_message))`, otherwise `Ok`.
775
        #[allow(clippy::needless_pass_by_value)] // clippy is IMO wrong about eob
776
        fn analyse_joined_info(
777
            which: WhichExample,
778
            uncommented: bool,
779
            eob: EitherOrBoth<&String, &ConfigException>,
780
        ) -> Result<(), (String, String)> {
781
            use EitherOrBoth::*;
782
            let (key, err) = match eob {
783
                // Unrecognised entry, no exception
784
                Left(found) => (found, "found in example but not processed".into()),
785
                Both(found, exc) => {
786
                    let but = match (exc.in_example(which), exc.in_code, uncommented) {
787
                        (Absent, _, _) => "but exception entry expected key to be absent",
788
                        (_, _, false) => "when processing still-commented-out file!",
789
                        (_, Some(true), _) => {
790
                            "but an exception entry says it should have been recognised"
791
                        }
792
                        (Present, Some(false), true) => return Ok(()), // that's as expected
793
                        (Present, None, true) => return Ok(()), // that's could be as expected
794
                    };
795
                    (
796
                        found,
797
                        format!("parser reported unrecognised config key, {but}"),
798
                    )
799
                }
800
                Right(exc) => {
801
                    // An exception entry exists.  The actual situation is either
802
                    //   - not found in file (so no "unrecognised" report)
803
                    //   - processed successfully (found in file and in code)
804
                    // but we don't know which.
805
                    let trouble = match (exc.in_example(which), exc.in_code, uncommented) {
806
                        (Absent, _, _) => return Ok(()), // not in file, no report expected
807
                        (_, _, false) => return Ok(()),  // not uncommented, no report expected
808
                        (_, Some(true), _) => return Ok(()), // code likes it, no report expected
809
                        (Present, Some(false), true) => {
810
                            "expected an 'unknown config key' report but didn't see one"
811
                        }
812
                        (Present, None, true) => return Ok(()), // not sure, have to just allow it
813
                    };
814
                    (&exc.key, trouble.into())
815
                }
816
            };
817
            Err((key.clone(), err))
818
        }
819

            
820
        let parses_to_defaults = |example: &str, which: WhichExample, uncommented: bool| {
821
            let cfg = {
822
                let mut sources = tor_config::ConfigurationSources::new_empty();
823
                sources.push_source(
824
                    tor_config::ConfigurationSource::from_verbatim(example.to_string()),
825
                    tor_config::sources::MustRead::MustRead,
826
                );
827
                sources.load().unwrap()
828
            };
829

            
830
            // This tests that the example settings do not *contradict* the defaults.
831
            let results: ResolutionResults<ArtiCombinedConfig> =
832
                tor_config::resolve_return_results(cfg).unwrap();
833

            
834
            assert_eq!(&results.value, &default, "{which:?} {uncommented:?}");
835
            assert_eq!(&results.value, &empty_config, "{which:?} {uncommented:?}");
836

            
837
            // We serialize the DisfavouredKey entries to strings to compare them against
838
            // `known_unrecognized_options`.
839
            let unrecognized = results
840
                .unrecognized
841
                .iter()
842
                .map(|k| k.to_string())
843
                .collect_vec();
844

            
845
            eprintln!(
846
                "parsing of {which:?} uncommented={uncommented:?}, unrecognized={unrecognized:#?}"
847
            );
848

            
849
            let reports =
850
                Itertools::merge_join_by(unrecognized.iter(), exceptions.iter(), |u, e| {
851
                    u.as_str().cmp(&e.key)
852
                })
853
                .filter_map(|eob| analyse_joined_info(which, uncommented, eob).err())
854
                .collect_vec();
855

            
856
            if !reports.is_empty() {
857
                let reports = reports.iter().fold(String::new(), |mut out, (k, s)| {
858
                    writeln!(out, "  {}: {}", s, k).unwrap();
859
                    out
860
                });
861

            
862
                panic!(
863
                    r"
864
mismatch: results of parsing example files (& vs declared exceptions):
865
example config file {which:?}, uncommented={uncommented:?}
866
{reports}
867
"
868
                );
869
            }
870

            
871
            results.value
872
        };
873

            
874
        let _ = parses_to_defaults(ARTI_EXAMPLE_CONFIG, WhichExample::New, false);
875
        let _ = parses_to_defaults(OLDEST_SUPPORTED_CONFIG, WhichExample::Old, false);
876

            
877
        let built_default = (
878
            ArtiConfigBuilder::default().build().unwrap(),
879
            TorClientConfigBuilder::default().build().unwrap(),
880
        );
881

            
882
        let parsed = parses_to_defaults(
883
            &uncomment_example_settings(ARTI_EXAMPLE_CONFIG),
884
            WhichExample::New,
885
            true,
886
        );
887
        let parsed_old = parses_to_defaults(
888
            &uncomment_example_settings(OLDEST_SUPPORTED_CONFIG),
889
            WhichExample::Old,
890
            true,
891
        );
892

            
893
        assert_eq!(&parsed, &built_default);
894
        assert_eq!(&parsed_old, &built_default);
895

            
896
        assert_eq!(&default, &built_default);
897
    }
898

            
899
    /// Config file exhaustiveness and default checking
900
    ///
901
    /// `example_file` is a putative configuration file text.
902
    /// It is expected to contain "example lines",
903
    /// which are lines in start with `#` *not followed by whitespace*.
904
    ///
905
    /// This function checks that:
906
    ///
907
    /// Positive check on the example lines that are present.
908
    ///  * `example_file`, when example lines are uncommented, can be parsed.
909
    ///  * The example values are the same as the default values.
910
    ///
911
    /// Check for missing examples:
912
    ///  * Every key `in `TorClientConfig` or `ArtiConfig` has a corresponding example value.
913
    ///  * Except as declared in [`declared_config_exceptions`]
914
    ///  * And also, tolerating absence in the example files of `deprecated` keys
915
    ///
916
    /// It handles straightforward cases, where the example line is in a `[section]`
917
    /// and is something like `#key = value`.
918
    ///
919
    /// More complex keys, eg those which don't appear in "example lines" starting with just `#`,
920
    /// must be dealt with ad-hoc and mentioned in `declared_config_exceptions`.
921
    ///
922
    /// For complex config keys, it may not be sufficient to simply write the default value in
923
    /// the example files (along with perhaps some other information).  In that case,
924
    ///   1. Write a bespoke example (with lines starting `# `) in the config file.
925
    ///   2. Write a bespoke test, to test the parsing of the bespoke example.
926
    ///      This will probably involve using `ExampleSectionLines` and may be quite ad-hoc.
927
    ///      The test function bridges(), below, is a complex worked example.
928
    ///   3. Either add a trivial example for the affected key(s) (starting with just `#`)
929
    ///      or add the affected key(s) to `declared_config_exceptions`
930
    fn exhaustive_1(example_file: &str, which: WhichExample, deprecated: &[String]) {
931
        use InExample::*;
932
        use serde_json::Value as JsValue;
933
        use std::collections::BTreeSet;
934

            
935
        let example = uncomment_example_settings(example_file);
936
        let example: toml::Value = toml::from_str(&example).unwrap();
937
        // dbg!(&example);
938
        let example = serde_json::to_value(example).unwrap();
939
        // dbg!(&example);
940

            
941
        // "Exhaustive" taxonomy of the recognized configuration keys
942
        //
943
        // We use the JSON serialization of the default builders, because Rust's toml
944
        // implementation likes to omit more things, that we want to see.
945
        //
946
        // I'm not sure this is quite perfect but it is pretty good,
947
        // and has found a number of un-exampled config keys.
948
        let exhausts = [
949
            serde_json::to_value(TorClientConfig::builder()).unwrap(),
950
            serde_json::to_value(ArtiConfig::builder()).unwrap(),
951
        ];
952

            
953
        /// This code does *not* record a problem for keys *in* the example file
954
        /// that are unrecognized.  That is handled by the `default_config` test.
955
        #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, derive_more::Display)]
956
        enum ProblemKind {
957
            #[display("recognised by serialisation, but missing from example config file")]
958
            MissingFromExample,
959
            #[display("expected that example config file should contain have this as a table")]
960
            ExpectedTableInExample,
961
            #[display(
962
                "declared exception says this key should be recognised but not in file, but that doesn't seem to be the case"
963
            )]
964
            UnusedException,
965
        }
966

            
967
        #[derive(Default, Debug)]
968
        struct Walk {
969
            current_path: Vec<String>,
970
            problems: Vec<(String, ProblemKind)>,
971
        }
972

            
973
        impl Walk {
974
            /// Records a problem
975
            fn bad(&mut self, kind: ProblemKind) {
976
                self.problems.push((self.current_path.join("."), kind));
977
            }
978

            
979
            /// Recurses, looking for problems
980
            ///
981
            /// Visited for every node in either or both of the starting `exhausts`.
982
            ///
983
            /// `E` is the number of elements in `exhausts`, ie the number of different
984
            /// top-level config types that Arti uses.  Ie, 2.
985
            fn walk<const E: usize>(
986
                &mut self,
987
                example: Option<&JsValue>,
988
                exhausts: [Option<&JsValue>; E],
989
            ) {
990
                assert! { exhausts.into_iter().any(|e| e.is_some()) }
991

            
992
                let example = if let Some(e) = example {
993
                    e
994
                } else {
995
                    self.bad(ProblemKind::MissingFromExample);
996
                    return;
997
                };
998

            
999
                let tables = exhausts.map(|e| e?.as_object());
                // Union of the keys of both exhausts' tables (insofar as they *are* tables)
                let table_keys = tables
                    .iter()
                    .flat_map(|t| t.map(|t| t.keys().cloned()).into_iter().flatten())
                    .collect::<BTreeSet<String>>();
                for key in table_keys {
                    let example = if let Some(e) = example.as_object() {
                        e
                    } else {
                        // At least one of the exhausts was a nonempty table,
                        // but the corresponding example node isn't a table.
                        self.bad(ProblemKind::ExpectedTableInExample);
                        continue;
                    };
                    // Descend the same key in all the places.
                    self.current_path.push(key.clone());
                    self.walk(example.get(&key), tables.map(|t| t?.get(&key)));
                    self.current_path.pop().unwrap();
                }
            }
        }
        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::{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),
        );
    }
}