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