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(inputs: &[String]) -> Result<HashMap<String, String>> {
45 let mut headers = HashMap::new();
46
47 for pair in inputs {
48 let pair = pair.trim();
49 if pair.is_empty() {
50 continue;
51 }
52 let parts: Vec<&str> = pair.splitn(2, ':').collect();
53 if parts.len() != 2 {
54 return Err(BenchError::Other(format!(
55 "Invalid header format: '{}'. Expected 'Key:Value'",
56 pair
57 )));
58 }
59 headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
60 }
61
62 Ok(headers)
63}
64
65pub struct BenchCommand {
67 pub spec: Vec<PathBuf>,
69 pub spec_dir: Option<PathBuf>,
71 pub merge_conflicts: String,
73 pub spec_mode: String,
75 pub dependency_config: Option<PathBuf>,
77 pub target: String,
78 pub base_path: Option<String>,
81 pub duration: String,
82 pub vus: u32,
83 pub target_rps: Option<u32>,
89 pub no_keep_alive: bool,
94 pub scenario: String,
95 pub operations: Option<String>,
96 pub exclude_operations: Option<String>,
100 pub auth: Option<String>,
101 pub headers: Vec<String>,
104 pub output: PathBuf,
105 pub generate_only: bool,
106 pub script_output: Option<PathBuf>,
107 pub threshold_percentile: String,
108 pub threshold_ms: u64,
109 pub max_error_rate: f64,
110 pub abort_on_error: bool,
115 pub abort_on_error_rate: f64,
119 pub verbose: bool,
120 pub skip_tls_verify: bool,
121 pub chunked_request_bodies: bool,
126 pub targets_file: Option<PathBuf>,
128 pub max_concurrency: Option<u32>,
130 pub results_format: String,
132 pub params_file: Option<PathBuf>,
137
138 pub crud_flow: bool,
141 pub flow_config: Option<PathBuf>,
143 pub extract_fields: Option<String>,
145
146 pub parallel_create: Option<u32>,
149
150 pub data_file: Option<PathBuf>,
153 pub data_distribution: String,
155 pub data_mappings: Option<String>,
157 pub per_uri_control: bool,
159
160 pub error_rate: Option<f64>,
163 pub error_types: Option<String>,
165
166 pub security_test: bool,
169 pub security_payloads: Option<PathBuf>,
171 pub security_categories: Option<String>,
173 pub security_target_fields: Option<String>,
175
176 pub wafbench_dir: Option<String>,
179 pub wafbench_cycle_all: bool,
181
182 pub conformance: bool,
185 pub conformance_api_key: Option<String>,
187 pub conformance_basic_auth: Option<String>,
189 pub conformance_report: PathBuf,
191 pub conformance_categories: Option<String>,
193 pub conformance_report_format: String,
195 pub conformance_headers: Vec<String>,
198 pub conformance_all_operations: bool,
201 pub conformance_custom: Option<PathBuf>,
203 pub conformance_delay_ms: u64,
206 pub use_k6: bool,
208 pub conformance_custom_filter: Option<String>,
212 pub export_requests: bool,
215 pub validate_requests: bool,
218 pub conformance_self_test: bool,
225 pub conformance_self_test_capture: bool,
229 pub validate_response_schemas: bool,
235 pub conformance_self_test_iterations: u32,
240 pub conformance_self_test_duration: Option<String>,
245
246 pub source_ips: Vec<String>,
251 pub geo_source_ips: Vec<String>,
255 pub geo_source_headers: Vec<String>,
259
260 pub report_missed_cap: Option<u32>,
267
268 pub discard_response_bodies: bool,
275
276 pub dns_policy: Option<String>,
282
283 pub owasp_api_top10: bool,
286 pub owasp_categories: Option<String>,
288 pub owasp_auth_header: String,
290 pub owasp_auth_token: Option<String>,
292 pub owasp_admin_paths: Option<PathBuf>,
294 pub owasp_id_fields: Option<String>,
296 pub owasp_report: Option<PathBuf>,
298 pub owasp_report_format: String,
300 pub owasp_iterations: u32,
302}
303
304fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
318 use std::net::IpAddr;
319 const MAX_CIDR_EXPANSION: usize = 256;
320 let mut out = Vec::new();
321 for entry in raw {
322 for piece in entry.split(',') {
323 let s = piece.trim();
324 if s.is_empty() {
325 continue;
326 }
327 if let Some((addr_part, prefix_part)) = s.split_once('/') {
329 let prefix: u32 = match prefix_part.parse() {
330 Ok(p) => p,
331 Err(e) => {
332 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR prefix: {e}");
333 continue;
334 }
335 };
336 let net_addr: IpAddr = match addr_part.parse() {
337 Ok(a) => a,
338 Err(e) => {
339 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR address: {e}");
340 continue;
341 }
342 };
343 expand_cidr(net_addr, prefix, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
344 continue;
345 }
346 if let Some((start_str, end_str)) = s.split_once('-') {
352 let start_s = start_str.trim();
353 let end_s = end_str.trim();
354 if start_s.contains(':') || end_s.contains(':') {
358 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{s}': IPv6 range syntax not supported (use CIDR like 2001:db8::/126 instead)");
359 continue;
360 }
361 let start: IpAddr = match start_s.parse() {
362 Ok(a) => a,
363 Err(e) => {
364 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range start: {e}");
365 continue;
366 }
367 };
368 let end: IpAddr = match end_s.parse() {
369 Ok(a) => a,
370 Err(e) => {
371 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range end: {e}");
372 continue;
373 }
374 };
375 expand_range(start, end, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
376 continue;
377 }
378 match s.parse::<IpAddr>() {
380 Ok(ip) => out.push(ip),
381 Err(e) => {
382 tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
383 }
384 }
385 }
386 }
387 out
388}
389
390fn expand_range(
394 start: std::net::IpAddr,
395 end: std::net::IpAddr,
396 cap: usize,
397 flag_name: &str,
398 raw: &str,
399 out: &mut Vec<std::net::IpAddr>,
400) {
401 use std::net::{IpAddr, Ipv4Addr};
402 let (start_v4, end_v4) = match (start, end) {
403 (IpAddr::V4(a), IpAddr::V4(b)) => (a, b),
404 _ => {
405 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range start/end must both be IPv4");
406 return;
407 }
408 };
409 let start_u32 = u32::from(start_v4);
410 let end_u32 = u32::from(end_v4);
411 if end_u32 < start_u32 {
412 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range end {end_v4} is before start {start_v4}");
413 return;
414 }
415 let total = (end_u32 - start_u32).saturating_add(1) as usize;
416 let take = total.min(cap);
417 if total > cap {
418 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range has {total} addresses, capping at {cap}");
419 }
420 for i in 0..take as u32 {
421 out.push(IpAddr::V4(Ipv4Addr::from(start_u32 + i)));
422 }
423}
424
425fn expand_cidr(
429 net: std::net::IpAddr,
430 prefix: u32,
431 cap: usize,
432 flag_name: &str,
433 raw: &str,
434 out: &mut Vec<std::net::IpAddr>,
435) {
436 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
437 match net {
438 IpAddr::V4(ipv4) => {
439 if prefix > 32 {
440 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv4 prefix must be <= 32");
441 return;
442 }
443 let total: u64 = 1u64 << (32 - prefix);
444 let take = total.min(cap as u64) as u32;
445 if total > cap as u64 {
446 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': CIDR has {total} addresses, capping at {cap}");
447 }
448 let mask: u32 = if prefix == 0 {
449 0
450 } else {
451 !0u32 << (32 - prefix)
452 };
453 let net_u32 = u32::from(ipv4) & mask;
454 for i in 0..take {
455 out.push(IpAddr::V4(Ipv4Addr::from(net_u32.wrapping_add(i))));
456 }
457 }
458 IpAddr::V6(ipv6) => {
459 if prefix > 128 {
460 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv6 prefix must be <= 128");
461 return;
462 }
463 let mask: u128 = if prefix == 0 {
467 0
468 } else {
469 !0u128 << (128 - prefix)
470 };
471 let net_u128 = u128::from(ipv6) & mask;
472 let remaining_bits = 128 - prefix;
473 let total_capped = if remaining_bits >= 64 {
476 cap as u128
477 } else {
478 (1u128 << remaining_bits).min(cap as u128)
479 };
480 if remaining_bits < 128 && (1u128 << remaining_bits) > cap as u128 {
481 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': IPv6 CIDR exceeds {cap} addresses, capping");
482 }
483 for i in 0..total_capped {
484 out.push(IpAddr::V6(Ipv6Addr::from(net_u128.wrapping_add(i))));
485 }
486 }
487 }
488}
489
490impl BenchCommand {
491 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
493 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
494
495 if !self.spec.is_empty() {
497 let specs = load_specs_from_files(self.spec.clone())
498 .await
499 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
500 all_specs.extend(specs);
501 }
502
503 if let Some(spec_dir) = &self.spec_dir {
505 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
506 BenchError::Other(format!("Failed to load specs from directory: {}", e))
507 })?;
508 all_specs.extend(dir_specs);
509 }
510
511 if all_specs.is_empty() {
512 return Err(BenchError::Other(
513 "No spec files provided. Use --spec or --spec-dir.".to_string(),
514 ));
515 }
516
517 if all_specs.len() == 1 {
519 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
521 }
522
523 let conflict_strategy = match self.merge_conflicts.as_str() {
525 "first" => ConflictStrategy::First,
526 "last" => ConflictStrategy::Last,
527 _ => ConflictStrategy::Error,
528 };
529
530 merge_specs(all_specs, conflict_strategy)
531 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
532 }
533
534 fn get_spec_display_name(&self) -> String {
536 if self.spec.len() == 1 {
537 self.spec[0].to_string_lossy().to_string()
538 } else if !self.spec.is_empty() {
539 format!("{} spec files", self.spec.len())
540 } else if let Some(dir) = &self.spec_dir {
541 format!("specs from {}", dir.display())
542 } else {
543 "no specs".to_string()
544 }
545 }
546
547 fn advise_capacity(&self) {
554 let target_count = self
555 .targets_file
556 .as_ref()
557 .and_then(|p| std::fs::read_to_string(p).ok())
558 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
559 .and_then(|v| v.as_array().map(|a| a.len()))
560 .unwrap_or(1);
561 let vus = self.vus.max(1);
562 let rps_total = self.target_rps.unwrap_or(0) as usize * target_count.max(1);
563 let load_product = target_count * vus as usize;
567 if load_product >= 150 {
568 let est_ram_gb =
569 (vus as usize * 50) / 1024 + (target_count * 10 * 2) / 1024 + target_count / 2;
570 let est_cores = ((vus as usize) / 50).max(2);
571 TerminalReporter::print_warning(&format!(
572 "Capacity advisory: targets={target_count}, VUs={vus}, RPS-total≈{rps_total}. \
573 Single-client estimate: ~{est_cores} CPU cores, ~{est_ram_gb} GB RAM. \
574 If your machine is below that, expect OOM hangs partway through the run. \
575 See https://docs.mockforge.dev/reference/bench-capacity-sizing.html \
576 for the sizing table and sharding guide."
577 ));
578 }
579 }
580
581 pub async fn execute(&self) -> Result<()> {
583 if self.conformance_self_test && self.use_k6 {
590 TerminalReporter::print_warning(
591 "--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.",
592 );
593 }
594
595 self.advise_capacity();
601
602 if let Some(targets_file) = &self.targets_file {
604 if self.conformance && self.conformance_self_test {
613 return self.execute_multi_target_self_test(targets_file).await;
614 }
615 if self.conformance {
616 return self.execute_multi_target_conformance(targets_file).await;
617 }
618 return self.execute_multi_target(targets_file).await;
619 }
620
621 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
623 return self.execute_sequential_specs().await;
624 }
625
626 TerminalReporter::print_header(
629 &self.get_spec_display_name(),
630 &self.target,
631 0, &self.scenario,
633 Self::parse_duration(&self.duration)?,
634 );
635
636 if !K6Executor::is_k6_installed() {
638 TerminalReporter::print_error("k6 is not installed");
639 TerminalReporter::print_warning(
640 "Install k6 from: https://k6.io/docs/get-started/installation/",
641 );
642 return Err(BenchError::K6NotFound);
643 }
644
645 if self.conformance {
647 return self.execute_conformance_test().await;
648 }
649
650 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
652 let merged_spec = self.load_and_merge_specs().await?;
653 let parser = SpecParser::from_spec(merged_spec);
654 if self.spec.len() > 1 || self.spec_dir.is_some() {
655 TerminalReporter::print_success(&format!(
656 "Loaded and merged {} specification(s)",
657 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
658 ));
659 } else {
660 TerminalReporter::print_success("Specification loaded");
661 }
662
663 let mock_config = self.build_mock_config().await;
665 if mock_config.is_mock_server {
666 TerminalReporter::print_progress("Mock server integration enabled");
667 }
668
669 if self.crud_flow {
671 return self.execute_crud_flow(&parser).await;
672 }
673
674 if self.owasp_api_top10 {
676 return self.execute_owasp_test(&parser).await;
677 }
678
679 TerminalReporter::print_progress("Extracting API operations...");
681 let mut operations = if let Some(filter) = &self.operations {
682 parser.filter_operations(filter)?
683 } else {
684 parser.get_operations()
685 };
686
687 if let Some(exclude) = &self.exclude_operations {
689 let before_count = operations.len();
690 operations = parser.exclude_operations(operations, exclude)?;
691 let excluded_count = before_count - operations.len();
692 if excluded_count > 0 {
693 TerminalReporter::print_progress(&format!(
694 "Excluded {} operations matching '{}'",
695 excluded_count, exclude
696 ));
697 }
698 }
699
700 if operations.is_empty() {
701 return Err(BenchError::Other("No operations found in spec".to_string()));
702 }
703
704 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
705
706 let param_overrides = if let Some(params_file) = &self.params_file {
708 TerminalReporter::print_progress("Loading parameter overrides...");
709 let overrides = ParameterOverrides::from_file(params_file)?;
710 TerminalReporter::print_success(&format!(
711 "Loaded parameter overrides ({} operation-specific, {} defaults)",
712 overrides.operations.len(),
713 if overrides.defaults.is_empty() { 0 } else { 1 }
714 ));
715 Some(overrides)
716 } else {
717 None
718 };
719
720 TerminalReporter::print_progress("Generating request templates...");
722 let templates: Vec<_> = operations
723 .iter()
724 .map(|op| {
725 let op_overrides = param_overrides.as_ref().map(|po| {
726 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
727 });
728 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
729 })
730 .collect::<Result<Vec<_>>>()?;
731 TerminalReporter::print_success("Request templates generated");
732
733 let custom_headers = self.parse_headers()?;
735
736 let base_path = self.resolve_base_path(&parser);
738 if let Some(ref bp) = base_path {
739 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
740 }
741
742 TerminalReporter::print_progress("Generating k6 load test script...");
744 let scenario =
745 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
746
747 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
748
749 let num_ops = operations.len() as u32;
767 if let Some(rps) = self.target_rps {
768 let probe =
769 crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
770
771 let (required_vus, basis) = match probe {
772 Some(p) => (
773 p.required_vus(rps, num_ops),
774 format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
775 ),
776 None => {
777 let fallback = (rps as u64)
779 .saturating_mul(num_ops.max(1) as u64)
780 .div_ceil(10)
781 .min(u32::MAX as u64) as u32;
782 (fallback, "~100ms (default — probe failed)".to_string())
783 }
784 };
785
786 if self.vus < required_vus {
787 const VU_RECOMMENDATION_CAP: u32 = 1000;
793 let recommendation = required_vus.max(self.vus + 1);
794 if recommendation > VU_RECOMMENDATION_CAP {
795 TerminalReporter::print_warning(&format!(
796 "Workload is very large: --rps {} × {} ops/iteration × {} \
797 baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
798 practical to drive. Two ways to fix:\n 1. Reduce \
799 operations per iteration with `--operations 'pattern,…'` \
800 (or `--exclude-operations`) to focus the bench on a \
801 representative subset.\n 2. Drop `--rps` and use \
802 `--vus {}` alone — closed-model load runs as fast as \
803 the VU pool allows, bounded by latency, with no per-\
804 iteration deadline. Expect 1-iteration coverage of ~{} \
805 operations in {}s.",
806 rps,
807 num_ops,
808 basis,
809 recommendation,
810 self.vus.max(5),
811 num_ops,
812 Self::parse_duration(&self.duration).unwrap_or(0),
813 ));
814 } else {
815 TerminalReporter::print_warning(&format!(
816 "--vus {} may be insufficient for --rps {} × {} ops/iteration \
817 (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
818 and each runs every operation in the spec — required ≈ rps × ops × \
819 latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
820 warnings.",
821 self.vus, rps, num_ops, basis, recommendation,
822 ));
823 }
824 } else if probe.is_some() {
825 TerminalReporter::print_progress(&format!(
826 "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
827 is sufficient for --rps {}",
828 basis, num_ops, self.vus, rps,
829 ));
830 }
831 }
832
833 let k6_config = K6Config {
834 target_url: self.target.clone(),
835 base_path,
836 scenario,
837 duration_secs: Self::parse_duration(&self.duration)?,
838 max_vus: self.vus,
839 threshold_percentile: self.threshold_percentile.clone(),
840 threshold_ms: self.threshold_ms,
841 max_error_rate: self.max_error_rate,
842 auth_header: self.auth.clone(),
843 custom_headers,
844 skip_tls_verify: self.skip_tls_verify,
845 security_testing_enabled,
846 chunked_request_bodies: self.chunked_request_bodies,
847 target_rps: self.target_rps,
848 no_keep_alive: self.no_keep_alive,
849 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
855 .into_iter()
856 .map(|ip| ip.to_string())
857 .collect(),
858 geo_source_headers: if self.geo_source_headers.is_empty()
859 && !self.geo_source_ips.is_empty()
860 {
861 crate::conformance::self_test::default_geo_source_headers()
862 } else {
863 self.geo_source_headers.clone()
864 },
865 };
866
867 let generator = K6ScriptGenerator::new(k6_config, templates)
868 .with_abort_valve(self.abort_on_error, self.abort_on_error_rate);
869 let mut script = generator.generate()?;
870 TerminalReporter::print_success("k6 script generated");
871
872 let has_advanced_features = self.data_file.is_some()
874 || self.error_rate.is_some()
875 || self.security_test
876 || self.parallel_create.is_some()
877 || self.wafbench_dir.is_some();
878
879 if has_advanced_features {
881 script = self.generate_enhanced_script(&script)?;
882 }
883
884 if mock_config.is_mock_server {
886 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
887 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
888 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
889
890 if let Some(import_end) = script.find("export const options") {
892 script.insert_str(
893 import_end,
894 &format!(
895 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
896 helper_code, setup_code, teardown_code
897 ),
898 );
899 }
900 }
901
902 TerminalReporter::print_progress("Validating k6 script...");
904 let validation_errors = K6ScriptGenerator::validate_script(&script);
905 if !validation_errors.is_empty() {
906 TerminalReporter::print_error("Script validation failed");
907 for error in &validation_errors {
908 eprintln!(" {}", error);
909 }
910 return Err(BenchError::Other(format!(
911 "Generated k6 script has {} validation error(s). Please check the output above.",
912 validation_errors.len()
913 )));
914 }
915 TerminalReporter::print_success("Script validation passed");
916
917 let script_path = if let Some(output) = &self.script_output {
919 output.clone()
920 } else {
921 self.output.join("k6-script.js")
922 };
923
924 if let Some(parent) = script_path.parent() {
925 std::fs::create_dir_all(parent)?;
926 }
927 std::fs::write(&script_path, &script)?;
928 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
929
930 if self.generate_only {
932 println!("\nScript generated successfully. Run it with:");
933 println!(" k6 run {}", script_path.display());
934 return Ok(());
935 }
936
937 TerminalReporter::print_progress("Executing load test...");
939 let executor = K6Executor::new()?
943 .with_local_ips(self.source_ips.join(","))
944 .with_dns_policy(self.dns_policy.clone().unwrap_or_default())
945 .with_discard_response_bodies(self.discard_response_bodies);
946
947 std::fs::create_dir_all(&self.output)?;
948
949 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
950
951 let duration_secs = Self::parse_duration(&self.duration)?;
953 TerminalReporter::print_summary_full(
954 &results,
955 duration_secs,
956 self.no_keep_alive,
957 Some(num_ops),
958 );
959
960 println!("\nResults saved to: {}", self.output.display());
961
962 Ok(())
963 }
964
965 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
967 TerminalReporter::print_progress("Parsing targets file...");
968 let targets = parse_targets_file(targets_file)?;
969 let num_targets = targets.len();
970 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
971
972 if targets.is_empty() {
973 return Err(BenchError::Other("No targets found in file".to_string()));
974 }
975
976 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
978 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
982 &self.get_spec_display_name(),
983 &format!("{} targets", num_targets),
984 0,
985 &self.scenario,
986 Self::parse_duration(&self.duration)?,
987 );
988
989 let executor = ParallelExecutor::new(
991 BenchCommand {
992 spec: self.spec.clone(),
994 spec_dir: self.spec_dir.clone(),
995 merge_conflicts: self.merge_conflicts.clone(),
996 spec_mode: self.spec_mode.clone(),
997 dependency_config: self.dependency_config.clone(),
998 target: self.target.clone(), base_path: self.base_path.clone(),
1000 duration: self.duration.clone(),
1001 vus: self.vus,
1002 target_rps: self.target_rps,
1003 no_keep_alive: self.no_keep_alive,
1004 scenario: self.scenario.clone(),
1005 operations: self.operations.clone(),
1006 exclude_operations: self.exclude_operations.clone(),
1007 auth: self.auth.clone(),
1008 headers: self.headers.clone(),
1009 output: self.output.clone(),
1010 generate_only: self.generate_only,
1011 script_output: self.script_output.clone(),
1012 threshold_percentile: self.threshold_percentile.clone(),
1013 threshold_ms: self.threshold_ms,
1014 max_error_rate: self.max_error_rate,
1015 abort_on_error: self.abort_on_error,
1016 abort_on_error_rate: self.abort_on_error_rate,
1017 verbose: self.verbose,
1018 skip_tls_verify: self.skip_tls_verify,
1019 chunked_request_bodies: self.chunked_request_bodies,
1020 targets_file: None,
1021 max_concurrency: None,
1022 results_format: self.results_format.clone(),
1023 params_file: self.params_file.clone(),
1024 crud_flow: self.crud_flow,
1025 flow_config: self.flow_config.clone(),
1026 extract_fields: self.extract_fields.clone(),
1027 parallel_create: self.parallel_create,
1028 data_file: self.data_file.clone(),
1029 data_distribution: self.data_distribution.clone(),
1030 data_mappings: self.data_mappings.clone(),
1031 per_uri_control: self.per_uri_control,
1032 error_rate: self.error_rate,
1033 error_types: self.error_types.clone(),
1034 security_test: self.security_test,
1035 security_payloads: self.security_payloads.clone(),
1036 security_categories: self.security_categories.clone(),
1037 security_target_fields: self.security_target_fields.clone(),
1038 wafbench_dir: self.wafbench_dir.clone(),
1039 wafbench_cycle_all: self.wafbench_cycle_all,
1040 owasp_api_top10: self.owasp_api_top10,
1041 owasp_categories: self.owasp_categories.clone(),
1042 owasp_auth_header: self.owasp_auth_header.clone(),
1043 owasp_auth_token: self.owasp_auth_token.clone(),
1044 owasp_admin_paths: self.owasp_admin_paths.clone(),
1045 owasp_id_fields: self.owasp_id_fields.clone(),
1046 owasp_report: self.owasp_report.clone(),
1047 owasp_report_format: self.owasp_report_format.clone(),
1048 owasp_iterations: self.owasp_iterations,
1049 conformance: false,
1050 conformance_api_key: self.conformance_api_key.clone(),
1066 conformance_basic_auth: self.conformance_basic_auth.clone(),
1067 conformance_report: PathBuf::from("conformance-report.json"),
1068 conformance_categories: None,
1069 conformance_report_format: "json".to_string(),
1070 conformance_headers: self.conformance_headers.clone(),
1074 conformance_all_operations: false,
1075 conformance_custom: None,
1076 conformance_delay_ms: 0,
1077 use_k6: false,
1078 conformance_custom_filter: None,
1079 export_requests: false,
1080 validate_requests: false,
1081 conformance_self_test: false,
1082 conformance_self_test_capture: false,
1083 conformance_self_test_iterations: 1,
1084 conformance_self_test_duration: None,
1085 validate_response_schemas: false,
1086 source_ips: self.source_ips.clone(),
1091 geo_source_ips: self.geo_source_ips.clone(),
1092 geo_source_headers: self.geo_source_headers.clone(),
1093 report_missed_cap: None,
1094 discard_response_bodies: self.discard_response_bodies,
1098 dns_policy: self.dns_policy.clone(),
1101 },
1102 targets,
1103 max_concurrency,
1104 );
1105
1106 let start_time = std::time::Instant::now();
1108 let aggregated_results = executor.execute_all().await?;
1109 let elapsed = start_time.elapsed();
1110
1111 self.report_multi_target_results(&aggregated_results, elapsed)?;
1113
1114 Ok(())
1115 }
1116
1117 fn report_multi_target_results(
1119 &self,
1120 results: &AggregatedResults,
1121 elapsed: std::time::Duration,
1122 ) -> Result<()> {
1123 TerminalReporter::print_multi_target_summary(results);
1125
1126 let total_secs = elapsed.as_secs();
1128 let hours = total_secs / 3600;
1129 let minutes = (total_secs % 3600) / 60;
1130 let seconds = total_secs % 60;
1131 if hours > 0 {
1132 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
1133 } else if minutes > 0 {
1134 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
1135 } else {
1136 println!("\n Total Elapsed Time: {}s", seconds);
1137 }
1138
1139 if self.results_format == "aggregated" || self.results_format == "both" {
1141 let summary_path = self.output.join("aggregated_summary.json");
1142 let summary_json = serde_json::json!({
1143 "total_elapsed_seconds": elapsed.as_secs(),
1144 "total_targets": results.total_targets,
1145 "successful_targets": results.successful_targets,
1146 "failed_targets": results.failed_targets,
1147 "aggregated_metrics": {
1148 "total_requests": results.aggregated_metrics.total_requests,
1149 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1150 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1151 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1152 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1153 "error_rate": results.aggregated_metrics.error_rate,
1154 "total_rps": results.aggregated_metrics.total_rps,
1155 "avg_rps": results.aggregated_metrics.avg_rps,
1156 "total_vus_max": results.aggregated_metrics.total_vus_max,
1157 },
1158 "target_results": results.target_results.iter().map(|r| {
1159 serde_json::json!({
1160 "target_url": r.target_url,
1161 "target_index": r.target_index,
1162 "success": r.success,
1163 "error": r.error,
1164 "total_requests": r.results.total_requests,
1165 "failed_requests": r.results.failed_requests,
1166 "avg_duration_ms": r.results.avg_duration_ms,
1167 "min_duration_ms": r.results.min_duration_ms,
1168 "med_duration_ms": r.results.med_duration_ms,
1169 "p90_duration_ms": r.results.p90_duration_ms,
1170 "p95_duration_ms": r.results.p95_duration_ms,
1171 "p99_duration_ms": r.results.p99_duration_ms,
1172 "max_duration_ms": r.results.max_duration_ms,
1173 "rps": r.results.rps,
1174 "vus_max": r.results.vus_max,
1175 "output_dir": r.output_dir.to_string_lossy(),
1176 })
1177 }).collect::<Vec<_>>(),
1178 });
1179
1180 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1181 TerminalReporter::print_success(&format!(
1182 "Aggregated summary saved to: {}",
1183 summary_path.display()
1184 ));
1185 }
1186
1187 let csv_path = self.output.join("all_targets.csv");
1189 let mut csv = String::from(
1190 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1191 );
1192 for r in &results.target_results {
1193 csv.push_str(&format!(
1194 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1195 r.target_url,
1196 r.success,
1197 r.results.total_requests,
1198 r.results.failed_requests,
1199 r.results.rps,
1200 r.results.vus_max,
1201 r.results.min_duration_ms,
1202 r.results.avg_duration_ms,
1203 r.results.med_duration_ms,
1204 r.results.p90_duration_ms,
1205 r.results.p95_duration_ms,
1206 r.results.p99_duration_ms,
1207 r.results.max_duration_ms,
1208 r.error.as_deref().unwrap_or(""),
1209 ));
1210 }
1211 let _ = std::fs::write(&csv_path, &csv);
1212
1213 println!("\nResults saved to: {}", self.output.display());
1214 println!(" - Per-target results: {}", self.output.join("target_*").display());
1215 println!(" - All targets CSV: {}", csv_path.display());
1216 if self.results_format == "aggregated" || self.results_format == "both" {
1217 println!(
1218 " - Aggregated summary: {}",
1219 self.output.join("aggregated_summary.json").display()
1220 );
1221 }
1222
1223 Ok(())
1224 }
1225
1226 pub fn parse_duration(duration: &str) -> Result<u64> {
1228 let duration = duration.trim();
1229
1230 if let Some(secs) = duration.strip_suffix('s') {
1231 secs.parse::<u64>()
1232 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1233 } else if let Some(mins) = duration.strip_suffix('m') {
1234 mins.parse::<u64>()
1235 .map(|m| m * 60)
1236 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1237 } else if let Some(hours) = duration.strip_suffix('h') {
1238 hours
1239 .parse::<u64>()
1240 .map(|h| h * 3600)
1241 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1242 } else {
1243 duration
1245 .parse::<u64>()
1246 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1247 }
1248 }
1249
1250 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1252 let mut headers = parse_header_string(&self.headers)?;
1253
1254 let already_has = |hs: &HashMap<String, String>, name: &str| -> bool {
1265 hs.keys().any(|k| k.eq_ignore_ascii_case(name))
1266 };
1267
1268 if !already_has(&headers, "Authorization") {
1269 if let Some(b) = self.conformance_basic_auth.as_ref().filter(|s| !s.is_empty()) {
1270 use base64::Engine as _;
1271 let encoded = base64::engine::general_purpose::STANDARD.encode(b.as_bytes());
1272 headers.insert("Authorization".to_string(), format!("Basic {}", encoded));
1273 }
1274 }
1275
1276 for line in &self.conformance_headers {
1282 let Some((name, value)) = line.split_once(':') else {
1283 continue;
1284 };
1285 let name = name.trim();
1286 let value = value.trim();
1287 if name.is_empty() || already_has(&headers, name) {
1288 continue;
1289 }
1290 headers.insert(name.to_string(), value.to_string());
1291 }
1292
1293 if !self.conformance && self.conformance_api_key.is_some() {
1299 TerminalReporter::print_warning(
1300 "--conformance-api-key only fires under --conformance. For plain bench use --header 'X-API-Key: ...'.",
1301 );
1302 }
1303
1304 Ok(headers)
1305 }
1306
1307 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1308 let extracted_path = output_dir.join("extracted_values.json");
1309 if !extracted_path.exists() {
1310 return Ok(ExtractedValues::new());
1311 }
1312
1313 let content = std::fs::read_to_string(&extracted_path)
1314 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1315 let parsed: serde_json::Value = serde_json::from_str(&content)
1316 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1317
1318 let mut extracted = ExtractedValues::new();
1319 if let Some(values) = parsed.as_object() {
1320 for (key, value) in values {
1321 extracted.set(key.clone(), value.clone());
1322 }
1323 }
1324
1325 Ok(extracted)
1326 }
1327
1328 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1337 if let Some(cli_base_path) = &self.base_path {
1339 if cli_base_path.is_empty() {
1340 return None;
1342 }
1343 return Some(cli_base_path.clone());
1344 }
1345
1346 parser.get_base_path()
1348 }
1349
1350 async fn build_mock_config(&self) -> MockIntegrationConfig {
1352 if MockServerDetector::looks_like_mock_server(&self.target) {
1354 if let Ok(info) = MockServerDetector::detect(&self.target).await {
1356 if info.is_mockforge {
1357 TerminalReporter::print_success(&format!(
1358 "Detected MockForge server (version: {})",
1359 info.version.as_deref().unwrap_or("unknown")
1360 ));
1361 return MockIntegrationConfig::mock_server();
1362 }
1363 }
1364 }
1365 MockIntegrationConfig::real_api()
1366 }
1367
1368 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1370 if !self.crud_flow {
1371 return None;
1372 }
1373
1374 if let Some(config_path) = &self.flow_config {
1376 match CrudFlowConfig::from_file(config_path) {
1377 Ok(config) => return Some(config),
1378 Err(e) => {
1379 TerminalReporter::print_warning(&format!(
1380 "Failed to load flow config: {}. Using auto-detection.",
1381 e
1382 ));
1383 }
1384 }
1385 }
1386
1387 let extract_fields = self
1389 .extract_fields
1390 .as_ref()
1391 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1392 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1393
1394 Some(CrudFlowConfig {
1395 flows: Vec::new(), default_extract_fields: extract_fields,
1397 })
1398 }
1399
1400 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1402 let data_file = self.data_file.as_ref()?;
1403
1404 let distribution = DataDistribution::from_str(&self.data_distribution)
1405 .unwrap_or(DataDistribution::UniquePerVu);
1406
1407 let mappings = self
1408 .data_mappings
1409 .as_ref()
1410 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1411 .unwrap_or_default();
1412
1413 Some(DataDrivenConfig {
1414 file_path: data_file.to_string_lossy().to_string(),
1415 distribution,
1416 mappings,
1417 csv_has_header: true,
1418 per_uri_control: self.per_uri_control,
1419 per_uri_columns: crate::data_driven::PerUriColumns::default(),
1420 })
1421 }
1422
1423 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1425 let error_rate = self.error_rate?;
1426
1427 let error_types = self
1428 .error_types
1429 .as_ref()
1430 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1431 .unwrap_or_default();
1432
1433 Some(InvalidDataConfig {
1434 error_rate,
1435 error_types,
1436 target_fields: Vec::new(),
1437 })
1438 }
1439
1440 fn build_security_config(&self) -> Option<SecurityTestConfig> {
1442 if !self.security_test {
1443 return None;
1444 }
1445
1446 let categories = self
1447 .security_categories
1448 .as_ref()
1449 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1450 .unwrap_or_else(|| {
1451 let mut default = HashSet::new();
1452 default.insert(SecurityCategory::SqlInjection);
1453 default.insert(SecurityCategory::Xss);
1454 default
1455 });
1456
1457 let target_fields = self
1458 .security_target_fields
1459 .as_ref()
1460 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1461 .unwrap_or_default();
1462
1463 let custom_payloads_file =
1464 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1465
1466 Some(SecurityTestConfig {
1467 enabled: true,
1468 categories,
1469 target_fields,
1470 custom_payloads_file,
1471 include_high_risk: false,
1472 })
1473 }
1474
1475 fn build_parallel_config(&self) -> Option<ParallelConfig> {
1477 let count = self.parallel_create?;
1478
1479 Some(ParallelConfig::new(count))
1480 }
1481
1482 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1484 let Some(ref wafbench_dir) = self.wafbench_dir else {
1485 return Vec::new();
1486 };
1487
1488 let mut loader = WafBenchLoader::new();
1489
1490 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1491 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1492 return Vec::new();
1493 }
1494
1495 let stats = loader.stats();
1496
1497 if stats.files_processed == 0 {
1498 TerminalReporter::print_warning(&format!(
1499 "No WAFBench YAML files found matching '{}'",
1500 wafbench_dir
1501 ));
1502 if !stats.parse_errors.is_empty() {
1504 TerminalReporter::print_warning("Some files were found but failed to parse:");
1505 for error in &stats.parse_errors {
1506 TerminalReporter::print_warning(&format!(" - {}", error));
1507 }
1508 }
1509 return Vec::new();
1510 }
1511
1512 TerminalReporter::print_progress(&format!(
1513 "Loaded {} WAFBench files, {} test cases, {} payloads",
1514 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1515 ));
1516
1517 for (category, count) in &stats.by_category {
1519 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1520 }
1521
1522 for error in &stats.parse_errors {
1524 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1525 }
1526
1527 loader.to_security_payloads()
1528 }
1529
1530 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1532 let mut enhanced_script = base_script.to_string();
1533 let mut additional_code = String::new();
1534
1535 if let Some(config) = self.build_data_driven_config() {
1537 TerminalReporter::print_progress("Adding data-driven testing support...");
1538 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1539 additional_code.push('\n');
1540 TerminalReporter::print_success("Data-driven testing enabled");
1541 }
1542
1543 if let Some(config) = self.build_invalid_data_config() {
1545 TerminalReporter::print_progress("Adding invalid data testing support...");
1546 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1547 additional_code.push('\n');
1548 additional_code
1549 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1550 additional_code.push('\n');
1551 additional_code
1552 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1553 additional_code.push('\n');
1554 TerminalReporter::print_success(&format!(
1555 "Invalid data testing enabled ({}% error rate)",
1556 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1557 ));
1558 }
1559
1560 let security_config = self.build_security_config();
1562 let wafbench_payloads = self.load_wafbench_payloads();
1563 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1564
1565 if security_config.is_some() || !wafbench_payloads.is_empty() {
1566 TerminalReporter::print_progress("Adding security testing support...");
1567
1568 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1570
1571 if let Some(ref config) = security_config {
1572 payload_list.extend(SecurityPayloads::get_payloads(config));
1573 }
1574
1575 if !wafbench_payloads.is_empty() {
1577 TerminalReporter::print_progress(&format!(
1578 "Loading {} WAFBench attack patterns...",
1579 wafbench_payloads.len()
1580 ));
1581 payload_list.extend(wafbench_payloads);
1582 }
1583
1584 let target_fields =
1585 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1586
1587 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1588 &payload_list,
1589 self.wafbench_cycle_all,
1590 ));
1591 additional_code.push('\n');
1592 additional_code
1593 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1594 additional_code.push('\n');
1595 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1596 additional_code.push('\n');
1597
1598 let mode = if self.wafbench_cycle_all {
1599 "cycle-all"
1600 } else {
1601 "random"
1602 };
1603 TerminalReporter::print_success(&format!(
1604 "Security testing enabled ({} payloads, {} mode)",
1605 payload_list.len(),
1606 mode
1607 ));
1608 } else if security_requested {
1609 TerminalReporter::print_warning(
1613 "Security testing was requested but no payloads were loaded. \
1614 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1615 );
1616 additional_code
1617 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1618 additional_code.push('\n');
1619 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1620 additional_code.push('\n');
1621 }
1622
1623 if let Some(config) = self.build_parallel_config() {
1625 TerminalReporter::print_progress("Adding parallel execution support...");
1626 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1627 additional_code.push('\n');
1628 TerminalReporter::print_success(&format!(
1629 "Parallel execution enabled (count: {})",
1630 config.count
1631 ));
1632 }
1633
1634 if !additional_code.is_empty() {
1636 if let Some(import_end) = enhanced_script.find("export const options") {
1638 enhanced_script.insert_str(
1639 import_end,
1640 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1641 );
1642 }
1643 }
1644
1645 Ok(enhanced_script)
1646 }
1647
1648 async fn execute_sequential_specs(&self) -> Result<()> {
1650 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1651
1652 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1654
1655 if !self.spec.is_empty() {
1656 let specs = load_specs_from_files(self.spec.clone())
1657 .await
1658 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1659 all_specs.extend(specs);
1660 }
1661
1662 if let Some(spec_dir) = &self.spec_dir {
1663 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1664 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1665 })?;
1666 all_specs.extend(dir_specs);
1667 }
1668
1669 if all_specs.is_empty() {
1670 return Err(BenchError::Other(
1671 "No spec files found for sequential execution".to_string(),
1672 ));
1673 }
1674
1675 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1676
1677 let execution_order = if let Some(config_path) = &self.dependency_config {
1679 TerminalReporter::print_progress("Loading dependency configuration...");
1680 let config = SpecDependencyConfig::from_file(config_path)?;
1681
1682 if !config.disable_auto_detect && config.execution_order.is_empty() {
1683 self.detect_and_sort_specs(&all_specs)?
1685 } else {
1686 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1688 }
1689 } else {
1690 self.detect_and_sort_specs(&all_specs)?
1692 };
1693
1694 TerminalReporter::print_success(&format!(
1695 "Execution order: {}",
1696 execution_order
1697 .iter()
1698 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1699 .collect::<Vec<_>>()
1700 .join(" → ")
1701 ));
1702
1703 let mut extracted_values = ExtractedValues::new();
1705 let total_specs = execution_order.len();
1706
1707 for (index, spec_path) in execution_order.iter().enumerate() {
1708 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1709
1710 TerminalReporter::print_progress(&format!(
1711 "[{}/{}] Executing spec: {}",
1712 index + 1,
1713 total_specs,
1714 spec_name
1715 ));
1716
1717 let spec = all_specs
1719 .iter()
1720 .find(|(p, _)| {
1721 p == spec_path
1722 || p.file_name() == spec_path.file_name()
1723 || p.file_name() == Some(spec_path.as_os_str())
1724 })
1725 .map(|(_, s)| s.clone())
1726 .ok_or_else(|| {
1727 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1728 })?;
1729
1730 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1732
1733 extracted_values.merge(&new_values);
1735
1736 TerminalReporter::print_success(&format!(
1737 "[{}/{}] Completed: {} (extracted {} values)",
1738 index + 1,
1739 total_specs,
1740 spec_name,
1741 new_values.values.len()
1742 ));
1743 }
1744
1745 TerminalReporter::print_success(&format!(
1746 "Sequential execution complete: {} specs executed",
1747 total_specs
1748 ));
1749
1750 Ok(())
1751 }
1752
1753 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1755 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1756
1757 let mut detector = DependencyDetector::new();
1758 let dependencies = detector.detect_dependencies(specs);
1759
1760 if dependencies.is_empty() {
1761 TerminalReporter::print_progress("No dependencies detected, using file order");
1762 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1763 }
1764
1765 TerminalReporter::print_progress(&format!(
1766 "Detected {} cross-spec dependencies",
1767 dependencies.len()
1768 ));
1769
1770 for dep in &dependencies {
1771 TerminalReporter::print_progress(&format!(
1772 " {} → {} (via field '{}')",
1773 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1774 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1775 dep.field_name
1776 ));
1777 }
1778
1779 topological_sort(specs, &dependencies)
1780 }
1781
1782 async fn execute_single_spec(
1784 &self,
1785 spec: &OpenApiSpec,
1786 spec_name: &str,
1787 _external_values: &ExtractedValues,
1788 ) -> Result<ExtractedValues> {
1789 let parser = SpecParser::from_spec(spec.clone());
1790
1791 if self.crud_flow {
1793 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1795 } else {
1796 self.execute_standard_spec(&parser, spec_name).await?;
1798 Ok(ExtractedValues::new())
1799 }
1800 }
1801
1802 async fn execute_crud_flow_with_extraction(
1804 &self,
1805 parser: &SpecParser,
1806 spec_name: &str,
1807 ) -> Result<ExtractedValues> {
1808 let operations = parser.get_operations();
1809 let flows = CrudFlowDetector::detect_flows(&operations);
1810
1811 if flows.is_empty() {
1812 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1813 return Ok(ExtractedValues::new());
1814 }
1815
1816 TerminalReporter::print_progress(&format!(
1817 " {} CRUD flow(s) in {}",
1818 flows.len(),
1819 spec_name
1820 ));
1821
1822 let mut handlebars = handlebars::Handlebars::new();
1824 handlebars.register_helper(
1826 "json",
1827 Box::new(
1828 |h: &handlebars::Helper,
1829 _: &handlebars::Handlebars,
1830 _: &handlebars::Context,
1831 _: &mut handlebars::RenderContext,
1832 out: &mut dyn handlebars::Output|
1833 -> handlebars::HelperResult {
1834 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1835 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1836 Ok(())
1837 },
1838 ),
1839 );
1840 let template = include_str!("templates/k6_crud_flow.hbs");
1841 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1842
1843 let custom_headers = self.parse_headers()?;
1844 let config = self.build_crud_flow_config().unwrap_or_default();
1845
1846 let param_overrides = if let Some(params_file) = &self.params_file {
1848 let overrides = ParameterOverrides::from_file(params_file)?;
1849 Some(overrides)
1850 } else {
1851 None
1852 };
1853
1854 let duration_secs = Self::parse_duration(&self.duration)?;
1856 let scenario =
1857 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1858 let stages = scenario.generate_stages(duration_secs, self.vus);
1859
1860 let api_base_path = self.resolve_base_path(parser);
1862
1863 let mut all_headers = custom_headers.clone();
1865 if let Some(auth) = &self.auth {
1866 all_headers.insert("Authorization".to_string(), auth.clone());
1867 }
1868 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1869
1870 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1872
1873 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1874 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1878 serde_json::json!({
1879 "name": sanitized_name.clone(),
1880 "display_name": f.name,
1881 "base_path": f.base_path,
1882 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1883 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1885 let method_raw = if !parts.is_empty() {
1886 parts[0].to_uppercase()
1887 } else {
1888 "GET".to_string()
1889 };
1890 let method = if !parts.is_empty() {
1891 let m = parts[0].to_lowercase();
1892 if m == "delete" { "del".to_string() } else { m }
1894 } else {
1895 "get".to_string()
1896 };
1897 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1898 let path = if let Some(ref bp) = api_base_path {
1900 format!("{}{}", bp, raw_path)
1901 } else {
1902 raw_path.to_string()
1903 };
1904 let is_get_or_head = method == "get" || method == "head";
1905 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1907
1908 let body_value = if has_body {
1910 param_overrides.as_ref()
1911 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1912 .and_then(|oo| oo.body)
1913 .unwrap_or_else(|| serde_json::json!({}))
1914 } else {
1915 serde_json::json!({})
1916 };
1917
1918 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1920
1921 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1923 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1924
1925 serde_json::json!({
1926 "operation": s.operation,
1927 "method": method,
1928 "path": path,
1929 "extract": s.extract,
1930 "use_values": s.use_values,
1931 "use_body": s.use_body,
1932 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1933 "inject_attacks": s.inject_attacks,
1934 "attack_types": s.attack_types,
1935 "description": s.description,
1936 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1937 "is_get_or_head": is_get_or_head,
1938 "has_body": has_body,
1939 "body": processed_body.value,
1940 "body_is_dynamic": body_is_dynamic,
1941 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1942 })
1943 }).collect::<Vec<_>>(),
1944 })
1945 }).collect();
1946
1947 for flow_data in &flows_data {
1949 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1950 for step in steps {
1951 if let Some(placeholders_arr) =
1952 step.get("_placeholders").and_then(|p| p.as_array())
1953 {
1954 for p_str in placeholders_arr {
1955 if let Some(p_name) = p_str.as_str() {
1956 match p_name {
1957 "VU" => {
1958 all_placeholders.insert(DynamicPlaceholder::VU);
1959 }
1960 "Iteration" => {
1961 all_placeholders.insert(DynamicPlaceholder::Iteration);
1962 }
1963 "Timestamp" => {
1964 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1965 }
1966 "UUID" => {
1967 all_placeholders.insert(DynamicPlaceholder::UUID);
1968 }
1969 "Random" => {
1970 all_placeholders.insert(DynamicPlaceholder::Random);
1971 }
1972 "Counter" => {
1973 all_placeholders.insert(DynamicPlaceholder::Counter);
1974 }
1975 "Date" => {
1976 all_placeholders.insert(DynamicPlaceholder::Date);
1977 }
1978 "VuIter" => {
1979 all_placeholders.insert(DynamicPlaceholder::VuIter);
1980 }
1981 _ => {}
1982 }
1983 }
1984 }
1985 }
1986 }
1987 }
1988 }
1989
1990 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1992 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1993
1994 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1996
1997 let data = serde_json::json!({
1998 "base_url": self.target,
1999 "flows": flows_data,
2000 "extract_fields": config.default_extract_fields,
2001 "duration_secs": duration_secs,
2002 "max_vus": self.vus,
2003 "auth_header": self.auth,
2004 "custom_headers": custom_headers,
2005 "skip_tls_verify": self.skip_tls_verify,
2006 "stages": stages.iter().map(|s| serde_json::json!({
2008 "duration": s.duration,
2009 "target": s.target,
2010 })).collect::<Vec<_>>(),
2011 "threshold_percentile": self.threshold_percentile,
2012 "threshold_ms": self.threshold_ms,
2013 "max_error_rate": self.max_error_rate,
2014 "abort_on_error": self.abort_on_error,
2015 "abort_on_error_rate": self.abort_on_error_rate,
2016 "headers": headers_json,
2017 "dynamic_imports": required_imports,
2018 "dynamic_globals": required_globals,
2019 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
2020 "security_testing_enabled": security_testing_enabled,
2022 "has_custom_headers": !custom_headers.is_empty(),
2023 });
2024
2025 let mut script = handlebars
2026 .render_template(template, &data)
2027 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2028
2029 if security_testing_enabled {
2031 script = self.generate_enhanced_script(&script)?;
2032 }
2033
2034 let script_path =
2036 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
2037
2038 std::fs::create_dir_all(self.output.clone())?;
2039 std::fs::write(&script_path, &script)?;
2040
2041 if !self.generate_only {
2042 let executor = K6Executor::new()?
2043 .with_local_ips(self.source_ips.join(","))
2044 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2045 std::fs::create_dir_all(&output_dir)?;
2046
2047 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2048
2049 let extracted = Self::parse_extracted_values(&output_dir)?;
2050 TerminalReporter::print_progress(&format!(
2051 " Extracted {} value(s) from {}",
2052 extracted.values.len(),
2053 spec_name
2054 ));
2055 return Ok(extracted);
2056 }
2057
2058 Ok(ExtractedValues::new())
2059 }
2060
2061 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
2063 let mut operations = if let Some(filter) = &self.operations {
2064 parser.filter_operations(filter)?
2065 } else {
2066 parser.get_operations()
2067 };
2068
2069 if let Some(exclude) = &self.exclude_operations {
2070 operations = parser.exclude_operations(operations, exclude)?;
2071 }
2072
2073 if operations.is_empty() {
2074 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
2075 return Ok(());
2076 }
2077
2078 TerminalReporter::print_progress(&format!(
2079 " {} operations in {}",
2080 operations.len(),
2081 spec_name
2082 ));
2083
2084 let templates: Vec<_> = operations
2086 .iter()
2087 .map(RequestGenerator::generate_template)
2088 .collect::<Result<Vec<_>>>()?;
2089
2090 let custom_headers = self.parse_headers()?;
2092
2093 let base_path = self.resolve_base_path(parser);
2095
2096 let scenario =
2098 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2099
2100 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
2101
2102 let k6_config = K6Config {
2103 target_url: self.target.clone(),
2104 base_path,
2105 scenario,
2106 duration_secs: Self::parse_duration(&self.duration)?,
2107 max_vus: self.vus,
2108 threshold_percentile: self.threshold_percentile.clone(),
2109 threshold_ms: self.threshold_ms,
2110 max_error_rate: self.max_error_rate,
2111 auth_header: self.auth.clone(),
2112 custom_headers,
2113 skip_tls_verify: self.skip_tls_verify,
2114 security_testing_enabled,
2115 chunked_request_bodies: self.chunked_request_bodies,
2116 target_rps: self.target_rps,
2117 no_keep_alive: self.no_keep_alive,
2118 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
2120 .into_iter()
2121 .map(|ip| ip.to_string())
2122 .collect(),
2123 geo_source_headers: if self.geo_source_headers.is_empty()
2124 && !self.geo_source_ips.is_empty()
2125 {
2126 crate::conformance::self_test::default_geo_source_headers()
2127 } else {
2128 self.geo_source_headers.clone()
2129 },
2130 };
2131
2132 let generator = K6ScriptGenerator::new(k6_config, templates)
2133 .with_abort_valve(self.abort_on_error, self.abort_on_error_rate);
2134 let mut script = generator.generate()?;
2135
2136 let has_advanced_features = self.data_file.is_some()
2138 || self.error_rate.is_some()
2139 || self.security_test
2140 || self.parallel_create.is_some()
2141 || self.wafbench_dir.is_some();
2142
2143 if has_advanced_features {
2144 script = self.generate_enhanced_script(&script)?;
2145 }
2146
2147 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
2149
2150 std::fs::create_dir_all(self.output.clone())?;
2151 std::fs::write(&script_path, &script)?;
2152
2153 if !self.generate_only {
2154 let executor = K6Executor::new()?
2157 .with_local_ips(self.source_ips.join(","))
2158 .with_dns_policy(self.dns_policy.clone().unwrap_or_default())
2159 .with_discard_response_bodies(self.discard_response_bodies);
2160 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2161 std::fs::create_dir_all(&output_dir)?;
2162
2163 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2164 }
2165
2166 Ok(())
2167 }
2168
2169 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
2171 let config = self.build_crud_flow_config().unwrap_or_default();
2173
2174 let flows = if !config.flows.is_empty() {
2176 TerminalReporter::print_progress("Using custom flow configuration...");
2177 config.flows.clone()
2178 } else {
2179 TerminalReporter::print_progress("Detecting CRUD operations...");
2180 let operations = parser.get_operations();
2181 CrudFlowDetector::detect_flows(&operations)
2182 };
2183
2184 if flows.is_empty() {
2185 return Err(BenchError::Other(
2186 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
2187 ));
2188 }
2189
2190 if config.flows.is_empty() {
2191 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2192 } else {
2193 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2194 }
2195
2196 for flow in &flows {
2197 TerminalReporter::print_progress(&format!(
2198 " - {}: {} steps",
2199 flow.name,
2200 flow.steps.len()
2201 ));
2202 }
2203
2204 let mut handlebars = handlebars::Handlebars::new();
2206 handlebars.register_helper(
2208 "json",
2209 Box::new(
2210 |h: &handlebars::Helper,
2211 _: &handlebars::Handlebars,
2212 _: &handlebars::Context,
2213 _: &mut handlebars::RenderContext,
2214 out: &mut dyn handlebars::Output|
2215 -> handlebars::HelperResult {
2216 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2217 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2218 Ok(())
2219 },
2220 ),
2221 );
2222 let template = include_str!("templates/k6_crud_flow.hbs");
2223
2224 let custom_headers = self.parse_headers()?;
2225
2226 let param_overrides = if let Some(params_file) = &self.params_file {
2228 TerminalReporter::print_progress("Loading parameter overrides...");
2229 let overrides = ParameterOverrides::from_file(params_file)?;
2230 TerminalReporter::print_success(&format!(
2231 "Loaded parameter overrides ({} operation-specific, {} defaults)",
2232 overrides.operations.len(),
2233 if overrides.defaults.is_empty() { 0 } else { 1 }
2234 ));
2235 Some(overrides)
2236 } else {
2237 None
2238 };
2239
2240 let duration_secs = Self::parse_duration(&self.duration)?;
2242 let scenario =
2243 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2244 let stages = scenario.generate_stages(duration_secs, self.vus);
2245
2246 let api_base_path = self.resolve_base_path(parser);
2248 if let Some(ref bp) = api_base_path {
2249 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2250 }
2251
2252 let mut all_headers = custom_headers.clone();
2254 if let Some(auth) = &self.auth {
2255 all_headers.insert("Authorization".to_string(), auth.clone());
2256 }
2257 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2258
2259 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2261
2262 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2263 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2268 serde_json::json!({
2269 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
2272 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2273 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2275 let method_raw = if !parts.is_empty() {
2276 parts[0].to_uppercase()
2277 } else {
2278 "GET".to_string()
2279 };
2280 let method = if !parts.is_empty() {
2281 let m = parts[0].to_lowercase();
2282 if m == "delete" { "del".to_string() } else { m }
2284 } else {
2285 "get".to_string()
2286 };
2287 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2288 let path = if let Some(ref bp) = api_base_path {
2290 format!("{}{}", bp, raw_path)
2291 } else {
2292 raw_path.to_string()
2293 };
2294 let is_get_or_head = method == "get" || method == "head";
2295 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2297
2298 let body_value = if has_body {
2300 param_overrides.as_ref()
2301 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2302 .and_then(|oo| oo.body)
2303 .unwrap_or_else(|| serde_json::json!({}))
2304 } else {
2305 serde_json::json!({})
2306 };
2307
2308 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2310 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2315 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2316
2317 serde_json::json!({
2318 "operation": s.operation,
2319 "method": method,
2320 "path": path,
2321 "extract": s.extract,
2322 "use_values": s.use_values,
2323 "use_body": s.use_body,
2324 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2325 "inject_attacks": s.inject_attacks,
2326 "attack_types": s.attack_types,
2327 "description": s.description,
2328 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2329 "is_get_or_head": is_get_or_head,
2330 "has_body": has_body,
2331 "body": processed_body.value,
2332 "body_is_dynamic": body_is_dynamic,
2333 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2334 })
2335 }).collect::<Vec<_>>(),
2336 })
2337 }).collect();
2338
2339 for flow_data in &flows_data {
2341 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2342 for step in steps {
2343 if let Some(placeholders_arr) =
2344 step.get("_placeholders").and_then(|p| p.as_array())
2345 {
2346 for p_str in placeholders_arr {
2347 if let Some(p_name) = p_str.as_str() {
2348 match p_name {
2350 "VU" => {
2351 all_placeholders.insert(DynamicPlaceholder::VU);
2352 }
2353 "Iteration" => {
2354 all_placeholders.insert(DynamicPlaceholder::Iteration);
2355 }
2356 "Timestamp" => {
2357 all_placeholders.insert(DynamicPlaceholder::Timestamp);
2358 }
2359 "UUID" => {
2360 all_placeholders.insert(DynamicPlaceholder::UUID);
2361 }
2362 "Random" => {
2363 all_placeholders.insert(DynamicPlaceholder::Random);
2364 }
2365 "Counter" => {
2366 all_placeholders.insert(DynamicPlaceholder::Counter);
2367 }
2368 "Date" => {
2369 all_placeholders.insert(DynamicPlaceholder::Date);
2370 }
2371 "VuIter" => {
2372 all_placeholders.insert(DynamicPlaceholder::VuIter);
2373 }
2374 _ => {}
2375 }
2376 }
2377 }
2378 }
2379 }
2380 }
2381 }
2382
2383 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2385 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2386
2387 let invalid_data_config = self.build_invalid_data_config();
2389 let error_injection_enabled = invalid_data_config.is_some();
2390 let error_rate = self.error_rate.unwrap_or(0.0);
2391 let error_types: Vec<String> = invalid_data_config
2392 .as_ref()
2393 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2394 .unwrap_or_default();
2395
2396 if error_injection_enabled {
2397 TerminalReporter::print_progress(&format!(
2398 "Error injection enabled ({}% rate)",
2399 (error_rate * 100.0) as u32
2400 ));
2401 }
2402
2403 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2405
2406 let data = serde_json::json!({
2407 "base_url": self.target,
2408 "flows": flows_data,
2409 "extract_fields": config.default_extract_fields,
2410 "duration_secs": duration_secs,
2411 "max_vus": self.vus,
2412 "auth_header": self.auth,
2413 "custom_headers": custom_headers,
2414 "skip_tls_verify": self.skip_tls_verify,
2415 "stages": stages.iter().map(|s| serde_json::json!({
2417 "duration": s.duration,
2418 "target": s.target,
2419 })).collect::<Vec<_>>(),
2420 "threshold_percentile": self.threshold_percentile,
2421 "threshold_ms": self.threshold_ms,
2422 "max_error_rate": self.max_error_rate,
2423 "abort_on_error": self.abort_on_error,
2424 "abort_on_error_rate": self.abort_on_error_rate,
2425 "headers": headers_json,
2426 "dynamic_imports": required_imports,
2427 "dynamic_globals": required_globals,
2428 "extracted_values_output_path": self
2429 .output
2430 .join("crud_flow_extracted_values.json")
2431 .to_string_lossy(),
2432 "error_injection_enabled": error_injection_enabled,
2434 "error_rate": error_rate,
2435 "error_types": error_types,
2436 "security_testing_enabled": security_testing_enabled,
2438 "has_custom_headers": !custom_headers.is_empty(),
2439 });
2440
2441 let mut script = handlebars
2442 .render_template(template, &data)
2443 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2444
2445 if security_testing_enabled {
2447 script = self.generate_enhanced_script(&script)?;
2448 }
2449
2450 TerminalReporter::print_progress("Validating CRUD flow script...");
2452 let validation_errors = K6ScriptGenerator::validate_script(&script);
2453 if !validation_errors.is_empty() {
2454 TerminalReporter::print_error("CRUD flow script validation failed");
2455 for error in &validation_errors {
2456 eprintln!(" {}", error);
2457 }
2458 return Err(BenchError::Other(format!(
2459 "CRUD flow script validation failed with {} error(s)",
2460 validation_errors.len()
2461 )));
2462 }
2463
2464 TerminalReporter::print_success("CRUD flow script generated");
2465
2466 let script_path = if let Some(output) = &self.script_output {
2468 output.clone()
2469 } else {
2470 self.output.join("k6-crud-flow-script.js")
2471 };
2472
2473 if let Some(parent) = script_path.parent() {
2474 std::fs::create_dir_all(parent)?;
2475 }
2476 std::fs::write(&script_path, &script)?;
2477 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2478
2479 if self.generate_only {
2480 println!("\nScript generated successfully. Run it with:");
2481 println!(" k6 run {}", script_path.display());
2482 return Ok(());
2483 }
2484
2485 TerminalReporter::print_progress("Executing CRUD flow test...");
2487 let executor = K6Executor::new()?
2488 .with_local_ips(self.source_ips.join(","))
2489 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2490 std::fs::create_dir_all(&self.output)?;
2491
2492 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2493
2494 let duration_secs = Self::parse_duration(&self.duration)?;
2495 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2496
2497 Ok(())
2498 }
2499
2500 async fn execute_conformance_test(&self) -> Result<()> {
2502 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2503 use crate::conformance::report::ConformanceReport;
2504 use crate::conformance::spec::ConformanceFeature;
2505
2506 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2507
2508 TerminalReporter::print_progress(
2511 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2512 );
2513
2514 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2516 cats_str
2517 .split(',')
2518 .filter_map(|s| {
2519 let trimmed = s.trim();
2520 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2521 Some(canonical.to_string())
2522 } else {
2523 TerminalReporter::print_warning(&format!(
2524 "Unknown conformance category: '{}'. Valid categories: {}",
2525 trimmed,
2526 ConformanceFeature::cli_category_names()
2527 .iter()
2528 .map(|(cli, _)| *cli)
2529 .collect::<Vec<_>>()
2530 .join(", ")
2531 ));
2532 None
2533 }
2534 })
2535 .collect::<Vec<String>>()
2536 });
2537
2538 let custom_headers: Vec<(String, String)> = self
2540 .conformance_headers
2541 .iter()
2542 .filter_map(|h| {
2543 let (name, value) = h.split_once(':')?;
2544 Some((name.trim().to_string(), value.trim().to_string()))
2545 })
2546 .collect();
2547
2548 if !custom_headers.is_empty() {
2549 TerminalReporter::print_progress(&format!(
2550 "Using {} custom header(s) for authentication",
2551 custom_headers.len()
2552 ));
2553 }
2554
2555 if self.conformance_delay_ms > 0 {
2556 TerminalReporter::print_progress(&format!(
2557 "Using {}ms delay between conformance requests",
2558 self.conformance_delay_ms
2559 ));
2560 }
2561
2562 std::fs::create_dir_all(&self.output)?;
2564
2565 let config = ConformanceConfig {
2566 target_url: self.target.clone(),
2567 api_key: self.conformance_api_key.clone(),
2568 basic_auth: self.conformance_basic_auth.clone(),
2569 skip_tls_verify: self.skip_tls_verify,
2570 categories,
2571 base_path: self.base_path.clone(),
2572 custom_headers,
2573 output_dir: Some(self.output.clone()),
2574 all_operations: self.conformance_all_operations,
2575 custom_checks_file: self.conformance_custom.clone(),
2576 request_delay_ms: self.conformance_delay_ms,
2577 custom_filter: self.conformance_custom_filter.clone(),
2578 export_requests: self.export_requests,
2579 validate_requests: self.validate_requests,
2580 };
2581
2582 let mut resolved_base_path: Option<String> = None;
2590 let annotated_ops = if !self.spec.is_empty() {
2591 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2592 let parser = SpecParser::from_file(&self.spec[0]).await?;
2593 resolved_base_path = self.resolve_base_path(&parser);
2594
2595 let mut operations = if let Some(filter) = &self.operations {
2600 parser.filter_operations(filter)?
2601 } else {
2602 parser.get_operations()
2603 };
2604 if let Some(exclude) = &self.exclude_operations {
2605 let before_count = operations.len();
2606 operations = parser.exclude_operations(operations, exclude)?;
2607 let excluded_count = before_count - operations.len();
2608 if excluded_count > 0 {
2609 TerminalReporter::print_progress(&format!(
2610 "Excluded {} operations matching '{}'",
2611 excluded_count, exclude
2612 ));
2613 }
2614 }
2615
2616 let annotated =
2617 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2618 &operations,
2619 parser.spec(),
2620 );
2621 TerminalReporter::print_success(&format!(
2622 "Analyzed {} operations, found {} feature annotations",
2623 operations.len(),
2624 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2625 ));
2626 Some(annotated)
2627 } else {
2628 None
2629 };
2630
2631 if self.conformance_self_test {
2638 let Some(ops) = annotated_ops else {
2639 TerminalReporter::print_error(
2640 "--conformance-self-test requires --spec; no operations to test",
2641 );
2642 return Ok(());
2643 };
2644 let cfg = crate::conformance::self_test::SelfTestConfig {
2645 target_url: self.target.clone(),
2646 skip_tls_verify: self.skip_tls_verify,
2647 timeout: std::time::Duration::from_secs(30),
2648 extra_headers: self
2652 .conformance_headers
2653 .iter()
2654 .filter_map(|h| {
2655 let (n, v) = h.split_once(':')?;
2656 Some((n.trim().to_string(), v.trim().to_string()))
2657 })
2658 .collect(),
2659 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2660 base_path: resolved_base_path.clone(),
2664 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2668 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2669 geo_source_headers: if self.geo_source_headers.is_empty() {
2670 crate::conformance::self_test::default_geo_source_headers()
2671 } else {
2672 self.geo_source_headers.clone()
2673 },
2674 capture: if self.conformance_self_test_capture
2678 || self.validate_response_schemas
2679 || self.validate_requests
2680 {
2681 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2692 } else {
2693 None
2694 },
2695 validate_response_schemas: self.validate_response_schemas,
2696 spec_label: self.spec.first().map(|p| {
2702 p.file_name()
2703 .map(|s| s.to_string_lossy().into_owned())
2704 .unwrap_or_else(|| p.to_string_lossy().into_owned())
2705 }),
2706 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2713 current_iteration: 1,
2714 };
2715 let capture_sink = cfg.capture.clone();
2716 let network_events_sink = cfg.network_events.clone();
2717 TerminalReporter::print_progress(&format!(
2718 "Self-test mode: driving {} operations with positive + per-category negative cases",
2719 ops.len()
2720 ));
2721 let target_iterations = self.conformance_self_test_iterations.max(1);
2728 let duration_budget = self
2729 .conformance_self_test_duration
2730 .as_ref()
2731 .map(|s| Self::parse_duration(s))
2732 .transpose()?
2733 .map(std::time::Duration::from_secs);
2734 let start = std::time::Instant::now();
2735 let deadline = duration_budget.map(|d| start + d);
2744 let mut cfg = cfg;
2748 cfg.current_iteration = 1;
2749 let mut report =
2750 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2751 .await
2752 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2753 let mut iter_done: u32 = 1;
2754 loop {
2755 let by_iter = iter_done >= target_iterations;
2756 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2757 if by_iter && by_dur {
2758 break;
2759 }
2760 cfg.current_iteration = iter_done.saturating_add(1);
2761 let next = crate::conformance::self_test::run_self_test_with_deadline(
2762 &ops, &cfg, deadline,
2763 )
2764 .await
2765 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2766 report.merge_iteration(next);
2767 iter_done = iter_done.saturating_add(1);
2768 }
2769 if iter_done > 1 {
2770 TerminalReporter::print_progress(&format!(
2771 "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2772 iter_done,
2773 start.elapsed(),
2774 ));
2775 }
2776 let per_endpoint_summary: Vec<
2786 crate::conformance::per_endpoint_summary::PerEndpointSummary,
2787 >;
2788 if let Some(sink) = capture_sink {
2789 if let Ok(guard) = sink.lock() {
2790 let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2791 let mut lines = String::with_capacity(guard.len() * 256);
2792 for entry in guard.iter() {
2793 if let Ok(line) = serde_json::to_string(entry) {
2794 lines.push_str(&line);
2795 lines.push('\n');
2796 }
2797 }
2798 let _ = std::fs::write(&jsonl_path, lines);
2799 let html_path = self.output.join("conformance-self-test-requests.html");
2800 let html =
2801 crate::conformance::capture_html::render_capture_html(guard.as_slice());
2802 let _ = std::fs::write(&html_path, html);
2803
2804 per_endpoint_summary =
2808 crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2809 let summary_path = self.output.join("conformance-per-endpoint.json");
2810 if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2811 let _ = std::fs::write(&summary_path, json);
2812 TerminalReporter::print_progress(&format!(
2813 "Self-test request/response capture written to {} ({} entries) + {} + {}",
2814 jsonl_path.display(),
2815 guard.len(),
2816 html_path.display(),
2817 summary_path.display(),
2818 ));
2819 } else {
2820 TerminalReporter::print_progress(&format!(
2821 "Self-test request/response capture written to {} ({} entries) + {}",
2822 jsonl_path.display(),
2823 guard.len(),
2824 html_path.display(),
2825 ));
2826 }
2827 } else {
2828 per_endpoint_summary = Vec::new();
2829 }
2830 } else {
2831 per_endpoint_summary = Vec::new();
2832 }
2833 TerminalReporter::print_progress(&report.render_summary());
2834 if let Some(sink) = network_events_sink {
2841 if let Ok(guard) = sink.lock() {
2842 let path = self.output.join("conformance-network-events.json");
2843 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
2844 let _ = std::fs::write(&path, json);
2845 if guard.is_empty() {
2846 TerminalReporter::print_progress(
2847 "No wire-level network failures during self-test (file written empty)",
2848 );
2849 } else {
2850 TerminalReporter::print_warning(&format!(
2851 "Recorded {} wire-level network event(s) to {}",
2852 guard.len(),
2853 path.display()
2854 ));
2855 }
2856 }
2857 }
2858 }
2859 let json_path = self.output.join("conformance-self-test.json");
2863 if let Ok(json) = serde_json::to_string_pretty(&report) {
2864 let _ = std::fs::write(&json_path, json);
2865 TerminalReporter::print_progress(&format!(
2866 "Self-test report written to {}",
2867 json_path.display()
2868 ));
2869 }
2870 let issues = report.definite_issues();
2874 let issues_path = self.output.join("conformance-definite-issues.json");
2875 if let Ok(json) = serde_json::to_string_pretty(&issues) {
2876 if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
2877 TerminalReporter::print_warning(&format!(
2878 "{} definite issue(s) — see {}",
2879 issues.len(),
2880 issues_path.display()
2881 ));
2882 }
2883 }
2884 let owasp_accepted = report.owasp_accepted_probes();
2887 if !owasp_accepted.is_empty() {
2888 let owasp_path = self.output.join("conformance-owasp-accepted.json");
2889 if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
2890 if std::fs::write(&owasp_path, json).is_ok() {
2891 TerminalReporter::print_warning(&format!(
2892 "{} owasp injection probe(s) accepted by the target — see {}",
2893 owasp_accepted.len(),
2894 owasp_path.display()
2895 ));
2896 }
2897 }
2898 }
2899 if let Some(status) = report.detect_target_misconfiguration() {
2908 let hint = match status {
2909 404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2910 401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2911 _ => "",
2912 };
2913 TerminalReporter::print_warning(&format!(
2914 "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2915 ));
2916 } else if !report.all_passed() {
2917 TerminalReporter::print_warning(
2918 "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2919 );
2920 } else {
2921 TerminalReporter::print_success(
2922 "Self-test passed — all positive cases accepted and all negative cases rejected",
2923 );
2924 }
2925 let html_path = self.output.join("conformance-report.html");
2932 let audit_path = self.output.join("conformance-spec-audit.json");
2933 let audit_value = std::fs::read_to_string(&audit_path)
2934 .ok()
2935 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2936 let render_opts = crate::conformance::report_html::RenderOptions {
2941 missed_cap: match self.report_missed_cap {
2942 Some(0) => None,
2943 Some(n) => Some(n as usize),
2944 None => Some(200),
2945 },
2946 };
2947 let mut html = crate::conformance::report_html::render_html_with_options(
2948 &report,
2949 audit_value.as_ref(),
2950 &render_opts,
2951 );
2952 let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
2958 &per_endpoint_summary,
2959 );
2960 if !summary_section.is_empty() {
2961 if let Some(idx) = html.rfind("</body>") {
2962 html.insert_str(idx, &summary_section);
2963 } else {
2964 html.push_str(&summary_section);
2965 }
2966 }
2967 if std::fs::write(&html_path, html).is_ok() {
2968 TerminalReporter::print_progress(&format!(
2969 "HTML report written to {}",
2970 html_path.display()
2971 ));
2972 }
2973
2974 if self.validate_requests && !self.spec.is_empty() {
2986 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2987 &self.spec,
2988 &self.output,
2989 self.base_path.as_deref(),
2990 )
2991 .await?;
2992 if n > 0 {
2993 TerminalReporter::print_warning(&format!(
2994 "{} emitted request(s) recorded against the spec — see conformance-request-violations.json",
2995 n
2996 ));
2997 }
2998 }
2999 return Ok(());
3000 }
3001
3002 if self.validate_requests && !self.spec.is_empty() {
3004 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
3005 let violation_count = crate::conformance::request_validator::run_request_validation(
3006 &self.spec,
3007 self.conformance_custom.as_deref(),
3008 self.base_path.as_deref(),
3009 &self.output,
3010 )
3011 .await?;
3012 if violation_count > 0 {
3013 TerminalReporter::print_warning(&format!(
3014 "{} request validation violation(s) found — see conformance-request-violations.json",
3015 violation_count
3016 ));
3017 } else {
3018 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
3019 }
3020 }
3021
3022 if self.generate_only || self.use_k6 {
3024 let script = if let Some(annotated) = &annotated_ops {
3025 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3026 config,
3027 annotated.clone(),
3028 );
3029 let op_count = gen.operation_count();
3030 let (script, check_count) = gen.generate()?;
3031 TerminalReporter::print_success(&format!(
3032 "Conformance: {} operations analyzed, {} unique checks generated",
3033 op_count, check_count
3034 ));
3035 script
3036 } else {
3037 let generator = ConformanceGenerator::new(config);
3038 generator.generate()?
3039 };
3040
3041 let script_path = self.output.join("k6-conformance.js");
3042 std::fs::write(&script_path, &script).map_err(|e| {
3043 BenchError::Other(format!("Failed to write conformance script: {}", e))
3044 })?;
3045 TerminalReporter::print_success(&format!(
3046 "Conformance script generated: {}",
3047 script_path.display()
3048 ));
3049
3050 if self.generate_only {
3051 println!("\nScript generated. Run with:");
3052 println!(" k6 run {}", script_path.display());
3053 return Ok(());
3054 }
3055
3056 if !K6Executor::is_k6_installed() {
3058 TerminalReporter::print_error("k6 is not installed");
3059 TerminalReporter::print_warning(
3060 "Install k6 from: https://k6.io/docs/get-started/installation/",
3061 );
3062 return Err(BenchError::K6NotFound);
3063 }
3064
3065 TerminalReporter::print_progress("Running conformance tests via k6...");
3066 let executor = K6Executor::new()?
3067 .with_local_ips(self.source_ips.join(","))
3068 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3069 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3070
3071 let report_path = self.output.join("conformance-report.json");
3072 if report_path.exists() {
3073 let report = ConformanceReport::from_file(&report_path)?;
3074 report.print_report_with_options(self.conformance_all_operations);
3075 self.save_conformance_report(&report, &report_path)?;
3076 } else {
3077 TerminalReporter::print_warning(
3078 "Conformance report not generated (k6 handleSummary may not have run)",
3079 );
3080 }
3081
3082 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3094 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3095 &self.spec,
3096 &self.output,
3097 self.base_path.as_deref(),
3098 )
3099 .await?;
3100 if n > 0 {
3101 TerminalReporter::print_warning(&format!(
3102 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3103 n
3104 ));
3105 }
3106 }
3107
3108 return Ok(());
3109 }
3110
3111 TerminalReporter::print_progress("Running conformance tests (native executor)...");
3113
3114 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3115
3116 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3126 executor = if let Some(annotated) = &annotated_ops {
3127 executor.with_spec_driven_checks(annotated)
3128 } else if custom_only {
3129 executor
3130 } else {
3131 executor.with_reference_checks()
3132 };
3133 executor = executor.with_custom_checks()?;
3134
3135 TerminalReporter::print_success(&format!(
3136 "Executing {} conformance checks...",
3137 executor.check_count()
3138 ));
3139
3140 let report = executor.execute().await?;
3141 report.print_report_with_options(self.conformance_all_operations);
3142
3143 let failure_details = report.failure_details();
3145 if !failure_details.is_empty() {
3146 let details_path = self.output.join("conformance-failure-details.json");
3147 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3148 let _ = std::fs::write(&details_path, json);
3149 TerminalReporter::print_success(&format!(
3150 "Failure details saved to: {}",
3151 details_path.display()
3152 ));
3153 }
3154 }
3155
3156 let report_path = self.output.join("conformance-report.json");
3158 let report_json = serde_json::to_string_pretty(&report.to_json())
3159 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3160 std::fs::write(&report_path, &report_json)
3161 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3162 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3163
3164 self.save_conformance_report(&report, &report_path)?;
3165
3166 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3177 let n =
3178 crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3179 &self.spec,
3180 &self.output,
3181 self.base_path.as_deref(),
3182 )
3183 .await?;
3184 if n > 0 {
3185 TerminalReporter::print_warning(&format!(
3186 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3187 n
3188 ));
3189 }
3190 }
3191
3192 Ok(())
3193 }
3194
3195 fn save_conformance_report(
3197 &self,
3198 report: &crate::conformance::report::ConformanceReport,
3199 report_path: &Path,
3200 ) -> Result<()> {
3201 if self.conformance_report_format == "sarif" {
3202 use crate::conformance::sarif::ConformanceSarifReport;
3203 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3204 TerminalReporter::print_success(&format!(
3205 "SARIF report saved to: {}",
3206 self.conformance_report.display()
3207 ));
3208 } else if self.conformance_report != *report_path {
3209 std::fs::copy(report_path, &self.conformance_report)?;
3210 TerminalReporter::print_success(&format!(
3211 "Report saved to: {}",
3212 self.conformance_report.display()
3213 ));
3214 }
3215 Ok(())
3216 }
3217
3218 async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3230 use crate::conformance::self_test::SelfTestConfig;
3231
3232 TerminalReporter::print_progress("Multi-target conformance self-test mode");
3233 let targets = parse_targets_file(targets_file)?;
3234 if targets.is_empty() {
3235 return Err(BenchError::Other("No targets found in file".to_string()));
3236 }
3237 TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3238
3239 let annotated_ops = if !self.spec.is_empty() {
3241 let parser = SpecParser::from_file(&self.spec[0]).await?;
3242 let operations = parser.get_operations();
3243 Some(
3244 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3245 &operations,
3246 parser.spec(),
3247 ),
3248 )
3249 } else {
3250 return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3251 };
3252 let Some(ops) = annotated_ops else {
3253 unreachable!()
3254 };
3255
3256 std::fs::create_dir_all(&self.output)?;
3257 let resolved_base_path = self.base_path.clone();
3258 let target_iterations = self.conformance_self_test_iterations.max(1);
3259 let duration_budget = self
3260 .conformance_self_test_duration
3261 .as_ref()
3262 .map(|s| Self::parse_duration(s))
3263 .transpose()?
3264 .map(std::time::Duration::from_secs);
3265
3266 for (idx, target) in targets.iter().enumerate() {
3267 let target_dir = self.output.join(format!("target_{}", idx));
3268 std::fs::create_dir_all(&target_dir)?;
3269 TerminalReporter::print_progress(&format!(
3270 "[target {}/{}] {}",
3271 idx + 1,
3272 targets.len(),
3273 target.url
3274 ));
3275
3276 let merged_headers: Vec<(String, String)> = self
3277 .conformance_headers
3278 .iter()
3279 .filter_map(|h| {
3280 let (n, v) = h.split_once(':')?;
3281 Some((n.trim().to_string(), v.trim().to_string()))
3282 })
3283 .collect();
3284
3285 let cfg = SelfTestConfig {
3286 target_url: target.url.clone(),
3287 skip_tls_verify: self.skip_tls_verify,
3288 timeout: std::time::Duration::from_secs(30),
3289 extra_headers: merged_headers,
3290 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3291 base_path: resolved_base_path.clone(),
3292 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3293 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3294 geo_source_headers: if self.geo_source_headers.is_empty() {
3295 crate::conformance::self_test::default_geo_source_headers()
3296 } else {
3297 self.geo_source_headers.clone()
3298 },
3299 capture: if self.conformance_self_test_capture
3300 || self.validate_response_schemas
3301 || self.validate_requests
3302 {
3303 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3307 } else {
3308 None
3309 },
3310 validate_response_schemas: self.validate_response_schemas,
3311 spec_label: self.spec.first().map(|p| {
3312 p.file_name()
3313 .map(|s| s.to_string_lossy().into_owned())
3314 .unwrap_or_else(|| p.to_string_lossy().into_owned())
3315 }),
3316 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3317 current_iteration: 1,
3318 };
3319 let capture_sink = cfg.capture.clone();
3320 let network_events_sink = cfg.network_events.clone();
3321
3322 let start = std::time::Instant::now();
3323 let deadline = duration_budget.map(|d| start + d);
3327 let mut cfg = cfg;
3331 cfg.current_iteration = 1;
3332 let mut report =
3333 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3334 .await
3335 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3336 let mut iter_done: u32 = 1;
3337 loop {
3338 let by_iter = iter_done >= target_iterations;
3339 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3340 if by_iter && by_dur {
3341 break;
3342 }
3343 cfg.current_iteration = iter_done.saturating_add(1);
3344 let next = crate::conformance::self_test::run_self_test_with_deadline(
3345 &ops, &cfg, deadline,
3346 )
3347 .await
3348 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3349 report.merge_iteration(next);
3350 iter_done = iter_done.saturating_add(1);
3351 }
3352 if iter_done > 1 {
3353 TerminalReporter::print_progress(&format!(
3354 " ran {} iteration(s) in {:.1?}",
3355 iter_done,
3356 start.elapsed(),
3357 ));
3358 }
3359
3360 if let Some(sink) = capture_sink {
3362 if let Ok(guard) = sink.lock() {
3363 let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3364 let mut lines = String::with_capacity(guard.len() * 256);
3365 for entry in guard.iter() {
3366 if let Ok(line) = serde_json::to_string(entry) {
3367 lines.push_str(&line);
3368 lines.push('\n');
3369 }
3370 }
3371 let _ = std::fs::write(&jsonl, lines);
3372 }
3373 }
3374 if let Some(sink) = network_events_sink {
3375 if let Ok(guard) = sink.lock() {
3376 let path = target_dir.join("conformance-network-events.json");
3377 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3378 let _ = std::fs::write(&path, json);
3379 if !guard.is_empty() {
3380 TerminalReporter::print_warning(&format!(
3381 " recorded {} wire-level network event(s)",
3382 guard.len()
3383 ));
3384 }
3385 }
3386 }
3387 }
3388
3389 let json_path = target_dir.join("conformance-self-test.json");
3390 if let Ok(json) = serde_json::to_string_pretty(&report) {
3391 let _ = std::fs::write(&json_path, json);
3392 }
3393 let issues = report.definite_issues();
3396 if let Ok(json) = serde_json::to_string_pretty(&issues) {
3397 let issues_path = target_dir.join("conformance-definite-issues.json");
3398 if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
3399 TerminalReporter::print_warning(&format!(
3400 " {} definite issue(s) — see {}",
3401 issues.len(),
3402 issues_path.display()
3403 ));
3404 }
3405 }
3406 let owasp_accepted = report.owasp_accepted_probes();
3408 if !owasp_accepted.is_empty() {
3409 if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
3410 let owasp_path = target_dir.join("conformance-owasp-accepted.json");
3411 if std::fs::write(&owasp_path, json).is_ok() {
3412 TerminalReporter::print_warning(&format!(
3413 " {} owasp injection probe(s) accepted by the target — see {}",
3414 owasp_accepted.len(),
3415 owasp_path.display()
3416 ));
3417 }
3418 }
3419 }
3420 TerminalReporter::print_progress(&report.render_summary());
3421
3422 if self.validate_requests {
3431 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3432 &self.spec,
3433 &target_dir,
3434 self.base_path.as_deref(),
3435 )
3436 .await?;
3437 if n > 0 {
3438 TerminalReporter::print_warning(&format!(
3439 " {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3440 n,
3441 target_dir.display(),
3442 ));
3443 }
3444 }
3445 }
3446
3447 Ok(())
3448 }
3449
3450 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3456 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3457 use crate::conformance::report::ConformanceReport;
3458 use crate::conformance::spec::ConformanceFeature;
3459
3460 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3461
3462 TerminalReporter::print_progress("Parsing targets file...");
3464 let targets = parse_targets_file(targets_file)?;
3465 let num_targets = targets.len();
3466 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3467
3468 if targets.is_empty() {
3469 return Err(BenchError::Other("No targets found in file".to_string()));
3470 }
3471
3472 TerminalReporter::print_progress(
3473 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
3474 );
3475
3476 let categories = self.conformance_categories.as_ref().map(|cats_str| {
3478 cats_str
3479 .split(',')
3480 .filter_map(|s| {
3481 let trimmed = s.trim();
3482 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3483 Some(canonical.to_string())
3484 } else {
3485 TerminalReporter::print_warning(&format!(
3486 "Unknown conformance category: '{}'. Valid categories: {}",
3487 trimmed,
3488 ConformanceFeature::cli_category_names()
3489 .iter()
3490 .map(|(cli, _)| *cli)
3491 .collect::<Vec<_>>()
3492 .join(", ")
3493 ));
3494 None
3495 }
3496 })
3497 .collect::<Vec<String>>()
3498 });
3499
3500 let base_custom_headers: Vec<(String, String)> = self
3502 .conformance_headers
3503 .iter()
3504 .filter_map(|h| {
3505 let (name, value) = h.split_once(':')?;
3506 Some((name.trim().to_string(), value.trim().to_string()))
3507 })
3508 .collect();
3509
3510 if !base_custom_headers.is_empty() {
3511 TerminalReporter::print_progress(&format!(
3512 "Using {} base custom header(s) for authentication",
3513 base_custom_headers.len()
3514 ));
3515 }
3516
3517 let annotated_ops = if !self.spec.is_empty() {
3519 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3520 let parser = SpecParser::from_file(&self.spec[0]).await?;
3521 let operations = parser.get_operations();
3522 let annotated =
3523 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3524 &operations,
3525 parser.spec(),
3526 );
3527 TerminalReporter::print_success(&format!(
3528 "Analyzed {} operations, found {} feature annotations",
3529 operations.len(),
3530 annotated.iter().map(|a| a.features.len()).sum::<usize>()
3531 ));
3532 Some(annotated)
3533 } else {
3534 None
3535 };
3536
3537 std::fs::create_dir_all(&self.output)?;
3539
3540 struct TargetResult {
3542 url: String,
3543 passed: usize,
3544 failed: usize,
3545 elapsed: std::time::Duration,
3546 report_json: serde_json::Value,
3547 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3548 }
3549
3550 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3551 let total_start = std::time::Instant::now();
3552
3553 for (idx, target) in targets.iter().enumerate() {
3554 tracing::info!(
3555 "Running conformance tests against target {}/{}: {}",
3556 idx + 1,
3557 num_targets,
3558 target.url
3559 );
3560 TerminalReporter::print_progress(&format!(
3561 "\n--- Target {}/{}: {} ---",
3562 idx + 1,
3563 num_targets,
3564 target.url
3565 ));
3566
3567 let mut merged_headers = base_custom_headers.clone();
3569 if let Some(ref target_headers) = target.headers {
3570 for (name, value) in target_headers {
3571 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3573 existing.1 = value.clone();
3574 } else {
3575 merged_headers.push((name.clone(), value.clone()));
3576 }
3577 }
3578 }
3579 if let Some(ref auth) = target.auth {
3581 if let Some(existing) =
3582 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3583 {
3584 existing.1 = auth.clone();
3585 } else {
3586 merged_headers.push(("Authorization".to_string(), auth.clone()));
3587 }
3588 }
3589
3590 let target_dir = self.output.join(format!("target_{}", idx));
3596 std::fs::create_dir_all(&target_dir)?;
3597
3598 let config = ConformanceConfig {
3599 target_url: target.url.clone(),
3600 api_key: self.conformance_api_key.clone(),
3601 basic_auth: self.conformance_basic_auth.clone(),
3602 skip_tls_verify: self.skip_tls_verify,
3603 categories: categories.clone(),
3604 base_path: self.base_path.clone(),
3605 custom_headers: merged_headers,
3606 output_dir: Some(target_dir.clone()),
3607 all_operations: self.conformance_all_operations,
3608 custom_checks_file: self.conformance_custom.clone(),
3609 request_delay_ms: self.conformance_delay_ms,
3610 custom_filter: self.conformance_custom_filter.clone(),
3611 export_requests: self.export_requests,
3612 validate_requests: self.validate_requests,
3613 };
3614
3615 let target_start = std::time::Instant::now();
3616 let report = if self.use_k6 {
3617 if !K6Executor::is_k6_installed() {
3618 TerminalReporter::print_error("k6 is not installed");
3619 TerminalReporter::print_warning(
3620 "Install k6 from: https://k6.io/docs/get-started/installation/",
3621 );
3622 return Err(BenchError::K6NotFound);
3623 }
3624
3625 let script = if let Some(ref annotated) = annotated_ops {
3626 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3627 config.clone(),
3628 annotated.clone(),
3629 );
3630 let (script, _check_count) = gen.generate()?;
3631 script
3632 } else {
3633 let generator = ConformanceGenerator::new(config.clone());
3634 generator.generate()?
3635 };
3636
3637 let script_path = target_dir.join("k6-conformance.js");
3638 std::fs::write(&script_path, &script).map_err(|e| {
3639 BenchError::Other(format!("Failed to write conformance script: {}", e))
3640 })?;
3641 TerminalReporter::print_success(&format!(
3642 "Conformance script generated: {}",
3643 script_path.display()
3644 ));
3645
3646 TerminalReporter::print_progress(&format!(
3647 "Running conformance tests via k6 against {}...",
3648 target.url
3649 ));
3650 let k6 = K6Executor::new()?
3651 .with_local_ips(self.source_ips.join(","))
3652 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3653 let api_port = 6565u16.saturating_add(idx as u16);
3655 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3656 .await?;
3657
3658 let report_path = target_dir.join("conformance-report.json");
3659 if report_path.exists() {
3660 ConformanceReport::from_file(&report_path)?
3661 } else {
3662 TerminalReporter::print_warning(&format!(
3663 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3664 target.url
3665 ));
3666 continue;
3667 }
3668 } else {
3669 let mut executor =
3670 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3671
3672 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3675 executor = if let Some(ref annotated) = annotated_ops {
3676 executor.with_spec_driven_checks(annotated)
3677 } else if custom_only {
3678 executor
3679 } else {
3680 executor.with_reference_checks()
3681 };
3682 executor = executor.with_custom_checks()?;
3683
3684 TerminalReporter::print_success(&format!(
3685 "Executing {} conformance checks against {}...",
3686 executor.check_count(),
3687 target.url
3688 ));
3689
3690 executor.execute().await?
3691 };
3692 let target_elapsed = target_start.elapsed();
3693
3694 let report_json = report.to_json();
3695
3696 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3698 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3699 let total_checks = passed + failed;
3700 let rate = if total_checks == 0 {
3701 0.0
3702 } else {
3703 (passed as f64 / total_checks as f64) * 100.0
3704 };
3705
3706 TerminalReporter::print_success(&format!(
3707 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3708 target.url,
3709 passed,
3710 total_checks,
3711 rate,
3712 target_elapsed.as_secs_f64()
3713 ));
3714
3715 let target_report_path = target_dir.join("conformance-report.json");
3717 let report_str = serde_json::to_string_pretty(&report_json)
3718 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3719 std::fs::write(&target_report_path, &report_str)
3720 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3721
3722 let failure_details = report.failure_details();
3724 if !failure_details.is_empty() {
3725 let details_path = target_dir.join("conformance-failure-details.json");
3726 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3727 let _ = std::fs::write(&details_path, json);
3728 }
3729 }
3730
3731 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3738 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3739 &self.spec,
3740 &target_dir,
3741 self.base_path.as_deref(),
3742 )
3743 .await?;
3744 if n > 0 {
3745 TerminalReporter::print_warning(&format!(
3746 "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3747 target.url,
3748 n,
3749 target_dir.display()
3750 ));
3751 }
3752 }
3753
3754 let owasp_coverage = report.owasp_coverage_data();
3756
3757 target_results.push(TargetResult {
3758 url: target.url.clone(),
3759 passed,
3760 failed,
3761 elapsed: target_elapsed,
3762 report_json,
3763 owasp_coverage,
3764 });
3765 }
3766
3767 let total_elapsed = total_start.elapsed();
3768
3769 println!("\n{}", "=".repeat(80));
3771 println!(" Multi-Target Conformance Summary");
3772 println!("{}", "=".repeat(80));
3773 println!(
3774 " {:<40} {:>8} {:>8} {:>8} {:>8}",
3775 "Target URL", "Passed", "Failed", "Rate", "Time"
3776 );
3777 println!(" {}", "-".repeat(76));
3778
3779 let mut total_passed = 0usize;
3780 let mut total_failed = 0usize;
3781
3782 for result in &target_results {
3783 let total_checks = result.passed + result.failed;
3784 let rate = if total_checks == 0 {
3785 0.0
3786 } else {
3787 (result.passed as f64 / total_checks as f64) * 100.0
3788 };
3789
3790 let display_url = if result.url.len() > 38 {
3792 format!("{}...", &result.url[..35])
3793 } else {
3794 result.url.clone()
3795 };
3796
3797 println!(
3798 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3799 display_url,
3800 result.passed,
3801 result.failed,
3802 rate,
3803 result.elapsed.as_secs_f64()
3804 );
3805
3806 total_passed += result.passed;
3807 total_failed += result.failed;
3808 }
3809
3810 let grand_total = total_passed + total_failed;
3811 let overall_rate = if grand_total == 0 {
3812 0.0
3813 } else {
3814 (total_passed as f64 / grand_total as f64) * 100.0
3815 };
3816
3817 println!(" {}", "-".repeat(76));
3818 println!(
3819 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3820 format!("TOTAL ({} targets)", num_targets),
3821 total_passed,
3822 total_failed,
3823 overall_rate,
3824 total_elapsed.as_secs_f64()
3825 );
3826 println!("{}", "=".repeat(80));
3827
3828 for result in &target_results {
3830 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
3831 for entry in &result.owasp_coverage {
3832 let status = if !entry.tested {
3833 "-"
3834 } else if entry.all_passed {
3835 "pass"
3836 } else {
3837 "FAIL"
3838 };
3839 let via = if entry.via_categories.is_empty() {
3840 String::new()
3841 } else {
3842 format!(" (via {})", entry.via_categories.join(", "))
3843 };
3844 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3845 }
3846 }
3847
3848 let per_target_summaries: Vec<serde_json::Value> = target_results
3850 .iter()
3851 .enumerate()
3852 .map(|(idx, r)| {
3853 let total_checks = r.passed + r.failed;
3854 let rate = if total_checks == 0 {
3855 0.0
3856 } else {
3857 (r.passed as f64 / total_checks as f64) * 100.0
3858 };
3859 let owasp_json: Vec<serde_json::Value> = r
3860 .owasp_coverage
3861 .iter()
3862 .map(|e| {
3863 serde_json::json!({
3864 "id": e.id,
3865 "name": e.name,
3866 "tested": e.tested,
3867 "all_passed": e.all_passed,
3868 "via_categories": e.via_categories,
3869 })
3870 })
3871 .collect();
3872 serde_json::json!({
3873 "target_url": r.url,
3874 "target_index": idx,
3875 "checks_passed": r.passed,
3876 "checks_failed": r.failed,
3877 "total_checks": total_checks,
3878 "pass_rate": rate,
3879 "elapsed_seconds": r.elapsed.as_secs_f64(),
3880 "report": r.report_json,
3881 "owasp_coverage": owasp_json,
3882 })
3883 })
3884 .collect();
3885
3886 let combined_summary = serde_json::json!({
3887 "total_targets": num_targets,
3888 "total_checks_passed": total_passed,
3889 "total_checks_failed": total_failed,
3890 "overall_pass_rate": overall_rate,
3891 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3892 "targets": per_target_summaries,
3893 });
3894
3895 let summary_path = self.output.join("multi-target-conformance-summary.json");
3896 let summary_str = serde_json::to_string_pretty(&combined_summary)
3897 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3898 std::fs::write(&summary_path, &summary_str)
3899 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3900 TerminalReporter::print_success(&format!(
3901 "Combined summary saved to: {}",
3902 summary_path.display()
3903 ));
3904
3905 Ok(())
3906 }
3907
3908 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3910 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3911
3912 let custom_headers = self.parse_headers()?;
3914
3915 let mut config = OwaspApiConfig::new()
3917 .with_auth_header(&self.owasp_auth_header)
3918 .with_verbose(self.verbose)
3919 .with_insecure(self.skip_tls_verify)
3920 .with_concurrency(self.vus as usize)
3921 .with_iterations(self.owasp_iterations as usize)
3922 .with_base_path(self.base_path.clone())
3923 .with_custom_headers(custom_headers);
3924
3925 if let Some(ref token) = self.owasp_auth_token {
3927 config = config.with_valid_auth_token(token);
3928 }
3929
3930 if let Some(ref cats_str) = self.owasp_categories {
3932 let categories: Vec<OwaspCategory> = cats_str
3933 .split(',')
3934 .filter_map(|s| {
3935 let trimmed = s.trim();
3936 match trimmed.parse::<OwaspCategory>() {
3937 Ok(cat) => Some(cat),
3938 Err(e) => {
3939 TerminalReporter::print_warning(&e);
3940 None
3941 }
3942 }
3943 })
3944 .collect();
3945
3946 if !categories.is_empty() {
3947 config = config.with_categories(categories);
3948 }
3949 }
3950
3951 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3953 config.admin_paths_file = Some(admin_paths_file.clone());
3954 if let Err(e) = config.load_admin_paths() {
3955 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3956 }
3957 }
3958
3959 if let Some(ref id_fields_str) = self.owasp_id_fields {
3961 let id_fields: Vec<String> = id_fields_str
3962 .split(',')
3963 .map(|s| s.trim().to_string())
3964 .filter(|s| !s.is_empty())
3965 .collect();
3966 if !id_fields.is_empty() {
3967 config = config.with_id_fields(id_fields);
3968 }
3969 }
3970
3971 if let Some(ref report_path) = self.owasp_report {
3973 config = config.with_report_path(report_path);
3974 }
3975 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3976 config = config.with_report_format(format);
3977 }
3978
3979 let categories = config.categories_to_test();
3981 TerminalReporter::print_success(&format!(
3982 "Testing {} OWASP categories: {}",
3983 categories.len(),
3984 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3985 ));
3986
3987 if config.valid_auth_token.is_some() {
3988 TerminalReporter::print_progress("Using provided auth token for baseline requests");
3989 }
3990
3991 TerminalReporter::print_progress("Generating OWASP security test script...");
3993 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3994
3995 let script = generator.generate()?;
3997 TerminalReporter::print_success("OWASP security test script generated");
3998
3999 let script_path = if let Some(output) = &self.script_output {
4001 output.clone()
4002 } else {
4003 self.output.join("k6-owasp-security-test.js")
4004 };
4005
4006 if let Some(parent) = script_path.parent() {
4007 std::fs::create_dir_all(parent)?;
4008 }
4009 std::fs::write(&script_path, &script)?;
4010 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
4011
4012 if self.generate_only {
4014 println!("\nOWASP security test script generated. Run it with:");
4015 println!(" k6 run {}", script_path.display());
4016 return Ok(());
4017 }
4018
4019 TerminalReporter::print_progress("Executing OWASP security tests...");
4021 let executor = K6Executor::new()?
4022 .with_local_ips(self.source_ips.join(","))
4023 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
4024 std::fs::create_dir_all(&self.output)?;
4025
4026 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
4027
4028 let duration_secs = Self::parse_duration(&self.duration)?;
4029 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
4030
4031 println!("\nOWASP security test results saved to: {}", self.output.display());
4032
4033 Ok(())
4034 }
4035}
4036
4037#[cfg(test)]
4038mod tests {
4039 use super::*;
4040 use tempfile::tempdir;
4041
4042 #[test]
4043 fn test_parse_duration() {
4044 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
4045 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
4046 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
4047 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
4048 }
4049
4050 #[test]
4054 fn parse_ip_list_ipv4_range_inclusive() {
4055 let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
4056 assert_eq!(v.len(), 23);
4057 assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
4058 assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
4059 }
4060
4061 #[test]
4064 fn parse_ip_list_range_rejects_backwards() {
4065 let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
4066 assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
4067 }
4068
4069 #[test]
4073 fn parse_ip_list_rejects_ipv6_range_syntax() {
4074 let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
4075 assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
4076 }
4077
4078 #[test]
4080 fn parse_ip_list_range_capped_at_256() {
4081 let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
4082 assert_eq!(v.len(), 256);
4083 assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
4084 }
4085
4086 #[test]
4089 fn parse_ip_list_plain_and_comma() {
4090 let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
4091 assert_eq!(v.len(), 3);
4092 assert_eq!(v[0].to_string(), "10.0.0.5");
4093 assert_eq!(v[2].to_string(), "10.0.0.7");
4094 }
4095
4096 #[test]
4099 fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
4100 let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
4101 assert_eq!(v.len(), 8);
4102 assert_eq!(v[0].to_string(), "10.0.0.0");
4103 assert_eq!(v[7].to_string(), "10.0.0.7");
4104 }
4105
4106 #[test]
4109 fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
4110 let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
4111 assert_eq!(v.len(), 256);
4112 assert_eq!(v[0].to_string(), "10.0.0.0");
4113 assert_eq!(v[255].to_string(), "10.0.0.255");
4114 }
4115
4116 #[test]
4118 fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
4119 let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
4120 assert_eq!(v.len(), 4);
4121 assert!(v[0].is_ipv6());
4122 assert_eq!(v[0].to_string(), "2001:db8::");
4123 assert_eq!(v[3].to_string(), "2001:db8::3");
4124 }
4125
4126 #[test]
4128 fn parse_ip_list_mixed_v4_v6_cidr() {
4129 let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
4130 assert_eq!(v.len(), 6); assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
4132 assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
4133 }
4134
4135 #[test]
4138 fn parse_ip_list_skips_malformed() {
4139 let v = parse_ip_list(
4140 &[
4141 "10.0.0.5".into(),
4142 "not-an-ip".into(),
4143 "10.0.0.6".into(),
4144 "/24".into(),
4145 "1.2.3.4/200".into(),
4146 ],
4147 "source-ip",
4148 );
4149 assert_eq!(v.len(), 2);
4150 assert_eq!(v[0].to_string(), "10.0.0.5");
4151 assert_eq!(v[1].to_string(), "10.0.0.6");
4152 }
4153
4154 #[test]
4155 fn test_parse_duration_invalid() {
4156 assert!(BenchCommand::parse_duration("invalid").is_err());
4157 assert!(BenchCommand::parse_duration("30x").is_err());
4158 }
4159
4160 #[test]
4161 fn test_parse_headers() {
4162 let cmd = BenchCommand {
4163 spec: vec![PathBuf::from("test.yaml")],
4164 spec_dir: None,
4165 merge_conflicts: "error".to_string(),
4166 spec_mode: "merge".to_string(),
4167 dependency_config: None,
4168 target: "http://localhost".to_string(),
4169 base_path: None,
4170 duration: "1m".to_string(),
4171 vus: 10,
4172 scenario: "ramp-up".to_string(),
4173 operations: None,
4174 exclude_operations: None,
4175 auth: None,
4176 headers: vec![
4177 "X-API-Key:test123".to_string(),
4178 "X-Client-ID:client456".to_string(),
4179 ],
4180 output: PathBuf::from("output"),
4181 generate_only: false,
4182 script_output: None,
4183 threshold_percentile: "p(95)".to_string(),
4184 threshold_ms: 500,
4185 max_error_rate: 0.05,
4186 abort_on_error: true,
4187 abort_on_error_rate: 0.95,
4188 verbose: false,
4189 skip_tls_verify: false,
4190 chunked_request_bodies: false,
4191 target_rps: None,
4192 no_keep_alive: false,
4193 targets_file: None,
4194 max_concurrency: None,
4195 results_format: "both".to_string(),
4196 params_file: None,
4197 crud_flow: false,
4198 flow_config: None,
4199 extract_fields: None,
4200 parallel_create: None,
4201 data_file: None,
4202 data_distribution: "unique-per-vu".to_string(),
4203 data_mappings: None,
4204 per_uri_control: false,
4205 error_rate: None,
4206 error_types: None,
4207 security_test: false,
4208 security_payloads: None,
4209 security_categories: None,
4210 security_target_fields: None,
4211 wafbench_dir: None,
4212 wafbench_cycle_all: false,
4213 owasp_api_top10: false,
4214 owasp_categories: None,
4215 owasp_auth_header: "Authorization".to_string(),
4216 owasp_auth_token: None,
4217 owasp_admin_paths: None,
4218 owasp_id_fields: None,
4219 owasp_report: None,
4220 owasp_report_format: "json".to_string(),
4221 owasp_iterations: 1,
4222 conformance: false,
4223 conformance_api_key: None,
4224 conformance_basic_auth: None,
4225 conformance_report: PathBuf::from("conformance-report.json"),
4226 conformance_categories: None,
4227 conformance_report_format: "json".to_string(),
4228 conformance_headers: vec![],
4229 conformance_all_operations: false,
4230 conformance_custom: None,
4231 conformance_delay_ms: 0,
4232 use_k6: false,
4233 conformance_custom_filter: None,
4234 export_requests: false,
4235 validate_requests: false,
4236 conformance_self_test: false,
4237 conformance_self_test_capture: false,
4238 conformance_self_test_iterations: 1,
4239 conformance_self_test_duration: None,
4240 validate_response_schemas: false,
4241 source_ips: Vec::new(),
4242 geo_source_ips: Vec::new(),
4243 geo_source_headers: Vec::new(),
4244 report_missed_cap: None,
4245 discard_response_bodies: false,
4246 dns_policy: None,
4247 };
4248
4249 let headers = cmd.parse_headers().unwrap();
4250 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4251 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4252 }
4253
4254 #[test]
4255 fn test_parse_header_string_preserves_comma_in_value() {
4256 let inputs = vec![
4259 "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4260 "X-Trace:1".to_string(),
4261 ];
4262 let headers = parse_header_string(&inputs).unwrap();
4263 assert_eq!(
4264 headers.get("Cookie"),
4265 Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4266 );
4267 assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4268 }
4269
4270 #[test]
4284 fn multi_target_clone_preserves_fields_parse_headers_reads() {
4285 let src = include_str!("command.rs");
4286
4287 let fn_start = src
4288 .find("async fn execute_multi_target(")
4289 .expect("execute_multi_target should exist");
4290 let block_start = src[fn_start..]
4291 .find("ParallelExecutor::new(")
4292 .map(|i| i + fn_start)
4293 .expect("multi-target path should build a ParallelExecutor");
4294 let block_end = src[block_start..]
4296 .find("\n );")
4297 .map(|i| i + block_start)
4298 .expect("ParallelExecutor::new(..) should be closed");
4299 let block = &src[block_start..block_end];
4300
4301 for field in ["conformance_basic_auth", "conformance_headers"] {
4304 for zeroed in [format!("{field}: None"), format!("{field}: vec![]")] {
4305 assert!(
4306 !block.contains(&zeroed),
4307 "execute_multi_target zeroes `{zeroed}`. parse_headers() folds `{field}` \
4308 into the header map, so zeroing it here strips auth from every \
4309 multi-target run while single-target keeps working (#79 round 64)."
4310 );
4311 }
4312 let passthrough = format!("{field}: self.{field}.clone()");
4313 assert!(
4314 block.contains(&passthrough),
4315 "execute_multi_target must carry `{field}` through as `{passthrough}` so \
4316 parse_headers() can fold it (#79 round 64)."
4317 );
4318 }
4319 }
4320
4321 #[test]
4322 fn test_get_spec_display_name() {
4323 let cmd = BenchCommand {
4324 spec: vec![PathBuf::from("test.yaml")],
4325 spec_dir: None,
4326 merge_conflicts: "error".to_string(),
4327 spec_mode: "merge".to_string(),
4328 dependency_config: None,
4329 target: "http://localhost".to_string(),
4330 base_path: None,
4331 duration: "1m".to_string(),
4332 vus: 10,
4333 scenario: "ramp-up".to_string(),
4334 operations: None,
4335 exclude_operations: None,
4336 auth: None,
4337 headers: Vec::new(),
4338 output: PathBuf::from("output"),
4339 generate_only: false,
4340 script_output: None,
4341 threshold_percentile: "p(95)".to_string(),
4342 threshold_ms: 500,
4343 max_error_rate: 0.05,
4344 abort_on_error: true,
4345 abort_on_error_rate: 0.95,
4346 verbose: false,
4347 skip_tls_verify: false,
4348 chunked_request_bodies: false,
4349 target_rps: None,
4350 no_keep_alive: false,
4351 targets_file: None,
4352 max_concurrency: None,
4353 results_format: "both".to_string(),
4354 params_file: None,
4355 crud_flow: false,
4356 flow_config: None,
4357 extract_fields: None,
4358 parallel_create: None,
4359 data_file: None,
4360 data_distribution: "unique-per-vu".to_string(),
4361 data_mappings: None,
4362 per_uri_control: false,
4363 error_rate: None,
4364 error_types: None,
4365 security_test: false,
4366 security_payloads: None,
4367 security_categories: None,
4368 security_target_fields: None,
4369 wafbench_dir: None,
4370 wafbench_cycle_all: false,
4371 owasp_api_top10: false,
4372 owasp_categories: None,
4373 owasp_auth_header: "Authorization".to_string(),
4374 owasp_auth_token: None,
4375 owasp_admin_paths: None,
4376 owasp_id_fields: None,
4377 owasp_report: None,
4378 owasp_report_format: "json".to_string(),
4379 owasp_iterations: 1,
4380 conformance: false,
4381 conformance_api_key: None,
4382 conformance_basic_auth: None,
4383 conformance_report: PathBuf::from("conformance-report.json"),
4384 conformance_categories: None,
4385 conformance_report_format: "json".to_string(),
4386 conformance_headers: vec![],
4387 conformance_all_operations: false,
4388 conformance_custom: None,
4389 conformance_delay_ms: 0,
4390 use_k6: false,
4391 conformance_custom_filter: None,
4392 export_requests: false,
4393 validate_requests: false,
4394 conformance_self_test: false,
4395 conformance_self_test_capture: false,
4396 conformance_self_test_iterations: 1,
4397 conformance_self_test_duration: None,
4398 validate_response_schemas: false,
4399 source_ips: Vec::new(),
4400 geo_source_ips: Vec::new(),
4401 geo_source_headers: Vec::new(),
4402 report_missed_cap: None,
4403 discard_response_bodies: false,
4404 dns_policy: None,
4405 };
4406
4407 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4408
4409 let cmd_multi = BenchCommand {
4411 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4412 spec_dir: None,
4413 merge_conflicts: "error".to_string(),
4414 spec_mode: "merge".to_string(),
4415 dependency_config: None,
4416 target: "http://localhost".to_string(),
4417 base_path: None,
4418 duration: "1m".to_string(),
4419 vus: 10,
4420 scenario: "ramp-up".to_string(),
4421 operations: None,
4422 exclude_operations: None,
4423 auth: None,
4424 headers: Vec::new(),
4425 output: PathBuf::from("output"),
4426 generate_only: false,
4427 script_output: None,
4428 threshold_percentile: "p(95)".to_string(),
4429 threshold_ms: 500,
4430 max_error_rate: 0.05,
4431 abort_on_error: true,
4432 abort_on_error_rate: 0.95,
4433 verbose: false,
4434 skip_tls_verify: false,
4435 chunked_request_bodies: false,
4436 target_rps: None,
4437 no_keep_alive: false,
4438 targets_file: None,
4439 max_concurrency: None,
4440 results_format: "both".to_string(),
4441 params_file: None,
4442 crud_flow: false,
4443 flow_config: None,
4444 extract_fields: None,
4445 parallel_create: None,
4446 data_file: None,
4447 data_distribution: "unique-per-vu".to_string(),
4448 data_mappings: None,
4449 per_uri_control: false,
4450 error_rate: None,
4451 error_types: None,
4452 security_test: false,
4453 security_payloads: None,
4454 security_categories: None,
4455 security_target_fields: None,
4456 wafbench_dir: None,
4457 wafbench_cycle_all: false,
4458 owasp_api_top10: false,
4459 owasp_categories: None,
4460 owasp_auth_header: "Authorization".to_string(),
4461 owasp_auth_token: None,
4462 owasp_admin_paths: None,
4463 owasp_id_fields: None,
4464 owasp_report: None,
4465 owasp_report_format: "json".to_string(),
4466 owasp_iterations: 1,
4467 conformance: false,
4468 conformance_api_key: None,
4469 conformance_basic_auth: None,
4470 conformance_report: PathBuf::from("conformance-report.json"),
4471 conformance_categories: None,
4472 conformance_report_format: "json".to_string(),
4473 conformance_headers: vec![],
4474 conformance_all_operations: false,
4475 conformance_custom: None,
4476 conformance_delay_ms: 0,
4477 use_k6: false,
4478 conformance_custom_filter: None,
4479 export_requests: false,
4480 validate_requests: false,
4481 conformance_self_test: false,
4482 conformance_self_test_capture: false,
4483 conformance_self_test_iterations: 1,
4484 conformance_self_test_duration: None,
4485 validate_response_schemas: false,
4486 source_ips: Vec::new(),
4487 geo_source_ips: Vec::new(),
4488 geo_source_headers: Vec::new(),
4489 report_missed_cap: None,
4490 discard_response_bodies: false,
4491 dns_policy: None,
4492 };
4493
4494 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4495 }
4496
4497 #[test]
4498 fn test_parse_extracted_values_from_output_dir() {
4499 let dir = tempdir().unwrap();
4500 let path = dir.path().join("extracted_values.json");
4501 std::fs::write(
4502 &path,
4503 r#"{
4504 "pool_id": "abc123",
4505 "count": 0,
4506 "enabled": false,
4507 "metadata": { "owner": "team-a" }
4508}"#,
4509 )
4510 .unwrap();
4511
4512 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4513 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4514 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4515 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4516 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4517 }
4518
4519 #[test]
4520 fn test_parse_extracted_values_missing_file() {
4521 let dir = tempdir().unwrap();
4522 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4523 assert!(extracted.values.is_empty());
4524 }
4525}