1use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11 MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21 SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24 topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30 load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37pub fn parse_header_string(input: &str) -> Result<HashMap<String, String>> {
45 let mut headers = HashMap::new();
46
47 for pair in input.split(',') {
48 let parts: Vec<&str> = pair.splitn(2, ':').collect();
49 if parts.len() != 2 {
50 return Err(BenchError::Other(format!(
51 "Invalid header format: '{}'. Expected 'Key:Value'",
52 pair
53 )));
54 }
55 headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
56 }
57
58 Ok(headers)
59}
60
61pub struct BenchCommand {
63 pub spec: Vec<PathBuf>,
65 pub spec_dir: Option<PathBuf>,
67 pub merge_conflicts: String,
69 pub spec_mode: String,
71 pub dependency_config: Option<PathBuf>,
73 pub target: String,
74 pub base_path: Option<String>,
77 pub duration: String,
78 pub vus: u32,
79 pub target_rps: Option<u32>,
85 pub no_keep_alive: bool,
90 pub scenario: String,
91 pub operations: Option<String>,
92 pub exclude_operations: Option<String>,
96 pub auth: Option<String>,
97 pub headers: Option<String>,
98 pub output: PathBuf,
99 pub generate_only: bool,
100 pub script_output: Option<PathBuf>,
101 pub threshold_percentile: String,
102 pub threshold_ms: u64,
103 pub max_error_rate: f64,
104 pub verbose: bool,
105 pub skip_tls_verify: bool,
106 pub chunked_request_bodies: bool,
111 pub targets_file: Option<PathBuf>,
113 pub max_concurrency: Option<u32>,
115 pub results_format: String,
117 pub params_file: Option<PathBuf>,
122
123 pub crud_flow: bool,
126 pub flow_config: Option<PathBuf>,
128 pub extract_fields: Option<String>,
130
131 pub parallel_create: Option<u32>,
134
135 pub data_file: Option<PathBuf>,
138 pub data_distribution: String,
140 pub data_mappings: Option<String>,
142 pub per_uri_control: bool,
144
145 pub error_rate: Option<f64>,
148 pub error_types: Option<String>,
150
151 pub security_test: bool,
154 pub security_payloads: Option<PathBuf>,
156 pub security_categories: Option<String>,
158 pub security_target_fields: Option<String>,
160
161 pub wafbench_dir: Option<String>,
164 pub wafbench_cycle_all: bool,
166
167 pub conformance: bool,
170 pub conformance_api_key: Option<String>,
172 pub conformance_basic_auth: Option<String>,
174 pub conformance_report: PathBuf,
176 pub conformance_categories: Option<String>,
178 pub conformance_report_format: String,
180 pub conformance_headers: Vec<String>,
183 pub conformance_all_operations: bool,
186 pub conformance_custom: Option<PathBuf>,
188 pub conformance_delay_ms: u64,
191 pub use_k6: bool,
193 pub conformance_custom_filter: Option<String>,
197 pub export_requests: bool,
200 pub validate_requests: bool,
203 pub conformance_self_test: bool,
210 pub conformance_self_test_capture: bool,
214 pub validate_response_schemas: bool,
220
221 pub source_ips: Vec<String>,
226 pub geo_source_ips: Vec<String>,
230 pub geo_source_headers: Vec<String>,
234
235 pub report_missed_cap: Option<u32>,
242
243 pub owasp_api_top10: bool,
246 pub owasp_categories: Option<String>,
248 pub owasp_auth_header: String,
250 pub owasp_auth_token: Option<String>,
252 pub owasp_admin_paths: Option<PathBuf>,
254 pub owasp_id_fields: Option<String>,
256 pub owasp_report: Option<PathBuf>,
258 pub owasp_report_format: String,
260 pub owasp_iterations: u32,
262}
263
264fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
278 use std::net::IpAddr;
279 const MAX_CIDR_EXPANSION: usize = 256;
280 let mut out = Vec::new();
281 for entry in raw {
282 for piece in entry.split(',') {
283 let s = piece.trim();
284 if s.is_empty() {
285 continue;
286 }
287 if let Some((addr_part, prefix_part)) = s.split_once('/') {
289 let prefix: u32 = match prefix_part.parse() {
290 Ok(p) => p,
291 Err(e) => {
292 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR prefix: {e}");
293 continue;
294 }
295 };
296 let net_addr: IpAddr = match addr_part.parse() {
297 Ok(a) => a,
298 Err(e) => {
299 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR address: {e}");
300 continue;
301 }
302 };
303 expand_cidr(net_addr, prefix, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
304 continue;
305 }
306 if let Some((start_str, end_str)) = s.split_once('-') {
312 let start_s = start_str.trim();
313 let end_s = end_str.trim();
314 if start_s.contains(':') || end_s.contains(':') {
318 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{s}': IPv6 range syntax not supported (use CIDR like 2001:db8::/126 instead)");
319 continue;
320 }
321 let start: IpAddr = match start_s.parse() {
322 Ok(a) => a,
323 Err(e) => {
324 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range start: {e}");
325 continue;
326 }
327 };
328 let end: IpAddr = match end_s.parse() {
329 Ok(a) => a,
330 Err(e) => {
331 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range end: {e}");
332 continue;
333 }
334 };
335 expand_range(start, end, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
336 continue;
337 }
338 match s.parse::<IpAddr>() {
340 Ok(ip) => out.push(ip),
341 Err(e) => {
342 tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
343 }
344 }
345 }
346 }
347 out
348}
349
350fn expand_range(
354 start: std::net::IpAddr,
355 end: std::net::IpAddr,
356 cap: usize,
357 flag_name: &str,
358 raw: &str,
359 out: &mut Vec<std::net::IpAddr>,
360) {
361 use std::net::{IpAddr, Ipv4Addr};
362 let (start_v4, end_v4) = match (start, end) {
363 (IpAddr::V4(a), IpAddr::V4(b)) => (a, b),
364 _ => {
365 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range start/end must both be IPv4");
366 return;
367 }
368 };
369 let start_u32 = u32::from(start_v4);
370 let end_u32 = u32::from(end_v4);
371 if end_u32 < start_u32 {
372 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range end {end_v4} is before start {start_v4}");
373 return;
374 }
375 let total = (end_u32 - start_u32).saturating_add(1) as usize;
376 let take = total.min(cap);
377 if total > cap {
378 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range has {total} addresses, capping at {cap}");
379 }
380 for i in 0..take as u32 {
381 out.push(IpAddr::V4(Ipv4Addr::from(start_u32 + i)));
382 }
383}
384
385fn expand_cidr(
389 net: std::net::IpAddr,
390 prefix: u32,
391 cap: usize,
392 flag_name: &str,
393 raw: &str,
394 out: &mut Vec<std::net::IpAddr>,
395) {
396 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
397 match net {
398 IpAddr::V4(ipv4) => {
399 if prefix > 32 {
400 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv4 prefix must be <= 32");
401 return;
402 }
403 let total: u64 = 1u64 << (32 - prefix);
404 let take = total.min(cap as u64) as u32;
405 if total > cap as u64 {
406 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': CIDR has {total} addresses, capping at {cap}");
407 }
408 let mask: u32 = if prefix == 0 {
409 0
410 } else {
411 !0u32 << (32 - prefix)
412 };
413 let net_u32 = u32::from(ipv4) & mask;
414 for i in 0..take {
415 out.push(IpAddr::V4(Ipv4Addr::from(net_u32.wrapping_add(i))));
416 }
417 }
418 IpAddr::V6(ipv6) => {
419 if prefix > 128 {
420 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv6 prefix must be <= 128");
421 return;
422 }
423 let mask: u128 = if prefix == 0 {
427 0
428 } else {
429 !0u128 << (128 - prefix)
430 };
431 let net_u128 = u128::from(ipv6) & mask;
432 let remaining_bits = 128 - prefix;
433 let total_capped = if remaining_bits >= 64 {
436 cap as u128
437 } else {
438 (1u128 << remaining_bits).min(cap as u128)
439 };
440 if remaining_bits < 128 && (1u128 << remaining_bits) > cap as u128 {
441 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': IPv6 CIDR exceeds {cap} addresses, capping");
442 }
443 for i in 0..total_capped {
444 out.push(IpAddr::V6(Ipv6Addr::from(net_u128.wrapping_add(i))));
445 }
446 }
447 }
448}
449
450impl BenchCommand {
451 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
453 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
454
455 if !self.spec.is_empty() {
457 let specs = load_specs_from_files(self.spec.clone())
458 .await
459 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
460 all_specs.extend(specs);
461 }
462
463 if let Some(spec_dir) = &self.spec_dir {
465 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
466 BenchError::Other(format!("Failed to load specs from directory: {}", e))
467 })?;
468 all_specs.extend(dir_specs);
469 }
470
471 if all_specs.is_empty() {
472 return Err(BenchError::Other(
473 "No spec files provided. Use --spec or --spec-dir.".to_string(),
474 ));
475 }
476
477 if all_specs.len() == 1 {
479 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
481 }
482
483 let conflict_strategy = match self.merge_conflicts.as_str() {
485 "first" => ConflictStrategy::First,
486 "last" => ConflictStrategy::Last,
487 _ => ConflictStrategy::Error,
488 };
489
490 merge_specs(all_specs, conflict_strategy)
491 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
492 }
493
494 fn get_spec_display_name(&self) -> String {
496 if self.spec.len() == 1 {
497 self.spec[0].to_string_lossy().to_string()
498 } else if !self.spec.is_empty() {
499 format!("{} spec files", self.spec.len())
500 } else if let Some(dir) = &self.spec_dir {
501 format!("specs from {}", dir.display())
502 } else {
503 "no specs".to_string()
504 }
505 }
506
507 pub async fn execute(&self) -> Result<()> {
509 if self.conformance_self_test && self.use_k6 {
516 TerminalReporter::print_warning(
517 "--use-k6 has no effect with --conformance-self-test: the self-test driver runs and returns before k6 is invoked. Drop one or the other depending on whether you want the spec-driven self-test or a k6 bench run.",
518 );
519 }
520
521 if let Some(targets_file) = &self.targets_file {
523 if self.conformance {
524 return self.execute_multi_target_conformance(targets_file).await;
525 }
526 return self.execute_multi_target(targets_file).await;
527 }
528
529 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
531 return self.execute_sequential_specs().await;
532 }
533
534 TerminalReporter::print_header(
537 &self.get_spec_display_name(),
538 &self.target,
539 0, &self.scenario,
541 Self::parse_duration(&self.duration)?,
542 );
543
544 if !K6Executor::is_k6_installed() {
546 TerminalReporter::print_error("k6 is not installed");
547 TerminalReporter::print_warning(
548 "Install k6 from: https://k6.io/docs/get-started/installation/",
549 );
550 return Err(BenchError::K6NotFound);
551 }
552
553 if self.conformance {
555 return self.execute_conformance_test().await;
556 }
557
558 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
560 let merged_spec = self.load_and_merge_specs().await?;
561 let parser = SpecParser::from_spec(merged_spec);
562 if self.spec.len() > 1 || self.spec_dir.is_some() {
563 TerminalReporter::print_success(&format!(
564 "Loaded and merged {} specification(s)",
565 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
566 ));
567 } else {
568 TerminalReporter::print_success("Specification loaded");
569 }
570
571 let mock_config = self.build_mock_config().await;
573 if mock_config.is_mock_server {
574 TerminalReporter::print_progress("Mock server integration enabled");
575 }
576
577 if self.crud_flow {
579 return self.execute_crud_flow(&parser).await;
580 }
581
582 if self.owasp_api_top10 {
584 return self.execute_owasp_test(&parser).await;
585 }
586
587 TerminalReporter::print_progress("Extracting API operations...");
589 let mut operations = if let Some(filter) = &self.operations {
590 parser.filter_operations(filter)?
591 } else {
592 parser.get_operations()
593 };
594
595 if let Some(exclude) = &self.exclude_operations {
597 let before_count = operations.len();
598 operations = parser.exclude_operations(operations, exclude)?;
599 let excluded_count = before_count - operations.len();
600 if excluded_count > 0 {
601 TerminalReporter::print_progress(&format!(
602 "Excluded {} operations matching '{}'",
603 excluded_count, exclude
604 ));
605 }
606 }
607
608 if operations.is_empty() {
609 return Err(BenchError::Other("No operations found in spec".to_string()));
610 }
611
612 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
613
614 let param_overrides = if let Some(params_file) = &self.params_file {
616 TerminalReporter::print_progress("Loading parameter overrides...");
617 let overrides = ParameterOverrides::from_file(params_file)?;
618 TerminalReporter::print_success(&format!(
619 "Loaded parameter overrides ({} operation-specific, {} defaults)",
620 overrides.operations.len(),
621 if overrides.defaults.is_empty() { 0 } else { 1 }
622 ));
623 Some(overrides)
624 } else {
625 None
626 };
627
628 TerminalReporter::print_progress("Generating request templates...");
630 let templates: Vec<_> = operations
631 .iter()
632 .map(|op| {
633 let op_overrides = param_overrides.as_ref().map(|po| {
634 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
635 });
636 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
637 })
638 .collect::<Result<Vec<_>>>()?;
639 TerminalReporter::print_success("Request templates generated");
640
641 let custom_headers = self.parse_headers()?;
643
644 let base_path = self.resolve_base_path(&parser);
646 if let Some(ref bp) = base_path {
647 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
648 }
649
650 TerminalReporter::print_progress("Generating k6 load test script...");
652 let scenario =
653 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
654
655 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
656
657 let num_ops = operations.len() as u32;
675 if let Some(rps) = self.target_rps {
676 let probe =
677 crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
678
679 let (required_vus, basis) = match probe {
680 Some(p) => (
681 p.required_vus(rps, num_ops),
682 format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
683 ),
684 None => {
685 let fallback = (rps as u64)
687 .saturating_mul(num_ops.max(1) as u64)
688 .div_ceil(10)
689 .min(u32::MAX as u64) as u32;
690 (fallback, "~100ms (default — probe failed)".to_string())
691 }
692 };
693
694 if self.vus < required_vus {
695 const VU_RECOMMENDATION_CAP: u32 = 1000;
701 let recommendation = required_vus.max(self.vus + 1);
702 if recommendation > VU_RECOMMENDATION_CAP {
703 TerminalReporter::print_warning(&format!(
704 "Workload is very large: --rps {} × {} ops/iteration × {} \
705 baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
706 practical to drive. Two ways to fix:\n 1. Reduce \
707 operations per iteration with `--operations 'pattern,…'` \
708 (or `--exclude-operations`) to focus the bench on a \
709 representative subset.\n 2. Drop `--rps` and use \
710 `--vus {}` alone — closed-model load runs as fast as \
711 the VU pool allows, bounded by latency, with no per-\
712 iteration deadline. Expect 1-iteration coverage of ~{} \
713 operations in {}s.",
714 rps,
715 num_ops,
716 basis,
717 recommendation,
718 self.vus.max(5),
719 num_ops,
720 Self::parse_duration(&self.duration).unwrap_or(0),
721 ));
722 } else {
723 TerminalReporter::print_warning(&format!(
724 "--vus {} may be insufficient for --rps {} × {} ops/iteration \
725 (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
726 and each runs every operation in the spec — required ≈ rps × ops × \
727 latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
728 warnings.",
729 self.vus, rps, num_ops, basis, recommendation,
730 ));
731 }
732 } else if probe.is_some() {
733 TerminalReporter::print_progress(&format!(
734 "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
735 is sufficient for --rps {}",
736 basis, num_ops, self.vus, rps,
737 ));
738 }
739 }
740
741 let k6_config = K6Config {
742 target_url: self.target.clone(),
743 base_path,
744 scenario,
745 duration_secs: Self::parse_duration(&self.duration)?,
746 max_vus: self.vus,
747 threshold_percentile: self.threshold_percentile.clone(),
748 threshold_ms: self.threshold_ms,
749 max_error_rate: self.max_error_rate,
750 auth_header: self.auth.clone(),
751 custom_headers,
752 skip_tls_verify: self.skip_tls_verify,
753 security_testing_enabled,
754 chunked_request_bodies: self.chunked_request_bodies,
755 target_rps: self.target_rps,
756 no_keep_alive: self.no_keep_alive,
757 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
763 .into_iter()
764 .map(|ip| ip.to_string())
765 .collect(),
766 geo_source_headers: if self.geo_source_headers.is_empty()
767 && !self.geo_source_ips.is_empty()
768 {
769 crate::conformance::self_test::default_geo_source_headers()
770 } else {
771 self.geo_source_headers.clone()
772 },
773 };
774
775 let generator = K6ScriptGenerator::new(k6_config, templates);
776 let mut script = generator.generate()?;
777 TerminalReporter::print_success("k6 script generated");
778
779 let has_advanced_features = self.data_file.is_some()
781 || self.error_rate.is_some()
782 || self.security_test
783 || self.parallel_create.is_some()
784 || self.wafbench_dir.is_some();
785
786 if has_advanced_features {
788 script = self.generate_enhanced_script(&script)?;
789 }
790
791 if mock_config.is_mock_server {
793 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
794 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
795 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
796
797 if let Some(import_end) = script.find("export const options") {
799 script.insert_str(
800 import_end,
801 &format!(
802 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
803 helper_code, setup_code, teardown_code
804 ),
805 );
806 }
807 }
808
809 TerminalReporter::print_progress("Validating k6 script...");
811 let validation_errors = K6ScriptGenerator::validate_script(&script);
812 if !validation_errors.is_empty() {
813 TerminalReporter::print_error("Script validation failed");
814 for error in &validation_errors {
815 eprintln!(" {}", error);
816 }
817 return Err(BenchError::Other(format!(
818 "Generated k6 script has {} validation error(s). Please check the output above.",
819 validation_errors.len()
820 )));
821 }
822 TerminalReporter::print_success("Script validation passed");
823
824 let script_path = if let Some(output) = &self.script_output {
826 output.clone()
827 } else {
828 self.output.join("k6-script.js")
829 };
830
831 if let Some(parent) = script_path.parent() {
832 std::fs::create_dir_all(parent)?;
833 }
834 std::fs::write(&script_path, &script)?;
835 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
836
837 if self.generate_only {
839 println!("\nScript generated successfully. Run it with:");
840 println!(" k6 run {}", script_path.display());
841 return Ok(());
842 }
843
844 TerminalReporter::print_progress("Executing load test...");
846 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
847
848 std::fs::create_dir_all(&self.output)?;
849
850 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
851
852 let duration_secs = Self::parse_duration(&self.duration)?;
854 TerminalReporter::print_summary_full(
855 &results,
856 duration_secs,
857 self.no_keep_alive,
858 Some(num_ops),
859 );
860
861 println!("\nResults saved to: {}", self.output.display());
862
863 Ok(())
864 }
865
866 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
868 TerminalReporter::print_progress("Parsing targets file...");
869 let targets = parse_targets_file(targets_file)?;
870 let num_targets = targets.len();
871 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
872
873 if targets.is_empty() {
874 return Err(BenchError::Other("No targets found in file".to_string()));
875 }
876
877 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
879 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
883 &self.get_spec_display_name(),
884 &format!("{} targets", num_targets),
885 0,
886 &self.scenario,
887 Self::parse_duration(&self.duration)?,
888 );
889
890 let executor = ParallelExecutor::new(
892 BenchCommand {
893 spec: self.spec.clone(),
895 spec_dir: self.spec_dir.clone(),
896 merge_conflicts: self.merge_conflicts.clone(),
897 spec_mode: self.spec_mode.clone(),
898 dependency_config: self.dependency_config.clone(),
899 target: self.target.clone(), base_path: self.base_path.clone(),
901 duration: self.duration.clone(),
902 vus: self.vus,
903 target_rps: self.target_rps,
904 no_keep_alive: self.no_keep_alive,
905 scenario: self.scenario.clone(),
906 operations: self.operations.clone(),
907 exclude_operations: self.exclude_operations.clone(),
908 auth: self.auth.clone(),
909 headers: self.headers.clone(),
910 output: self.output.clone(),
911 generate_only: self.generate_only,
912 script_output: self.script_output.clone(),
913 threshold_percentile: self.threshold_percentile.clone(),
914 threshold_ms: self.threshold_ms,
915 max_error_rate: self.max_error_rate,
916 verbose: self.verbose,
917 skip_tls_verify: self.skip_tls_verify,
918 chunked_request_bodies: self.chunked_request_bodies,
919 targets_file: None,
920 max_concurrency: None,
921 results_format: self.results_format.clone(),
922 params_file: self.params_file.clone(),
923 crud_flow: self.crud_flow,
924 flow_config: self.flow_config.clone(),
925 extract_fields: self.extract_fields.clone(),
926 parallel_create: self.parallel_create,
927 data_file: self.data_file.clone(),
928 data_distribution: self.data_distribution.clone(),
929 data_mappings: self.data_mappings.clone(),
930 per_uri_control: self.per_uri_control,
931 error_rate: self.error_rate,
932 error_types: self.error_types.clone(),
933 security_test: self.security_test,
934 security_payloads: self.security_payloads.clone(),
935 security_categories: self.security_categories.clone(),
936 security_target_fields: self.security_target_fields.clone(),
937 wafbench_dir: self.wafbench_dir.clone(),
938 wafbench_cycle_all: self.wafbench_cycle_all,
939 owasp_api_top10: self.owasp_api_top10,
940 owasp_categories: self.owasp_categories.clone(),
941 owasp_auth_header: self.owasp_auth_header.clone(),
942 owasp_auth_token: self.owasp_auth_token.clone(),
943 owasp_admin_paths: self.owasp_admin_paths.clone(),
944 owasp_id_fields: self.owasp_id_fields.clone(),
945 owasp_report: self.owasp_report.clone(),
946 owasp_report_format: self.owasp_report_format.clone(),
947 owasp_iterations: self.owasp_iterations,
948 conformance: false,
949 conformance_api_key: None,
950 conformance_basic_auth: None,
951 conformance_report: PathBuf::from("conformance-report.json"),
952 conformance_categories: None,
953 conformance_report_format: "json".to_string(),
954 conformance_headers: vec![],
955 conformance_all_operations: false,
956 conformance_custom: None,
957 conformance_delay_ms: 0,
958 use_k6: false,
959 conformance_custom_filter: None,
960 export_requests: false,
961 validate_requests: false,
962 conformance_self_test: false,
963 conformance_self_test_capture: false,
964 validate_response_schemas: false,
965 source_ips: Vec::new(),
966 geo_source_ips: Vec::new(),
967 geo_source_headers: Vec::new(),
968 report_missed_cap: None,
969 },
970 targets,
971 max_concurrency,
972 );
973
974 let start_time = std::time::Instant::now();
976 let aggregated_results = executor.execute_all().await?;
977 let elapsed = start_time.elapsed();
978
979 self.report_multi_target_results(&aggregated_results, elapsed)?;
981
982 Ok(())
983 }
984
985 fn report_multi_target_results(
987 &self,
988 results: &AggregatedResults,
989 elapsed: std::time::Duration,
990 ) -> Result<()> {
991 TerminalReporter::print_multi_target_summary(results);
993
994 let total_secs = elapsed.as_secs();
996 let hours = total_secs / 3600;
997 let minutes = (total_secs % 3600) / 60;
998 let seconds = total_secs % 60;
999 if hours > 0 {
1000 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
1001 } else if minutes > 0 {
1002 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
1003 } else {
1004 println!("\n Total Elapsed Time: {}s", seconds);
1005 }
1006
1007 if self.results_format == "aggregated" || self.results_format == "both" {
1009 let summary_path = self.output.join("aggregated_summary.json");
1010 let summary_json = serde_json::json!({
1011 "total_elapsed_seconds": elapsed.as_secs(),
1012 "total_targets": results.total_targets,
1013 "successful_targets": results.successful_targets,
1014 "failed_targets": results.failed_targets,
1015 "aggregated_metrics": {
1016 "total_requests": results.aggregated_metrics.total_requests,
1017 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1018 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1019 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1020 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1021 "error_rate": results.aggregated_metrics.error_rate,
1022 "total_rps": results.aggregated_metrics.total_rps,
1023 "avg_rps": results.aggregated_metrics.avg_rps,
1024 "total_vus_max": results.aggregated_metrics.total_vus_max,
1025 },
1026 "target_results": results.target_results.iter().map(|r| {
1027 serde_json::json!({
1028 "target_url": r.target_url,
1029 "target_index": r.target_index,
1030 "success": r.success,
1031 "error": r.error,
1032 "total_requests": r.results.total_requests,
1033 "failed_requests": r.results.failed_requests,
1034 "avg_duration_ms": r.results.avg_duration_ms,
1035 "min_duration_ms": r.results.min_duration_ms,
1036 "med_duration_ms": r.results.med_duration_ms,
1037 "p90_duration_ms": r.results.p90_duration_ms,
1038 "p95_duration_ms": r.results.p95_duration_ms,
1039 "p99_duration_ms": r.results.p99_duration_ms,
1040 "max_duration_ms": r.results.max_duration_ms,
1041 "rps": r.results.rps,
1042 "vus_max": r.results.vus_max,
1043 "output_dir": r.output_dir.to_string_lossy(),
1044 })
1045 }).collect::<Vec<_>>(),
1046 });
1047
1048 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1049 TerminalReporter::print_success(&format!(
1050 "Aggregated summary saved to: {}",
1051 summary_path.display()
1052 ));
1053 }
1054
1055 let csv_path = self.output.join("all_targets.csv");
1057 let mut csv = String::from(
1058 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1059 );
1060 for r in &results.target_results {
1061 csv.push_str(&format!(
1062 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1063 r.target_url,
1064 r.success,
1065 r.results.total_requests,
1066 r.results.failed_requests,
1067 r.results.rps,
1068 r.results.vus_max,
1069 r.results.min_duration_ms,
1070 r.results.avg_duration_ms,
1071 r.results.med_duration_ms,
1072 r.results.p90_duration_ms,
1073 r.results.p95_duration_ms,
1074 r.results.p99_duration_ms,
1075 r.results.max_duration_ms,
1076 r.error.as_deref().unwrap_or(""),
1077 ));
1078 }
1079 let _ = std::fs::write(&csv_path, &csv);
1080
1081 println!("\nResults saved to: {}", self.output.display());
1082 println!(" - Per-target results: {}", self.output.join("target_*").display());
1083 println!(" - All targets CSV: {}", csv_path.display());
1084 if self.results_format == "aggregated" || self.results_format == "both" {
1085 println!(
1086 " - Aggregated summary: {}",
1087 self.output.join("aggregated_summary.json").display()
1088 );
1089 }
1090
1091 Ok(())
1092 }
1093
1094 pub fn parse_duration(duration: &str) -> Result<u64> {
1096 let duration = duration.trim();
1097
1098 if let Some(secs) = duration.strip_suffix('s') {
1099 secs.parse::<u64>()
1100 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1101 } else if let Some(mins) = duration.strip_suffix('m') {
1102 mins.parse::<u64>()
1103 .map(|m| m * 60)
1104 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1105 } else if let Some(hours) = duration.strip_suffix('h') {
1106 hours
1107 .parse::<u64>()
1108 .map(|h| h * 3600)
1109 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1110 } else {
1111 duration
1113 .parse::<u64>()
1114 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1115 }
1116 }
1117
1118 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1120 match &self.headers {
1121 Some(s) => parse_header_string(s),
1122 None => Ok(HashMap::new()),
1123 }
1124 }
1125
1126 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1127 let extracted_path = output_dir.join("extracted_values.json");
1128 if !extracted_path.exists() {
1129 return Ok(ExtractedValues::new());
1130 }
1131
1132 let content = std::fs::read_to_string(&extracted_path)
1133 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1134 let parsed: serde_json::Value = serde_json::from_str(&content)
1135 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1136
1137 let mut extracted = ExtractedValues::new();
1138 if let Some(values) = parsed.as_object() {
1139 for (key, value) in values {
1140 extracted.set(key.clone(), value.clone());
1141 }
1142 }
1143
1144 Ok(extracted)
1145 }
1146
1147 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1156 if let Some(cli_base_path) = &self.base_path {
1158 if cli_base_path.is_empty() {
1159 return None;
1161 }
1162 return Some(cli_base_path.clone());
1163 }
1164
1165 parser.get_base_path()
1167 }
1168
1169 async fn build_mock_config(&self) -> MockIntegrationConfig {
1171 if MockServerDetector::looks_like_mock_server(&self.target) {
1173 if let Ok(info) = MockServerDetector::detect(&self.target).await {
1175 if info.is_mockforge {
1176 TerminalReporter::print_success(&format!(
1177 "Detected MockForge server (version: {})",
1178 info.version.as_deref().unwrap_or("unknown")
1179 ));
1180 return MockIntegrationConfig::mock_server();
1181 }
1182 }
1183 }
1184 MockIntegrationConfig::real_api()
1185 }
1186
1187 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1189 if !self.crud_flow {
1190 return None;
1191 }
1192
1193 if let Some(config_path) = &self.flow_config {
1195 match CrudFlowConfig::from_file(config_path) {
1196 Ok(config) => return Some(config),
1197 Err(e) => {
1198 TerminalReporter::print_warning(&format!(
1199 "Failed to load flow config: {}. Using auto-detection.",
1200 e
1201 ));
1202 }
1203 }
1204 }
1205
1206 let extract_fields = self
1208 .extract_fields
1209 .as_ref()
1210 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1211 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1212
1213 Some(CrudFlowConfig {
1214 flows: Vec::new(), default_extract_fields: extract_fields,
1216 })
1217 }
1218
1219 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1221 let data_file = self.data_file.as_ref()?;
1222
1223 let distribution = DataDistribution::from_str(&self.data_distribution)
1224 .unwrap_or(DataDistribution::UniquePerVu);
1225
1226 let mappings = self
1227 .data_mappings
1228 .as_ref()
1229 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1230 .unwrap_or_default();
1231
1232 Some(DataDrivenConfig {
1233 file_path: data_file.to_string_lossy().to_string(),
1234 distribution,
1235 mappings,
1236 csv_has_header: true,
1237 per_uri_control: self.per_uri_control,
1238 per_uri_columns: crate::data_driven::PerUriColumns::default(),
1239 })
1240 }
1241
1242 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1244 let error_rate = self.error_rate?;
1245
1246 let error_types = self
1247 .error_types
1248 .as_ref()
1249 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1250 .unwrap_or_default();
1251
1252 Some(InvalidDataConfig {
1253 error_rate,
1254 error_types,
1255 target_fields: Vec::new(),
1256 })
1257 }
1258
1259 fn build_security_config(&self) -> Option<SecurityTestConfig> {
1261 if !self.security_test {
1262 return None;
1263 }
1264
1265 let categories = self
1266 .security_categories
1267 .as_ref()
1268 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1269 .unwrap_or_else(|| {
1270 let mut default = HashSet::new();
1271 default.insert(SecurityCategory::SqlInjection);
1272 default.insert(SecurityCategory::Xss);
1273 default
1274 });
1275
1276 let target_fields = self
1277 .security_target_fields
1278 .as_ref()
1279 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1280 .unwrap_or_default();
1281
1282 let custom_payloads_file =
1283 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1284
1285 Some(SecurityTestConfig {
1286 enabled: true,
1287 categories,
1288 target_fields,
1289 custom_payloads_file,
1290 include_high_risk: false,
1291 })
1292 }
1293
1294 fn build_parallel_config(&self) -> Option<ParallelConfig> {
1296 let count = self.parallel_create?;
1297
1298 Some(ParallelConfig::new(count))
1299 }
1300
1301 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1303 let Some(ref wafbench_dir) = self.wafbench_dir else {
1304 return Vec::new();
1305 };
1306
1307 let mut loader = WafBenchLoader::new();
1308
1309 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1310 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1311 return Vec::new();
1312 }
1313
1314 let stats = loader.stats();
1315
1316 if stats.files_processed == 0 {
1317 TerminalReporter::print_warning(&format!(
1318 "No WAFBench YAML files found matching '{}'",
1319 wafbench_dir
1320 ));
1321 if !stats.parse_errors.is_empty() {
1323 TerminalReporter::print_warning("Some files were found but failed to parse:");
1324 for error in &stats.parse_errors {
1325 TerminalReporter::print_warning(&format!(" - {}", error));
1326 }
1327 }
1328 return Vec::new();
1329 }
1330
1331 TerminalReporter::print_progress(&format!(
1332 "Loaded {} WAFBench files, {} test cases, {} payloads",
1333 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1334 ));
1335
1336 for (category, count) in &stats.by_category {
1338 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1339 }
1340
1341 for error in &stats.parse_errors {
1343 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1344 }
1345
1346 loader.to_security_payloads()
1347 }
1348
1349 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1351 let mut enhanced_script = base_script.to_string();
1352 let mut additional_code = String::new();
1353
1354 if let Some(config) = self.build_data_driven_config() {
1356 TerminalReporter::print_progress("Adding data-driven testing support...");
1357 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1358 additional_code.push('\n');
1359 TerminalReporter::print_success("Data-driven testing enabled");
1360 }
1361
1362 if let Some(config) = self.build_invalid_data_config() {
1364 TerminalReporter::print_progress("Adding invalid data testing support...");
1365 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1366 additional_code.push('\n');
1367 additional_code
1368 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1369 additional_code.push('\n');
1370 additional_code
1371 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1372 additional_code.push('\n');
1373 TerminalReporter::print_success(&format!(
1374 "Invalid data testing enabled ({}% error rate)",
1375 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1376 ));
1377 }
1378
1379 let security_config = self.build_security_config();
1381 let wafbench_payloads = self.load_wafbench_payloads();
1382 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1383
1384 if security_config.is_some() || !wafbench_payloads.is_empty() {
1385 TerminalReporter::print_progress("Adding security testing support...");
1386
1387 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1389
1390 if let Some(ref config) = security_config {
1391 payload_list.extend(SecurityPayloads::get_payloads(config));
1392 }
1393
1394 if !wafbench_payloads.is_empty() {
1396 TerminalReporter::print_progress(&format!(
1397 "Loading {} WAFBench attack patterns...",
1398 wafbench_payloads.len()
1399 ));
1400 payload_list.extend(wafbench_payloads);
1401 }
1402
1403 let target_fields =
1404 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1405
1406 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1407 &payload_list,
1408 self.wafbench_cycle_all,
1409 ));
1410 additional_code.push('\n');
1411 additional_code
1412 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1413 additional_code.push('\n');
1414 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1415 additional_code.push('\n');
1416
1417 let mode = if self.wafbench_cycle_all {
1418 "cycle-all"
1419 } else {
1420 "random"
1421 };
1422 TerminalReporter::print_success(&format!(
1423 "Security testing enabled ({} payloads, {} mode)",
1424 payload_list.len(),
1425 mode
1426 ));
1427 } else if security_requested {
1428 TerminalReporter::print_warning(
1432 "Security testing was requested but no payloads were loaded. \
1433 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1434 );
1435 additional_code
1436 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1437 additional_code.push('\n');
1438 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1439 additional_code.push('\n');
1440 }
1441
1442 if let Some(config) = self.build_parallel_config() {
1444 TerminalReporter::print_progress("Adding parallel execution support...");
1445 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1446 additional_code.push('\n');
1447 TerminalReporter::print_success(&format!(
1448 "Parallel execution enabled (count: {})",
1449 config.count
1450 ));
1451 }
1452
1453 if !additional_code.is_empty() {
1455 if let Some(import_end) = enhanced_script.find("export const options") {
1457 enhanced_script.insert_str(
1458 import_end,
1459 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1460 );
1461 }
1462 }
1463
1464 Ok(enhanced_script)
1465 }
1466
1467 async fn execute_sequential_specs(&self) -> Result<()> {
1469 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1470
1471 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1473
1474 if !self.spec.is_empty() {
1475 let specs = load_specs_from_files(self.spec.clone())
1476 .await
1477 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1478 all_specs.extend(specs);
1479 }
1480
1481 if let Some(spec_dir) = &self.spec_dir {
1482 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1483 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1484 })?;
1485 all_specs.extend(dir_specs);
1486 }
1487
1488 if all_specs.is_empty() {
1489 return Err(BenchError::Other(
1490 "No spec files found for sequential execution".to_string(),
1491 ));
1492 }
1493
1494 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1495
1496 let execution_order = if let Some(config_path) = &self.dependency_config {
1498 TerminalReporter::print_progress("Loading dependency configuration...");
1499 let config = SpecDependencyConfig::from_file(config_path)?;
1500
1501 if !config.disable_auto_detect && config.execution_order.is_empty() {
1502 self.detect_and_sort_specs(&all_specs)?
1504 } else {
1505 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1507 }
1508 } else {
1509 self.detect_and_sort_specs(&all_specs)?
1511 };
1512
1513 TerminalReporter::print_success(&format!(
1514 "Execution order: {}",
1515 execution_order
1516 .iter()
1517 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1518 .collect::<Vec<_>>()
1519 .join(" → ")
1520 ));
1521
1522 let mut extracted_values = ExtractedValues::new();
1524 let total_specs = execution_order.len();
1525
1526 for (index, spec_path) in execution_order.iter().enumerate() {
1527 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1528
1529 TerminalReporter::print_progress(&format!(
1530 "[{}/{}] Executing spec: {}",
1531 index + 1,
1532 total_specs,
1533 spec_name
1534 ));
1535
1536 let spec = all_specs
1538 .iter()
1539 .find(|(p, _)| {
1540 p == spec_path
1541 || p.file_name() == spec_path.file_name()
1542 || p.file_name() == Some(spec_path.as_os_str())
1543 })
1544 .map(|(_, s)| s.clone())
1545 .ok_or_else(|| {
1546 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1547 })?;
1548
1549 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1551
1552 extracted_values.merge(&new_values);
1554
1555 TerminalReporter::print_success(&format!(
1556 "[{}/{}] Completed: {} (extracted {} values)",
1557 index + 1,
1558 total_specs,
1559 spec_name,
1560 new_values.values.len()
1561 ));
1562 }
1563
1564 TerminalReporter::print_success(&format!(
1565 "Sequential execution complete: {} specs executed",
1566 total_specs
1567 ));
1568
1569 Ok(())
1570 }
1571
1572 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1574 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1575
1576 let mut detector = DependencyDetector::new();
1577 let dependencies = detector.detect_dependencies(specs);
1578
1579 if dependencies.is_empty() {
1580 TerminalReporter::print_progress("No dependencies detected, using file order");
1581 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1582 }
1583
1584 TerminalReporter::print_progress(&format!(
1585 "Detected {} cross-spec dependencies",
1586 dependencies.len()
1587 ));
1588
1589 for dep in &dependencies {
1590 TerminalReporter::print_progress(&format!(
1591 " {} → {} (via field '{}')",
1592 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1593 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1594 dep.field_name
1595 ));
1596 }
1597
1598 topological_sort(specs, &dependencies)
1599 }
1600
1601 async fn execute_single_spec(
1603 &self,
1604 spec: &OpenApiSpec,
1605 spec_name: &str,
1606 _external_values: &ExtractedValues,
1607 ) -> Result<ExtractedValues> {
1608 let parser = SpecParser::from_spec(spec.clone());
1609
1610 if self.crud_flow {
1612 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1614 } else {
1615 self.execute_standard_spec(&parser, spec_name).await?;
1617 Ok(ExtractedValues::new())
1618 }
1619 }
1620
1621 async fn execute_crud_flow_with_extraction(
1623 &self,
1624 parser: &SpecParser,
1625 spec_name: &str,
1626 ) -> Result<ExtractedValues> {
1627 let operations = parser.get_operations();
1628 let flows = CrudFlowDetector::detect_flows(&operations);
1629
1630 if flows.is_empty() {
1631 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1632 return Ok(ExtractedValues::new());
1633 }
1634
1635 TerminalReporter::print_progress(&format!(
1636 " {} CRUD flow(s) in {}",
1637 flows.len(),
1638 spec_name
1639 ));
1640
1641 let mut handlebars = handlebars::Handlebars::new();
1643 handlebars.register_helper(
1645 "json",
1646 Box::new(
1647 |h: &handlebars::Helper,
1648 _: &handlebars::Handlebars,
1649 _: &handlebars::Context,
1650 _: &mut handlebars::RenderContext,
1651 out: &mut dyn handlebars::Output|
1652 -> handlebars::HelperResult {
1653 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1654 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1655 Ok(())
1656 },
1657 ),
1658 );
1659 let template = include_str!("templates/k6_crud_flow.hbs");
1660 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1661
1662 let custom_headers = self.parse_headers()?;
1663 let config = self.build_crud_flow_config().unwrap_or_default();
1664
1665 let param_overrides = if let Some(params_file) = &self.params_file {
1667 let overrides = ParameterOverrides::from_file(params_file)?;
1668 Some(overrides)
1669 } else {
1670 None
1671 };
1672
1673 let duration_secs = Self::parse_duration(&self.duration)?;
1675 let scenario =
1676 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1677 let stages = scenario.generate_stages(duration_secs, self.vus);
1678
1679 let api_base_path = self.resolve_base_path(parser);
1681
1682 let mut all_headers = custom_headers.clone();
1684 if let Some(auth) = &self.auth {
1685 all_headers.insert("Authorization".to_string(), auth.clone());
1686 }
1687 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1688
1689 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1691
1692 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1693 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1697 serde_json::json!({
1698 "name": sanitized_name.clone(),
1699 "display_name": f.name,
1700 "base_path": f.base_path,
1701 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1702 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1704 let method_raw = if !parts.is_empty() {
1705 parts[0].to_uppercase()
1706 } else {
1707 "GET".to_string()
1708 };
1709 let method = if !parts.is_empty() {
1710 let m = parts[0].to_lowercase();
1711 if m == "delete" { "del".to_string() } else { m }
1713 } else {
1714 "get".to_string()
1715 };
1716 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1717 let path = if let Some(ref bp) = api_base_path {
1719 format!("{}{}", bp, raw_path)
1720 } else {
1721 raw_path.to_string()
1722 };
1723 let is_get_or_head = method == "get" || method == "head";
1724 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1726
1727 let body_value = if has_body {
1729 param_overrides.as_ref()
1730 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1731 .and_then(|oo| oo.body)
1732 .unwrap_or_else(|| serde_json::json!({}))
1733 } else {
1734 serde_json::json!({})
1735 };
1736
1737 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1739
1740 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1742 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1743
1744 serde_json::json!({
1745 "operation": s.operation,
1746 "method": method,
1747 "path": path,
1748 "extract": s.extract,
1749 "use_values": s.use_values,
1750 "use_body": s.use_body,
1751 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1752 "inject_attacks": s.inject_attacks,
1753 "attack_types": s.attack_types,
1754 "description": s.description,
1755 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1756 "is_get_or_head": is_get_or_head,
1757 "has_body": has_body,
1758 "body": processed_body.value,
1759 "body_is_dynamic": body_is_dynamic,
1760 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1761 })
1762 }).collect::<Vec<_>>(),
1763 })
1764 }).collect();
1765
1766 for flow_data in &flows_data {
1768 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1769 for step in steps {
1770 if let Some(placeholders_arr) =
1771 step.get("_placeholders").and_then(|p| p.as_array())
1772 {
1773 for p_str in placeholders_arr {
1774 if let Some(p_name) = p_str.as_str() {
1775 match p_name {
1776 "VU" => {
1777 all_placeholders.insert(DynamicPlaceholder::VU);
1778 }
1779 "Iteration" => {
1780 all_placeholders.insert(DynamicPlaceholder::Iteration);
1781 }
1782 "Timestamp" => {
1783 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1784 }
1785 "UUID" => {
1786 all_placeholders.insert(DynamicPlaceholder::UUID);
1787 }
1788 "Random" => {
1789 all_placeholders.insert(DynamicPlaceholder::Random);
1790 }
1791 "Counter" => {
1792 all_placeholders.insert(DynamicPlaceholder::Counter);
1793 }
1794 "Date" => {
1795 all_placeholders.insert(DynamicPlaceholder::Date);
1796 }
1797 "VuIter" => {
1798 all_placeholders.insert(DynamicPlaceholder::VuIter);
1799 }
1800 _ => {}
1801 }
1802 }
1803 }
1804 }
1805 }
1806 }
1807 }
1808
1809 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1811 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1812
1813 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1815
1816 let data = serde_json::json!({
1817 "base_url": self.target,
1818 "flows": flows_data,
1819 "extract_fields": config.default_extract_fields,
1820 "duration_secs": duration_secs,
1821 "max_vus": self.vus,
1822 "auth_header": self.auth,
1823 "custom_headers": custom_headers,
1824 "skip_tls_verify": self.skip_tls_verify,
1825 "stages": stages.iter().map(|s| serde_json::json!({
1827 "duration": s.duration,
1828 "target": s.target,
1829 })).collect::<Vec<_>>(),
1830 "threshold_percentile": self.threshold_percentile,
1831 "threshold_ms": self.threshold_ms,
1832 "max_error_rate": self.max_error_rate,
1833 "headers": headers_json,
1834 "dynamic_imports": required_imports,
1835 "dynamic_globals": required_globals,
1836 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1837 "security_testing_enabled": security_testing_enabled,
1839 "has_custom_headers": !custom_headers.is_empty(),
1840 });
1841
1842 let mut script = handlebars
1843 .render_template(template, &data)
1844 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1845
1846 if security_testing_enabled {
1848 script = self.generate_enhanced_script(&script)?;
1849 }
1850
1851 let script_path =
1853 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1854
1855 std::fs::create_dir_all(self.output.clone())?;
1856 std::fs::write(&script_path, &script)?;
1857
1858 if !self.generate_only {
1859 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
1860 std::fs::create_dir_all(&output_dir)?;
1861
1862 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1863
1864 let extracted = Self::parse_extracted_values(&output_dir)?;
1865 TerminalReporter::print_progress(&format!(
1866 " Extracted {} value(s) from {}",
1867 extracted.values.len(),
1868 spec_name
1869 ));
1870 return Ok(extracted);
1871 }
1872
1873 Ok(ExtractedValues::new())
1874 }
1875
1876 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1878 let mut operations = if let Some(filter) = &self.operations {
1879 parser.filter_operations(filter)?
1880 } else {
1881 parser.get_operations()
1882 };
1883
1884 if let Some(exclude) = &self.exclude_operations {
1885 operations = parser.exclude_operations(operations, exclude)?;
1886 }
1887
1888 if operations.is_empty() {
1889 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1890 return Ok(());
1891 }
1892
1893 TerminalReporter::print_progress(&format!(
1894 " {} operations in {}",
1895 operations.len(),
1896 spec_name
1897 ));
1898
1899 let templates: Vec<_> = operations
1901 .iter()
1902 .map(RequestGenerator::generate_template)
1903 .collect::<Result<Vec<_>>>()?;
1904
1905 let custom_headers = self.parse_headers()?;
1907
1908 let base_path = self.resolve_base_path(parser);
1910
1911 let scenario =
1913 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1914
1915 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1916
1917 let k6_config = K6Config {
1918 target_url: self.target.clone(),
1919 base_path,
1920 scenario,
1921 duration_secs: Self::parse_duration(&self.duration)?,
1922 max_vus: self.vus,
1923 threshold_percentile: self.threshold_percentile.clone(),
1924 threshold_ms: self.threshold_ms,
1925 max_error_rate: self.max_error_rate,
1926 auth_header: self.auth.clone(),
1927 custom_headers,
1928 skip_tls_verify: self.skip_tls_verify,
1929 security_testing_enabled,
1930 chunked_request_bodies: self.chunked_request_bodies,
1931 target_rps: self.target_rps,
1932 no_keep_alive: self.no_keep_alive,
1933 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
1935 .into_iter()
1936 .map(|ip| ip.to_string())
1937 .collect(),
1938 geo_source_headers: if self.geo_source_headers.is_empty()
1939 && !self.geo_source_ips.is_empty()
1940 {
1941 crate::conformance::self_test::default_geo_source_headers()
1942 } else {
1943 self.geo_source_headers.clone()
1944 },
1945 };
1946
1947 let generator = K6ScriptGenerator::new(k6_config, templates);
1948 let mut script = generator.generate()?;
1949
1950 let has_advanced_features = self.data_file.is_some()
1952 || self.error_rate.is_some()
1953 || self.security_test
1954 || self.parallel_create.is_some()
1955 || self.wafbench_dir.is_some();
1956
1957 if has_advanced_features {
1958 script = self.generate_enhanced_script(&script)?;
1959 }
1960
1961 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1963
1964 std::fs::create_dir_all(self.output.clone())?;
1965 std::fs::write(&script_path, &script)?;
1966
1967 if !self.generate_only {
1968 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
1969 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1970 std::fs::create_dir_all(&output_dir)?;
1971
1972 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1973 }
1974
1975 Ok(())
1976 }
1977
1978 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1980 let config = self.build_crud_flow_config().unwrap_or_default();
1982
1983 let flows = if !config.flows.is_empty() {
1985 TerminalReporter::print_progress("Using custom flow configuration...");
1986 config.flows.clone()
1987 } else {
1988 TerminalReporter::print_progress("Detecting CRUD operations...");
1989 let operations = parser.get_operations();
1990 CrudFlowDetector::detect_flows(&operations)
1991 };
1992
1993 if flows.is_empty() {
1994 return Err(BenchError::Other(
1995 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1996 ));
1997 }
1998
1999 if config.flows.is_empty() {
2000 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2001 } else {
2002 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2003 }
2004
2005 for flow in &flows {
2006 TerminalReporter::print_progress(&format!(
2007 " - {}: {} steps",
2008 flow.name,
2009 flow.steps.len()
2010 ));
2011 }
2012
2013 let mut handlebars = handlebars::Handlebars::new();
2015 handlebars.register_helper(
2017 "json",
2018 Box::new(
2019 |h: &handlebars::Helper,
2020 _: &handlebars::Handlebars,
2021 _: &handlebars::Context,
2022 _: &mut handlebars::RenderContext,
2023 out: &mut dyn handlebars::Output|
2024 -> handlebars::HelperResult {
2025 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2026 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2027 Ok(())
2028 },
2029 ),
2030 );
2031 let template = include_str!("templates/k6_crud_flow.hbs");
2032
2033 let custom_headers = self.parse_headers()?;
2034
2035 let param_overrides = if let Some(params_file) = &self.params_file {
2037 TerminalReporter::print_progress("Loading parameter overrides...");
2038 let overrides = ParameterOverrides::from_file(params_file)?;
2039 TerminalReporter::print_success(&format!(
2040 "Loaded parameter overrides ({} operation-specific, {} defaults)",
2041 overrides.operations.len(),
2042 if overrides.defaults.is_empty() { 0 } else { 1 }
2043 ));
2044 Some(overrides)
2045 } else {
2046 None
2047 };
2048
2049 let duration_secs = Self::parse_duration(&self.duration)?;
2051 let scenario =
2052 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2053 let stages = scenario.generate_stages(duration_secs, self.vus);
2054
2055 let api_base_path = self.resolve_base_path(parser);
2057 if let Some(ref bp) = api_base_path {
2058 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2059 }
2060
2061 let mut all_headers = custom_headers.clone();
2063 if let Some(auth) = &self.auth {
2064 all_headers.insert("Authorization".to_string(), auth.clone());
2065 }
2066 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2067
2068 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2070
2071 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2072 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2077 serde_json::json!({
2078 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
2081 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2082 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2084 let method_raw = if !parts.is_empty() {
2085 parts[0].to_uppercase()
2086 } else {
2087 "GET".to_string()
2088 };
2089 let method = if !parts.is_empty() {
2090 let m = parts[0].to_lowercase();
2091 if m == "delete" { "del".to_string() } else { m }
2093 } else {
2094 "get".to_string()
2095 };
2096 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2097 let path = if let Some(ref bp) = api_base_path {
2099 format!("{}{}", bp, raw_path)
2100 } else {
2101 raw_path.to_string()
2102 };
2103 let is_get_or_head = method == "get" || method == "head";
2104 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2106
2107 let body_value = if has_body {
2109 param_overrides.as_ref()
2110 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2111 .and_then(|oo| oo.body)
2112 .unwrap_or_else(|| serde_json::json!({}))
2113 } else {
2114 serde_json::json!({})
2115 };
2116
2117 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2119 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2124 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2125
2126 serde_json::json!({
2127 "operation": s.operation,
2128 "method": method,
2129 "path": path,
2130 "extract": s.extract,
2131 "use_values": s.use_values,
2132 "use_body": s.use_body,
2133 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2134 "inject_attacks": s.inject_attacks,
2135 "attack_types": s.attack_types,
2136 "description": s.description,
2137 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2138 "is_get_or_head": is_get_or_head,
2139 "has_body": has_body,
2140 "body": processed_body.value,
2141 "body_is_dynamic": body_is_dynamic,
2142 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2143 })
2144 }).collect::<Vec<_>>(),
2145 })
2146 }).collect();
2147
2148 for flow_data in &flows_data {
2150 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2151 for step in steps {
2152 if let Some(placeholders_arr) =
2153 step.get("_placeholders").and_then(|p| p.as_array())
2154 {
2155 for p_str in placeholders_arr {
2156 if let Some(p_name) = p_str.as_str() {
2157 match p_name {
2159 "VU" => {
2160 all_placeholders.insert(DynamicPlaceholder::VU);
2161 }
2162 "Iteration" => {
2163 all_placeholders.insert(DynamicPlaceholder::Iteration);
2164 }
2165 "Timestamp" => {
2166 all_placeholders.insert(DynamicPlaceholder::Timestamp);
2167 }
2168 "UUID" => {
2169 all_placeholders.insert(DynamicPlaceholder::UUID);
2170 }
2171 "Random" => {
2172 all_placeholders.insert(DynamicPlaceholder::Random);
2173 }
2174 "Counter" => {
2175 all_placeholders.insert(DynamicPlaceholder::Counter);
2176 }
2177 "Date" => {
2178 all_placeholders.insert(DynamicPlaceholder::Date);
2179 }
2180 "VuIter" => {
2181 all_placeholders.insert(DynamicPlaceholder::VuIter);
2182 }
2183 _ => {}
2184 }
2185 }
2186 }
2187 }
2188 }
2189 }
2190 }
2191
2192 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2194 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2195
2196 let invalid_data_config = self.build_invalid_data_config();
2198 let error_injection_enabled = invalid_data_config.is_some();
2199 let error_rate = self.error_rate.unwrap_or(0.0);
2200 let error_types: Vec<String> = invalid_data_config
2201 .as_ref()
2202 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2203 .unwrap_or_default();
2204
2205 if error_injection_enabled {
2206 TerminalReporter::print_progress(&format!(
2207 "Error injection enabled ({}% rate)",
2208 (error_rate * 100.0) as u32
2209 ));
2210 }
2211
2212 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2214
2215 let data = serde_json::json!({
2216 "base_url": self.target,
2217 "flows": flows_data,
2218 "extract_fields": config.default_extract_fields,
2219 "duration_secs": duration_secs,
2220 "max_vus": self.vus,
2221 "auth_header": self.auth,
2222 "custom_headers": custom_headers,
2223 "skip_tls_verify": self.skip_tls_verify,
2224 "stages": stages.iter().map(|s| serde_json::json!({
2226 "duration": s.duration,
2227 "target": s.target,
2228 })).collect::<Vec<_>>(),
2229 "threshold_percentile": self.threshold_percentile,
2230 "threshold_ms": self.threshold_ms,
2231 "max_error_rate": self.max_error_rate,
2232 "headers": headers_json,
2233 "dynamic_imports": required_imports,
2234 "dynamic_globals": required_globals,
2235 "extracted_values_output_path": self
2236 .output
2237 .join("crud_flow_extracted_values.json")
2238 .to_string_lossy(),
2239 "error_injection_enabled": error_injection_enabled,
2241 "error_rate": error_rate,
2242 "error_types": error_types,
2243 "security_testing_enabled": security_testing_enabled,
2245 "has_custom_headers": !custom_headers.is_empty(),
2246 });
2247
2248 let mut script = handlebars
2249 .render_template(template, &data)
2250 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2251
2252 if security_testing_enabled {
2254 script = self.generate_enhanced_script(&script)?;
2255 }
2256
2257 TerminalReporter::print_progress("Validating CRUD flow script...");
2259 let validation_errors = K6ScriptGenerator::validate_script(&script);
2260 if !validation_errors.is_empty() {
2261 TerminalReporter::print_error("CRUD flow script validation failed");
2262 for error in &validation_errors {
2263 eprintln!(" {}", error);
2264 }
2265 return Err(BenchError::Other(format!(
2266 "CRUD flow script validation failed with {} error(s)",
2267 validation_errors.len()
2268 )));
2269 }
2270
2271 TerminalReporter::print_success("CRUD flow script generated");
2272
2273 let script_path = if let Some(output) = &self.script_output {
2275 output.clone()
2276 } else {
2277 self.output.join("k6-crud-flow-script.js")
2278 };
2279
2280 if let Some(parent) = script_path.parent() {
2281 std::fs::create_dir_all(parent)?;
2282 }
2283 std::fs::write(&script_path, &script)?;
2284 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2285
2286 if self.generate_only {
2287 println!("\nScript generated successfully. Run it with:");
2288 println!(" k6 run {}", script_path.display());
2289 return Ok(());
2290 }
2291
2292 TerminalReporter::print_progress("Executing CRUD flow test...");
2294 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2295 std::fs::create_dir_all(&self.output)?;
2296
2297 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2298
2299 let duration_secs = Self::parse_duration(&self.duration)?;
2300 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2301
2302 Ok(())
2303 }
2304
2305 async fn execute_conformance_test(&self) -> Result<()> {
2307 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2308 use crate::conformance::report::ConformanceReport;
2309 use crate::conformance::spec::ConformanceFeature;
2310
2311 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2312
2313 TerminalReporter::print_progress(
2316 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2317 );
2318
2319 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2321 cats_str
2322 .split(',')
2323 .filter_map(|s| {
2324 let trimmed = s.trim();
2325 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2326 Some(canonical.to_string())
2327 } else {
2328 TerminalReporter::print_warning(&format!(
2329 "Unknown conformance category: '{}'. Valid categories: {}",
2330 trimmed,
2331 ConformanceFeature::cli_category_names()
2332 .iter()
2333 .map(|(cli, _)| *cli)
2334 .collect::<Vec<_>>()
2335 .join(", ")
2336 ));
2337 None
2338 }
2339 })
2340 .collect::<Vec<String>>()
2341 });
2342
2343 let custom_headers: Vec<(String, String)> = self
2345 .conformance_headers
2346 .iter()
2347 .filter_map(|h| {
2348 let (name, value) = h.split_once(':')?;
2349 Some((name.trim().to_string(), value.trim().to_string()))
2350 })
2351 .collect();
2352
2353 if !custom_headers.is_empty() {
2354 TerminalReporter::print_progress(&format!(
2355 "Using {} custom header(s) for authentication",
2356 custom_headers.len()
2357 ));
2358 }
2359
2360 if self.conformance_delay_ms > 0 {
2361 TerminalReporter::print_progress(&format!(
2362 "Using {}ms delay between conformance requests",
2363 self.conformance_delay_ms
2364 ));
2365 }
2366
2367 std::fs::create_dir_all(&self.output)?;
2369
2370 let config = ConformanceConfig {
2371 target_url: self.target.clone(),
2372 api_key: self.conformance_api_key.clone(),
2373 basic_auth: self.conformance_basic_auth.clone(),
2374 skip_tls_verify: self.skip_tls_verify,
2375 categories,
2376 base_path: self.base_path.clone(),
2377 custom_headers,
2378 output_dir: Some(self.output.clone()),
2379 all_operations: self.conformance_all_operations,
2380 custom_checks_file: self.conformance_custom.clone(),
2381 request_delay_ms: self.conformance_delay_ms,
2382 custom_filter: self.conformance_custom_filter.clone(),
2383 export_requests: self.export_requests,
2384 validate_requests: self.validate_requests,
2385 };
2386
2387 let mut resolved_base_path: Option<String> = None;
2395 let annotated_ops = if !self.spec.is_empty() {
2396 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2397 let parser = SpecParser::from_file(&self.spec[0]).await?;
2398 resolved_base_path = self.resolve_base_path(&parser);
2399
2400 let mut operations = if let Some(filter) = &self.operations {
2405 parser.filter_operations(filter)?
2406 } else {
2407 parser.get_operations()
2408 };
2409 if let Some(exclude) = &self.exclude_operations {
2410 let before_count = operations.len();
2411 operations = parser.exclude_operations(operations, exclude)?;
2412 let excluded_count = before_count - operations.len();
2413 if excluded_count > 0 {
2414 TerminalReporter::print_progress(&format!(
2415 "Excluded {} operations matching '{}'",
2416 excluded_count, exclude
2417 ));
2418 }
2419 }
2420
2421 let annotated =
2422 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2423 &operations,
2424 parser.spec(),
2425 );
2426 TerminalReporter::print_success(&format!(
2427 "Analyzed {} operations, found {} feature annotations",
2428 operations.len(),
2429 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2430 ));
2431 Some(annotated)
2432 } else {
2433 None
2434 };
2435
2436 if self.conformance_self_test {
2443 let Some(ops) = annotated_ops else {
2444 TerminalReporter::print_error(
2445 "--conformance-self-test requires --spec; no operations to test",
2446 );
2447 return Ok(());
2448 };
2449 let cfg = crate::conformance::self_test::SelfTestConfig {
2450 target_url: self.target.clone(),
2451 skip_tls_verify: self.skip_tls_verify,
2452 timeout: std::time::Duration::from_secs(30),
2453 extra_headers: self
2457 .conformance_headers
2458 .iter()
2459 .filter_map(|h| {
2460 let (n, v) = h.split_once(':')?;
2461 Some((n.trim().to_string(), v.trim().to_string()))
2462 })
2463 .collect(),
2464 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2465 base_path: resolved_base_path.clone(),
2469 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2473 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2474 geo_source_headers: if self.geo_source_headers.is_empty() {
2475 crate::conformance::self_test::default_geo_source_headers()
2476 } else {
2477 self.geo_source_headers.clone()
2478 },
2479 capture: if self.conformance_self_test_capture || self.validate_response_schemas {
2483 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2489 } else {
2490 None
2491 },
2492 validate_response_schemas: self.validate_response_schemas,
2493 };
2494 let capture_sink = cfg.capture.clone();
2495 TerminalReporter::print_progress(&format!(
2496 "Self-test mode: driving {} operations with positive + per-category negative cases",
2497 ops.len()
2498 ));
2499 let report = crate::conformance::self_test::run_self_test(&ops, &cfg)
2500 .await
2501 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2502 if let Some(sink) = capture_sink {
2509 if let Ok(guard) = sink.lock() {
2510 let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2511 let mut lines = String::with_capacity(guard.len() * 256);
2512 for entry in guard.iter() {
2513 if let Ok(line) = serde_json::to_string(entry) {
2514 lines.push_str(&line);
2515 lines.push('\n');
2516 }
2517 }
2518 let _ = std::fs::write(&jsonl_path, lines);
2519 let html_path = self.output.join("conformance-self-test-requests.html");
2520 let html =
2521 crate::conformance::capture_html::render_capture_html(guard.as_slice());
2522 let _ = std::fs::write(&html_path, html);
2523 TerminalReporter::print_progress(&format!(
2524 "Self-test request/response capture written to {} ({} entries) + {}",
2525 jsonl_path.display(),
2526 guard.len(),
2527 html_path.display(),
2528 ));
2529 }
2530 }
2531 TerminalReporter::print_progress(&report.render_summary());
2532 let json_path = self.output.join("conformance-self-test.json");
2536 if let Ok(json) = serde_json::to_string_pretty(&report) {
2537 let _ = std::fs::write(&json_path, json);
2538 TerminalReporter::print_progress(&format!(
2539 "Self-test report written to {}",
2540 json_path.display()
2541 ));
2542 }
2543 if let Some(status) = report.detect_target_misconfiguration() {
2552 let hint = match status {
2553 404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2554 401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2555 _ => "",
2556 };
2557 TerminalReporter::print_warning(&format!(
2558 "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2559 ));
2560 } else if !report.all_passed() {
2561 TerminalReporter::print_warning(
2562 "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2563 );
2564 } else {
2565 TerminalReporter::print_success(
2566 "Self-test passed — all positive cases accepted and all negative cases rejected",
2567 );
2568 }
2569 let html_path = self.output.join("conformance-report.html");
2576 let audit_path = self.output.join("conformance-spec-audit.json");
2577 let audit_value = std::fs::read_to_string(&audit_path)
2578 .ok()
2579 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2580 let render_opts = crate::conformance::report_html::RenderOptions {
2585 missed_cap: match self.report_missed_cap {
2586 Some(0) => None,
2587 Some(n) => Some(n as usize),
2588 None => Some(200),
2589 },
2590 };
2591 let html = crate::conformance::report_html::render_html_with_options(
2592 &report,
2593 audit_value.as_ref(),
2594 &render_opts,
2595 );
2596 if std::fs::write(&html_path, html).is_ok() {
2597 TerminalReporter::print_progress(&format!(
2598 "HTML report written to {}",
2599 html_path.display()
2600 ));
2601 }
2602 return Ok(());
2603 }
2604
2605 if self.validate_requests && !self.spec.is_empty() {
2607 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2608 let violation_count = crate::conformance::request_validator::run_request_validation(
2609 &self.spec,
2610 self.conformance_custom.as_deref(),
2611 self.base_path.as_deref(),
2612 &self.output,
2613 )
2614 .await?;
2615 if violation_count > 0 {
2616 TerminalReporter::print_warning(&format!(
2617 "{} request validation violation(s) found — see conformance-request-violations.json",
2618 violation_count
2619 ));
2620 } else {
2621 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2622 }
2623 }
2624
2625 if self.generate_only || self.use_k6 {
2627 let script = if let Some(annotated) = &annotated_ops {
2628 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2629 config,
2630 annotated.clone(),
2631 );
2632 let op_count = gen.operation_count();
2633 let (script, check_count) = gen.generate()?;
2634 TerminalReporter::print_success(&format!(
2635 "Conformance: {} operations analyzed, {} unique checks generated",
2636 op_count, check_count
2637 ));
2638 script
2639 } else {
2640 let generator = ConformanceGenerator::new(config);
2641 generator.generate()?
2642 };
2643
2644 let script_path = self.output.join("k6-conformance.js");
2645 std::fs::write(&script_path, &script).map_err(|e| {
2646 BenchError::Other(format!("Failed to write conformance script: {}", e))
2647 })?;
2648 TerminalReporter::print_success(&format!(
2649 "Conformance script generated: {}",
2650 script_path.display()
2651 ));
2652
2653 if self.generate_only {
2654 println!("\nScript generated. Run with:");
2655 println!(" k6 run {}", script_path.display());
2656 return Ok(());
2657 }
2658
2659 if !K6Executor::is_k6_installed() {
2661 TerminalReporter::print_error("k6 is not installed");
2662 TerminalReporter::print_warning(
2663 "Install k6 from: https://k6.io/docs/get-started/installation/",
2664 );
2665 return Err(BenchError::K6NotFound);
2666 }
2667
2668 TerminalReporter::print_progress("Running conformance tests via k6...");
2669 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2670 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2671
2672 let report_path = self.output.join("conformance-report.json");
2673 if report_path.exists() {
2674 let report = ConformanceReport::from_file(&report_path)?;
2675 report.print_report_with_options(self.conformance_all_operations);
2676 self.save_conformance_report(&report, &report_path)?;
2677 } else {
2678 TerminalReporter::print_warning(
2679 "Conformance report not generated (k6 handleSummary may not have run)",
2680 );
2681 }
2682
2683 return Ok(());
2684 }
2685
2686 TerminalReporter::print_progress("Running conformance tests (native executor)...");
2688
2689 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2690
2691 executor = if let Some(annotated) = &annotated_ops {
2692 executor.with_spec_driven_checks(annotated)
2693 } else {
2694 executor.with_reference_checks()
2695 };
2696 executor = executor.with_custom_checks()?;
2697
2698 TerminalReporter::print_success(&format!(
2699 "Executing {} conformance checks...",
2700 executor.check_count()
2701 ));
2702
2703 let report = executor.execute().await?;
2704 report.print_report_with_options(self.conformance_all_operations);
2705
2706 let failure_details = report.failure_details();
2708 if !failure_details.is_empty() {
2709 let details_path = self.output.join("conformance-failure-details.json");
2710 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2711 let _ = std::fs::write(&details_path, json);
2712 TerminalReporter::print_success(&format!(
2713 "Failure details saved to: {}",
2714 details_path.display()
2715 ));
2716 }
2717 }
2718
2719 let report_path = self.output.join("conformance-report.json");
2721 let report_json = serde_json::to_string_pretty(&report.to_json())
2722 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2723 std::fs::write(&report_path, &report_json)
2724 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2725 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2726
2727 self.save_conformance_report(&report, &report_path)?;
2728
2729 Ok(())
2730 }
2731
2732 fn save_conformance_report(
2734 &self,
2735 report: &crate::conformance::report::ConformanceReport,
2736 report_path: &Path,
2737 ) -> Result<()> {
2738 if self.conformance_report_format == "sarif" {
2739 use crate::conformance::sarif::ConformanceSarifReport;
2740 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2741 TerminalReporter::print_success(&format!(
2742 "SARIF report saved to: {}",
2743 self.conformance_report.display()
2744 ));
2745 } else if self.conformance_report != *report_path {
2746 std::fs::copy(report_path, &self.conformance_report)?;
2747 TerminalReporter::print_success(&format!(
2748 "Report saved to: {}",
2749 self.conformance_report.display()
2750 ));
2751 }
2752 Ok(())
2753 }
2754
2755 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2761 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2762 use crate::conformance::report::ConformanceReport;
2763 use crate::conformance::spec::ConformanceFeature;
2764
2765 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2766
2767 TerminalReporter::print_progress("Parsing targets file...");
2769 let targets = parse_targets_file(targets_file)?;
2770 let num_targets = targets.len();
2771 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2772
2773 if targets.is_empty() {
2774 return Err(BenchError::Other("No targets found in file".to_string()));
2775 }
2776
2777 TerminalReporter::print_progress(
2778 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2779 );
2780
2781 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2783 cats_str
2784 .split(',')
2785 .filter_map(|s| {
2786 let trimmed = s.trim();
2787 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2788 Some(canonical.to_string())
2789 } else {
2790 TerminalReporter::print_warning(&format!(
2791 "Unknown conformance category: '{}'. Valid categories: {}",
2792 trimmed,
2793 ConformanceFeature::cli_category_names()
2794 .iter()
2795 .map(|(cli, _)| *cli)
2796 .collect::<Vec<_>>()
2797 .join(", ")
2798 ));
2799 None
2800 }
2801 })
2802 .collect::<Vec<String>>()
2803 });
2804
2805 let base_custom_headers: Vec<(String, String)> = self
2807 .conformance_headers
2808 .iter()
2809 .filter_map(|h| {
2810 let (name, value) = h.split_once(':')?;
2811 Some((name.trim().to_string(), value.trim().to_string()))
2812 })
2813 .collect();
2814
2815 if !base_custom_headers.is_empty() {
2816 TerminalReporter::print_progress(&format!(
2817 "Using {} base custom header(s) for authentication",
2818 base_custom_headers.len()
2819 ));
2820 }
2821
2822 let annotated_ops = if !self.spec.is_empty() {
2824 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2825 let parser = SpecParser::from_file(&self.spec[0]).await?;
2826 let operations = parser.get_operations();
2827 let annotated =
2828 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2829 &operations,
2830 parser.spec(),
2831 );
2832 TerminalReporter::print_success(&format!(
2833 "Analyzed {} operations, found {} feature annotations",
2834 operations.len(),
2835 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2836 ));
2837 Some(annotated)
2838 } else {
2839 None
2840 };
2841
2842 std::fs::create_dir_all(&self.output)?;
2844
2845 struct TargetResult {
2847 url: String,
2848 passed: usize,
2849 failed: usize,
2850 elapsed: std::time::Duration,
2851 report_json: serde_json::Value,
2852 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
2853 }
2854
2855 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2856 let total_start = std::time::Instant::now();
2857
2858 for (idx, target) in targets.iter().enumerate() {
2859 tracing::info!(
2860 "Running conformance tests against target {}/{}: {}",
2861 idx + 1,
2862 num_targets,
2863 target.url
2864 );
2865 TerminalReporter::print_progress(&format!(
2866 "\n--- Target {}/{}: {} ---",
2867 idx + 1,
2868 num_targets,
2869 target.url
2870 ));
2871
2872 let mut merged_headers = base_custom_headers.clone();
2874 if let Some(ref target_headers) = target.headers {
2875 for (name, value) in target_headers {
2876 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2878 existing.1 = value.clone();
2879 } else {
2880 merged_headers.push((name.clone(), value.clone()));
2881 }
2882 }
2883 }
2884 if let Some(ref auth) = target.auth {
2886 if let Some(existing) =
2887 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2888 {
2889 existing.1 = auth.clone();
2890 } else {
2891 merged_headers.push(("Authorization".to_string(), auth.clone()));
2892 }
2893 }
2894
2895 let target_dir = self.output.join(format!("target_{}", idx));
2901 std::fs::create_dir_all(&target_dir)?;
2902
2903 let config = ConformanceConfig {
2904 target_url: target.url.clone(),
2905 api_key: self.conformance_api_key.clone(),
2906 basic_auth: self.conformance_basic_auth.clone(),
2907 skip_tls_verify: self.skip_tls_verify,
2908 categories: categories.clone(),
2909 base_path: self.base_path.clone(),
2910 custom_headers: merged_headers,
2911 output_dir: Some(target_dir.clone()),
2912 all_operations: self.conformance_all_operations,
2913 custom_checks_file: self.conformance_custom.clone(),
2914 request_delay_ms: self.conformance_delay_ms,
2915 custom_filter: self.conformance_custom_filter.clone(),
2916 export_requests: self.export_requests,
2917 validate_requests: self.validate_requests,
2918 };
2919
2920 let target_start = std::time::Instant::now();
2921 let report = if self.use_k6 {
2922 if !K6Executor::is_k6_installed() {
2923 TerminalReporter::print_error("k6 is not installed");
2924 TerminalReporter::print_warning(
2925 "Install k6 from: https://k6.io/docs/get-started/installation/",
2926 );
2927 return Err(BenchError::K6NotFound);
2928 }
2929
2930 let script = if let Some(ref annotated) = annotated_ops {
2931 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2932 config.clone(),
2933 annotated.clone(),
2934 );
2935 let (script, _check_count) = gen.generate()?;
2936 script
2937 } else {
2938 let generator = ConformanceGenerator::new(config.clone());
2939 generator.generate()?
2940 };
2941
2942 let script_path = target_dir.join("k6-conformance.js");
2943 std::fs::write(&script_path, &script).map_err(|e| {
2944 BenchError::Other(format!("Failed to write conformance script: {}", e))
2945 })?;
2946 TerminalReporter::print_success(&format!(
2947 "Conformance script generated: {}",
2948 script_path.display()
2949 ));
2950
2951 TerminalReporter::print_progress(&format!(
2952 "Running conformance tests via k6 against {}...",
2953 target.url
2954 ));
2955 let k6 = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2956 let api_port = 6565u16.saturating_add(idx as u16);
2958 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
2959 .await?;
2960
2961 let report_path = target_dir.join("conformance-report.json");
2962 if report_path.exists() {
2963 ConformanceReport::from_file(&report_path)?
2964 } else {
2965 TerminalReporter::print_warning(&format!(
2966 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
2967 target.url
2968 ));
2969 continue;
2970 }
2971 } else {
2972 let mut executor =
2973 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2974
2975 executor = if let Some(ref annotated) = annotated_ops {
2976 executor.with_spec_driven_checks(annotated)
2977 } else {
2978 executor.with_reference_checks()
2979 };
2980 executor = executor.with_custom_checks()?;
2981
2982 TerminalReporter::print_success(&format!(
2983 "Executing {} conformance checks against {}...",
2984 executor.check_count(),
2985 target.url
2986 ));
2987
2988 executor.execute().await?
2989 };
2990 let target_elapsed = target_start.elapsed();
2991
2992 let report_json = report.to_json();
2993
2994 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2996 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2997 let total_checks = passed + failed;
2998 let rate = if total_checks == 0 {
2999 0.0
3000 } else {
3001 (passed as f64 / total_checks as f64) * 100.0
3002 };
3003
3004 TerminalReporter::print_success(&format!(
3005 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3006 target.url,
3007 passed,
3008 total_checks,
3009 rate,
3010 target_elapsed.as_secs_f64()
3011 ));
3012
3013 let target_report_path = target_dir.join("conformance-report.json");
3015 let report_str = serde_json::to_string_pretty(&report_json)
3016 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3017 std::fs::write(&target_report_path, &report_str)
3018 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3019
3020 let failure_details = report.failure_details();
3022 if !failure_details.is_empty() {
3023 let details_path = target_dir.join("conformance-failure-details.json");
3024 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3025 let _ = std::fs::write(&details_path, json);
3026 }
3027 }
3028
3029 let owasp_coverage = report.owasp_coverage_data();
3031
3032 target_results.push(TargetResult {
3033 url: target.url.clone(),
3034 passed,
3035 failed,
3036 elapsed: target_elapsed,
3037 report_json,
3038 owasp_coverage,
3039 });
3040 }
3041
3042 let total_elapsed = total_start.elapsed();
3043
3044 println!("\n{}", "=".repeat(80));
3046 println!(" Multi-Target Conformance Summary");
3047 println!("{}", "=".repeat(80));
3048 println!(
3049 " {:<40} {:>8} {:>8} {:>8} {:>8}",
3050 "Target URL", "Passed", "Failed", "Rate", "Time"
3051 );
3052 println!(" {}", "-".repeat(76));
3053
3054 let mut total_passed = 0usize;
3055 let mut total_failed = 0usize;
3056
3057 for result in &target_results {
3058 let total_checks = result.passed + result.failed;
3059 let rate = if total_checks == 0 {
3060 0.0
3061 } else {
3062 (result.passed as f64 / total_checks as f64) * 100.0
3063 };
3064
3065 let display_url = if result.url.len() > 38 {
3067 format!("{}...", &result.url[..35])
3068 } else {
3069 result.url.clone()
3070 };
3071
3072 println!(
3073 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3074 display_url,
3075 result.passed,
3076 result.failed,
3077 rate,
3078 result.elapsed.as_secs_f64()
3079 );
3080
3081 total_passed += result.passed;
3082 total_failed += result.failed;
3083 }
3084
3085 let grand_total = total_passed + total_failed;
3086 let overall_rate = if grand_total == 0 {
3087 0.0
3088 } else {
3089 (total_passed as f64 / grand_total as f64) * 100.0
3090 };
3091
3092 println!(" {}", "-".repeat(76));
3093 println!(
3094 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3095 format!("TOTAL ({} targets)", num_targets),
3096 total_passed,
3097 total_failed,
3098 overall_rate,
3099 total_elapsed.as_secs_f64()
3100 );
3101 println!("{}", "=".repeat(80));
3102
3103 for result in &target_results {
3105 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
3106 for entry in &result.owasp_coverage {
3107 let status = if !entry.tested {
3108 "-"
3109 } else if entry.all_passed {
3110 "pass"
3111 } else {
3112 "FAIL"
3113 };
3114 let via = if entry.via_categories.is_empty() {
3115 String::new()
3116 } else {
3117 format!(" (via {})", entry.via_categories.join(", "))
3118 };
3119 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3120 }
3121 }
3122
3123 let per_target_summaries: Vec<serde_json::Value> = target_results
3125 .iter()
3126 .enumerate()
3127 .map(|(idx, r)| {
3128 let total_checks = r.passed + r.failed;
3129 let rate = if total_checks == 0 {
3130 0.0
3131 } else {
3132 (r.passed as f64 / total_checks as f64) * 100.0
3133 };
3134 let owasp_json: Vec<serde_json::Value> = r
3135 .owasp_coverage
3136 .iter()
3137 .map(|e| {
3138 serde_json::json!({
3139 "id": e.id,
3140 "name": e.name,
3141 "tested": e.tested,
3142 "all_passed": e.all_passed,
3143 "via_categories": e.via_categories,
3144 })
3145 })
3146 .collect();
3147 serde_json::json!({
3148 "target_url": r.url,
3149 "target_index": idx,
3150 "checks_passed": r.passed,
3151 "checks_failed": r.failed,
3152 "total_checks": total_checks,
3153 "pass_rate": rate,
3154 "elapsed_seconds": r.elapsed.as_secs_f64(),
3155 "report": r.report_json,
3156 "owasp_coverage": owasp_json,
3157 })
3158 })
3159 .collect();
3160
3161 let combined_summary = serde_json::json!({
3162 "total_targets": num_targets,
3163 "total_checks_passed": total_passed,
3164 "total_checks_failed": total_failed,
3165 "overall_pass_rate": overall_rate,
3166 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3167 "targets": per_target_summaries,
3168 });
3169
3170 let summary_path = self.output.join("multi-target-conformance-summary.json");
3171 let summary_str = serde_json::to_string_pretty(&combined_summary)
3172 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3173 std::fs::write(&summary_path, &summary_str)
3174 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3175 TerminalReporter::print_success(&format!(
3176 "Combined summary saved to: {}",
3177 summary_path.display()
3178 ));
3179
3180 Ok(())
3181 }
3182
3183 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3185 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3186
3187 let custom_headers = self.parse_headers()?;
3189
3190 let mut config = OwaspApiConfig::new()
3192 .with_auth_header(&self.owasp_auth_header)
3193 .with_verbose(self.verbose)
3194 .with_insecure(self.skip_tls_verify)
3195 .with_concurrency(self.vus as usize)
3196 .with_iterations(self.owasp_iterations as usize)
3197 .with_base_path(self.base_path.clone())
3198 .with_custom_headers(custom_headers);
3199
3200 if let Some(ref token) = self.owasp_auth_token {
3202 config = config.with_valid_auth_token(token);
3203 }
3204
3205 if let Some(ref cats_str) = self.owasp_categories {
3207 let categories: Vec<OwaspCategory> = cats_str
3208 .split(',')
3209 .filter_map(|s| {
3210 let trimmed = s.trim();
3211 match trimmed.parse::<OwaspCategory>() {
3212 Ok(cat) => Some(cat),
3213 Err(e) => {
3214 TerminalReporter::print_warning(&e);
3215 None
3216 }
3217 }
3218 })
3219 .collect();
3220
3221 if !categories.is_empty() {
3222 config = config.with_categories(categories);
3223 }
3224 }
3225
3226 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3228 config.admin_paths_file = Some(admin_paths_file.clone());
3229 if let Err(e) = config.load_admin_paths() {
3230 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3231 }
3232 }
3233
3234 if let Some(ref id_fields_str) = self.owasp_id_fields {
3236 let id_fields: Vec<String> = id_fields_str
3237 .split(',')
3238 .map(|s| s.trim().to_string())
3239 .filter(|s| !s.is_empty())
3240 .collect();
3241 if !id_fields.is_empty() {
3242 config = config.with_id_fields(id_fields);
3243 }
3244 }
3245
3246 if let Some(ref report_path) = self.owasp_report {
3248 config = config.with_report_path(report_path);
3249 }
3250 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3251 config = config.with_report_format(format);
3252 }
3253
3254 let categories = config.categories_to_test();
3256 TerminalReporter::print_success(&format!(
3257 "Testing {} OWASP categories: {}",
3258 categories.len(),
3259 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3260 ));
3261
3262 if config.valid_auth_token.is_some() {
3263 TerminalReporter::print_progress("Using provided auth token for baseline requests");
3264 }
3265
3266 TerminalReporter::print_progress("Generating OWASP security test script...");
3268 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3269
3270 let script = generator.generate()?;
3272 TerminalReporter::print_success("OWASP security test script generated");
3273
3274 let script_path = if let Some(output) = &self.script_output {
3276 output.clone()
3277 } else {
3278 self.output.join("k6-owasp-security-test.js")
3279 };
3280
3281 if let Some(parent) = script_path.parent() {
3282 std::fs::create_dir_all(parent)?;
3283 }
3284 std::fs::write(&script_path, &script)?;
3285 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3286
3287 if self.generate_only {
3289 println!("\nOWASP security test script generated. Run it with:");
3290 println!(" k6 run {}", script_path.display());
3291 return Ok(());
3292 }
3293
3294 TerminalReporter::print_progress("Executing OWASP security tests...");
3296 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3297 std::fs::create_dir_all(&self.output)?;
3298
3299 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3300
3301 let duration_secs = Self::parse_duration(&self.duration)?;
3302 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
3303
3304 println!("\nOWASP security test results saved to: {}", self.output.display());
3305
3306 Ok(())
3307 }
3308}
3309
3310#[cfg(test)]
3311mod tests {
3312 use super::*;
3313 use tempfile::tempdir;
3314
3315 #[test]
3316 fn test_parse_duration() {
3317 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
3318 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
3319 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
3320 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
3321 }
3322
3323 #[test]
3327 fn parse_ip_list_ipv4_range_inclusive() {
3328 let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
3329 assert_eq!(v.len(), 23);
3330 assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
3331 assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
3332 }
3333
3334 #[test]
3337 fn parse_ip_list_range_rejects_backwards() {
3338 let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
3339 assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
3340 }
3341
3342 #[test]
3346 fn parse_ip_list_rejects_ipv6_range_syntax() {
3347 let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
3348 assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
3349 }
3350
3351 #[test]
3353 fn parse_ip_list_range_capped_at_256() {
3354 let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
3355 assert_eq!(v.len(), 256);
3356 assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
3357 }
3358
3359 #[test]
3362 fn parse_ip_list_plain_and_comma() {
3363 let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
3364 assert_eq!(v.len(), 3);
3365 assert_eq!(v[0].to_string(), "10.0.0.5");
3366 assert_eq!(v[2].to_string(), "10.0.0.7");
3367 }
3368
3369 #[test]
3372 fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
3373 let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
3374 assert_eq!(v.len(), 8);
3375 assert_eq!(v[0].to_string(), "10.0.0.0");
3376 assert_eq!(v[7].to_string(), "10.0.0.7");
3377 }
3378
3379 #[test]
3382 fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
3383 let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
3384 assert_eq!(v.len(), 256);
3385 assert_eq!(v[0].to_string(), "10.0.0.0");
3386 assert_eq!(v[255].to_string(), "10.0.0.255");
3387 }
3388
3389 #[test]
3391 fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
3392 let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
3393 assert_eq!(v.len(), 4);
3394 assert!(v[0].is_ipv6());
3395 assert_eq!(v[0].to_string(), "2001:db8::");
3396 assert_eq!(v[3].to_string(), "2001:db8::3");
3397 }
3398
3399 #[test]
3401 fn parse_ip_list_mixed_v4_v6_cidr() {
3402 let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
3403 assert_eq!(v.len(), 6); assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
3405 assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
3406 }
3407
3408 #[test]
3411 fn parse_ip_list_skips_malformed() {
3412 let v = parse_ip_list(
3413 &[
3414 "10.0.0.5".into(),
3415 "not-an-ip".into(),
3416 "10.0.0.6".into(),
3417 "/24".into(),
3418 "1.2.3.4/200".into(),
3419 ],
3420 "source-ip",
3421 );
3422 assert_eq!(v.len(), 2);
3423 assert_eq!(v[0].to_string(), "10.0.0.5");
3424 assert_eq!(v[1].to_string(), "10.0.0.6");
3425 }
3426
3427 #[test]
3428 fn test_parse_duration_invalid() {
3429 assert!(BenchCommand::parse_duration("invalid").is_err());
3430 assert!(BenchCommand::parse_duration("30x").is_err());
3431 }
3432
3433 #[test]
3434 fn test_parse_headers() {
3435 let cmd = BenchCommand {
3436 spec: vec![PathBuf::from("test.yaml")],
3437 spec_dir: None,
3438 merge_conflicts: "error".to_string(),
3439 spec_mode: "merge".to_string(),
3440 dependency_config: None,
3441 target: "http://localhost".to_string(),
3442 base_path: None,
3443 duration: "1m".to_string(),
3444 vus: 10,
3445 scenario: "ramp-up".to_string(),
3446 operations: None,
3447 exclude_operations: None,
3448 auth: None,
3449 headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
3450 output: PathBuf::from("output"),
3451 generate_only: false,
3452 script_output: None,
3453 threshold_percentile: "p(95)".to_string(),
3454 threshold_ms: 500,
3455 max_error_rate: 0.05,
3456 verbose: false,
3457 skip_tls_verify: false,
3458 chunked_request_bodies: false,
3459 target_rps: None,
3460 no_keep_alive: false,
3461 targets_file: None,
3462 max_concurrency: None,
3463 results_format: "both".to_string(),
3464 params_file: None,
3465 crud_flow: false,
3466 flow_config: None,
3467 extract_fields: None,
3468 parallel_create: None,
3469 data_file: None,
3470 data_distribution: "unique-per-vu".to_string(),
3471 data_mappings: None,
3472 per_uri_control: false,
3473 error_rate: None,
3474 error_types: None,
3475 security_test: false,
3476 security_payloads: None,
3477 security_categories: None,
3478 security_target_fields: None,
3479 wafbench_dir: None,
3480 wafbench_cycle_all: false,
3481 owasp_api_top10: false,
3482 owasp_categories: None,
3483 owasp_auth_header: "Authorization".to_string(),
3484 owasp_auth_token: None,
3485 owasp_admin_paths: None,
3486 owasp_id_fields: None,
3487 owasp_report: None,
3488 owasp_report_format: "json".to_string(),
3489 owasp_iterations: 1,
3490 conformance: false,
3491 conformance_api_key: None,
3492 conformance_basic_auth: None,
3493 conformance_report: PathBuf::from("conformance-report.json"),
3494 conformance_categories: None,
3495 conformance_report_format: "json".to_string(),
3496 conformance_headers: vec![],
3497 conformance_all_operations: false,
3498 conformance_custom: None,
3499 conformance_delay_ms: 0,
3500 use_k6: false,
3501 conformance_custom_filter: None,
3502 export_requests: false,
3503 validate_requests: false,
3504 conformance_self_test: false,
3505 conformance_self_test_capture: false,
3506 validate_response_schemas: false,
3507 source_ips: Vec::new(),
3508 geo_source_ips: Vec::new(),
3509 geo_source_headers: Vec::new(),
3510 report_missed_cap: None,
3511 };
3512
3513 let headers = cmd.parse_headers().unwrap();
3514 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
3515 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
3516 }
3517
3518 #[test]
3519 fn test_get_spec_display_name() {
3520 let cmd = BenchCommand {
3521 spec: vec![PathBuf::from("test.yaml")],
3522 spec_dir: None,
3523 merge_conflicts: "error".to_string(),
3524 spec_mode: "merge".to_string(),
3525 dependency_config: None,
3526 target: "http://localhost".to_string(),
3527 base_path: None,
3528 duration: "1m".to_string(),
3529 vus: 10,
3530 scenario: "ramp-up".to_string(),
3531 operations: None,
3532 exclude_operations: None,
3533 auth: None,
3534 headers: None,
3535 output: PathBuf::from("output"),
3536 generate_only: false,
3537 script_output: None,
3538 threshold_percentile: "p(95)".to_string(),
3539 threshold_ms: 500,
3540 max_error_rate: 0.05,
3541 verbose: false,
3542 skip_tls_verify: false,
3543 chunked_request_bodies: false,
3544 target_rps: None,
3545 no_keep_alive: false,
3546 targets_file: None,
3547 max_concurrency: None,
3548 results_format: "both".to_string(),
3549 params_file: None,
3550 crud_flow: false,
3551 flow_config: None,
3552 extract_fields: None,
3553 parallel_create: None,
3554 data_file: None,
3555 data_distribution: "unique-per-vu".to_string(),
3556 data_mappings: None,
3557 per_uri_control: false,
3558 error_rate: None,
3559 error_types: None,
3560 security_test: false,
3561 security_payloads: None,
3562 security_categories: None,
3563 security_target_fields: None,
3564 wafbench_dir: None,
3565 wafbench_cycle_all: false,
3566 owasp_api_top10: false,
3567 owasp_categories: None,
3568 owasp_auth_header: "Authorization".to_string(),
3569 owasp_auth_token: None,
3570 owasp_admin_paths: None,
3571 owasp_id_fields: None,
3572 owasp_report: None,
3573 owasp_report_format: "json".to_string(),
3574 owasp_iterations: 1,
3575 conformance: false,
3576 conformance_api_key: None,
3577 conformance_basic_auth: None,
3578 conformance_report: PathBuf::from("conformance-report.json"),
3579 conformance_categories: None,
3580 conformance_report_format: "json".to_string(),
3581 conformance_headers: vec![],
3582 conformance_all_operations: false,
3583 conformance_custom: None,
3584 conformance_delay_ms: 0,
3585 use_k6: false,
3586 conformance_custom_filter: None,
3587 export_requests: false,
3588 validate_requests: false,
3589 conformance_self_test: false,
3590 conformance_self_test_capture: false,
3591 validate_response_schemas: false,
3592 source_ips: Vec::new(),
3593 geo_source_ips: Vec::new(),
3594 geo_source_headers: Vec::new(),
3595 report_missed_cap: None,
3596 };
3597
3598 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
3599
3600 let cmd_multi = BenchCommand {
3602 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
3603 spec_dir: None,
3604 merge_conflicts: "error".to_string(),
3605 spec_mode: "merge".to_string(),
3606 dependency_config: None,
3607 target: "http://localhost".to_string(),
3608 base_path: None,
3609 duration: "1m".to_string(),
3610 vus: 10,
3611 scenario: "ramp-up".to_string(),
3612 operations: None,
3613 exclude_operations: None,
3614 auth: None,
3615 headers: None,
3616 output: PathBuf::from("output"),
3617 generate_only: false,
3618 script_output: None,
3619 threshold_percentile: "p(95)".to_string(),
3620 threshold_ms: 500,
3621 max_error_rate: 0.05,
3622 verbose: false,
3623 skip_tls_verify: false,
3624 chunked_request_bodies: false,
3625 target_rps: None,
3626 no_keep_alive: false,
3627 targets_file: None,
3628 max_concurrency: None,
3629 results_format: "both".to_string(),
3630 params_file: None,
3631 crud_flow: false,
3632 flow_config: None,
3633 extract_fields: None,
3634 parallel_create: None,
3635 data_file: None,
3636 data_distribution: "unique-per-vu".to_string(),
3637 data_mappings: None,
3638 per_uri_control: false,
3639 error_rate: None,
3640 error_types: None,
3641 security_test: false,
3642 security_payloads: None,
3643 security_categories: None,
3644 security_target_fields: None,
3645 wafbench_dir: None,
3646 wafbench_cycle_all: false,
3647 owasp_api_top10: false,
3648 owasp_categories: None,
3649 owasp_auth_header: "Authorization".to_string(),
3650 owasp_auth_token: None,
3651 owasp_admin_paths: None,
3652 owasp_id_fields: None,
3653 owasp_report: None,
3654 owasp_report_format: "json".to_string(),
3655 owasp_iterations: 1,
3656 conformance: false,
3657 conformance_api_key: None,
3658 conformance_basic_auth: None,
3659 conformance_report: PathBuf::from("conformance-report.json"),
3660 conformance_categories: None,
3661 conformance_report_format: "json".to_string(),
3662 conformance_headers: vec![],
3663 conformance_all_operations: false,
3664 conformance_custom: None,
3665 conformance_delay_ms: 0,
3666 use_k6: false,
3667 conformance_custom_filter: None,
3668 export_requests: false,
3669 validate_requests: false,
3670 conformance_self_test: false,
3671 conformance_self_test_capture: false,
3672 validate_response_schemas: false,
3673 source_ips: Vec::new(),
3674 geo_source_ips: Vec::new(),
3675 geo_source_headers: Vec::new(),
3676 report_missed_cap: None,
3677 };
3678
3679 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
3680 }
3681
3682 #[test]
3683 fn test_parse_extracted_values_from_output_dir() {
3684 let dir = tempdir().unwrap();
3685 let path = dir.path().join("extracted_values.json");
3686 std::fs::write(
3687 &path,
3688 r#"{
3689 "pool_id": "abc123",
3690 "count": 0,
3691 "enabled": false,
3692 "metadata": { "owner": "team-a" }
3693}"#,
3694 )
3695 .unwrap();
3696
3697 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3698 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
3699 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
3700 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
3701 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
3702 }
3703
3704 #[test]
3705 fn test_parse_extracted_values_missing_file() {
3706 let dir = tempdir().unwrap();
3707 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3708 assert!(extracted.values.is_empty());
3709 }
3710}