sqc 0.4.84

Software Code Quality - CERT C compliance checker
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
// Integration tests for CERT C rules
// Tests are auto-generated by build.rs from C test files in src/rules/cert_c/CATEGORY/RULE-ID/tests/
//
// To add a new test:
// 1. Drop a .c file in the appropriate src/rules/cert_c/CATEGORY/RULE-ID/tests/fail/ or pass/ directory
// 2. Run: cargo build (regenerates tests)
// 3. Run: cargo test (executes all tests including the new one)
//
// Debugging a failing generated test:
//   The assertion message already prints everything needed to triage a failure:
//     - `source:` / `at:` — a clickable `<path>:<line>` jump to the fixture (for a
//       pass-test false positive, the line is the exact offending finding).
//     - `reproduce:` — a copy-paste command to re-run the rule against just that one
//       file: `cargo run -- --rules <RULE-ID> <path>`.
//     - `about:` — the fixture's wiki-derived Description/Source, linking it back to
//       the CERT C example it was scraped from.
//   Do NOT add `println!`/`dbg!` debugging into the generated test bodies under
//   OUT_DIR — they are overwritten on every `cargo build`. Instead use the
//   `reproduce:` command above and add instrumentation in the rule implementation.
//

use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Mutex;

lazy_static::lazy_static! {
    static ref TEST_RESULTS: Mutex<BTreeMap<String, TestResult>> = Mutex::new(BTreeMap::new());
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
struct TestResult {
    passed: bool,
    expected_to_fail: bool,
}

// Record a test result
pub fn record_test_result(test_name: &str, passed: bool, expected_to_fail: bool) {
    let mut results = TEST_RESULTS.lock().unwrap_or_else(|e| e.into_inner());
    results.insert(
        test_name.to_string(),
        TestResult {
            passed,
            expected_to_fail,
        },
    );
}

// Generate test summary report AFTER running tests
#[cfg(test)]
#[ctor::dtor]
fn generate_test_report() {
    if let Err(e) = create_test_summary_report() {
        eprintln!("Warning: Failed to generate test summary report: {}", e);
    }
}

fn create_test_summary_report() -> Result<(), Box<dyn std::error::Error>> {
    let output_path = PathBuf::from("docs/test-summary.md");

    // Ensure docs directory exists
    if let Some(parent) = output_path.parent() {
        fs::create_dir_all(parent)?;
    }

    let (categories, total_rules, implemented_rules, total_tests) = collect_rule_data()?;

    // Generate markdown report
    let mut report = String::new();
    report.push_str("# CERT C Rules Test Summary\n\n");
    render_overview(&mut report, total_rules, implemented_rules, total_tests);
    render_toc(&mut report, &categories);
    render_detail_sections(&mut report, &categories);
    render_summary_table(&mut report, &categories);

    fs::write(&output_path, report)?;
    println!("Generated test summary report: {}", output_path.display());
    Ok(())
}

/// Walk `src/rules/cert_c/CATEGORY/RULE-ID`, parse each rule's TOML and test files,
/// and return the per-category rule data plus aggregate counts
/// (total_rules, implemented_rules, total_tests).
#[allow(clippy::type_complexity)]
fn collect_rule_data(
) -> Result<(BTreeMap<String, Vec<RuleInfo>>, usize, usize, usize), Box<dyn std::error::Error>> {
    let cert_c_dir = PathBuf::from("src/rules/cert_c");

    let mut categories: BTreeMap<String, Vec<RuleInfo>> = BTreeMap::new();
    let mut total_rules = 0;
    let mut implemented_rules = 0;
    let mut total_tests = 0;

    for category_entry in fs::read_dir(&cert_c_dir)? {
        let category_entry = category_entry?;
        let category_path = category_entry.path();

        if !category_path.is_dir() {
            continue;
        }

        let category_name = category_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("Unknown")
            .to_string();

        // Skip special directories
        if category_name.starts_with('.') {
            continue;
        }

        for rule_entry in fs::read_dir(&category_path)? {
            let rule_entry = rule_entry?;
            let rule_path = rule_entry.path();

            if !rule_path.is_dir() {
                continue;
            }

            let rule_id = rule_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("Unknown")
                .to_string();

            // Read TOML metadata
            let toml_path = rule_path.join(format!("{}.toml", rule_id));
            let (title, description, is_enabled) = parse_rule_toml(&toml_path);

            // Count test files and look up their results
            let tests_dir = rule_path.join("tests");
            let mut fail_tests = collect_test_cases(&tests_dir.join("fail"), &rule_id, "fail")?;
            let mut pass_tests = collect_test_cases(&tests_dir.join("pass"), &rule_id, "pass")?;

            fail_tests.sort_by(|a, b| a.name.cmp(&b.name));
            pass_tests.sort_by(|a, b| a.name.cmp(&b.name));

            total_tests += fail_tests.len() + pass_tests.len();
            total_rules += 1;
            if is_enabled {
                implemented_rules += 1;
            }

            let rule_info = RuleInfo {
                id: rule_id,
                category: category_name.clone(),
                title,
                description,
                is_enabled,
                fail_tests,
                pass_tests,
            };

            categories
                .entry(category_name.clone())
                .or_default()
                .push(rule_info);
        }
    }

    Ok((categories, total_rules, implemented_rules, total_tests))
}

/// Parse a rule's TOML for its title, multi-line description, and enabled flag.
/// Returns defaults if the file is missing or fields are absent.
fn parse_rule_toml(toml_path: &Path) -> (String, String, bool) {
    let Ok(content) = fs::read_to_string(toml_path) else {
        return ("No title".to_string(), "No description".to_string(), false);
    };

    let title = content
        .lines()
        .find(|line| line.trim().starts_with("title = "))
        .and_then(|line| line.split('=').nth(1))
        .map(|s| s.trim().trim_matches('"').to_string())
        .unwrap_or_else(|| "No title".to_string());

    // Extract multi-line description
    let description = if let Some(desc_start) = content.find("description = \"\"\"") {
        let desc_content = &content[desc_start + 18..];
        if let Some(desc_end) = desc_content.find("\"\"\"") {
            desc_content[..desc_end].trim().to_string()
        } else {
            "No description".to_string()
        }
    } else {
        "No description".to_string()
    };

    let enabled = content.contains("enabled = true");
    (title, description, enabled)
}

/// Collect the `.c` test fixtures in one test-type directory (`fail`/`pass`),
/// pairing each with its recorded test result. Missing directories yield an empty list.
fn collect_test_cases(
    dir: &Path,
    rule_id: &str,
    test_type: &str,
) -> Result<Vec<TestCaseInfo>, Box<dyn std::error::Error>> {
    let mut tests = Vec::new();
    if !dir.exists() {
        return Ok(tests);
    }

    let results = TEST_RESULTS.lock().unwrap_or_else(|e| e.into_inner());

    for test_file in fs::read_dir(dir)? {
        let test_file = test_file?;
        if test_file.path().extension().is_some_and(|e| e == "c") {
            let file_name = test_file.file_name().to_string_lossy().to_string();
            let func_name = test_func_name(rule_id, test_type, &file_name);
            let result = results.get(&func_name).cloned();
            tests.push(TestCaseInfo {
                name: file_name,
                result,
            });
        }
    }

    Ok(tests)
}

/// Build the generated test function name for a fixture, e.g.
/// `test_arr00_c_fail_wiki_noncompliant_1` from rule `ARR00-C`, type `fail`, file `wiki-noncompliant-1.c`.
fn test_func_name(rule_id: &str, test_type: &str, file_name: &str) -> String {
    let test_name = file_name.trim_end_matches(".c");
    format!(
        "test_{}_{}_{}",
        rule_id.to_lowercase().replace("-", "_"),
        test_type,
        test_name.replace("-", "_").replace(".", "_")
    )
}

/// Status icon and label for a rule given its computed test total.
fn rule_status(rule: &RuleInfo, total: usize) -> (&'static str, &'static str) {
    if rule.is_enabled {
        ("", "Implemented")
    } else if total > 0 {
        ("🔶", "Not Implemented (has tests)")
    } else {
        ("", "Not Implemented (no tests)")
    }
}

/// Render the top-level Overview section with aggregate counts.
fn render_overview(
    report: &mut String,
    total_rules: usize,
    implemented_rules: usize,
    total_tests: usize,
) {
    report.push_str("## Overview\n\n");
    report.push_str(&format!("- **Total Rules:** {}\n", total_rules));
    report.push_str(&format!(
        "- **Implemented Rules:** {} ({:.1}%)\n",
        implemented_rules,
        (implemented_rules as f64 / total_rules as f64) * 100.0
    ));
    report.push_str(&format!("- **Total Test Cases:** {}\n", total_tests));
    report.push_str(&format!(
        "- **Average Tests per Rule:** {:.1}\n\n",
        total_tests as f64 / total_rules as f64
    ));
}

/// Render the Table of Contents linking each category and rule.
fn render_toc(report: &mut String, categories: &BTreeMap<String, Vec<RuleInfo>>) {
    report.push_str("## Table of Contents\n\n");
    for (category, rules) in categories {
        let implemented_count = rules.iter().filter(|r| r.is_enabled).count();
        report.push_str(&format!(
            "- [{}](#category-{}) ({} implemented / {} total)\n",
            category,
            category.to_lowercase(),
            implemented_count,
            rules.len()
        ));
        for rule in rules {
            let (passed, total, not_run) = calculate_rule_metrics(rule);
            let (status_icon, status_text) = rule_status(rule, total);

            let pass_rate = if total > 0 {
                format!(
                    "{}/{} ({:.1}%)",
                    passed,
                    total,
                    (passed as f64 / total as f64) * 100.0
                )
            } else {
                "0/0 (N/A)".to_string()
            };
            let not_run_str = if not_run > 0 {
                format!(" [{} not run]", not_run)
            } else {
                String::new()
            };
            report.push_str(&format!(
                "  - {} [{}](#rule-{}) - {}: Pass {}{}\n",
                status_icon,
                rule.id,
                rule.id.to_lowercase().replace("-", ""),
                status_text,
                pass_rate,
                not_run_str
            ));
        }
    }
    report.push('\n');
}

/// Render the per-category detailed sections.
fn render_detail_sections(report: &mut String, categories: &BTreeMap<String, Vec<RuleInfo>>) {
    for (category, rules) in categories {
        report.push_str(&format!("## Category: {}\n\n", category));
        report.push_str(&format!(
            "<a id=\"category-{}\"></a>\n\n",
            category.to_lowercase()
        ));

        let implemented_count = rules.iter().filter(|r| r.is_enabled).count();
        report.push_str(&format!(
            "**Implementation Status:** {} / {} rules ({:.1}%)\n\n",
            implemented_count,
            rules.len(),
            (implemented_count as f64 / rules.len() as f64) * 100.0
        ));

        for rule in rules {
            render_rule_detail(report, rule);
        }
    }
}

/// Render the detailed block for a single rule (header, metadata, results, test lists).
fn render_rule_detail(report: &mut String, rule: &RuleInfo) {
    let (passed, total, not_run) = calculate_rule_metrics(rule);
    let test_count = rule.fail_tests.len() + rule.pass_tests.len();
    let (status_icon, status_text) = rule_status(rule, total);

    report.push_str(&format!(
        "### {} {} - {}\n\n",
        status_icon, rule.id, status_text
    ));
    report.push_str(&format!(
        "<a id=\"rule-{}\"></a>\n\n",
        rule.id.to_lowercase().replace("-", "")
    ));
    report.push_str(&format!("**Title:** {}\n\n", rule.title));
    report.push_str(&format!("**Description:** {}\n\n", rule.description));
    report.push_str(&format!(
        "**Test Coverage:** {} tests ({} fail, {} pass)\n\n",
        test_count,
        rule.fail_tests.len(),
        rule.pass_tests.len()
    ));

    if total > 0 {
        let pass_rate = (passed as f64 / total as f64) * 100.0;
        report.push_str(&format!(
            "**Test Results:** {}/{} passed ({:.1}%)",
            passed, total, pass_rate
        ));
        if not_run > 0 {
            report.push_str(&format!(", {} not run", not_run));
        }
        report.push_str("\n\n");
    }

    render_test_list(
        report,
        rule,
        &rule.fail_tests,
        "fail",
        "#### Fail Tests (Should Detect Violations)\n\n",
    );
    render_test_list(
        report,
        rule,
        &rule.pass_tests,
        "pass",
        "#### Pass Tests (Should NOT Detect Violations)\n\n",
    );

    report.push_str("---\n\n");
}

/// Render a list of test cases (fail or pass) with their pass/fail/not-run status.
fn render_test_list(
    report: &mut String,
    rule: &RuleInfo,
    tests: &[TestCaseInfo],
    test_type: &str,
    header: &str,
) {
    if tests.is_empty() {
        return;
    }
    report.push_str(header);
    for test in tests {
        let func_name = test_func_name(&rule.id, test_type, &test.name);
        let status = match &test.result {
            Some(result) if result.passed => "✅ PASS",
            Some(_) => "❌ FAIL",
            None => "⏭️ NOT RUN",
        };
        report.push_str(&format!("- {} `{}` → `{}`\n", status, test.name, func_name));
    }
    report.push('\n');
}

/// Render the closing per-category summary table.
fn render_summary_table(report: &mut String, categories: &BTreeMap<String, Vec<RuleInfo>>) {
    report.push_str("## Summary by Category\n\n");
    report.push_str("| Category | Rules | Implemented | Tests | Avg Tests/Rule |\n");
    report.push_str("|----------|-------|-------------|-------|----------------|\n");

    for (category, rules) in categories {
        let implemented = rules.iter().filter(|r| r.is_enabled).count();
        let tests: usize = rules
            .iter()
            .map(|r| r.fail_tests.len() + r.pass_tests.len())
            .sum();
        let avg = tests as f64 / rules.len() as f64;
        report.push_str(&format!(
            "| {} | {} | {} | {} | {:.1} |\n",
            category,
            rules.len(),
            implemented,
            tests,
            avg
        ));
    }
    report.push('\n');
}

fn calculate_rule_metrics(rule: &RuleInfo) -> (usize, usize, usize) {
    let mut passed = 0;
    let mut total = 0;
    let mut not_run = 0;

    // For both fail and pass tests: if result.passed is true, the test behaved correctly
    for test in &rule.fail_tests {
        total += 1;
        if let Some(result) = &test.result {
            if result.passed {
                passed += 1;
            }
        } else {
            not_run += 1;
        }
    }

    for test in &rule.pass_tests {
        total += 1;
        if let Some(result) = &test.result {
            if result.passed {
                passed += 1;
            }
        } else {
            not_run += 1;
        }
    }

    (passed, total, not_run)
}

#[derive(Debug, Clone)]
struct TestCaseInfo {
    name: String,
    result: Option<TestResult>,
}

#[derive(Debug)]
#[allow(dead_code)]
struct RuleInfo {
    id: String,
    category: String,
    title: String,
    description: String,
    is_enabled: bool,
    fail_tests: Vec<TestCaseInfo>,
    pass_tests: Vec<TestCaseInfo>,
}

// Include the auto-generated test functions from build.rs
include!(concat!(env!("OUT_DIR"), "/integration_tests.rs"));