allow-core 0.1.11

Core types and matching primitives for cargo-allow source exception policies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use super::*;
use std::path::PathBuf;
use std::str::FromStr;

mod date;
mod fingerprint;
mod identity;
mod source_tree_path;

#[test]
fn glob_question_mark_matches_one_unicode_character() {
    assert!(glob_matches_str("docs/?.md", "docs/é.md"));
    assert!(glob_matches_str("docs/??.md", "docs/éx.md"));
    assert!(!glob_matches_str("docs/?.md", "docs/ee.md"));
}

#[test]
fn finding_kind_accepts_hyphenated_cli_aliases() {
    assert_eq!(
        FindingKind::from_str("non-rust"),
        Ok(FindingKind::NonRustFile)
    );
    assert_eq!(
        FindingKind::from_str("lint-exception"),
        Ok(FindingKind::LintException)
    );
    assert_eq!(
        FindingKind::from_str("generated-code"),
        Ok(FindingKind::GeneratedCode)
    );
}

#[test]
fn source_code_kinds_require_selector_identity() {
    assert!(FindingKind::Panic.requires_source_selector_identity());
    assert!(FindingKind::Unsafe.requires_source_selector_identity());
    assert!(FindingKind::LintException.requires_source_selector_identity());
    assert!(!FindingKind::NonRustFile.requires_source_selector_identity());
    assert!(!FindingKind::GeneratedCode.requires_source_selector_identity());
    assert!(!FindingKind::PolicyException.requires_source_selector_identity());
}

#[test]
fn json_escape_covers_quotes_backslashes_whitespace_and_control_chars() {
    assert_eq!(
        json_escape("quote: \" slash: \\ newline:\n tab:\t return:\r bell:\u{0007}"),
        "quote: \\\" slash: \\\\ newline:\\n tab:\\t return:\\r bell:\\u0007"
    );
}

#[test]
fn allow_config_empty_sets_document_defaults() {
    let config = AllowConfig::empty();

    assert_eq!(config.schema_version, "0.1");
    assert_eq!(config.policy, "cargo-allow");
    assert_eq!(config.owner, None);
    assert_eq!(config.status.as_deref(), Some("active"));
    assert_eq!(config.workspace, WorkspaceConfig::default());
    assert_eq!(config.requirements, Requirements::default());
    assert!(config.allow.is_empty());
}

#[test]
fn allow_config_empty_validate_passes() {
    // The documented-empty config must validate: programmatic core consumers
    // building `AllowConfig::empty()` get a valid baseline.
    assert_eq!(AllowConfig::empty().validate(), Ok(()));
}

#[test]
fn validate_rejects_empty_schema_version() {
    let mut config = AllowConfig::empty();
    config.schema_version = "  ".to_string();
    let err = config
        .validate()
        .expect_err("empty schema_version should fail");
    assert!(err.to_string().contains("schema_version must not be empty"));
    assert_eq!(err.kind(), CargoAllowErrorKind::InvalidPolicy);
}

#[test]
fn validate_rejects_unsupported_schema_version() {
    let mut config = AllowConfig::empty();
    config.schema_version = "0.2".to_string();
    let err = config
        .validate()
        .expect_err("unsupported schema_version should fail");
    assert!(
        err.to_string()
            .contains("unsupported policy schema_version `0.2`")
    );
}

#[test]
fn validate_rejects_empty_policy_name() {
    let mut config = AllowConfig::empty();
    config.policy = String::new();
    let err = config.validate().expect_err("empty policy should fail");
    assert!(err.to_string().contains("policy name must not be empty"));
}

#[test]
fn validate_rejects_unsupported_policy_name() {
    let mut config = AllowConfig::empty();
    config.policy = "not-cargo-allow".to_string();
    let err = config
        .validate()
        .expect_err("unsupported policy name should fail");
    assert!(
        err.to_string()
            .contains("unsupported policy `not-cargo-allow`")
    );
}

#[test]
fn validate_rejects_typoed_default_mode() {
    // `no_new` (underscore) is a common typo for `no-new` and must be rejected
    // at the core data-model level, not silently treated as a string.
    let mut config = AllowConfig::empty();
    config.workspace.default_mode = "no_new".to_string();
    let err = config
        .validate()
        .expect_err("typoed default_mode should fail");
    assert!(
        err.to_string()
            .contains("unsupported workspace default_mode `no_new`")
    );
}

#[test]
fn validate_rejects_empty_status() {
    let mut config = AllowConfig::empty();
    config.status = Some("  ".to_string());
    let err = config.validate().expect_err("empty status should fail");
    assert!(err.to_string().contains("policy status must not be empty"));
}

#[test]
fn validate_aggregates_multiple_errors() {
    let mut config = AllowConfig::empty();
    config.schema_version = String::new();
    config.policy = String::new();
    config.workspace.default_mode = "bogus".to_string();
    let err = config
        .validate()
        .expect_err("multiple problems should fail");
    let message = err.to_string();
    assert!(
        message.starts_with("3 policy validation errors:"),
        "{message}"
    );
    assert!(message.contains("schema_version must not be empty"));
    assert!(message.contains("policy name must not be empty"));
    assert!(message.contains("unsupported workspace default_mode `bogus`"));
    assert_eq!(err.diagnostics().len(), 3);
    assert!(
        err.diagnostics()
            .iter()
            .all(|diagnostic| diagnostic.category == "policy_validation")
    );
}

#[test]
fn workspace_mode_round_trips_and_rejects_unknown() {
    for mode in WorkspaceMode::ALL {
        let s = mode.as_str();
        assert_eq!(WorkspaceMode::from_str(s), Ok(*mode), "{s}");
    }
    // The default is `no-new`.
    assert_eq!(WorkspaceMode::default(), WorkspaceMode::NoNew);
    assert_eq!(WorkspaceMode::default().as_str(), "no-new");
    // `no_new` (underscore) must not parse — it is a distinct value from
    // `no-new` and a common copy-paste typo.
    let err = WorkspaceMode::from_str("no_new").expect_err("underscore form must not parse");
    assert!(
        err.to_string()
            .contains("unsupported workspace default_mode `no_new`")
    );
}

#[test]
fn match_status_strings_and_failure_modes_cover_all_statuses() {
    for status in [
        MatchStatus::Matched,
        MatchStatus::New,
        MatchStatus::Stale,
        MatchStatus::Expired,
        MatchStatus::ReviewDue,
        MatchStatus::LocationDrift,
        MatchStatus::Ambiguous,
        MatchStatus::InvalidSelector,
        MatchStatus::MissingRequiredField,
        MatchStatus::EvidenceMissing,
        MatchStatus::BaselineDebt,
    ] {
        let (expected_name, strict_failure, no_new_failure) = match status {
            MatchStatus::Matched => ("matched", false, false),
            MatchStatus::New => ("new", true, true),
            MatchStatus::Stale => ("stale", true, false),
            MatchStatus::Expired => ("expired", true, true),
            MatchStatus::ReviewDue => ("review_due", true, false),
            MatchStatus::LocationDrift => ("location_drift", false, false),
            MatchStatus::Ambiguous => ("ambiguous", true, true),
            MatchStatus::InvalidSelector => ("invalid_selector", true, true),
            MatchStatus::MissingRequiredField => ("missing_required_field", true, true),
            MatchStatus::EvidenceMissing => ("evidence_missing", true, true),
            MatchStatus::BaselineDebt => ("baseline_debt", true, false),
        };

        assert_eq!(status.as_str(), expected_name);
        assert_eq!(status.is_failure_in_strict(), strict_failure, "{status:?}");
        assert_eq!(status.is_failure_in_no_new(), no_new_failure, "{status:?}");
    }
}

#[test]
fn finding_kind_display_and_parser_cover_policy_aliases_and_errors() {
    assert_eq!(FindingKind::PolicyException.to_string(), "policy_exception");
    assert_eq!(
        FindingKind::from_str(" policy-exception "),
        Ok(FindingKind::PolicyException)
    );
    assert_eq!(
        FindingKind::from_str("generated"),
        Ok(FindingKind::GeneratedCode)
    );
    let error = FindingKind::from_str("unknown-kind")
        .unwrap_err()
        .to_string();
    assert!(error.contains("unsupported finding kind `unknown-kind`"));
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    fn path_segment() -> impl Strategy<Value = String> {
        prop_oneof![
            Just(".".to_string()),
            Just("..".to_string()),
            "[A-Za-z0-9_-]{1,8}".prop_map(|s| s),
        ]
    }

    fn relative_path_text() -> impl Strategy<Value = String> {
        prop::collection::vec(path_segment(), 0..12).prop_map(|segments| segments.join("/"))
    }

    fn stable_text() -> impl Strategy<Value = String> {
        "[A-Za-z0-9_:/|.-]{0,24}".prop_map(|s| s)
    }

    fn maybe_stable_text() -> impl Strategy<Value = Option<String>> {
        prop::option::of(stable_text())
    }

    prop_compose! {
        fn structural_identity_strategy()(
            language in stable_text(),
            ast_kind in stable_text(),
            crate_name in maybe_stable_text(),
            module in maybe_stable_text(),
            container in maybe_stable_text(),
            symbol in maybe_stable_text(),
            callee in maybe_stable_text(),
            macro_name in maybe_stable_text(),
            lint in maybe_stable_text(),
            receiver_fingerprint in maybe_stable_text(),
            target_fingerprint in maybe_stable_text(),
            normalized_snippet_hash in maybe_stable_text(),
            line_hint in prop::option::of(any::<u32>()),
            column_hint in prop::option::of(any::<u32>()),
        ) -> StructuralIdentity {
            StructuralIdentity {
                language,
                crate_name,
                module,
                container,
                ast_kind,
                symbol,
                callee,
                macro_name,
                lint,
                receiver_fingerprint,
                target_fingerprint,
                normalized_snippet_hash,
                line_hint,
                column_hint,
            }
        }
    }

    proptest! {
        #[test]
        fn normalize_path_is_idempotent(path in relative_path_text()) {
            let normalized = normalize_path(&path);
            prop_assert_eq!(normalize_path(&normalized), normalized);
        }

        #[test]
        fn normalize_path_removes_current_and_empty_segments(path in relative_path_text()) {
            let normalized = normalize_path(&path);
            prop_assert!(
                normalized.is_empty()
                    || !normalized
                        .split('/')
                        .any(|part| part.is_empty() || part == ".")
            );
        }

        #[test]
        fn double_star_glob_matches_any_number_of_whole_segments(
            prefix in "[A-Za-z0-9_-]{1,8}",
            middle in prop::collection::vec("[A-Za-z0-9_-]{1,8}", 0..6),
            file in r"[A-Za-z0-9_-]{1,8}\.rs",
        ) {
            let pattern = format!("{prefix}/**/*.rs");
            let path = std::iter::once(prefix)
                .chain(middle)
                .chain(std::iter::once(file))
                .collect::<Vec<_>>()
                .join("/");

            prop_assert!(glob_matches_str(&pattern, &path));
        }

        #[test]
        fn subtree_ignore_pattern_does_not_match_shared_prefix_sibling(
            prefix in "[A-Za-z][A-Za-z0-9_-]{0,8}",
            suffix in "[A-Za-z0-9_-]{1,8}",
            leaf in "[A-Za-z0-9_-]{1,8}",
        ) {
            let pattern = format!("{prefix}/**");
            let ignored = format!("{prefix}/{leaf}");
            let sibling = format!("{prefix}{suffix}/{leaf}");
            let patterns = vec![pattern];

            prop_assert!(source_tree_path_is_ignored(&ignored, &patterns));
            prop_assert!(!source_tree_path_is_ignored(&sibling, &patterns));
        }

        #[test]
        fn date_epoch_day_roundtrips(days in 0_i64..2_000_000_i64) {
            let date = SimpleDate::from_days_since_unix_epoch(days);

            prop_assert_eq!(date.days_since_unix_epoch(), days);
            prop_assert_eq!(SimpleDate::parse(&date.to_string()), Some(date));
        }

        #[test]
        fn date_add_days_matches_days_until(
            start_days in -2_000_000_i64..2_000_000_i64,
            delta in -20_000_i64..20_000_i64,
        ) {
            let start = SimpleDate::from_days_since_unix_epoch(start_days);
            let end = start.add_days(delta);

            prop_assert_eq!(start.days_until(end), delta);
            prop_assert_eq!(end.days_since_unix_epoch(), start_days + delta);
        }

        #[test]
        fn stable_hash_hex_has_expected_shape_and_is_deterministic(input in ".{0,256}") {
            let first = stable_hash_hex(&input);
            let second = stable_hash_hex(&input);

            prop_assert_eq!(&first, &second);
            prop_assert!(first.starts_with("fnv1a64:"));
            prop_assert_eq!(first.len(), "fnv1a64:".len() + 16);
            prop_assert!(first["fnv1a64:".len()..].chars().all(|ch| ch.is_ascii_hexdigit()));
        }

        #[test]
        fn stable_identity_keys_ignore_location_hints(
            mut identity in structural_identity_strategy(),
            line_hint in any::<u32>(),
            column_hint in any::<u32>(),
        ) {
            let base = identity.stable_key();
            identity.line_hint = Some(line_hint);
            identity.column_hint = Some(column_hint);

            prop_assert_eq!(identity.stable_key(), base);
        }

        #[test]
        fn finding_identity_keys_ignore_span(
            mut finding_path in relative_path_text(),
            mut identity in structural_identity_strategy(),
            family in maybe_stable_text(),
            line in any::<u32>(),
            column in any::<u32>(),
        ) {
            if finding_path.is_empty() {
                finding_path = "src/lib.rs".to_string();
            }
            identity.line_hint = Some(line);
            identity.column_hint = Some(column);
            let finding = Finding {
                kind: FindingKind::Unsafe,
                family,
                path: PathBuf::from(finding_path),
                span: Some(Span { line, column }),
                identity,
                message: "generated finding".to_string(),
                ledger: None,
            };
            let mut moved = finding.clone();
            moved.span = Some(Span {
                line: line.wrapping_add(1),
                column: column.wrapping_add(1),
            });

            prop_assert_eq!(finding_identity_key(&finding), finding_identity_key(&moved));
        }
    }
}