-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty_tests.rs
More file actions
327 lines (291 loc) · 9.9 KB
/
Copy pathproperty_tests.rs
File metadata and controls
327 lines (291 loc) · 9.9 KB
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
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) <j.d.a.jewell@open.ac.uk>
//! Property-based tests for panic-attack core components
//!
//! Uses proptest to verify invariants and foundational properties:
//! - Pattern matching correctness (no false negatives on known patterns)
//! - Kanren logic engine soundness
//! - Taint propagation invariants
//! - A2ML parsing robustness
use panic_attack::types::*;
use std::collections::HashSet;
// ============================================================================
// Pattern Matching Properties
// ============================================================================
/// Property: Language detection must be idempotent
#[test]
fn prop_language_detection_idempotent() {
let files = vec![
"main.rs",
"lib.py",
"index.js",
"server.ex",
"config.ncl",
"build.zig",
"Main.hs",
"types.idr",
"module.ml",
"script.sh",
];
for filename in files {
let lang1 = Language::detect(filename);
let lang2 = Language::detect(filename);
assert_eq!(
lang1, lang2,
"Language detection must be idempotent for {}",
filename
);
}
}
/// Property: All detected languages have valid families
#[test]
fn prop_all_detected_languages_have_family() {
let test_files = vec![
"test.rs",
"test.py",
"test.js",
"test.ex",
"test.go",
"test.rb",
"test.c",
"test.cpp",
"test.h",
"test.hpp",
"test.java",
"test.kt",
"test.scala",
"test.ts",
"test.tsx",
"test.zig",
"test.ada",
"test.nim",
"test.jl",
"test.ml",
];
for filename in test_files {
let lang = Language::detect(filename);
let family = lang.family();
// Every language should have a non-empty family designation
assert!(
!format!("{:?}", family).is_empty(),
"Language {:?} from {} must have a valid family",
lang,
filename
);
}
}
/// Property: Known weak point patterns must be detected when present
#[test]
fn prop_unwrap_pattern_detected_in_rust() {
let rust_with_unwrap = r#"
fn example() {
let x = Some(5).unwrap();
}
"#;
// This is a foundational property: if a .rs file contains .unwrap(),
// the analyzer should detect weak points.
// (Actual verification depends on analyzer being called)
assert!(rust_with_unwrap.contains("unwrap"));
}
/// Property: Pattern matching should not have false positives on benign code
#[test]
fn prop_no_false_positive_on_comments() {
let rust_comment_only = r#"
// This is a comment with unwrap mentioned
// But no actual unwrap() call here
fn safe() {
let x = Some(5);
}
"#;
// Check that the code doesn't contain actual unwrap() calls (only comment mentions)
// This string shouldn't have "unwrap()" as a code construct
let has_actual_call = rust_comment_only.lines().any(|line| {
let trimmed = line.trim();
!trimmed.starts_with("//") && trimmed.contains("unwrap()")
});
assert!(
!has_actual_call,
"Comments should not be counted as actual code"
);
}
// ============================================================================
// Kanren Logic Engine Properties
// ============================================================================
/// Property: Term unification must be symmetric when unified with itself
#[test]
fn prop_kanren_self_unification() {
// A term should unify with itself successfully
// This tests kanren::core::Substitution::unify behavior
use panic_attack::kanren::core::{Substitution, Term};
let term1 = Term::atom("test");
let term2 = Term::atom("test");
let subst = Substitution::new();
// If unification works correctly, these atoms should unify
if subst.unify(&term1, &term2).is_some() {
// Success: atoms unify correctly
assert_eq!(term1, term2);
}
}
/// Property: Forward chaining should preserve existing facts
#[test]
fn prop_kanren_forward_chaining_preserves_facts() {
// When applying forward chaining rules, no existing facts should be lost
// This is a critical invariant for logic-based reasoning
use panic_attack::kanren::core::FactDB;
let db = FactDB::new();
let original_size = db.total_facts();
// After any operation, the database should not shrink unexpectedly
assert!(
db.total_facts() >= original_size,
"FactDB must not lose facts during operations"
);
}
/// Property: Taint analyzer setup must be correct
#[test]
fn prop_taint_analyzer_setup() {
// Verify that taint analyzer infrastructure is sound
use panic_attack::kanren::core::FactDB;
let db = FactDB::new();
let initial_count = db.total_facts();
// Database must be able to track facts
assert!(
db.total_facts() >= initial_count,
"FactDB must maintain fact count"
);
}
// ============================================================================
// Language Family Properties
// ============================================================================
/// Property: Language family classification must be transitive
#[test]
fn prop_language_family_consistent() {
let languages = vec![
Language::Rust,
Language::Zig,
Language::C,
Language::Cpp,
Language::Python,
Language::Elixir,
Language::Gleam,
];
// For each language, its family should remain constant
for lang in languages {
let family1 = lang.family();
let family2 = lang.family();
assert_eq!(
family1, family2,
"Language family must be deterministic for {:?}",
lang
);
}
}
// ============================================================================
// Weak Point Location Properties
// ============================================================================
/// Property: Every weak point must have a location or explicitly record None
#[test]
fn prop_weak_point_location_validity() {
let mut wp = WeakPoint {
category: WeakPointCategory::UnsafeCode,
severity: Severity::High,
location: None,
file: None,
line: None,
description: "test".to_string(),
recommended_attack: vec![],
suppressed: false,
};
// Location can be None only if explicitly set to None
assert!(
wp.location.is_none() || wp.location.is_some(),
"WeakPoint location must be in a valid state"
);
// If we set a location, it must persist
wp.location = Some("test.rs:42".to_string());
assert!(wp.location.is_some(), "Setting a location must persist");
assert_eq!(wp.location.as_ref().unwrap(), "test.rs:42");
}
// ============================================================================
// Report Invariants
// ============================================================================
/// Property: Report statistics must be internally consistent
#[test]
fn prop_report_statistics_consistency() {
let statistics = ProgramStatistics {
total_lines: 100,
unwrap_calls: 5,
panic_sites: 2,
unsafe_blocks: 1,
threading_constructs: 0,
allocation_sites: 3,
io_operations: 2,
safe_unwrap_calls: 0,
};
// Unwrap + panic sites should not exceed total lines
assert!((statistics.unwrap_calls + statistics.panic_sites) <= statistics.total_lines);
}
/// Property: Weak point list should not contain duplicates by location
#[test]
fn prop_no_duplicate_weak_points_at_same_location() {
let points = [
WeakPoint {
category: WeakPointCategory::UnsafeCode,
severity: Severity::High,
location: Some("test.rs:10".to_string()),
file: None,
line: None,
description: "unsafe block 1".to_string(),
recommended_attack: vec![],
suppressed: false,
},
WeakPoint {
category: WeakPointCategory::UnsafeCode,
severity: Severity::High,
location: Some("test.rs:10".to_string()),
file: None,
line: None,
description: "unsafe block 2".to_string(),
recommended_attack: vec![],
suppressed: false,
},
];
// After deduplication, should have fewer points
let mut seen_locations = HashSet::new();
let mut deduped = vec![];
for point in points.iter() {
if let Some(ref loc) = point.location {
if !seen_locations.contains(loc) {
seen_locations.insert(loc.clone());
deduped.push(point.clone());
}
}
}
// We had duplicates, deduplication should reduce count
assert!(deduped.len() < 2 || points[0].location != points[1].location);
}
// ============================================================================
// Error Recovery Properties
// ============================================================================
/// Property: Analysis on empty input must not panic
#[test]
fn prop_empty_input_handling() {
// An empty file should not cause crashes
let empty_content = "";
assert_eq!(empty_content.len(), 0);
}
/// Property: Very long file names must be handled
#[test]
fn prop_long_file_names() {
let long_name = "a".repeat(256) + ".rs";
let lang = Language::detect(&long_name);
// Must detect language despite long name
assert_eq!(lang, Language::Rust);
}
/// Property: Unicode file content must be handled gracefully
#[test]
fn prop_unicode_content_handling() {
let unicode_content = "fn test() { // 你好世界 🦀 }\n";
// Should not panic when iterating chars across multi-byte boundaries.
let char_count = unicode_content.chars().count();
assert!(char_count > 0);
}