bpt 0.1.6

Bedrock Linux package manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use crate::{
    constant::*,
    error::*,
    io::*,
    location::{RootDir, adjust_bedrock_local_path_for_prefix, current_bedrock_prefix},
    metadata::*,
    str::*,
};
use camino::Utf8PathBuf;
use nix::unistd::{Group, User, geteuid};
use std::{io::ErrorKind, str::FromStr};

/// bpt configuration
///
/// Organized by sections
#[derive(Debug, PartialEq)]
pub struct BptConf {
    pub general: BptConfGeneral,
    pub build: BptConfBuild,
    pub make_repo: BptConfMakeRepo,
    pub networking: BptConfNetworking,
    pub cache: BptConfCache,
}

#[derive(Debug, PartialEq)]
pub struct BptConfGeneral {
    pub default_archs: Vec<Arch>,
    pub pin_direct_pkgver: bool,
}

#[derive(Debug, PartialEq)]
pub struct BptConfBuild {
    pub unprivileged_user: String,
    pub unprivileged_group: String,
    pub tmp: Utf8PathBuf,
}

#[derive(Debug, PartialEq)]
pub struct BptConfMakeRepo {
    pub archs: Vec<Arch>,
}

#[derive(Debug, PartialEq)]
pub struct BptConfNetworking {
    pub utils: Vec<NetworkingUtil>,
    pub print_stderr: bool,
}

#[derive(Debug, PartialEq)]
pub struct NetworkingUtil(Vec<String>);

#[derive(Debug, PartialEq)]
pub struct BptConfCache {
    pub cache_pkg_max_days: Option<u32>,
    pub cache_src_max_days: Option<u32>,
}

enum Section {
    Preamble, // before any section header
    Build,
    General,
    MakeRepo,
    Networking,
    Cache,
}

impl FromStr for Section {
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "[general]" => Ok(Section::General),
            "[build]" => Ok(Section::Build),
            "[make-repo]" => Ok(Section::MakeRepo),
            "[networking]" => Ok(Section::Networking),
            "[cache]" => Ok(Section::Cache),
            _ => Err("section header"),
        }
    }
}

impl BptConfGeneral {
    pub fn update(&mut self, key: &str, value: &str) -> Result<(), &'static str> {
        match key {
            "default-archs" => {
                self.default_archs = value.parse_arch_list()?;
            }
            "pin-direct-pkgver" => {
                self.pin_direct_pkgver = value.parse_bool()?;
            }
            _ => return Err("[general] key"),
        }
        Ok(())
    }
}

impl BptConfBuild {
    pub fn update(&mut self, key: &str, value: &str) -> Result<(), &'static str> {
        match key {
            "unprivileged-user" => self.unprivileged_user = value.into(),
            "unprivileged-group" => self.unprivileged_group = value.into(),
            "tmp" => self.tmp = Utf8PathBuf::from_str(value).map_err(|_| "file path")?,
            _ => return Err("[build] key"),
        }
        Ok(())
    }
}

impl BptConfMakeRepo {
    pub fn update(&mut self, key: &str, value: &str) -> Result<(), &'static str> {
        match key {
            "archs" => self.archs = value.parse_arch_list()?,
            _ => return Err("[make-repo] key"),
        }
        Ok(())
    }
}

impl BptConfNetworking {
    pub fn update(&mut self, key: &str, value: &str) -> Result<(), &'static str> {
        match key {
            "util" => {
                if !value.split_ascii_whitespace().any(|s| s == "{}") {
                    return Err("netutil description (missing `{}`)");
                }
                let terms = value.split_ascii_whitespace().map(String::from).collect();
                self.utils.push(NetworkingUtil(terms));
            }
            "print-stderr" => self.print_stderr = value.parse_bool()?,
            _ => return Err("[networking] key"),
        }
        Ok(())
    }
}

impl BptConfCache {
    pub fn update(&mut self, key: &str, value: &str) -> Result<(), &'static str> {
        match key {
            "pkg-max-days" => self.cache_pkg_max_days = parse_max_days(value)?,
            "src-max-days" => self.cache_src_max_days = parse_max_days(value)?,
            _ => return Err("[cache] key"),
        }
        Ok(())
    }
}

/// Parse a max-days value: "forever" → None, non-negative integer → Some(n)
fn parse_max_days(value: &str) -> Result<Option<u32>, &'static str> {
    if value == "forever" {
        return Ok(None);
    }
    value
        .parse::<u32>()
        .map(Some)
        .map_err(|_| "non-negative integer or \"forever\"")
}

impl BptConf {
    pub(crate) fn build_credentials_for_euid(
        &self,
        euid: nix::unistd::Uid,
    ) -> Result<Option<ProcessCredentials>, Err> {
        if !euid.is_root() {
            return Ok(None);
        }

        let user_name = self.build.unprivileged_user.clone();
        let group_name = self.build.unprivileged_group.clone();

        let user = User::from_name(&user_name)
            .map_err(|e| Err::InputFieldInvalid("build unprivileged-user", e.to_string()))?
            .ok_or_else(|| {
                Err::InputFieldInvalid(
                    "build unprivileged-user",
                    format!("user `{user_name}` was not found"),
                )
            })?;
        let group = Group::from_name(&group_name)
            .map_err(|e| Err::InputFieldInvalid("build unprivileged-group", e.to_string()))?
            .ok_or_else(|| {
                Err::InputFieldInvalid(
                    "build unprivileged-group",
                    format!("group `{group_name}` was not found"),
                )
            })?;
        let home_dir = Utf8PathBuf::from_path_buf(user.dir.clone()).map_err(|path| {
            Err::InputFieldInvalid(
                "build unprivileged-user",
                format!(
                    "user `{user_name}` has a non-UTF-8 home directory `{}`",
                    path.display()
                ),
            )
        })?;
        let home_dir =
            adjust_bedrock_local_path_for_prefix(&home_dir, current_bedrock_prefix()?.as_deref());

        Ok(Some(ProcessCredentials {
            user_name,
            uid: user.uid,
            gid: group.gid,
            home_dir: home_dir.into_std_path_buf(),
        }))
    }

    pub fn build_credentials(&self) -> Result<Option<ProcessCredentials>, Err> {
        self.build_credentials_for_euid(geteuid())
    }

    pub fn from_root_path(root: &RootDir) -> Result<Self, Err> {
        let path = root.as_path().join(BPT_CONF_PATH);
        let contents = match path.read_small_file_string() {
            Ok(contents) => contents,
            Err(Err::Open(_, e)) if e.kind() == ErrorKind::NotFound => {
                // If file doesn't exist; use hard-coded defaults
                return Ok(Self::default());
            }
            Err(e) => Err(e)?,
        };

        Self::from_file_contents(&contents).loc(path)
    }

    pub fn from_file_contents(contents: &str) -> Result<Self, AnonLocErr> {
        // If any items are unspecified, fall back to the hard-coded default.
        let mut conf = Self::default();

        let mut section = Section::Preamble;

        // Parse networking into a separate struct starting empty; if any utils are specified,
        // they replace the defaults.
        let mut networking = BptConfNetworking {
            utils: Vec::new(),
            print_stderr: conf.networking.print_stderr,
        };

        for (nr, line) in contents.lines().enumerate().map(|(nr, l)| (nr + 1, l)) {
            let line = line.strip_comment();
            if line.is_empty() {
                continue;
            }

            if line.starts_with('[') {
                section = Section::from_str(line)
                    .map_err(|invalid| AnonLocErr::BptConfInvalidLine(nr, invalid, line.into()))?;
                continue;
            }

            let (key, value) = line
                .split_once('=')
                .map(|(key, value)| (key.trim(), value.trim()))
                .ok_or_else(|| {
                    AnonLocErr::BptConfInvalidLine(nr, "\"key = value\" pair", line.into())
                })?;

            match section {
                Section::Preamble => {
                    return Err(AnonLocErr::BptConfInvalidLine(
                        nr,
                        "line before any [section] header",
                        line.into(),
                    ));
                }
                Section::Build => conf.build.update(key, value),
                Section::General => conf.general.update(key, value),
                Section::MakeRepo => conf.make_repo.update(key, value),
                Section::Networking => networking.update(key, value),
                Section::Cache => conf.cache.update(key, value),
            }
            .map_err(|invalid| AnonLocErr::BptConfInvalidLine(nr, invalid, line.to_string()))?;
        }

        // If the config specified any utils, use those instead of the defaults.
        if !networking.utils.is_empty() {
            conf.networking.utils = networking.utils;
        }
        conf.networking.print_stderr = networking.print_stderr;

        Ok(conf)
    }
}

impl Default for BptConf {
    fn default() -> Self {
        Self {
            general: BptConfGeneral {
                default_archs: vec![Arch::noarch, Arch::host()],
                pin_direct_pkgver: false,
            },
            build: BptConfBuild {
                unprivileged_user: "bpt".to_owned(),
                unprivileged_group: "bpt".to_owned(),
                tmp: Utf8PathBuf::from("/tmp"),
            },
            make_repo: BptConfMakeRepo {
                archs: vec![Arch::noarch, Arch::host(), Arch::bbuild],
            },
            networking: BptConfNetworking {
                utils: vec![
                    NetworkingUtil(vec!["curl".into(), "-f".into(), "-L".into(), "{}".into()]),
                    NetworkingUtil(vec!["wget".into(), "-O-".into(), "{}".into()]),
                ],
                print_stderr: false,
            },
            cache: BptConfCache {
                cache_pkg_max_days: Some(90),
                cache_src_max_days: Some(90),
            },
        }
    }
}

trait ParseBool {
    fn parse_bool(&self) -> Result<bool, &'static str>;
}

impl ParseBool for str {
    fn parse_bool(&self) -> Result<bool, &'static str> {
        match self {
            "true" => Ok(true),
            "false" => Ok(false),
            _ => Err("boolean value"),
        }
    }
}

trait ParseArchList {
    fn parse_arch_list(&self) -> Result<Vec<Arch>, &'static str>;
}

impl ParseArchList for str {
    fn parse_arch_list(&self) -> Result<Vec<Arch>, &'static str> {
        self.split(',')
            .map(|field| field.trim())
            .map(|arch| Arch::try_from(arch).map_err(|_| "arch"))
            .collect()
    }
}

impl NetworkingUtil {
    pub fn as_slice(&self) -> &[String] {
        &self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::location::RootDir;
    use crate::testutil::unit_test_tmp_dir;
    use camino::Utf8PathBuf;
    use nix::unistd::Uid;

    #[test]
    fn test_missing_root_config_uses_defaults() {
        let tmp = unit_test_tmp_dir("bpt_conf", "test_missing_root_config_uses_defaults");
        let root = RootDir::from_path(&tmp);

        let conf = BptConf::from_root_path(&root).unwrap();

        assert_eq!(conf, BptConf::default());
    }

    #[test]
    fn test_default_config_file_matches_default_impl() {
        let from_file = BptConf::from_file_contents(
            std::str::from_utf8(include_bytes!("../../assets/default-configs/bpt.conf")).unwrap(),
        )
        .expect("Failed to parse default bpt.conf");

        assert_eq!(from_file, BptConf::default());
    }

    #[test]
    fn test_build_credentials_returns_none_for_non_root() {
        let conf = BptConf::default();
        let creds = conf
            .build_credentials_for_euid(Uid::from_raw(1000))
            .unwrap();
        assert_eq!(creds, None);
    }

    #[test]
    fn test_parse_valid_full_config() {
        let config_str = r#"
            [general]
            default-archs = x86_64, aarch64

            [build]
            unprivileged-user = builder
            unprivileged-group = builders
            tmp = /var/tmp

            [make-repo]
            archs = noarch, host

            [networking]
            util = curl -f -L {}
            util = wget -O- {}
            print-stderr = true

            [cache]
            pkg-max-days = 30
            src-max-days = forever
        "#;

        let parsed_conf =
            BptConf::from_file_contents(config_str).expect("Failed to parse valid full config");

        let expected_conf = BptConf {
            general: BptConfGeneral {
                default_archs: vec![Arch::x86_64, Arch::aarch64],
                pin_direct_pkgver: false,
            },
            build: BptConfBuild {
                unprivileged_user: "builder".to_owned(),
                unprivileged_group: "builders".to_owned(),
                tmp: Utf8PathBuf::from("/var/tmp"),
            },
            make_repo: BptConfMakeRepo {
                archs: vec![Arch::noarch, Arch::host()],
            },
            networking: BptConfNetworking {
                utils: vec![
                    NetworkingUtil(vec!["curl".into(), "-f".into(), "-L".into(), "{}".into()]),
                    NetworkingUtil(vec!["wget".into(), "-O-".into(), "{}".into()]),
                ],
                print_stderr: true,
            },
            cache: BptConfCache {
                cache_pkg_max_days: Some(30),
                cache_src_max_days: None,
            },
        };

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_missing_sections() {
        let config_str = r#"
            [general]
            default-archs = x86_64

            [build]
            unprivileged-user = builder
        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse config with missing sections");

        let mut expected_conf = BptConf::default();
        expected_conf.general.default_archs = vec![Arch::x86_64];
        expected_conf.build.unprivileged_user = "builder".to_owned();
        expected_conf.build.unprivileged_group = "bpt".to_owned();
        expected_conf.build.tmp = Utf8PathBuf::from("/tmp");
        // make_repo, networking, and cache should remain as defaults.

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_invalid_section_header() {
        let config_str = r#"
            [general]
            default-archs = x86_64

            [invalid-section]
            key = value
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 5);
                assert_eq!(invalid, "section header");
                assert_eq!(line, "[invalid-section]");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_invalid_key_in_section() {
        let config_str = r#"
            [build]
            unprivileged-user = builder
            invalid-key = value
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 4);
                assert_eq!(invalid, "[build] key");
                assert_eq!(line, "invalid-key = value");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_invalid_value_type() {
        let config_str = r#"
            [cache]
            pkg-max-days = not_an_integer
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 3);
                assert_eq!(invalid, "non-negative integer or \"forever\"");
                assert_eq!(line, "pkg-max-days = not_an_integer");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_missing_key_value_separator() {
        let config_str = r#"
            [general]
            default-archs x86_64, aarch64
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 3);
                assert_eq!(invalid, "\"key = value\" pair");
                assert_eq!(line, "default-archs x86_64, aarch64");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_invalid_boolean() {
        let config_str = r#"
            [networking]
            print-stderr = not_a_bool
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 3);
                assert_eq!(invalid, "boolean value");
                assert_eq!(line, "print-stderr = not_a_bool");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_with_whitespace_and_comments() {
        let config_str = r#"
            # This is a comment
            [general]
            default-archs = x86_64,    aarch64  # Inline comment

            [build]   # Another comment
            unprivileged-user = builder
            unprivileged-group = builders

            [cache]
            pkg-max-days = 45
        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse config with whitespace and comments");

        let mut expected_conf = BptConf::default();
        expected_conf.general.default_archs = vec![Arch::x86_64, Arch::aarch64];
        expected_conf.build.unprivileged_user = "builder".to_owned();
        expected_conf.build.unprivileged_group = "builders".to_owned();
        expected_conf.cache.cache_pkg_max_days = Some(45);
        // Other fields should remain as defaults.

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_only_comments_and_empty_lines() {
        let config_str = r#"
            # Entire configuration is comments and empty lines

            # Another comment

        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse config with only comments and empty lines");

        let expected_conf = BptConf::default();

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_duplicate_keys() {
        let config_str = r#"
            [build]
            unprivileged-user = builder1
            unprivileged-user = builder2
            tmp = /tmp1
            tmp = /tmp2
        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse config with duplicate keys");

        let mut expected_conf = BptConf::default();
        expected_conf.build.unprivileged_user = "builder2".to_owned();
        expected_conf.build.tmp = Utf8PathBuf::from("/tmp2");
        // Other fields should remain as defaults.

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_networking_utils_missing_placeholder() {
        let config_str = r#"
            [networking]
            util = curl -f -L
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 3);
                assert_eq!(invalid, "netutil description (missing `{}`)");
                assert_eq!(line, "util = curl -f -L");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_invalid_arch_in_general() {
        let config_str = r#"
            [general]
            default-archs = x86_64, invalid_arch
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 3);
                assert_eq!(invalid, "arch");
                assert_eq!(line, "default-archs = x86_64, invalid_arch");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_empty_configuration() {
        let config_str = "";

        let parsed_conf =
            BptConf::from_file_contents(config_str).expect("Failed to parse empty configuration");

        let expected_conf = BptConf::default();

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_only_one_section() {
        let config_str = r#"
            [cache]
            pkg-max-days = 60
            src-max-days = 60
        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse config with only cache section");

        let mut expected_conf = BptConf::default();
        expected_conf.cache.cache_pkg_max_days = Some(60);
        expected_conf.cache.cache_src_max_days = Some(60);
        // Other fields should remain as defaults.

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_cache_forever() {
        let config_str = r#"
            [cache]
            pkg-max-days = forever
            src-max-days = forever
        "#;

        let parsed_conf =
            BptConf::from_file_contents(config_str).expect("Failed to parse cache forever");

        assert_eq!(parsed_conf.cache.cache_pkg_max_days, None);
        assert_eq!(parsed_conf.cache.cache_src_max_days, None);
    }

    #[test]
    fn test_parse_cache_zero() {
        let config_str = r#"
            [cache]
            pkg-max-days = 0
            src-max-days = 0
        "#;

        let parsed_conf =
            BptConf::from_file_contents(config_str).expect("Failed to parse cache zero");

        assert_eq!(parsed_conf.cache.cache_pkg_max_days, Some(0));
        assert_eq!(parsed_conf.cache.cache_src_max_days, Some(0));
    }

    #[test]
    fn test_parse_cache_negative_rejected() {
        let config_str = r#"
            [cache]
            pkg-max-days = -1
        "#;

        let result = BptConf::from_file_contents(config_str);
        assert!(result.is_err());

        match result {
            Err(AnonLocErr::BptConfInvalidLine(nr, invalid, line)) => {
                assert_eq!(nr, 3);
                assert_eq!(invalid, "non-negative integer or \"forever\"");
                assert_eq!(line, "pkg-max-days = -1");
            }
            _ => panic!("Unexpected error variant"),
        }
    }

    #[test]
    fn test_parse_networking_single_util() {
        let config_str = r#"
            [networking]
            util = curl -f -L {}
        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse networking with single util");

        let mut expected_conf = BptConf::default();
        expected_conf.networking.utils = vec![NetworkingUtil(vec![
            "curl".into(),
            "-f".into(),
            "-L".into(),
            "{}".into(),
        ])];
        // Other fields should remain as defaults.

        assert_eq!(&parsed_conf, &expected_conf);
    }

    #[test]
    fn test_parse_networking_no_utils() {
        let config_str = r#"
            [networking]
            print-stderr = true
        "#;

        let parsed_conf = BptConf::from_file_contents(config_str)
            .expect("Failed to parse networking with no utils");

        let mut expected_conf = BptConf::default();
        expected_conf.networking.print_stderr = true;
        // utils should remain as defaults.

        assert_eq!(&parsed_conf, &expected_conf);
    }
}