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: None,
1051 conformance_basic_auth: None,
1052 conformance_report: PathBuf::from("conformance-report.json"),
1053 conformance_categories: None,
1054 conformance_report_format: "json".to_string(),
1055 conformance_headers: vec![],
1056 conformance_all_operations: false,
1057 conformance_custom: None,
1058 conformance_delay_ms: 0,
1059 use_k6: false,
1060 conformance_custom_filter: None,
1061 export_requests: false,
1062 validate_requests: false,
1063 conformance_self_test: false,
1064 conformance_self_test_capture: false,
1065 conformance_self_test_iterations: 1,
1066 conformance_self_test_duration: None,
1067 validate_response_schemas: false,
1068 source_ips: self.source_ips.clone(),
1073 geo_source_ips: self.geo_source_ips.clone(),
1074 geo_source_headers: self.geo_source_headers.clone(),
1075 report_missed_cap: None,
1076 discard_response_bodies: self.discard_response_bodies,
1080 dns_policy: self.dns_policy.clone(),
1083 },
1084 targets,
1085 max_concurrency,
1086 );
1087
1088 let start_time = std::time::Instant::now();
1090 let aggregated_results = executor.execute_all().await?;
1091 let elapsed = start_time.elapsed();
1092
1093 self.report_multi_target_results(&aggregated_results, elapsed)?;
1095
1096 Ok(())
1097 }
1098
1099 fn report_multi_target_results(
1101 &self,
1102 results: &AggregatedResults,
1103 elapsed: std::time::Duration,
1104 ) -> Result<()> {
1105 TerminalReporter::print_multi_target_summary(results);
1107
1108 let total_secs = elapsed.as_secs();
1110 let hours = total_secs / 3600;
1111 let minutes = (total_secs % 3600) / 60;
1112 let seconds = total_secs % 60;
1113 if hours > 0 {
1114 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
1115 } else if minutes > 0 {
1116 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
1117 } else {
1118 println!("\n Total Elapsed Time: {}s", seconds);
1119 }
1120
1121 if self.results_format == "aggregated" || self.results_format == "both" {
1123 let summary_path = self.output.join("aggregated_summary.json");
1124 let summary_json = serde_json::json!({
1125 "total_elapsed_seconds": elapsed.as_secs(),
1126 "total_targets": results.total_targets,
1127 "successful_targets": results.successful_targets,
1128 "failed_targets": results.failed_targets,
1129 "aggregated_metrics": {
1130 "total_requests": results.aggregated_metrics.total_requests,
1131 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1132 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1133 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1134 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1135 "error_rate": results.aggregated_metrics.error_rate,
1136 "total_rps": results.aggregated_metrics.total_rps,
1137 "avg_rps": results.aggregated_metrics.avg_rps,
1138 "total_vus_max": results.aggregated_metrics.total_vus_max,
1139 },
1140 "target_results": results.target_results.iter().map(|r| {
1141 serde_json::json!({
1142 "target_url": r.target_url,
1143 "target_index": r.target_index,
1144 "success": r.success,
1145 "error": r.error,
1146 "total_requests": r.results.total_requests,
1147 "failed_requests": r.results.failed_requests,
1148 "avg_duration_ms": r.results.avg_duration_ms,
1149 "min_duration_ms": r.results.min_duration_ms,
1150 "med_duration_ms": r.results.med_duration_ms,
1151 "p90_duration_ms": r.results.p90_duration_ms,
1152 "p95_duration_ms": r.results.p95_duration_ms,
1153 "p99_duration_ms": r.results.p99_duration_ms,
1154 "max_duration_ms": r.results.max_duration_ms,
1155 "rps": r.results.rps,
1156 "vus_max": r.results.vus_max,
1157 "output_dir": r.output_dir.to_string_lossy(),
1158 })
1159 }).collect::<Vec<_>>(),
1160 });
1161
1162 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1163 TerminalReporter::print_success(&format!(
1164 "Aggregated summary saved to: {}",
1165 summary_path.display()
1166 ));
1167 }
1168
1169 let csv_path = self.output.join("all_targets.csv");
1171 let mut csv = String::from(
1172 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1173 );
1174 for r in &results.target_results {
1175 csv.push_str(&format!(
1176 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1177 r.target_url,
1178 r.success,
1179 r.results.total_requests,
1180 r.results.failed_requests,
1181 r.results.rps,
1182 r.results.vus_max,
1183 r.results.min_duration_ms,
1184 r.results.avg_duration_ms,
1185 r.results.med_duration_ms,
1186 r.results.p90_duration_ms,
1187 r.results.p95_duration_ms,
1188 r.results.p99_duration_ms,
1189 r.results.max_duration_ms,
1190 r.error.as_deref().unwrap_or(""),
1191 ));
1192 }
1193 let _ = std::fs::write(&csv_path, &csv);
1194
1195 println!("\nResults saved to: {}", self.output.display());
1196 println!(" - Per-target results: {}", self.output.join("target_*").display());
1197 println!(" - All targets CSV: {}", csv_path.display());
1198 if self.results_format == "aggregated" || self.results_format == "both" {
1199 println!(
1200 " - Aggregated summary: {}",
1201 self.output.join("aggregated_summary.json").display()
1202 );
1203 }
1204
1205 Ok(())
1206 }
1207
1208 pub fn parse_duration(duration: &str) -> Result<u64> {
1210 let duration = duration.trim();
1211
1212 if let Some(secs) = duration.strip_suffix('s') {
1213 secs.parse::<u64>()
1214 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1215 } else if let Some(mins) = duration.strip_suffix('m') {
1216 mins.parse::<u64>()
1217 .map(|m| m * 60)
1218 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1219 } else if let Some(hours) = duration.strip_suffix('h') {
1220 hours
1221 .parse::<u64>()
1222 .map(|h| h * 3600)
1223 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1224 } else {
1225 duration
1227 .parse::<u64>()
1228 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1229 }
1230 }
1231
1232 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1234 let mut headers = parse_header_string(&self.headers)?;
1235
1236 let already_has = |hs: &HashMap<String, String>, name: &str| -> bool {
1247 hs.keys().any(|k| k.eq_ignore_ascii_case(name))
1248 };
1249
1250 if !already_has(&headers, "Authorization") {
1251 if let Some(b) = self.conformance_basic_auth.as_ref().filter(|s| !s.is_empty()) {
1252 use base64::Engine as _;
1253 let encoded = base64::engine::general_purpose::STANDARD.encode(b.as_bytes());
1254 headers.insert("Authorization".to_string(), format!("Basic {}", encoded));
1255 }
1256 }
1257
1258 for line in &self.conformance_headers {
1264 let Some((name, value)) = line.split_once(':') else {
1265 continue;
1266 };
1267 let name = name.trim();
1268 let value = value.trim();
1269 if name.is_empty() || already_has(&headers, name) {
1270 continue;
1271 }
1272 headers.insert(name.to_string(), value.to_string());
1273 }
1274
1275 if !self.conformance && self.conformance_api_key.is_some() {
1281 TerminalReporter::print_warning(
1282 "--conformance-api-key only fires under --conformance. For plain bench use --header 'X-API-Key: ...'.",
1283 );
1284 }
1285
1286 Ok(headers)
1287 }
1288
1289 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1290 let extracted_path = output_dir.join("extracted_values.json");
1291 if !extracted_path.exists() {
1292 return Ok(ExtractedValues::new());
1293 }
1294
1295 let content = std::fs::read_to_string(&extracted_path)
1296 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1297 let parsed: serde_json::Value = serde_json::from_str(&content)
1298 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1299
1300 let mut extracted = ExtractedValues::new();
1301 if let Some(values) = parsed.as_object() {
1302 for (key, value) in values {
1303 extracted.set(key.clone(), value.clone());
1304 }
1305 }
1306
1307 Ok(extracted)
1308 }
1309
1310 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1319 if let Some(cli_base_path) = &self.base_path {
1321 if cli_base_path.is_empty() {
1322 return None;
1324 }
1325 return Some(cli_base_path.clone());
1326 }
1327
1328 parser.get_base_path()
1330 }
1331
1332 async fn build_mock_config(&self) -> MockIntegrationConfig {
1334 if MockServerDetector::looks_like_mock_server(&self.target) {
1336 if let Ok(info) = MockServerDetector::detect(&self.target).await {
1338 if info.is_mockforge {
1339 TerminalReporter::print_success(&format!(
1340 "Detected MockForge server (version: {})",
1341 info.version.as_deref().unwrap_or("unknown")
1342 ));
1343 return MockIntegrationConfig::mock_server();
1344 }
1345 }
1346 }
1347 MockIntegrationConfig::real_api()
1348 }
1349
1350 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1352 if !self.crud_flow {
1353 return None;
1354 }
1355
1356 if let Some(config_path) = &self.flow_config {
1358 match CrudFlowConfig::from_file(config_path) {
1359 Ok(config) => return Some(config),
1360 Err(e) => {
1361 TerminalReporter::print_warning(&format!(
1362 "Failed to load flow config: {}. Using auto-detection.",
1363 e
1364 ));
1365 }
1366 }
1367 }
1368
1369 let extract_fields = self
1371 .extract_fields
1372 .as_ref()
1373 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1374 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1375
1376 Some(CrudFlowConfig {
1377 flows: Vec::new(), default_extract_fields: extract_fields,
1379 })
1380 }
1381
1382 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1384 let data_file = self.data_file.as_ref()?;
1385
1386 let distribution = DataDistribution::from_str(&self.data_distribution)
1387 .unwrap_or(DataDistribution::UniquePerVu);
1388
1389 let mappings = self
1390 .data_mappings
1391 .as_ref()
1392 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1393 .unwrap_or_default();
1394
1395 Some(DataDrivenConfig {
1396 file_path: data_file.to_string_lossy().to_string(),
1397 distribution,
1398 mappings,
1399 csv_has_header: true,
1400 per_uri_control: self.per_uri_control,
1401 per_uri_columns: crate::data_driven::PerUriColumns::default(),
1402 })
1403 }
1404
1405 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1407 let error_rate = self.error_rate?;
1408
1409 let error_types = self
1410 .error_types
1411 .as_ref()
1412 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1413 .unwrap_or_default();
1414
1415 Some(InvalidDataConfig {
1416 error_rate,
1417 error_types,
1418 target_fields: Vec::new(),
1419 })
1420 }
1421
1422 fn build_security_config(&self) -> Option<SecurityTestConfig> {
1424 if !self.security_test {
1425 return None;
1426 }
1427
1428 let categories = self
1429 .security_categories
1430 .as_ref()
1431 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1432 .unwrap_or_else(|| {
1433 let mut default = HashSet::new();
1434 default.insert(SecurityCategory::SqlInjection);
1435 default.insert(SecurityCategory::Xss);
1436 default
1437 });
1438
1439 let target_fields = self
1440 .security_target_fields
1441 .as_ref()
1442 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1443 .unwrap_or_default();
1444
1445 let custom_payloads_file =
1446 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1447
1448 Some(SecurityTestConfig {
1449 enabled: true,
1450 categories,
1451 target_fields,
1452 custom_payloads_file,
1453 include_high_risk: false,
1454 })
1455 }
1456
1457 fn build_parallel_config(&self) -> Option<ParallelConfig> {
1459 let count = self.parallel_create?;
1460
1461 Some(ParallelConfig::new(count))
1462 }
1463
1464 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1466 let Some(ref wafbench_dir) = self.wafbench_dir else {
1467 return Vec::new();
1468 };
1469
1470 let mut loader = WafBenchLoader::new();
1471
1472 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1473 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1474 return Vec::new();
1475 }
1476
1477 let stats = loader.stats();
1478
1479 if stats.files_processed == 0 {
1480 TerminalReporter::print_warning(&format!(
1481 "No WAFBench YAML files found matching '{}'",
1482 wafbench_dir
1483 ));
1484 if !stats.parse_errors.is_empty() {
1486 TerminalReporter::print_warning("Some files were found but failed to parse:");
1487 for error in &stats.parse_errors {
1488 TerminalReporter::print_warning(&format!(" - {}", error));
1489 }
1490 }
1491 return Vec::new();
1492 }
1493
1494 TerminalReporter::print_progress(&format!(
1495 "Loaded {} WAFBench files, {} test cases, {} payloads",
1496 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1497 ));
1498
1499 for (category, count) in &stats.by_category {
1501 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1502 }
1503
1504 for error in &stats.parse_errors {
1506 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1507 }
1508
1509 loader.to_security_payloads()
1510 }
1511
1512 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1514 let mut enhanced_script = base_script.to_string();
1515 let mut additional_code = String::new();
1516
1517 if let Some(config) = self.build_data_driven_config() {
1519 TerminalReporter::print_progress("Adding data-driven testing support...");
1520 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1521 additional_code.push('\n');
1522 TerminalReporter::print_success("Data-driven testing enabled");
1523 }
1524
1525 if let Some(config) = self.build_invalid_data_config() {
1527 TerminalReporter::print_progress("Adding invalid data testing support...");
1528 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1529 additional_code.push('\n');
1530 additional_code
1531 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1532 additional_code.push('\n');
1533 additional_code
1534 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1535 additional_code.push('\n');
1536 TerminalReporter::print_success(&format!(
1537 "Invalid data testing enabled ({}% error rate)",
1538 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1539 ));
1540 }
1541
1542 let security_config = self.build_security_config();
1544 let wafbench_payloads = self.load_wafbench_payloads();
1545 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1546
1547 if security_config.is_some() || !wafbench_payloads.is_empty() {
1548 TerminalReporter::print_progress("Adding security testing support...");
1549
1550 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1552
1553 if let Some(ref config) = security_config {
1554 payload_list.extend(SecurityPayloads::get_payloads(config));
1555 }
1556
1557 if !wafbench_payloads.is_empty() {
1559 TerminalReporter::print_progress(&format!(
1560 "Loading {} WAFBench attack patterns...",
1561 wafbench_payloads.len()
1562 ));
1563 payload_list.extend(wafbench_payloads);
1564 }
1565
1566 let target_fields =
1567 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1568
1569 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1570 &payload_list,
1571 self.wafbench_cycle_all,
1572 ));
1573 additional_code.push('\n');
1574 additional_code
1575 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1576 additional_code.push('\n');
1577 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1578 additional_code.push('\n');
1579
1580 let mode = if self.wafbench_cycle_all {
1581 "cycle-all"
1582 } else {
1583 "random"
1584 };
1585 TerminalReporter::print_success(&format!(
1586 "Security testing enabled ({} payloads, {} mode)",
1587 payload_list.len(),
1588 mode
1589 ));
1590 } else if security_requested {
1591 TerminalReporter::print_warning(
1595 "Security testing was requested but no payloads were loaded. \
1596 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1597 );
1598 additional_code
1599 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1600 additional_code.push('\n');
1601 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1602 additional_code.push('\n');
1603 }
1604
1605 if let Some(config) = self.build_parallel_config() {
1607 TerminalReporter::print_progress("Adding parallel execution support...");
1608 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1609 additional_code.push('\n');
1610 TerminalReporter::print_success(&format!(
1611 "Parallel execution enabled (count: {})",
1612 config.count
1613 ));
1614 }
1615
1616 if !additional_code.is_empty() {
1618 if let Some(import_end) = enhanced_script.find("export const options") {
1620 enhanced_script.insert_str(
1621 import_end,
1622 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1623 );
1624 }
1625 }
1626
1627 Ok(enhanced_script)
1628 }
1629
1630 async fn execute_sequential_specs(&self) -> Result<()> {
1632 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1633
1634 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1636
1637 if !self.spec.is_empty() {
1638 let specs = load_specs_from_files(self.spec.clone())
1639 .await
1640 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1641 all_specs.extend(specs);
1642 }
1643
1644 if let Some(spec_dir) = &self.spec_dir {
1645 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1646 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1647 })?;
1648 all_specs.extend(dir_specs);
1649 }
1650
1651 if all_specs.is_empty() {
1652 return Err(BenchError::Other(
1653 "No spec files found for sequential execution".to_string(),
1654 ));
1655 }
1656
1657 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1658
1659 let execution_order = if let Some(config_path) = &self.dependency_config {
1661 TerminalReporter::print_progress("Loading dependency configuration...");
1662 let config = SpecDependencyConfig::from_file(config_path)?;
1663
1664 if !config.disable_auto_detect && config.execution_order.is_empty() {
1665 self.detect_and_sort_specs(&all_specs)?
1667 } else {
1668 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1670 }
1671 } else {
1672 self.detect_and_sort_specs(&all_specs)?
1674 };
1675
1676 TerminalReporter::print_success(&format!(
1677 "Execution order: {}",
1678 execution_order
1679 .iter()
1680 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1681 .collect::<Vec<_>>()
1682 .join(" → ")
1683 ));
1684
1685 let mut extracted_values = ExtractedValues::new();
1687 let total_specs = execution_order.len();
1688
1689 for (index, spec_path) in execution_order.iter().enumerate() {
1690 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1691
1692 TerminalReporter::print_progress(&format!(
1693 "[{}/{}] Executing spec: {}",
1694 index + 1,
1695 total_specs,
1696 spec_name
1697 ));
1698
1699 let spec = all_specs
1701 .iter()
1702 .find(|(p, _)| {
1703 p == spec_path
1704 || p.file_name() == spec_path.file_name()
1705 || p.file_name() == Some(spec_path.as_os_str())
1706 })
1707 .map(|(_, s)| s.clone())
1708 .ok_or_else(|| {
1709 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1710 })?;
1711
1712 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1714
1715 extracted_values.merge(&new_values);
1717
1718 TerminalReporter::print_success(&format!(
1719 "[{}/{}] Completed: {} (extracted {} values)",
1720 index + 1,
1721 total_specs,
1722 spec_name,
1723 new_values.values.len()
1724 ));
1725 }
1726
1727 TerminalReporter::print_success(&format!(
1728 "Sequential execution complete: {} specs executed",
1729 total_specs
1730 ));
1731
1732 Ok(())
1733 }
1734
1735 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1737 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1738
1739 let mut detector = DependencyDetector::new();
1740 let dependencies = detector.detect_dependencies(specs);
1741
1742 if dependencies.is_empty() {
1743 TerminalReporter::print_progress("No dependencies detected, using file order");
1744 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1745 }
1746
1747 TerminalReporter::print_progress(&format!(
1748 "Detected {} cross-spec dependencies",
1749 dependencies.len()
1750 ));
1751
1752 for dep in &dependencies {
1753 TerminalReporter::print_progress(&format!(
1754 " {} → {} (via field '{}')",
1755 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1756 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1757 dep.field_name
1758 ));
1759 }
1760
1761 topological_sort(specs, &dependencies)
1762 }
1763
1764 async fn execute_single_spec(
1766 &self,
1767 spec: &OpenApiSpec,
1768 spec_name: &str,
1769 _external_values: &ExtractedValues,
1770 ) -> Result<ExtractedValues> {
1771 let parser = SpecParser::from_spec(spec.clone());
1772
1773 if self.crud_flow {
1775 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1777 } else {
1778 self.execute_standard_spec(&parser, spec_name).await?;
1780 Ok(ExtractedValues::new())
1781 }
1782 }
1783
1784 async fn execute_crud_flow_with_extraction(
1786 &self,
1787 parser: &SpecParser,
1788 spec_name: &str,
1789 ) -> Result<ExtractedValues> {
1790 let operations = parser.get_operations();
1791 let flows = CrudFlowDetector::detect_flows(&operations);
1792
1793 if flows.is_empty() {
1794 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1795 return Ok(ExtractedValues::new());
1796 }
1797
1798 TerminalReporter::print_progress(&format!(
1799 " {} CRUD flow(s) in {}",
1800 flows.len(),
1801 spec_name
1802 ));
1803
1804 let mut handlebars = handlebars::Handlebars::new();
1806 handlebars.register_helper(
1808 "json",
1809 Box::new(
1810 |h: &handlebars::Helper,
1811 _: &handlebars::Handlebars,
1812 _: &handlebars::Context,
1813 _: &mut handlebars::RenderContext,
1814 out: &mut dyn handlebars::Output|
1815 -> handlebars::HelperResult {
1816 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1817 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1818 Ok(())
1819 },
1820 ),
1821 );
1822 let template = include_str!("templates/k6_crud_flow.hbs");
1823 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1824
1825 let custom_headers = self.parse_headers()?;
1826 let config = self.build_crud_flow_config().unwrap_or_default();
1827
1828 let param_overrides = if let Some(params_file) = &self.params_file {
1830 let overrides = ParameterOverrides::from_file(params_file)?;
1831 Some(overrides)
1832 } else {
1833 None
1834 };
1835
1836 let duration_secs = Self::parse_duration(&self.duration)?;
1838 let scenario =
1839 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1840 let stages = scenario.generate_stages(duration_secs, self.vus);
1841
1842 let api_base_path = self.resolve_base_path(parser);
1844
1845 let mut all_headers = custom_headers.clone();
1847 if let Some(auth) = &self.auth {
1848 all_headers.insert("Authorization".to_string(), auth.clone());
1849 }
1850 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1851
1852 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1854
1855 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1856 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1860 serde_json::json!({
1861 "name": sanitized_name.clone(),
1862 "display_name": f.name,
1863 "base_path": f.base_path,
1864 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1865 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1867 let method_raw = if !parts.is_empty() {
1868 parts[0].to_uppercase()
1869 } else {
1870 "GET".to_string()
1871 };
1872 let method = if !parts.is_empty() {
1873 let m = parts[0].to_lowercase();
1874 if m == "delete" { "del".to_string() } else { m }
1876 } else {
1877 "get".to_string()
1878 };
1879 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1880 let path = if let Some(ref bp) = api_base_path {
1882 format!("{}{}", bp, raw_path)
1883 } else {
1884 raw_path.to_string()
1885 };
1886 let is_get_or_head = method == "get" || method == "head";
1887 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1889
1890 let body_value = if has_body {
1892 param_overrides.as_ref()
1893 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1894 .and_then(|oo| oo.body)
1895 .unwrap_or_else(|| serde_json::json!({}))
1896 } else {
1897 serde_json::json!({})
1898 };
1899
1900 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1902
1903 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1905 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1906
1907 serde_json::json!({
1908 "operation": s.operation,
1909 "method": method,
1910 "path": path,
1911 "extract": s.extract,
1912 "use_values": s.use_values,
1913 "use_body": s.use_body,
1914 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1915 "inject_attacks": s.inject_attacks,
1916 "attack_types": s.attack_types,
1917 "description": s.description,
1918 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1919 "is_get_or_head": is_get_or_head,
1920 "has_body": has_body,
1921 "body": processed_body.value,
1922 "body_is_dynamic": body_is_dynamic,
1923 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1924 })
1925 }).collect::<Vec<_>>(),
1926 })
1927 }).collect();
1928
1929 for flow_data in &flows_data {
1931 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1932 for step in steps {
1933 if let Some(placeholders_arr) =
1934 step.get("_placeholders").and_then(|p| p.as_array())
1935 {
1936 for p_str in placeholders_arr {
1937 if let Some(p_name) = p_str.as_str() {
1938 match p_name {
1939 "VU" => {
1940 all_placeholders.insert(DynamicPlaceholder::VU);
1941 }
1942 "Iteration" => {
1943 all_placeholders.insert(DynamicPlaceholder::Iteration);
1944 }
1945 "Timestamp" => {
1946 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1947 }
1948 "UUID" => {
1949 all_placeholders.insert(DynamicPlaceholder::UUID);
1950 }
1951 "Random" => {
1952 all_placeholders.insert(DynamicPlaceholder::Random);
1953 }
1954 "Counter" => {
1955 all_placeholders.insert(DynamicPlaceholder::Counter);
1956 }
1957 "Date" => {
1958 all_placeholders.insert(DynamicPlaceholder::Date);
1959 }
1960 "VuIter" => {
1961 all_placeholders.insert(DynamicPlaceholder::VuIter);
1962 }
1963 _ => {}
1964 }
1965 }
1966 }
1967 }
1968 }
1969 }
1970 }
1971
1972 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1974 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1975
1976 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1978
1979 let data = serde_json::json!({
1980 "base_url": self.target,
1981 "flows": flows_data,
1982 "extract_fields": config.default_extract_fields,
1983 "duration_secs": duration_secs,
1984 "max_vus": self.vus,
1985 "auth_header": self.auth,
1986 "custom_headers": custom_headers,
1987 "skip_tls_verify": self.skip_tls_verify,
1988 "stages": stages.iter().map(|s| serde_json::json!({
1990 "duration": s.duration,
1991 "target": s.target,
1992 })).collect::<Vec<_>>(),
1993 "threshold_percentile": self.threshold_percentile,
1994 "threshold_ms": self.threshold_ms,
1995 "max_error_rate": self.max_error_rate,
1996 "abort_on_error": self.abort_on_error,
1997 "abort_on_error_rate": self.abort_on_error_rate,
1998 "headers": headers_json,
1999 "dynamic_imports": required_imports,
2000 "dynamic_globals": required_globals,
2001 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
2002 "security_testing_enabled": security_testing_enabled,
2004 "has_custom_headers": !custom_headers.is_empty(),
2005 });
2006
2007 let mut script = handlebars
2008 .render_template(template, &data)
2009 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2010
2011 if security_testing_enabled {
2013 script = self.generate_enhanced_script(&script)?;
2014 }
2015
2016 let script_path =
2018 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
2019
2020 std::fs::create_dir_all(self.output.clone())?;
2021 std::fs::write(&script_path, &script)?;
2022
2023 if !self.generate_only {
2024 let executor = K6Executor::new()?
2025 .with_local_ips(self.source_ips.join(","))
2026 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2027 std::fs::create_dir_all(&output_dir)?;
2028
2029 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2030
2031 let extracted = Self::parse_extracted_values(&output_dir)?;
2032 TerminalReporter::print_progress(&format!(
2033 " Extracted {} value(s) from {}",
2034 extracted.values.len(),
2035 spec_name
2036 ));
2037 return Ok(extracted);
2038 }
2039
2040 Ok(ExtractedValues::new())
2041 }
2042
2043 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
2045 let mut operations = if let Some(filter) = &self.operations {
2046 parser.filter_operations(filter)?
2047 } else {
2048 parser.get_operations()
2049 };
2050
2051 if let Some(exclude) = &self.exclude_operations {
2052 operations = parser.exclude_operations(operations, exclude)?;
2053 }
2054
2055 if operations.is_empty() {
2056 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
2057 return Ok(());
2058 }
2059
2060 TerminalReporter::print_progress(&format!(
2061 " {} operations in {}",
2062 operations.len(),
2063 spec_name
2064 ));
2065
2066 let templates: Vec<_> = operations
2068 .iter()
2069 .map(RequestGenerator::generate_template)
2070 .collect::<Result<Vec<_>>>()?;
2071
2072 let custom_headers = self.parse_headers()?;
2074
2075 let base_path = self.resolve_base_path(parser);
2077
2078 let scenario =
2080 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2081
2082 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
2083
2084 let k6_config = K6Config {
2085 target_url: self.target.clone(),
2086 base_path,
2087 scenario,
2088 duration_secs: Self::parse_duration(&self.duration)?,
2089 max_vus: self.vus,
2090 threshold_percentile: self.threshold_percentile.clone(),
2091 threshold_ms: self.threshold_ms,
2092 max_error_rate: self.max_error_rate,
2093 auth_header: self.auth.clone(),
2094 custom_headers,
2095 skip_tls_verify: self.skip_tls_verify,
2096 security_testing_enabled,
2097 chunked_request_bodies: self.chunked_request_bodies,
2098 target_rps: self.target_rps,
2099 no_keep_alive: self.no_keep_alive,
2100 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
2102 .into_iter()
2103 .map(|ip| ip.to_string())
2104 .collect(),
2105 geo_source_headers: if self.geo_source_headers.is_empty()
2106 && !self.geo_source_ips.is_empty()
2107 {
2108 crate::conformance::self_test::default_geo_source_headers()
2109 } else {
2110 self.geo_source_headers.clone()
2111 },
2112 };
2113
2114 let generator = K6ScriptGenerator::new(k6_config, templates)
2115 .with_abort_valve(self.abort_on_error, self.abort_on_error_rate);
2116 let mut script = generator.generate()?;
2117
2118 let has_advanced_features = self.data_file.is_some()
2120 || self.error_rate.is_some()
2121 || self.security_test
2122 || self.parallel_create.is_some()
2123 || self.wafbench_dir.is_some();
2124
2125 if has_advanced_features {
2126 script = self.generate_enhanced_script(&script)?;
2127 }
2128
2129 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
2131
2132 std::fs::create_dir_all(self.output.clone())?;
2133 std::fs::write(&script_path, &script)?;
2134
2135 if !self.generate_only {
2136 let executor = K6Executor::new()?
2139 .with_local_ips(self.source_ips.join(","))
2140 .with_dns_policy(self.dns_policy.clone().unwrap_or_default())
2141 .with_discard_response_bodies(self.discard_response_bodies);
2142 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2143 std::fs::create_dir_all(&output_dir)?;
2144
2145 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2146 }
2147
2148 Ok(())
2149 }
2150
2151 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
2153 let config = self.build_crud_flow_config().unwrap_or_default();
2155
2156 let flows = if !config.flows.is_empty() {
2158 TerminalReporter::print_progress("Using custom flow configuration...");
2159 config.flows.clone()
2160 } else {
2161 TerminalReporter::print_progress("Detecting CRUD operations...");
2162 let operations = parser.get_operations();
2163 CrudFlowDetector::detect_flows(&operations)
2164 };
2165
2166 if flows.is_empty() {
2167 return Err(BenchError::Other(
2168 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
2169 ));
2170 }
2171
2172 if config.flows.is_empty() {
2173 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2174 } else {
2175 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2176 }
2177
2178 for flow in &flows {
2179 TerminalReporter::print_progress(&format!(
2180 " - {}: {} steps",
2181 flow.name,
2182 flow.steps.len()
2183 ));
2184 }
2185
2186 let mut handlebars = handlebars::Handlebars::new();
2188 handlebars.register_helper(
2190 "json",
2191 Box::new(
2192 |h: &handlebars::Helper,
2193 _: &handlebars::Handlebars,
2194 _: &handlebars::Context,
2195 _: &mut handlebars::RenderContext,
2196 out: &mut dyn handlebars::Output|
2197 -> handlebars::HelperResult {
2198 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2199 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2200 Ok(())
2201 },
2202 ),
2203 );
2204 let template = include_str!("templates/k6_crud_flow.hbs");
2205
2206 let custom_headers = self.parse_headers()?;
2207
2208 let param_overrides = if let Some(params_file) = &self.params_file {
2210 TerminalReporter::print_progress("Loading parameter overrides...");
2211 let overrides = ParameterOverrides::from_file(params_file)?;
2212 TerminalReporter::print_success(&format!(
2213 "Loaded parameter overrides ({} operation-specific, {} defaults)",
2214 overrides.operations.len(),
2215 if overrides.defaults.is_empty() { 0 } else { 1 }
2216 ));
2217 Some(overrides)
2218 } else {
2219 None
2220 };
2221
2222 let duration_secs = Self::parse_duration(&self.duration)?;
2224 let scenario =
2225 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2226 let stages = scenario.generate_stages(duration_secs, self.vus);
2227
2228 let api_base_path = self.resolve_base_path(parser);
2230 if let Some(ref bp) = api_base_path {
2231 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2232 }
2233
2234 let mut all_headers = custom_headers.clone();
2236 if let Some(auth) = &self.auth {
2237 all_headers.insert("Authorization".to_string(), auth.clone());
2238 }
2239 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2240
2241 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2243
2244 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2245 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2250 serde_json::json!({
2251 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
2254 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2255 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2257 let method_raw = if !parts.is_empty() {
2258 parts[0].to_uppercase()
2259 } else {
2260 "GET".to_string()
2261 };
2262 let method = if !parts.is_empty() {
2263 let m = parts[0].to_lowercase();
2264 if m == "delete" { "del".to_string() } else { m }
2266 } else {
2267 "get".to_string()
2268 };
2269 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2270 let path = if let Some(ref bp) = api_base_path {
2272 format!("{}{}", bp, raw_path)
2273 } else {
2274 raw_path.to_string()
2275 };
2276 let is_get_or_head = method == "get" || method == "head";
2277 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2279
2280 let body_value = if has_body {
2282 param_overrides.as_ref()
2283 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2284 .and_then(|oo| oo.body)
2285 .unwrap_or_else(|| serde_json::json!({}))
2286 } else {
2287 serde_json::json!({})
2288 };
2289
2290 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2292 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2297 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2298
2299 serde_json::json!({
2300 "operation": s.operation,
2301 "method": method,
2302 "path": path,
2303 "extract": s.extract,
2304 "use_values": s.use_values,
2305 "use_body": s.use_body,
2306 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2307 "inject_attacks": s.inject_attacks,
2308 "attack_types": s.attack_types,
2309 "description": s.description,
2310 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2311 "is_get_or_head": is_get_or_head,
2312 "has_body": has_body,
2313 "body": processed_body.value,
2314 "body_is_dynamic": body_is_dynamic,
2315 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2316 })
2317 }).collect::<Vec<_>>(),
2318 })
2319 }).collect();
2320
2321 for flow_data in &flows_data {
2323 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2324 for step in steps {
2325 if let Some(placeholders_arr) =
2326 step.get("_placeholders").and_then(|p| p.as_array())
2327 {
2328 for p_str in placeholders_arr {
2329 if let Some(p_name) = p_str.as_str() {
2330 match p_name {
2332 "VU" => {
2333 all_placeholders.insert(DynamicPlaceholder::VU);
2334 }
2335 "Iteration" => {
2336 all_placeholders.insert(DynamicPlaceholder::Iteration);
2337 }
2338 "Timestamp" => {
2339 all_placeholders.insert(DynamicPlaceholder::Timestamp);
2340 }
2341 "UUID" => {
2342 all_placeholders.insert(DynamicPlaceholder::UUID);
2343 }
2344 "Random" => {
2345 all_placeholders.insert(DynamicPlaceholder::Random);
2346 }
2347 "Counter" => {
2348 all_placeholders.insert(DynamicPlaceholder::Counter);
2349 }
2350 "Date" => {
2351 all_placeholders.insert(DynamicPlaceholder::Date);
2352 }
2353 "VuIter" => {
2354 all_placeholders.insert(DynamicPlaceholder::VuIter);
2355 }
2356 _ => {}
2357 }
2358 }
2359 }
2360 }
2361 }
2362 }
2363 }
2364
2365 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2367 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2368
2369 let invalid_data_config = self.build_invalid_data_config();
2371 let error_injection_enabled = invalid_data_config.is_some();
2372 let error_rate = self.error_rate.unwrap_or(0.0);
2373 let error_types: Vec<String> = invalid_data_config
2374 .as_ref()
2375 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2376 .unwrap_or_default();
2377
2378 if error_injection_enabled {
2379 TerminalReporter::print_progress(&format!(
2380 "Error injection enabled ({}% rate)",
2381 (error_rate * 100.0) as u32
2382 ));
2383 }
2384
2385 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2387
2388 let data = serde_json::json!({
2389 "base_url": self.target,
2390 "flows": flows_data,
2391 "extract_fields": config.default_extract_fields,
2392 "duration_secs": duration_secs,
2393 "max_vus": self.vus,
2394 "auth_header": self.auth,
2395 "custom_headers": custom_headers,
2396 "skip_tls_verify": self.skip_tls_verify,
2397 "stages": stages.iter().map(|s| serde_json::json!({
2399 "duration": s.duration,
2400 "target": s.target,
2401 })).collect::<Vec<_>>(),
2402 "threshold_percentile": self.threshold_percentile,
2403 "threshold_ms": self.threshold_ms,
2404 "max_error_rate": self.max_error_rate,
2405 "abort_on_error": self.abort_on_error,
2406 "abort_on_error_rate": self.abort_on_error_rate,
2407 "headers": headers_json,
2408 "dynamic_imports": required_imports,
2409 "dynamic_globals": required_globals,
2410 "extracted_values_output_path": self
2411 .output
2412 .join("crud_flow_extracted_values.json")
2413 .to_string_lossy(),
2414 "error_injection_enabled": error_injection_enabled,
2416 "error_rate": error_rate,
2417 "error_types": error_types,
2418 "security_testing_enabled": security_testing_enabled,
2420 "has_custom_headers": !custom_headers.is_empty(),
2421 });
2422
2423 let mut script = handlebars
2424 .render_template(template, &data)
2425 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2426
2427 if security_testing_enabled {
2429 script = self.generate_enhanced_script(&script)?;
2430 }
2431
2432 TerminalReporter::print_progress("Validating CRUD flow script...");
2434 let validation_errors = K6ScriptGenerator::validate_script(&script);
2435 if !validation_errors.is_empty() {
2436 TerminalReporter::print_error("CRUD flow script validation failed");
2437 for error in &validation_errors {
2438 eprintln!(" {}", error);
2439 }
2440 return Err(BenchError::Other(format!(
2441 "CRUD flow script validation failed with {} error(s)",
2442 validation_errors.len()
2443 )));
2444 }
2445
2446 TerminalReporter::print_success("CRUD flow script generated");
2447
2448 let script_path = if let Some(output) = &self.script_output {
2450 output.clone()
2451 } else {
2452 self.output.join("k6-crud-flow-script.js")
2453 };
2454
2455 if let Some(parent) = script_path.parent() {
2456 std::fs::create_dir_all(parent)?;
2457 }
2458 std::fs::write(&script_path, &script)?;
2459 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2460
2461 if self.generate_only {
2462 println!("\nScript generated successfully. Run it with:");
2463 println!(" k6 run {}", script_path.display());
2464 return Ok(());
2465 }
2466
2467 TerminalReporter::print_progress("Executing CRUD flow test...");
2469 let executor = K6Executor::new()?
2470 .with_local_ips(self.source_ips.join(","))
2471 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2472 std::fs::create_dir_all(&self.output)?;
2473
2474 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2475
2476 let duration_secs = Self::parse_duration(&self.duration)?;
2477 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2478
2479 Ok(())
2480 }
2481
2482 async fn execute_conformance_test(&self) -> Result<()> {
2484 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2485 use crate::conformance::report::ConformanceReport;
2486 use crate::conformance::spec::ConformanceFeature;
2487
2488 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2489
2490 TerminalReporter::print_progress(
2493 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2494 );
2495
2496 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2498 cats_str
2499 .split(',')
2500 .filter_map(|s| {
2501 let trimmed = s.trim();
2502 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2503 Some(canonical.to_string())
2504 } else {
2505 TerminalReporter::print_warning(&format!(
2506 "Unknown conformance category: '{}'. Valid categories: {}",
2507 trimmed,
2508 ConformanceFeature::cli_category_names()
2509 .iter()
2510 .map(|(cli, _)| *cli)
2511 .collect::<Vec<_>>()
2512 .join(", ")
2513 ));
2514 None
2515 }
2516 })
2517 .collect::<Vec<String>>()
2518 });
2519
2520 let custom_headers: Vec<(String, String)> = self
2522 .conformance_headers
2523 .iter()
2524 .filter_map(|h| {
2525 let (name, value) = h.split_once(':')?;
2526 Some((name.trim().to_string(), value.trim().to_string()))
2527 })
2528 .collect();
2529
2530 if !custom_headers.is_empty() {
2531 TerminalReporter::print_progress(&format!(
2532 "Using {} custom header(s) for authentication",
2533 custom_headers.len()
2534 ));
2535 }
2536
2537 if self.conformance_delay_ms > 0 {
2538 TerminalReporter::print_progress(&format!(
2539 "Using {}ms delay between conformance requests",
2540 self.conformance_delay_ms
2541 ));
2542 }
2543
2544 std::fs::create_dir_all(&self.output)?;
2546
2547 let config = ConformanceConfig {
2548 target_url: self.target.clone(),
2549 api_key: self.conformance_api_key.clone(),
2550 basic_auth: self.conformance_basic_auth.clone(),
2551 skip_tls_verify: self.skip_tls_verify,
2552 categories,
2553 base_path: self.base_path.clone(),
2554 custom_headers,
2555 output_dir: Some(self.output.clone()),
2556 all_operations: self.conformance_all_operations,
2557 custom_checks_file: self.conformance_custom.clone(),
2558 request_delay_ms: self.conformance_delay_ms,
2559 custom_filter: self.conformance_custom_filter.clone(),
2560 export_requests: self.export_requests,
2561 validate_requests: self.validate_requests,
2562 };
2563
2564 let mut resolved_base_path: Option<String> = None;
2572 let annotated_ops = if !self.spec.is_empty() {
2573 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2574 let parser = SpecParser::from_file(&self.spec[0]).await?;
2575 resolved_base_path = self.resolve_base_path(&parser);
2576
2577 let mut operations = if let Some(filter) = &self.operations {
2582 parser.filter_operations(filter)?
2583 } else {
2584 parser.get_operations()
2585 };
2586 if let Some(exclude) = &self.exclude_operations {
2587 let before_count = operations.len();
2588 operations = parser.exclude_operations(operations, exclude)?;
2589 let excluded_count = before_count - operations.len();
2590 if excluded_count > 0 {
2591 TerminalReporter::print_progress(&format!(
2592 "Excluded {} operations matching '{}'",
2593 excluded_count, exclude
2594 ));
2595 }
2596 }
2597
2598 let annotated =
2599 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2600 &operations,
2601 parser.spec(),
2602 );
2603 TerminalReporter::print_success(&format!(
2604 "Analyzed {} operations, found {} feature annotations",
2605 operations.len(),
2606 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2607 ));
2608 Some(annotated)
2609 } else {
2610 None
2611 };
2612
2613 if self.conformance_self_test {
2620 let Some(ops) = annotated_ops else {
2621 TerminalReporter::print_error(
2622 "--conformance-self-test requires --spec; no operations to test",
2623 );
2624 return Ok(());
2625 };
2626 let cfg = crate::conformance::self_test::SelfTestConfig {
2627 target_url: self.target.clone(),
2628 skip_tls_verify: self.skip_tls_verify,
2629 timeout: std::time::Duration::from_secs(30),
2630 extra_headers: self
2634 .conformance_headers
2635 .iter()
2636 .filter_map(|h| {
2637 let (n, v) = h.split_once(':')?;
2638 Some((n.trim().to_string(), v.trim().to_string()))
2639 })
2640 .collect(),
2641 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2642 base_path: resolved_base_path.clone(),
2646 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2650 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2651 geo_source_headers: if self.geo_source_headers.is_empty() {
2652 crate::conformance::self_test::default_geo_source_headers()
2653 } else {
2654 self.geo_source_headers.clone()
2655 },
2656 capture: if self.conformance_self_test_capture
2660 || self.validate_response_schemas
2661 || self.validate_requests
2662 {
2663 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2674 } else {
2675 None
2676 },
2677 validate_response_schemas: self.validate_response_schemas,
2678 spec_label: self.spec.first().map(|p| {
2684 p.file_name()
2685 .map(|s| s.to_string_lossy().into_owned())
2686 .unwrap_or_else(|| p.to_string_lossy().into_owned())
2687 }),
2688 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2695 current_iteration: 1,
2696 };
2697 let capture_sink = cfg.capture.clone();
2698 let network_events_sink = cfg.network_events.clone();
2699 TerminalReporter::print_progress(&format!(
2700 "Self-test mode: driving {} operations with positive + per-category negative cases",
2701 ops.len()
2702 ));
2703 let target_iterations = self.conformance_self_test_iterations.max(1);
2710 let duration_budget = self
2711 .conformance_self_test_duration
2712 .as_ref()
2713 .map(|s| Self::parse_duration(s))
2714 .transpose()?
2715 .map(std::time::Duration::from_secs);
2716 let start = std::time::Instant::now();
2717 let deadline = duration_budget.map(|d| start + d);
2726 let mut cfg = cfg;
2730 cfg.current_iteration = 1;
2731 let mut report =
2732 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2733 .await
2734 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2735 let mut iter_done: u32 = 1;
2736 loop {
2737 let by_iter = iter_done >= target_iterations;
2738 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2739 if by_iter && by_dur {
2740 break;
2741 }
2742 cfg.current_iteration = iter_done.saturating_add(1);
2743 let next = crate::conformance::self_test::run_self_test_with_deadline(
2744 &ops, &cfg, deadline,
2745 )
2746 .await
2747 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2748 report.merge_iteration(next);
2749 iter_done = iter_done.saturating_add(1);
2750 }
2751 if iter_done > 1 {
2752 TerminalReporter::print_progress(&format!(
2753 "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2754 iter_done,
2755 start.elapsed(),
2756 ));
2757 }
2758 let per_endpoint_summary: Vec<
2768 crate::conformance::per_endpoint_summary::PerEndpointSummary,
2769 >;
2770 if let Some(sink) = capture_sink {
2771 if let Ok(guard) = sink.lock() {
2772 let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2773 let mut lines = String::with_capacity(guard.len() * 256);
2774 for entry in guard.iter() {
2775 if let Ok(line) = serde_json::to_string(entry) {
2776 lines.push_str(&line);
2777 lines.push('\n');
2778 }
2779 }
2780 let _ = std::fs::write(&jsonl_path, lines);
2781 let html_path = self.output.join("conformance-self-test-requests.html");
2782 let html =
2783 crate::conformance::capture_html::render_capture_html(guard.as_slice());
2784 let _ = std::fs::write(&html_path, html);
2785
2786 per_endpoint_summary =
2790 crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2791 let summary_path = self.output.join("conformance-per-endpoint.json");
2792 if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2793 let _ = std::fs::write(&summary_path, json);
2794 TerminalReporter::print_progress(&format!(
2795 "Self-test request/response capture written to {} ({} entries) + {} + {}",
2796 jsonl_path.display(),
2797 guard.len(),
2798 html_path.display(),
2799 summary_path.display(),
2800 ));
2801 } else {
2802 TerminalReporter::print_progress(&format!(
2803 "Self-test request/response capture written to {} ({} entries) + {}",
2804 jsonl_path.display(),
2805 guard.len(),
2806 html_path.display(),
2807 ));
2808 }
2809 } else {
2810 per_endpoint_summary = Vec::new();
2811 }
2812 } else {
2813 per_endpoint_summary = Vec::new();
2814 }
2815 TerminalReporter::print_progress(&report.render_summary());
2816 if let Some(sink) = network_events_sink {
2823 if let Ok(guard) = sink.lock() {
2824 let path = self.output.join("conformance-network-events.json");
2825 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
2826 let _ = std::fs::write(&path, json);
2827 if guard.is_empty() {
2828 TerminalReporter::print_progress(
2829 "No wire-level network failures during self-test (file written empty)",
2830 );
2831 } else {
2832 TerminalReporter::print_warning(&format!(
2833 "Recorded {} wire-level network event(s) to {}",
2834 guard.len(),
2835 path.display()
2836 ));
2837 }
2838 }
2839 }
2840 }
2841 let json_path = self.output.join("conformance-self-test.json");
2845 if let Ok(json) = serde_json::to_string_pretty(&report) {
2846 let _ = std::fs::write(&json_path, json);
2847 TerminalReporter::print_progress(&format!(
2848 "Self-test report written to {}",
2849 json_path.display()
2850 ));
2851 }
2852 let issues = report.definite_issues();
2856 let issues_path = self.output.join("conformance-definite-issues.json");
2857 if let Ok(json) = serde_json::to_string_pretty(&issues) {
2858 if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
2859 TerminalReporter::print_warning(&format!(
2860 "{} definite issue(s) — see {}",
2861 issues.len(),
2862 issues_path.display()
2863 ));
2864 }
2865 }
2866 let owasp_accepted = report.owasp_accepted_probes();
2869 if !owasp_accepted.is_empty() {
2870 let owasp_path = self.output.join("conformance-owasp-accepted.json");
2871 if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
2872 if std::fs::write(&owasp_path, json).is_ok() {
2873 TerminalReporter::print_warning(&format!(
2874 "{} owasp injection probe(s) accepted by the target — see {}",
2875 owasp_accepted.len(),
2876 owasp_path.display()
2877 ));
2878 }
2879 }
2880 }
2881 if let Some(status) = report.detect_target_misconfiguration() {
2890 let hint = match status {
2891 404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2892 401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2893 _ => "",
2894 };
2895 TerminalReporter::print_warning(&format!(
2896 "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2897 ));
2898 } else if !report.all_passed() {
2899 TerminalReporter::print_warning(
2900 "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2901 );
2902 } else {
2903 TerminalReporter::print_success(
2904 "Self-test passed — all positive cases accepted and all negative cases rejected",
2905 );
2906 }
2907 let html_path = self.output.join("conformance-report.html");
2914 let audit_path = self.output.join("conformance-spec-audit.json");
2915 let audit_value = std::fs::read_to_string(&audit_path)
2916 .ok()
2917 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2918 let render_opts = crate::conformance::report_html::RenderOptions {
2923 missed_cap: match self.report_missed_cap {
2924 Some(0) => None,
2925 Some(n) => Some(n as usize),
2926 None => Some(200),
2927 },
2928 };
2929 let mut html = crate::conformance::report_html::render_html_with_options(
2930 &report,
2931 audit_value.as_ref(),
2932 &render_opts,
2933 );
2934 let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
2940 &per_endpoint_summary,
2941 );
2942 if !summary_section.is_empty() {
2943 if let Some(idx) = html.rfind("</body>") {
2944 html.insert_str(idx, &summary_section);
2945 } else {
2946 html.push_str(&summary_section);
2947 }
2948 }
2949 if std::fs::write(&html_path, html).is_ok() {
2950 TerminalReporter::print_progress(&format!(
2951 "HTML report written to {}",
2952 html_path.display()
2953 ));
2954 }
2955
2956 if self.validate_requests && !self.spec.is_empty() {
2968 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2969 &self.spec,
2970 &self.output,
2971 self.base_path.as_deref(),
2972 )
2973 .await?;
2974 if n > 0 {
2975 TerminalReporter::print_warning(&format!(
2976 "{} emitted request(s) recorded against the spec — see conformance-request-violations.json",
2977 n
2978 ));
2979 }
2980 }
2981 return Ok(());
2982 }
2983
2984 if self.validate_requests && !self.spec.is_empty() {
2986 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2987 let violation_count = crate::conformance::request_validator::run_request_validation(
2988 &self.spec,
2989 self.conformance_custom.as_deref(),
2990 self.base_path.as_deref(),
2991 &self.output,
2992 )
2993 .await?;
2994 if violation_count > 0 {
2995 TerminalReporter::print_warning(&format!(
2996 "{} request validation violation(s) found — see conformance-request-violations.json",
2997 violation_count
2998 ));
2999 } else {
3000 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
3001 }
3002 }
3003
3004 if self.generate_only || self.use_k6 {
3006 let script = if let Some(annotated) = &annotated_ops {
3007 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3008 config,
3009 annotated.clone(),
3010 );
3011 let op_count = gen.operation_count();
3012 let (script, check_count) = gen.generate()?;
3013 TerminalReporter::print_success(&format!(
3014 "Conformance: {} operations analyzed, {} unique checks generated",
3015 op_count, check_count
3016 ));
3017 script
3018 } else {
3019 let generator = ConformanceGenerator::new(config);
3020 generator.generate()?
3021 };
3022
3023 let script_path = self.output.join("k6-conformance.js");
3024 std::fs::write(&script_path, &script).map_err(|e| {
3025 BenchError::Other(format!("Failed to write conformance script: {}", e))
3026 })?;
3027 TerminalReporter::print_success(&format!(
3028 "Conformance script generated: {}",
3029 script_path.display()
3030 ));
3031
3032 if self.generate_only {
3033 println!("\nScript generated. Run with:");
3034 println!(" k6 run {}", script_path.display());
3035 return Ok(());
3036 }
3037
3038 if !K6Executor::is_k6_installed() {
3040 TerminalReporter::print_error("k6 is not installed");
3041 TerminalReporter::print_warning(
3042 "Install k6 from: https://k6.io/docs/get-started/installation/",
3043 );
3044 return Err(BenchError::K6NotFound);
3045 }
3046
3047 TerminalReporter::print_progress("Running conformance tests via k6...");
3048 let executor = K6Executor::new()?
3049 .with_local_ips(self.source_ips.join(","))
3050 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3051 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3052
3053 let report_path = self.output.join("conformance-report.json");
3054 if report_path.exists() {
3055 let report = ConformanceReport::from_file(&report_path)?;
3056 report.print_report_with_options(self.conformance_all_operations);
3057 self.save_conformance_report(&report, &report_path)?;
3058 } else {
3059 TerminalReporter::print_warning(
3060 "Conformance report not generated (k6 handleSummary may not have run)",
3061 );
3062 }
3063
3064 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3076 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3077 &self.spec,
3078 &self.output,
3079 self.base_path.as_deref(),
3080 )
3081 .await?;
3082 if n > 0 {
3083 TerminalReporter::print_warning(&format!(
3084 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3085 n
3086 ));
3087 }
3088 }
3089
3090 return Ok(());
3091 }
3092
3093 TerminalReporter::print_progress("Running conformance tests (native executor)...");
3095
3096 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3097
3098 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3108 executor = if let Some(annotated) = &annotated_ops {
3109 executor.with_spec_driven_checks(annotated)
3110 } else if custom_only {
3111 executor
3112 } else {
3113 executor.with_reference_checks()
3114 };
3115 executor = executor.with_custom_checks()?;
3116
3117 TerminalReporter::print_success(&format!(
3118 "Executing {} conformance checks...",
3119 executor.check_count()
3120 ));
3121
3122 let report = executor.execute().await?;
3123 report.print_report_with_options(self.conformance_all_operations);
3124
3125 let failure_details = report.failure_details();
3127 if !failure_details.is_empty() {
3128 let details_path = self.output.join("conformance-failure-details.json");
3129 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3130 let _ = std::fs::write(&details_path, json);
3131 TerminalReporter::print_success(&format!(
3132 "Failure details saved to: {}",
3133 details_path.display()
3134 ));
3135 }
3136 }
3137
3138 let report_path = self.output.join("conformance-report.json");
3140 let report_json = serde_json::to_string_pretty(&report.to_json())
3141 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3142 std::fs::write(&report_path, &report_json)
3143 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3144 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3145
3146 self.save_conformance_report(&report, &report_path)?;
3147
3148 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3159 let n =
3160 crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3161 &self.spec,
3162 &self.output,
3163 self.base_path.as_deref(),
3164 )
3165 .await?;
3166 if n > 0 {
3167 TerminalReporter::print_warning(&format!(
3168 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3169 n
3170 ));
3171 }
3172 }
3173
3174 Ok(())
3175 }
3176
3177 fn save_conformance_report(
3179 &self,
3180 report: &crate::conformance::report::ConformanceReport,
3181 report_path: &Path,
3182 ) -> Result<()> {
3183 if self.conformance_report_format == "sarif" {
3184 use crate::conformance::sarif::ConformanceSarifReport;
3185 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3186 TerminalReporter::print_success(&format!(
3187 "SARIF report saved to: {}",
3188 self.conformance_report.display()
3189 ));
3190 } else if self.conformance_report != *report_path {
3191 std::fs::copy(report_path, &self.conformance_report)?;
3192 TerminalReporter::print_success(&format!(
3193 "Report saved to: {}",
3194 self.conformance_report.display()
3195 ));
3196 }
3197 Ok(())
3198 }
3199
3200 async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3212 use crate::conformance::self_test::SelfTestConfig;
3213
3214 TerminalReporter::print_progress("Multi-target conformance self-test mode");
3215 let targets = parse_targets_file(targets_file)?;
3216 if targets.is_empty() {
3217 return Err(BenchError::Other("No targets found in file".to_string()));
3218 }
3219 TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3220
3221 let annotated_ops = if !self.spec.is_empty() {
3223 let parser = SpecParser::from_file(&self.spec[0]).await?;
3224 let operations = parser.get_operations();
3225 Some(
3226 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3227 &operations,
3228 parser.spec(),
3229 ),
3230 )
3231 } else {
3232 return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3233 };
3234 let Some(ops) = annotated_ops else {
3235 unreachable!()
3236 };
3237
3238 std::fs::create_dir_all(&self.output)?;
3239 let resolved_base_path = self.base_path.clone();
3240 let target_iterations = self.conformance_self_test_iterations.max(1);
3241 let duration_budget = self
3242 .conformance_self_test_duration
3243 .as_ref()
3244 .map(|s| Self::parse_duration(s))
3245 .transpose()?
3246 .map(std::time::Duration::from_secs);
3247
3248 for (idx, target) in targets.iter().enumerate() {
3249 let target_dir = self.output.join(format!("target_{}", idx));
3250 std::fs::create_dir_all(&target_dir)?;
3251 TerminalReporter::print_progress(&format!(
3252 "[target {}/{}] {}",
3253 idx + 1,
3254 targets.len(),
3255 target.url
3256 ));
3257
3258 let merged_headers: Vec<(String, String)> = self
3259 .conformance_headers
3260 .iter()
3261 .filter_map(|h| {
3262 let (n, v) = h.split_once(':')?;
3263 Some((n.trim().to_string(), v.trim().to_string()))
3264 })
3265 .collect();
3266
3267 let cfg = SelfTestConfig {
3268 target_url: target.url.clone(),
3269 skip_tls_verify: self.skip_tls_verify,
3270 timeout: std::time::Duration::from_secs(30),
3271 extra_headers: merged_headers,
3272 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3273 base_path: resolved_base_path.clone(),
3274 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3275 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3276 geo_source_headers: if self.geo_source_headers.is_empty() {
3277 crate::conformance::self_test::default_geo_source_headers()
3278 } else {
3279 self.geo_source_headers.clone()
3280 },
3281 capture: if self.conformance_self_test_capture
3282 || self.validate_response_schemas
3283 || self.validate_requests
3284 {
3285 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3289 } else {
3290 None
3291 },
3292 validate_response_schemas: self.validate_response_schemas,
3293 spec_label: self.spec.first().map(|p| {
3294 p.file_name()
3295 .map(|s| s.to_string_lossy().into_owned())
3296 .unwrap_or_else(|| p.to_string_lossy().into_owned())
3297 }),
3298 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3299 current_iteration: 1,
3300 };
3301 let capture_sink = cfg.capture.clone();
3302 let network_events_sink = cfg.network_events.clone();
3303
3304 let start = std::time::Instant::now();
3305 let deadline = duration_budget.map(|d| start + d);
3309 let mut cfg = cfg;
3313 cfg.current_iteration = 1;
3314 let mut report =
3315 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3316 .await
3317 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3318 let mut iter_done: u32 = 1;
3319 loop {
3320 let by_iter = iter_done >= target_iterations;
3321 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3322 if by_iter && by_dur {
3323 break;
3324 }
3325 cfg.current_iteration = iter_done.saturating_add(1);
3326 let next = crate::conformance::self_test::run_self_test_with_deadline(
3327 &ops, &cfg, deadline,
3328 )
3329 .await
3330 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3331 report.merge_iteration(next);
3332 iter_done = iter_done.saturating_add(1);
3333 }
3334 if iter_done > 1 {
3335 TerminalReporter::print_progress(&format!(
3336 " ran {} iteration(s) in {:.1?}",
3337 iter_done,
3338 start.elapsed(),
3339 ));
3340 }
3341
3342 if let Some(sink) = capture_sink {
3344 if let Ok(guard) = sink.lock() {
3345 let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3346 let mut lines = String::with_capacity(guard.len() * 256);
3347 for entry in guard.iter() {
3348 if let Ok(line) = serde_json::to_string(entry) {
3349 lines.push_str(&line);
3350 lines.push('\n');
3351 }
3352 }
3353 let _ = std::fs::write(&jsonl, lines);
3354 }
3355 }
3356 if let Some(sink) = network_events_sink {
3357 if let Ok(guard) = sink.lock() {
3358 let path = target_dir.join("conformance-network-events.json");
3359 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3360 let _ = std::fs::write(&path, json);
3361 if !guard.is_empty() {
3362 TerminalReporter::print_warning(&format!(
3363 " recorded {} wire-level network event(s)",
3364 guard.len()
3365 ));
3366 }
3367 }
3368 }
3369 }
3370
3371 let json_path = target_dir.join("conformance-self-test.json");
3372 if let Ok(json) = serde_json::to_string_pretty(&report) {
3373 let _ = std::fs::write(&json_path, json);
3374 }
3375 let issues = report.definite_issues();
3378 if let Ok(json) = serde_json::to_string_pretty(&issues) {
3379 let issues_path = target_dir.join("conformance-definite-issues.json");
3380 if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
3381 TerminalReporter::print_warning(&format!(
3382 " {} definite issue(s) — see {}",
3383 issues.len(),
3384 issues_path.display()
3385 ));
3386 }
3387 }
3388 let owasp_accepted = report.owasp_accepted_probes();
3390 if !owasp_accepted.is_empty() {
3391 if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
3392 let owasp_path = target_dir.join("conformance-owasp-accepted.json");
3393 if std::fs::write(&owasp_path, json).is_ok() {
3394 TerminalReporter::print_warning(&format!(
3395 " {} owasp injection probe(s) accepted by the target — see {}",
3396 owasp_accepted.len(),
3397 owasp_path.display()
3398 ));
3399 }
3400 }
3401 }
3402 TerminalReporter::print_progress(&report.render_summary());
3403
3404 if self.validate_requests {
3413 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3414 &self.spec,
3415 &target_dir,
3416 self.base_path.as_deref(),
3417 )
3418 .await?;
3419 if n > 0 {
3420 TerminalReporter::print_warning(&format!(
3421 " {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3422 n,
3423 target_dir.display(),
3424 ));
3425 }
3426 }
3427 }
3428
3429 Ok(())
3430 }
3431
3432 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3438 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3439 use crate::conformance::report::ConformanceReport;
3440 use crate::conformance::spec::ConformanceFeature;
3441
3442 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3443
3444 TerminalReporter::print_progress("Parsing targets file...");
3446 let targets = parse_targets_file(targets_file)?;
3447 let num_targets = targets.len();
3448 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3449
3450 if targets.is_empty() {
3451 return Err(BenchError::Other("No targets found in file".to_string()));
3452 }
3453
3454 TerminalReporter::print_progress(
3455 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
3456 );
3457
3458 let categories = self.conformance_categories.as_ref().map(|cats_str| {
3460 cats_str
3461 .split(',')
3462 .filter_map(|s| {
3463 let trimmed = s.trim();
3464 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3465 Some(canonical.to_string())
3466 } else {
3467 TerminalReporter::print_warning(&format!(
3468 "Unknown conformance category: '{}'. Valid categories: {}",
3469 trimmed,
3470 ConformanceFeature::cli_category_names()
3471 .iter()
3472 .map(|(cli, _)| *cli)
3473 .collect::<Vec<_>>()
3474 .join(", ")
3475 ));
3476 None
3477 }
3478 })
3479 .collect::<Vec<String>>()
3480 });
3481
3482 let base_custom_headers: Vec<(String, String)> = self
3484 .conformance_headers
3485 .iter()
3486 .filter_map(|h| {
3487 let (name, value) = h.split_once(':')?;
3488 Some((name.trim().to_string(), value.trim().to_string()))
3489 })
3490 .collect();
3491
3492 if !base_custom_headers.is_empty() {
3493 TerminalReporter::print_progress(&format!(
3494 "Using {} base custom header(s) for authentication",
3495 base_custom_headers.len()
3496 ));
3497 }
3498
3499 let annotated_ops = if !self.spec.is_empty() {
3501 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3502 let parser = SpecParser::from_file(&self.spec[0]).await?;
3503 let operations = parser.get_operations();
3504 let annotated =
3505 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3506 &operations,
3507 parser.spec(),
3508 );
3509 TerminalReporter::print_success(&format!(
3510 "Analyzed {} operations, found {} feature annotations",
3511 operations.len(),
3512 annotated.iter().map(|a| a.features.len()).sum::<usize>()
3513 ));
3514 Some(annotated)
3515 } else {
3516 None
3517 };
3518
3519 std::fs::create_dir_all(&self.output)?;
3521
3522 struct TargetResult {
3524 url: String,
3525 passed: usize,
3526 failed: usize,
3527 elapsed: std::time::Duration,
3528 report_json: serde_json::Value,
3529 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3530 }
3531
3532 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3533 let total_start = std::time::Instant::now();
3534
3535 for (idx, target) in targets.iter().enumerate() {
3536 tracing::info!(
3537 "Running conformance tests against target {}/{}: {}",
3538 idx + 1,
3539 num_targets,
3540 target.url
3541 );
3542 TerminalReporter::print_progress(&format!(
3543 "\n--- Target {}/{}: {} ---",
3544 idx + 1,
3545 num_targets,
3546 target.url
3547 ));
3548
3549 let mut merged_headers = base_custom_headers.clone();
3551 if let Some(ref target_headers) = target.headers {
3552 for (name, value) in target_headers {
3553 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3555 existing.1 = value.clone();
3556 } else {
3557 merged_headers.push((name.clone(), value.clone()));
3558 }
3559 }
3560 }
3561 if let Some(ref auth) = target.auth {
3563 if let Some(existing) =
3564 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3565 {
3566 existing.1 = auth.clone();
3567 } else {
3568 merged_headers.push(("Authorization".to_string(), auth.clone()));
3569 }
3570 }
3571
3572 let target_dir = self.output.join(format!("target_{}", idx));
3578 std::fs::create_dir_all(&target_dir)?;
3579
3580 let config = ConformanceConfig {
3581 target_url: target.url.clone(),
3582 api_key: self.conformance_api_key.clone(),
3583 basic_auth: self.conformance_basic_auth.clone(),
3584 skip_tls_verify: self.skip_tls_verify,
3585 categories: categories.clone(),
3586 base_path: self.base_path.clone(),
3587 custom_headers: merged_headers,
3588 output_dir: Some(target_dir.clone()),
3589 all_operations: self.conformance_all_operations,
3590 custom_checks_file: self.conformance_custom.clone(),
3591 request_delay_ms: self.conformance_delay_ms,
3592 custom_filter: self.conformance_custom_filter.clone(),
3593 export_requests: self.export_requests,
3594 validate_requests: self.validate_requests,
3595 };
3596
3597 let target_start = std::time::Instant::now();
3598 let report = if self.use_k6 {
3599 if !K6Executor::is_k6_installed() {
3600 TerminalReporter::print_error("k6 is not installed");
3601 TerminalReporter::print_warning(
3602 "Install k6 from: https://k6.io/docs/get-started/installation/",
3603 );
3604 return Err(BenchError::K6NotFound);
3605 }
3606
3607 let script = if let Some(ref annotated) = annotated_ops {
3608 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3609 config.clone(),
3610 annotated.clone(),
3611 );
3612 let (script, _check_count) = gen.generate()?;
3613 script
3614 } else {
3615 let generator = ConformanceGenerator::new(config.clone());
3616 generator.generate()?
3617 };
3618
3619 let script_path = target_dir.join("k6-conformance.js");
3620 std::fs::write(&script_path, &script).map_err(|e| {
3621 BenchError::Other(format!("Failed to write conformance script: {}", e))
3622 })?;
3623 TerminalReporter::print_success(&format!(
3624 "Conformance script generated: {}",
3625 script_path.display()
3626 ));
3627
3628 TerminalReporter::print_progress(&format!(
3629 "Running conformance tests via k6 against {}...",
3630 target.url
3631 ));
3632 let k6 = K6Executor::new()?
3633 .with_local_ips(self.source_ips.join(","))
3634 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3635 let api_port = 6565u16.saturating_add(idx as u16);
3637 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3638 .await?;
3639
3640 let report_path = target_dir.join("conformance-report.json");
3641 if report_path.exists() {
3642 ConformanceReport::from_file(&report_path)?
3643 } else {
3644 TerminalReporter::print_warning(&format!(
3645 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3646 target.url
3647 ));
3648 continue;
3649 }
3650 } else {
3651 let mut executor =
3652 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3653
3654 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3657 executor = if let Some(ref annotated) = annotated_ops {
3658 executor.with_spec_driven_checks(annotated)
3659 } else if custom_only {
3660 executor
3661 } else {
3662 executor.with_reference_checks()
3663 };
3664 executor = executor.with_custom_checks()?;
3665
3666 TerminalReporter::print_success(&format!(
3667 "Executing {} conformance checks against {}...",
3668 executor.check_count(),
3669 target.url
3670 ));
3671
3672 executor.execute().await?
3673 };
3674 let target_elapsed = target_start.elapsed();
3675
3676 let report_json = report.to_json();
3677
3678 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3680 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3681 let total_checks = passed + failed;
3682 let rate = if total_checks == 0 {
3683 0.0
3684 } else {
3685 (passed as f64 / total_checks as f64) * 100.0
3686 };
3687
3688 TerminalReporter::print_success(&format!(
3689 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3690 target.url,
3691 passed,
3692 total_checks,
3693 rate,
3694 target_elapsed.as_secs_f64()
3695 ));
3696
3697 let target_report_path = target_dir.join("conformance-report.json");
3699 let report_str = serde_json::to_string_pretty(&report_json)
3700 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3701 std::fs::write(&target_report_path, &report_str)
3702 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3703
3704 let failure_details = report.failure_details();
3706 if !failure_details.is_empty() {
3707 let details_path = target_dir.join("conformance-failure-details.json");
3708 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3709 let _ = std::fs::write(&details_path, json);
3710 }
3711 }
3712
3713 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3720 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3721 &self.spec,
3722 &target_dir,
3723 self.base_path.as_deref(),
3724 )
3725 .await?;
3726 if n > 0 {
3727 TerminalReporter::print_warning(&format!(
3728 "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3729 target.url,
3730 n,
3731 target_dir.display()
3732 ));
3733 }
3734 }
3735
3736 let owasp_coverage = report.owasp_coverage_data();
3738
3739 target_results.push(TargetResult {
3740 url: target.url.clone(),
3741 passed,
3742 failed,
3743 elapsed: target_elapsed,
3744 report_json,
3745 owasp_coverage,
3746 });
3747 }
3748
3749 let total_elapsed = total_start.elapsed();
3750
3751 println!("\n{}", "=".repeat(80));
3753 println!(" Multi-Target Conformance Summary");
3754 println!("{}", "=".repeat(80));
3755 println!(
3756 " {:<40} {:>8} {:>8} {:>8} {:>8}",
3757 "Target URL", "Passed", "Failed", "Rate", "Time"
3758 );
3759 println!(" {}", "-".repeat(76));
3760
3761 let mut total_passed = 0usize;
3762 let mut total_failed = 0usize;
3763
3764 for result in &target_results {
3765 let total_checks = result.passed + result.failed;
3766 let rate = if total_checks == 0 {
3767 0.0
3768 } else {
3769 (result.passed as f64 / total_checks as f64) * 100.0
3770 };
3771
3772 let display_url = if result.url.len() > 38 {
3774 format!("{}...", &result.url[..35])
3775 } else {
3776 result.url.clone()
3777 };
3778
3779 println!(
3780 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3781 display_url,
3782 result.passed,
3783 result.failed,
3784 rate,
3785 result.elapsed.as_secs_f64()
3786 );
3787
3788 total_passed += result.passed;
3789 total_failed += result.failed;
3790 }
3791
3792 let grand_total = total_passed + total_failed;
3793 let overall_rate = if grand_total == 0 {
3794 0.0
3795 } else {
3796 (total_passed as f64 / grand_total as f64) * 100.0
3797 };
3798
3799 println!(" {}", "-".repeat(76));
3800 println!(
3801 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3802 format!("TOTAL ({} targets)", num_targets),
3803 total_passed,
3804 total_failed,
3805 overall_rate,
3806 total_elapsed.as_secs_f64()
3807 );
3808 println!("{}", "=".repeat(80));
3809
3810 for result in &target_results {
3812 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
3813 for entry in &result.owasp_coverage {
3814 let status = if !entry.tested {
3815 "-"
3816 } else if entry.all_passed {
3817 "pass"
3818 } else {
3819 "FAIL"
3820 };
3821 let via = if entry.via_categories.is_empty() {
3822 String::new()
3823 } else {
3824 format!(" (via {})", entry.via_categories.join(", "))
3825 };
3826 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3827 }
3828 }
3829
3830 let per_target_summaries: Vec<serde_json::Value> = target_results
3832 .iter()
3833 .enumerate()
3834 .map(|(idx, r)| {
3835 let total_checks = r.passed + r.failed;
3836 let rate = if total_checks == 0 {
3837 0.0
3838 } else {
3839 (r.passed as f64 / total_checks as f64) * 100.0
3840 };
3841 let owasp_json: Vec<serde_json::Value> = r
3842 .owasp_coverage
3843 .iter()
3844 .map(|e| {
3845 serde_json::json!({
3846 "id": e.id,
3847 "name": e.name,
3848 "tested": e.tested,
3849 "all_passed": e.all_passed,
3850 "via_categories": e.via_categories,
3851 })
3852 })
3853 .collect();
3854 serde_json::json!({
3855 "target_url": r.url,
3856 "target_index": idx,
3857 "checks_passed": r.passed,
3858 "checks_failed": r.failed,
3859 "total_checks": total_checks,
3860 "pass_rate": rate,
3861 "elapsed_seconds": r.elapsed.as_secs_f64(),
3862 "report": r.report_json,
3863 "owasp_coverage": owasp_json,
3864 })
3865 })
3866 .collect();
3867
3868 let combined_summary = serde_json::json!({
3869 "total_targets": num_targets,
3870 "total_checks_passed": total_passed,
3871 "total_checks_failed": total_failed,
3872 "overall_pass_rate": overall_rate,
3873 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3874 "targets": per_target_summaries,
3875 });
3876
3877 let summary_path = self.output.join("multi-target-conformance-summary.json");
3878 let summary_str = serde_json::to_string_pretty(&combined_summary)
3879 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3880 std::fs::write(&summary_path, &summary_str)
3881 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3882 TerminalReporter::print_success(&format!(
3883 "Combined summary saved to: {}",
3884 summary_path.display()
3885 ));
3886
3887 Ok(())
3888 }
3889
3890 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3892 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3893
3894 let custom_headers = self.parse_headers()?;
3896
3897 let mut config = OwaspApiConfig::new()
3899 .with_auth_header(&self.owasp_auth_header)
3900 .with_verbose(self.verbose)
3901 .with_insecure(self.skip_tls_verify)
3902 .with_concurrency(self.vus as usize)
3903 .with_iterations(self.owasp_iterations as usize)
3904 .with_base_path(self.base_path.clone())
3905 .with_custom_headers(custom_headers);
3906
3907 if let Some(ref token) = self.owasp_auth_token {
3909 config = config.with_valid_auth_token(token);
3910 }
3911
3912 if let Some(ref cats_str) = self.owasp_categories {
3914 let categories: Vec<OwaspCategory> = cats_str
3915 .split(',')
3916 .filter_map(|s| {
3917 let trimmed = s.trim();
3918 match trimmed.parse::<OwaspCategory>() {
3919 Ok(cat) => Some(cat),
3920 Err(e) => {
3921 TerminalReporter::print_warning(&e);
3922 None
3923 }
3924 }
3925 })
3926 .collect();
3927
3928 if !categories.is_empty() {
3929 config = config.with_categories(categories);
3930 }
3931 }
3932
3933 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3935 config.admin_paths_file = Some(admin_paths_file.clone());
3936 if let Err(e) = config.load_admin_paths() {
3937 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3938 }
3939 }
3940
3941 if let Some(ref id_fields_str) = self.owasp_id_fields {
3943 let id_fields: Vec<String> = id_fields_str
3944 .split(',')
3945 .map(|s| s.trim().to_string())
3946 .filter(|s| !s.is_empty())
3947 .collect();
3948 if !id_fields.is_empty() {
3949 config = config.with_id_fields(id_fields);
3950 }
3951 }
3952
3953 if let Some(ref report_path) = self.owasp_report {
3955 config = config.with_report_path(report_path);
3956 }
3957 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3958 config = config.with_report_format(format);
3959 }
3960
3961 let categories = config.categories_to_test();
3963 TerminalReporter::print_success(&format!(
3964 "Testing {} OWASP categories: {}",
3965 categories.len(),
3966 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3967 ));
3968
3969 if config.valid_auth_token.is_some() {
3970 TerminalReporter::print_progress("Using provided auth token for baseline requests");
3971 }
3972
3973 TerminalReporter::print_progress("Generating OWASP security test script...");
3975 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3976
3977 let script = generator.generate()?;
3979 TerminalReporter::print_success("OWASP security test script generated");
3980
3981 let script_path = if let Some(output) = &self.script_output {
3983 output.clone()
3984 } else {
3985 self.output.join("k6-owasp-security-test.js")
3986 };
3987
3988 if let Some(parent) = script_path.parent() {
3989 std::fs::create_dir_all(parent)?;
3990 }
3991 std::fs::write(&script_path, &script)?;
3992 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3993
3994 if self.generate_only {
3996 println!("\nOWASP security test script generated. Run it with:");
3997 println!(" k6 run {}", script_path.display());
3998 return Ok(());
3999 }
4000
4001 TerminalReporter::print_progress("Executing OWASP security tests...");
4003 let executor = K6Executor::new()?
4004 .with_local_ips(self.source_ips.join(","))
4005 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
4006 std::fs::create_dir_all(&self.output)?;
4007
4008 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
4009
4010 let duration_secs = Self::parse_duration(&self.duration)?;
4011 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
4012
4013 println!("\nOWASP security test results saved to: {}", self.output.display());
4014
4015 Ok(())
4016 }
4017}
4018
4019#[cfg(test)]
4020mod tests {
4021 use super::*;
4022 use tempfile::tempdir;
4023
4024 #[test]
4025 fn test_parse_duration() {
4026 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
4027 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
4028 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
4029 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
4030 }
4031
4032 #[test]
4036 fn parse_ip_list_ipv4_range_inclusive() {
4037 let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
4038 assert_eq!(v.len(), 23);
4039 assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
4040 assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
4041 }
4042
4043 #[test]
4046 fn parse_ip_list_range_rejects_backwards() {
4047 let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
4048 assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
4049 }
4050
4051 #[test]
4055 fn parse_ip_list_rejects_ipv6_range_syntax() {
4056 let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
4057 assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
4058 }
4059
4060 #[test]
4062 fn parse_ip_list_range_capped_at_256() {
4063 let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
4064 assert_eq!(v.len(), 256);
4065 assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
4066 }
4067
4068 #[test]
4071 fn parse_ip_list_plain_and_comma() {
4072 let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
4073 assert_eq!(v.len(), 3);
4074 assert_eq!(v[0].to_string(), "10.0.0.5");
4075 assert_eq!(v[2].to_string(), "10.0.0.7");
4076 }
4077
4078 #[test]
4081 fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
4082 let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
4083 assert_eq!(v.len(), 8);
4084 assert_eq!(v[0].to_string(), "10.0.0.0");
4085 assert_eq!(v[7].to_string(), "10.0.0.7");
4086 }
4087
4088 #[test]
4091 fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
4092 let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
4093 assert_eq!(v.len(), 256);
4094 assert_eq!(v[0].to_string(), "10.0.0.0");
4095 assert_eq!(v[255].to_string(), "10.0.0.255");
4096 }
4097
4098 #[test]
4100 fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
4101 let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
4102 assert_eq!(v.len(), 4);
4103 assert!(v[0].is_ipv6());
4104 assert_eq!(v[0].to_string(), "2001:db8::");
4105 assert_eq!(v[3].to_string(), "2001:db8::3");
4106 }
4107
4108 #[test]
4110 fn parse_ip_list_mixed_v4_v6_cidr() {
4111 let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
4112 assert_eq!(v.len(), 6); assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
4114 assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
4115 }
4116
4117 #[test]
4120 fn parse_ip_list_skips_malformed() {
4121 let v = parse_ip_list(
4122 &[
4123 "10.0.0.5".into(),
4124 "not-an-ip".into(),
4125 "10.0.0.6".into(),
4126 "/24".into(),
4127 "1.2.3.4/200".into(),
4128 ],
4129 "source-ip",
4130 );
4131 assert_eq!(v.len(), 2);
4132 assert_eq!(v[0].to_string(), "10.0.0.5");
4133 assert_eq!(v[1].to_string(), "10.0.0.6");
4134 }
4135
4136 #[test]
4137 fn test_parse_duration_invalid() {
4138 assert!(BenchCommand::parse_duration("invalid").is_err());
4139 assert!(BenchCommand::parse_duration("30x").is_err());
4140 }
4141
4142 #[test]
4143 fn test_parse_headers() {
4144 let cmd = BenchCommand {
4145 spec: vec![PathBuf::from("test.yaml")],
4146 spec_dir: None,
4147 merge_conflicts: "error".to_string(),
4148 spec_mode: "merge".to_string(),
4149 dependency_config: None,
4150 target: "http://localhost".to_string(),
4151 base_path: None,
4152 duration: "1m".to_string(),
4153 vus: 10,
4154 scenario: "ramp-up".to_string(),
4155 operations: None,
4156 exclude_operations: None,
4157 auth: None,
4158 headers: vec![
4159 "X-API-Key:test123".to_string(),
4160 "X-Client-ID:client456".to_string(),
4161 ],
4162 output: PathBuf::from("output"),
4163 generate_only: false,
4164 script_output: None,
4165 threshold_percentile: "p(95)".to_string(),
4166 threshold_ms: 500,
4167 max_error_rate: 0.05,
4168 abort_on_error: true,
4169 abort_on_error_rate: 0.95,
4170 verbose: false,
4171 skip_tls_verify: false,
4172 chunked_request_bodies: false,
4173 target_rps: None,
4174 no_keep_alive: false,
4175 targets_file: None,
4176 max_concurrency: None,
4177 results_format: "both".to_string(),
4178 params_file: None,
4179 crud_flow: false,
4180 flow_config: None,
4181 extract_fields: None,
4182 parallel_create: None,
4183 data_file: None,
4184 data_distribution: "unique-per-vu".to_string(),
4185 data_mappings: None,
4186 per_uri_control: false,
4187 error_rate: None,
4188 error_types: None,
4189 security_test: false,
4190 security_payloads: None,
4191 security_categories: None,
4192 security_target_fields: None,
4193 wafbench_dir: None,
4194 wafbench_cycle_all: false,
4195 owasp_api_top10: false,
4196 owasp_categories: None,
4197 owasp_auth_header: "Authorization".to_string(),
4198 owasp_auth_token: None,
4199 owasp_admin_paths: None,
4200 owasp_id_fields: None,
4201 owasp_report: None,
4202 owasp_report_format: "json".to_string(),
4203 owasp_iterations: 1,
4204 conformance: false,
4205 conformance_api_key: None,
4206 conformance_basic_auth: None,
4207 conformance_report: PathBuf::from("conformance-report.json"),
4208 conformance_categories: None,
4209 conformance_report_format: "json".to_string(),
4210 conformance_headers: vec![],
4211 conformance_all_operations: false,
4212 conformance_custom: None,
4213 conformance_delay_ms: 0,
4214 use_k6: false,
4215 conformance_custom_filter: None,
4216 export_requests: false,
4217 validate_requests: false,
4218 conformance_self_test: false,
4219 conformance_self_test_capture: false,
4220 conformance_self_test_iterations: 1,
4221 conformance_self_test_duration: None,
4222 validate_response_schemas: false,
4223 source_ips: Vec::new(),
4224 geo_source_ips: Vec::new(),
4225 geo_source_headers: Vec::new(),
4226 report_missed_cap: None,
4227 discard_response_bodies: false,
4228 dns_policy: None,
4229 };
4230
4231 let headers = cmd.parse_headers().unwrap();
4232 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4233 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4234 }
4235
4236 #[test]
4237 fn test_parse_header_string_preserves_comma_in_value() {
4238 let inputs = vec![
4241 "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4242 "X-Trace:1".to_string(),
4243 ];
4244 let headers = parse_header_string(&inputs).unwrap();
4245 assert_eq!(
4246 headers.get("Cookie"),
4247 Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4248 );
4249 assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4250 }
4251
4252 #[test]
4253 fn test_get_spec_display_name() {
4254 let cmd = BenchCommand {
4255 spec: vec![PathBuf::from("test.yaml")],
4256 spec_dir: None,
4257 merge_conflicts: "error".to_string(),
4258 spec_mode: "merge".to_string(),
4259 dependency_config: None,
4260 target: "http://localhost".to_string(),
4261 base_path: None,
4262 duration: "1m".to_string(),
4263 vus: 10,
4264 scenario: "ramp-up".to_string(),
4265 operations: None,
4266 exclude_operations: None,
4267 auth: None,
4268 headers: Vec::new(),
4269 output: PathBuf::from("output"),
4270 generate_only: false,
4271 script_output: None,
4272 threshold_percentile: "p(95)".to_string(),
4273 threshold_ms: 500,
4274 max_error_rate: 0.05,
4275 abort_on_error: true,
4276 abort_on_error_rate: 0.95,
4277 verbose: false,
4278 skip_tls_verify: false,
4279 chunked_request_bodies: false,
4280 target_rps: None,
4281 no_keep_alive: false,
4282 targets_file: None,
4283 max_concurrency: None,
4284 results_format: "both".to_string(),
4285 params_file: None,
4286 crud_flow: false,
4287 flow_config: None,
4288 extract_fields: None,
4289 parallel_create: None,
4290 data_file: None,
4291 data_distribution: "unique-per-vu".to_string(),
4292 data_mappings: None,
4293 per_uri_control: false,
4294 error_rate: None,
4295 error_types: None,
4296 security_test: false,
4297 security_payloads: None,
4298 security_categories: None,
4299 security_target_fields: None,
4300 wafbench_dir: None,
4301 wafbench_cycle_all: false,
4302 owasp_api_top10: false,
4303 owasp_categories: None,
4304 owasp_auth_header: "Authorization".to_string(),
4305 owasp_auth_token: None,
4306 owasp_admin_paths: None,
4307 owasp_id_fields: None,
4308 owasp_report: None,
4309 owasp_report_format: "json".to_string(),
4310 owasp_iterations: 1,
4311 conformance: false,
4312 conformance_api_key: None,
4313 conformance_basic_auth: None,
4314 conformance_report: PathBuf::from("conformance-report.json"),
4315 conformance_categories: None,
4316 conformance_report_format: "json".to_string(),
4317 conformance_headers: vec![],
4318 conformance_all_operations: false,
4319 conformance_custom: None,
4320 conformance_delay_ms: 0,
4321 use_k6: false,
4322 conformance_custom_filter: None,
4323 export_requests: false,
4324 validate_requests: false,
4325 conformance_self_test: false,
4326 conformance_self_test_capture: false,
4327 conformance_self_test_iterations: 1,
4328 conformance_self_test_duration: None,
4329 validate_response_schemas: false,
4330 source_ips: Vec::new(),
4331 geo_source_ips: Vec::new(),
4332 geo_source_headers: Vec::new(),
4333 report_missed_cap: None,
4334 discard_response_bodies: false,
4335 dns_policy: None,
4336 };
4337
4338 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4339
4340 let cmd_multi = BenchCommand {
4342 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4343 spec_dir: None,
4344 merge_conflicts: "error".to_string(),
4345 spec_mode: "merge".to_string(),
4346 dependency_config: None,
4347 target: "http://localhost".to_string(),
4348 base_path: None,
4349 duration: "1m".to_string(),
4350 vus: 10,
4351 scenario: "ramp-up".to_string(),
4352 operations: None,
4353 exclude_operations: None,
4354 auth: None,
4355 headers: Vec::new(),
4356 output: PathBuf::from("output"),
4357 generate_only: false,
4358 script_output: None,
4359 threshold_percentile: "p(95)".to_string(),
4360 threshold_ms: 500,
4361 max_error_rate: 0.05,
4362 abort_on_error: true,
4363 abort_on_error_rate: 0.95,
4364 verbose: false,
4365 skip_tls_verify: false,
4366 chunked_request_bodies: false,
4367 target_rps: None,
4368 no_keep_alive: false,
4369 targets_file: None,
4370 max_concurrency: None,
4371 results_format: "both".to_string(),
4372 params_file: None,
4373 crud_flow: false,
4374 flow_config: None,
4375 extract_fields: None,
4376 parallel_create: None,
4377 data_file: None,
4378 data_distribution: "unique-per-vu".to_string(),
4379 data_mappings: None,
4380 per_uri_control: false,
4381 error_rate: None,
4382 error_types: None,
4383 security_test: false,
4384 security_payloads: None,
4385 security_categories: None,
4386 security_target_fields: None,
4387 wafbench_dir: None,
4388 wafbench_cycle_all: false,
4389 owasp_api_top10: false,
4390 owasp_categories: None,
4391 owasp_auth_header: "Authorization".to_string(),
4392 owasp_auth_token: None,
4393 owasp_admin_paths: None,
4394 owasp_id_fields: None,
4395 owasp_report: None,
4396 owasp_report_format: "json".to_string(),
4397 owasp_iterations: 1,
4398 conformance: false,
4399 conformance_api_key: None,
4400 conformance_basic_auth: None,
4401 conformance_report: PathBuf::from("conformance-report.json"),
4402 conformance_categories: None,
4403 conformance_report_format: "json".to_string(),
4404 conformance_headers: vec![],
4405 conformance_all_operations: false,
4406 conformance_custom: None,
4407 conformance_delay_ms: 0,
4408 use_k6: false,
4409 conformance_custom_filter: None,
4410 export_requests: false,
4411 validate_requests: false,
4412 conformance_self_test: false,
4413 conformance_self_test_capture: false,
4414 conformance_self_test_iterations: 1,
4415 conformance_self_test_duration: None,
4416 validate_response_schemas: false,
4417 source_ips: Vec::new(),
4418 geo_source_ips: Vec::new(),
4419 geo_source_headers: Vec::new(),
4420 report_missed_cap: None,
4421 discard_response_bodies: false,
4422 dns_policy: None,
4423 };
4424
4425 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4426 }
4427
4428 #[test]
4429 fn test_parse_extracted_values_from_output_dir() {
4430 let dir = tempdir().unwrap();
4431 let path = dir.path().join("extracted_values.json");
4432 std::fs::write(
4433 &path,
4434 r#"{
4435 "pool_id": "abc123",
4436 "count": 0,
4437 "enabled": false,
4438 "metadata": { "owner": "team-a" }
4439}"#,
4440 )
4441 .unwrap();
4442
4443 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4444 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4445 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4446 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4447 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4448 }
4449
4450 #[test]
4451 fn test_parse_extracted_values_missing_file() {
4452 let dir = tempdir().unwrap();
4453 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4454 assert!(extracted.values.is_empty());
4455 }
4456}