use mockforge_core::conditions::{evaluate_condition, ConditionContext};
use mockforge_core::routing::{HttpMethod, Route, RouteRegistry};
use mockforge_core::templating::expand_str;
use mockforge_core::validation::validate_json_schema;
use serde_json::json;
#[cfg(test)]
mod malformed_input_tests {
use super::*;
#[test]
fn condition_evaluation_with_malformed_jsonpath() {
let context = ConditionContext::new();
let malformed_paths = vec![
"$.",
"$..",
"$[",
"$]",
"$.field[",
"$.field[]",
"$[invalid]",
"$.field..nested",
];
for path in malformed_paths {
let result = evaluate_condition(path, &context);
let _ = result;
}
}
#[test]
fn condition_evaluation_with_malformed_logical_operators() {
let context = ConditionContext::new();
let malformed = vec![
"AND(", "OR(", "NOT(", "AND())", "OR(,)", "AND(OR(", "NOT(NOT(",
];
for condition in malformed {
let result = evaluate_condition(condition, &context);
let _ = result;
}
}
#[test]
fn route_matching_with_malformed_paths() {
let mut registry = RouteRegistry::new();
let route = Route::new(HttpMethod::GET, "/api/users".to_string());
registry.add_http_route(route).unwrap();
let malformed_paths = vec![
"",
"//",
"/api//users",
"/api/users/",
"api/users", "/api/users?query=test", "/api/users#fragment", ];
for path in malformed_paths {
let _ = registry.find_http_routes(&HttpMethod::GET, path);
}
}
#[test]
fn validation_with_malformed_schemas() {
let malformed_schemas = vec![
json!({}), json!({"type": "invalid_type"}),
json!({"type": "object", "properties": null}),
json!({"type": "array", "items": null}),
json!({"type": "string", "pattern": "[invalid regex"}),
json!({"type": "number", "minimum": "not a number"}),
json!({"allOf": null}),
json!({"oneOf": []}),
json!({"$ref": "#/definitions/nonexistent"}),
];
let test_data = json!({"test": "value"});
for schema in malformed_schemas {
let result = validate_json_schema(&test_data, &schema);
let _ = result;
}
}
#[test]
fn template_expansion_with_malformed_templates() {
let malformed = vec![
"{{",
"}}",
"{{{",
"}}}",
"{{{{",
"{{.}}",
"{{..}}",
"{{/etc/passwd}}",
"{{../../etc/passwd}}",
"{{field",
"field}}",
];
for template in malformed {
let _ = expand_str(template);
}
}
}
#[cfg(test)]
mod large_payload_tests {
use super::*;
#[test]
fn condition_evaluation_with_very_large_input() {
let context = ConditionContext::new();
let large_condition = "a".repeat(1_000_000);
let result = evaluate_condition(&large_condition, &context);
let _ = result;
}
#[test]
fn route_matching_with_very_long_path() {
let mut registry = RouteRegistry::new();
let route = Route::new(HttpMethod::GET, "/api/users".to_string());
registry.add_http_route(route).unwrap();
let long_path = "/".to_string() + &"a".repeat(100_000);
let _ = registry.find_http_routes(&HttpMethod::GET, &long_path);
}
#[test]
fn validation_with_very_large_schema() {
let mut properties = serde_json::Map::new();
for i in 0..10_000 {
properties.insert(format!("field_{}", i), json!({"type": "string"}));
}
let large_schema = json!({
"type": "object",
"properties": properties
});
let test_data = json!({"field_0": "value"});
let result = validate_json_schema(&test_data, &large_schema);
let _ = result;
}
#[test]
fn template_expansion_with_very_large_template() {
let large_template = "{{field}} ".repeat(100_000);
let _ = expand_str(&large_template);
}
}
#[cfg(test)]
mod invalid_utf8_tests {
use super::*;
#[test]
fn condition_evaluation_handles_invalid_utf8_gracefully() {
let context = ConditionContext::new();
let invalid_utf8 = vec![
&[0xFF, 0xFE, 0xFD][..],
&[0xC0, 0x80][..], &[0xE0, 0x80, 0x80][..], ];
for bytes in invalid_utf8 {
if let Ok(condition_str) = std::str::from_utf8(bytes) {
let _ = evaluate_condition(condition_str, &context);
}
}
}
#[test]
fn validation_handles_invalid_utf8_in_strings() {
let schema = json!({
"type": "object",
"properties": {
"field": {"type": "string"}
}
});
let test_cases = vec![
json!({"field": "\u{0000}"}), json!({"field": "\u{FFFD}"}), json!({"field": "\u{1F4A9}"}), ];
for test_data in test_cases {
let _ = validate_json_schema(&test_data, &schema);
}
}
}
#[cfg(test)]
mod resource_exhaustion_tests {
use super::*;
#[test]
fn condition_evaluation_with_deeply_nested_conditions() {
let context = ConditionContext::new();
let mut nested = "true".to_string();
for _ in 0..100 {
nested = format!("AND({})", nested);
}
let result = evaluate_condition(&nested, &context);
let _ = result;
}
#[test]
fn route_registry_with_many_routes() {
let mut registry = RouteRegistry::new();
for i in 0..10_000 {
let route = Route::new(HttpMethod::GET, format!("/api/route_{}", i));
let _ = registry.add_http_route(route);
}
let matches = registry.find_http_routes(&HttpMethod::GET, "/api/route_0");
assert!(!matches.is_empty());
}
#[test]
fn validation_with_deeply_nested_schemas() {
let mut nested = json!({"type": "string"});
for _ in 0..100 {
nested = json!({
"type": "object",
"properties": {
"nested": nested
}
});
}
let test_data = json!({"nested": "value"});
let result = validate_json_schema(&test_data, &nested);
let _ = result;
}
}
#[cfg(test)]
mod edge_case_tests {
use super::*;
#[test]
fn condition_evaluation_with_empty_context() {
let context = ConditionContext::new();
let conditions = vec![
"",
"true",
"false",
"$.field",
"headers.test == value",
"query.param == value",
];
for condition in conditions {
let _ = evaluate_condition(condition, &context);
}
}
#[test]
fn route_matching_with_special_characters() {
let mut registry = RouteRegistry::new();
let special_routes = vec![
"/api/test%20path",
"/api/test+path",
"/api/test@path",
"/api/test#path",
"/api/test$path",
];
for route_path in special_routes {
let route = Route::new(HttpMethod::GET, route_path.to_string());
let _ = registry.add_http_route(route);
}
}
#[test]
fn validation_with_null_values() {
let schema = json!({
"type": "object",
"properties": {
"field": {"type": "string"}
}
});
let test_cases = vec![
json!({"field": null}),
json!({"field": "value"}),
json!({}), ];
for test_data in test_cases {
let _ = validate_json_schema(&test_data, &schema);
}
}
#[test]
fn template_expansion_with_empty_context() {
let templates = vec!["{{field}}", "{{nested.field}}", "{{array.0}}"];
for template in templates {
let _ = expand_str(template);
}
}
}
#[cfg(test)]
mod concurrent_access_tests {
use super::*;
use std::sync::Arc;
use std::thread;
#[test]
fn route_registry_concurrent_access() {
let registry = Arc::new(RouteRegistry::new());
let mut handles = vec![];
for i in 0..10 {
let _registry_clone = Arc::clone(®istry);
let handle = thread::spawn(move || {
for j in 0..100 {
let mut reg = RouteRegistry::new();
let route = Route::new(HttpMethod::GET, format!("/api/route_{}_{}", i, j));
let _ = reg.add_http_route(route);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}
#[test]
fn condition_evaluation_concurrent_access() {
let context = Arc::new(ConditionContext::new());
let mut handles = vec![];
for _ in 0..10 {
let context_clone = Arc::clone(&context);
let handle = thread::spawn(move || {
for _ in 0..100 {
let _ = evaluate_condition("true", &context_clone);
}
});
handles.push(handle);
}
for handle in handles {
handle.join().unwrap();
}
}
}