1use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11 MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21 SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24 topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30 load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37pub fn parse_header_string(inputs: &[String]) -> Result<HashMap<String, String>> {
45 let mut headers = HashMap::new();
46
47 for pair in inputs {
48 let pair = pair.trim();
49 if pair.is_empty() {
50 continue;
51 }
52 let parts: Vec<&str> = pair.splitn(2, ':').collect();
53 if parts.len() != 2 {
54 return Err(BenchError::Other(format!(
55 "Invalid header format: '{}'. Expected 'Key:Value'",
56 pair
57 )));
58 }
59 headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
60 }
61
62 Ok(headers)
63}
64
65const CONFORMANCE_REPLACES_LOAD_ADVISORY: &str =
85 "Conformance mode REPLACES the load run: 1 VU, 1 iteration per endpoint. \
86 --vus, --rps and -d are ignored. Run bench a second time without \
87 --conformance if you also want a load test.";
88
89pub struct BenchCommand {
91 pub spec: Vec<PathBuf>,
93 pub spec_dir: Option<PathBuf>,
95 pub merge_conflicts: String,
97 pub spec_mode: String,
99 pub dependency_config: Option<PathBuf>,
101 pub target: String,
102 pub base_path: Option<String>,
105 pub duration: String,
106 pub vus: u32,
107 pub target_rps: Option<u32>,
113 pub no_keep_alive: bool,
118 pub scenario: String,
119 pub operations: Option<String>,
120 pub exclude_operations: Option<String>,
124 pub auth: Option<String>,
125 pub headers: Vec<String>,
128 pub output: PathBuf,
129 pub generate_only: bool,
130 pub script_output: Option<PathBuf>,
131 pub threshold_percentile: String,
132 pub threshold_ms: u64,
133 pub max_error_rate: f64,
134 pub abort_on_error: bool,
139 pub abort_on_error_rate: f64,
143 pub verbose: bool,
144 pub skip_tls_verify: bool,
145 pub chunked_request_bodies: bool,
150 pub targets_file: Option<PathBuf>,
152 pub max_concurrency: Option<u32>,
154 pub results_format: String,
156 pub params_file: Option<PathBuf>,
161
162 pub crud_flow: bool,
165 pub flow_config: Option<PathBuf>,
167 pub extract_fields: Option<String>,
169
170 pub parallel_create: Option<u32>,
173
174 pub data_file: Option<PathBuf>,
177 pub data_distribution: String,
179 pub data_mappings: Option<String>,
181 pub per_uri_control: bool,
183
184 pub error_rate: Option<f64>,
187 pub error_types: Option<String>,
189
190 pub security_test: bool,
193 pub security_payloads: Option<PathBuf>,
195 pub security_categories: Option<String>,
197 pub security_target_fields: Option<String>,
199
200 pub wafbench_dir: Option<String>,
203 pub wafbench_cycle_all: bool,
205 pub wafbench_verbatim: bool,
208
209 pub conformance: bool,
212 pub conformance_api_key: Option<String>,
214 pub conformance_basic_auth: Option<String>,
216 pub conformance_report: PathBuf,
218 pub conformance_categories: Option<String>,
220 pub conformance_report_format: String,
222 pub conformance_headers: Vec<String>,
225 pub conformance_all_operations: bool,
228 pub conformance_custom: Option<PathBuf>,
230 pub conformance_delay_ms: u64,
233 pub use_k6: bool,
235 pub conformance_custom_filter: Option<String>,
239 pub export_requests: bool,
242 pub validate_requests: bool,
245 pub conformance_self_test: bool,
252 pub conformance_self_test_capture: bool,
256 pub validate_response_schemas: bool,
262 pub conformance_self_test_iterations: u32,
267 pub conformance_self_test_duration: Option<String>,
272
273 pub source_ips: Vec<String>,
278 pub geo_source_ips: Vec<String>,
282 pub geo_source_headers: Vec<String>,
286
287 pub report_missed_cap: Option<u32>,
294
295 pub discard_response_bodies: bool,
302
303 pub dns_policy: Option<String>,
309
310 pub owasp_api_top10: bool,
313 pub owasp_categories: Option<String>,
315 pub owasp_auth_header: String,
317 pub owasp_auth_token: Option<String>,
319 pub owasp_admin_paths: Option<PathBuf>,
321 pub owasp_id_fields: Option<String>,
323 pub owasp_report: Option<PathBuf>,
325 pub owasp_report_format: String,
327 pub owasp_iterations: u32,
329}
330
331fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
345 use std::net::IpAddr;
346 const MAX_CIDR_EXPANSION: usize = 256;
347 let mut out = Vec::new();
348 for entry in raw {
349 for piece in entry.split(',') {
350 let s = piece.trim();
351 if s.is_empty() {
352 continue;
353 }
354 if let Some((addr_part, prefix_part)) = s.split_once('/') {
356 let prefix: u32 = match prefix_part.parse() {
357 Ok(p) => p,
358 Err(e) => {
359 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR prefix: {e}");
360 continue;
361 }
362 };
363 let net_addr: IpAddr = match addr_part.parse() {
364 Ok(a) => a,
365 Err(e) => {
366 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR address: {e}");
367 continue;
368 }
369 };
370 expand_cidr(net_addr, prefix, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
371 continue;
372 }
373 if let Some((start_str, end_str)) = s.split_once('-') {
379 let start_s = start_str.trim();
380 let end_s = end_str.trim();
381 if start_s.contains(':') || end_s.contains(':') {
385 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{s}': IPv6 range syntax not supported (use CIDR like 2001:db8::/126 instead)");
386 continue;
387 }
388 let start: IpAddr = match start_s.parse() {
389 Ok(a) => a,
390 Err(e) => {
391 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range start: {e}");
392 continue;
393 }
394 };
395 let end: IpAddr = match end_s.parse() {
396 Ok(a) => a,
397 Err(e) => {
398 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range end: {e}");
399 continue;
400 }
401 };
402 expand_range(start, end, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
403 continue;
404 }
405 match s.parse::<IpAddr>() {
407 Ok(ip) => out.push(ip),
408 Err(e) => {
409 tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
410 }
411 }
412 }
413 }
414 out
415}
416
417fn expand_range(
421 start: std::net::IpAddr,
422 end: std::net::IpAddr,
423 cap: usize,
424 flag_name: &str,
425 raw: &str,
426 out: &mut Vec<std::net::IpAddr>,
427) {
428 use std::net::{IpAddr, Ipv4Addr};
429 let (start_v4, end_v4) = match (start, end) {
430 (IpAddr::V4(a), IpAddr::V4(b)) => (a, b),
431 _ => {
432 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range start/end must both be IPv4");
433 return;
434 }
435 };
436 let start_u32 = u32::from(start_v4);
437 let end_u32 = u32::from(end_v4);
438 if end_u32 < start_u32 {
439 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range end {end_v4} is before start {start_v4}");
440 return;
441 }
442 let total = (end_u32 - start_u32).saturating_add(1) as usize;
443 let take = total.min(cap);
444 if total > cap {
445 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range has {total} addresses, capping at {cap}");
446 }
447 for i in 0..take as u32 {
448 out.push(IpAddr::V4(Ipv4Addr::from(start_u32 + i)));
449 }
450}
451
452fn expand_cidr(
456 net: std::net::IpAddr,
457 prefix: u32,
458 cap: usize,
459 flag_name: &str,
460 raw: &str,
461 out: &mut Vec<std::net::IpAddr>,
462) {
463 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
464 match net {
465 IpAddr::V4(ipv4) => {
466 if prefix > 32 {
467 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv4 prefix must be <= 32");
468 return;
469 }
470 let total: u64 = 1u64 << (32 - prefix);
471 let take = total.min(cap as u64) as u32;
472 if total > cap as u64 {
473 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': CIDR has {total} addresses, capping at {cap}");
474 }
475 let mask: u32 = if prefix == 0 {
476 0
477 } else {
478 !0u32 << (32 - prefix)
479 };
480 let net_u32 = u32::from(ipv4) & mask;
481 for i in 0..take {
482 out.push(IpAddr::V4(Ipv4Addr::from(net_u32.wrapping_add(i))));
483 }
484 }
485 IpAddr::V6(ipv6) => {
486 if prefix > 128 {
487 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv6 prefix must be <= 128");
488 return;
489 }
490 let mask: u128 = if prefix == 0 {
494 0
495 } else {
496 !0u128 << (128 - prefix)
497 };
498 let net_u128 = u128::from(ipv6) & mask;
499 let remaining_bits = 128 - prefix;
500 let total_capped = if remaining_bits >= 64 {
503 cap as u128
504 } else {
505 (1u128 << remaining_bits).min(cap as u128)
506 };
507 if remaining_bits < 128 && (1u128 << remaining_bits) > cap as u128 {
508 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': IPv6 CIDR exceeds {cap} addresses, capping");
509 }
510 for i in 0..total_capped {
511 out.push(IpAddr::V6(Ipv6Addr::from(net_u128.wrapping_add(i))));
512 }
513 }
514 }
515}
516
517impl BenchCommand {
518 pub fn security_testing_enabled(&self) -> bool {
534 if self.wafbench_verbatim {
535 return false;
536 }
537 self.security_test || self.wafbench_dir.is_some()
538 }
539
540 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
542 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
543
544 if !self.spec.is_empty() {
546 let specs = load_specs_from_files(self.spec.clone())
547 .await
548 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
549 all_specs.extend(specs);
550 }
551
552 if let Some(spec_dir) = &self.spec_dir {
554 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
555 BenchError::Other(format!("Failed to load specs from directory: {}", e))
556 })?;
557 all_specs.extend(dir_specs);
558 }
559
560 if all_specs.is_empty() {
561 return Err(BenchError::Other(
562 "No spec files provided. Use --spec or --spec-dir.".to_string(),
563 ));
564 }
565
566 if all_specs.len() == 1 {
568 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
570 }
571
572 let conflict_strategy = match self.merge_conflicts.as_str() {
574 "first" => ConflictStrategy::First,
575 "last" => ConflictStrategy::Last,
576 _ => ConflictStrategy::Error,
577 };
578
579 merge_specs(all_specs, conflict_strategy)
580 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
581 }
582
583 fn get_spec_display_name(&self) -> String {
585 if self.spec.len() == 1 {
586 self.spec[0].to_string_lossy().to_string()
587 } else if !self.spec.is_empty() {
588 format!("{} spec files", self.spec.len())
589 } else if let Some(dir) = &self.spec_dir {
590 format!("specs from {}", dir.display())
591 } else {
592 "no specs".to_string()
593 }
594 }
595
596 fn advise_capacity(&self) {
603 let target_count = self
604 .targets_file
605 .as_ref()
606 .and_then(|p| std::fs::read_to_string(p).ok())
607 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
608 .and_then(|v| v.as_array().map(|a| a.len()))
609 .unwrap_or(1);
610 let vus = self.vus.max(1);
611 let rps_total = self.target_rps.unwrap_or(0) as usize * target_count.max(1);
612 let load_product = target_count * vus as usize;
616 if load_product >= 150 {
617 let est_ram_gb =
618 (vus as usize * 50) / 1024 + (target_count * 10 * 2) / 1024 + target_count / 2;
619 let est_cores = ((vus as usize) / 50).max(2);
620 TerminalReporter::print_warning(&format!(
621 "Capacity advisory: targets={target_count}, VUs={vus}, RPS-total≈{rps_total}. \
622 Single-client estimate: ~{est_cores} CPU cores, ~{est_ram_gb} GB RAM. \
623 If your machine is below that, expect OOM hangs partway through the run. \
624 See https://docs.mockforge.dev/reference/bench-capacity-sizing.html \
625 for the sizing table and sharding guide."
626 ));
627 }
628 }
629
630 pub async fn execute(&self) -> Result<()> {
632 if self.conformance_self_test && self.use_k6 {
639 TerminalReporter::print_warning(
640 "--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.",
641 );
642 }
643
644 self.advise_capacity();
650
651 if let Some(targets_file) = &self.targets_file {
653 if self.conformance && self.conformance_self_test {
662 return self.execute_multi_target_self_test(targets_file).await;
663 }
664 if self.conformance {
665 return self.execute_multi_target_conformance(targets_file).await;
666 }
667 return self.execute_multi_target(targets_file).await;
668 }
669
670 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
672 return self.execute_sequential_specs().await;
673 }
674
675 TerminalReporter::print_header(
678 &self.get_spec_display_name(),
679 &self.target,
680 0, &self.scenario,
682 Self::parse_duration(&self.duration)?,
683 );
684
685 if !K6Executor::is_k6_installed() {
687 TerminalReporter::print_error("k6 is not installed");
688 TerminalReporter::print_warning(
689 "Install k6 from: https://k6.io/docs/get-started/installation/",
690 );
691 return Err(BenchError::K6NotFound);
692 }
693 K6Executor::warn_if_pre_v1().await;
694
695 if self.conformance {
697 return self.execute_conformance_test().await;
698 }
699
700 let spec_supplied = !self.spec.is_empty() || self.spec_dir.is_some();
707 let merged_spec = if self.wafbench_verbatim && !spec_supplied {
708 tracing::info!(
709 target: "mockforge::bench",
710 "--wafbench-verbatim without --spec: sending only the traffic file's requests"
711 );
712 OpenApiSpec {
713 spec: Default::default(),
714 file_path: None,
715 raw_document: None,
716 }
717 } else {
718 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
719 self.load_and_merge_specs().await?
720 };
721 let parser = SpecParser::from_spec(merged_spec);
722 if self.spec.len() > 1 || self.spec_dir.is_some() {
723 TerminalReporter::print_success(&format!(
724 "Loaded and merged {} specification(s)",
725 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
726 ));
727 } else {
728 TerminalReporter::print_success("Specification loaded");
729 }
730
731 let mock_config = self.build_mock_config().await;
733 if mock_config.is_mock_server {
734 TerminalReporter::print_progress("Mock server integration enabled");
735 }
736
737 if self.crud_flow {
739 return self.execute_crud_flow(&parser).await;
740 }
741
742 if self.owasp_api_top10 {
744 return self.execute_owasp_test(&parser).await;
745 }
746
747 TerminalReporter::print_progress("Extracting API operations...");
749 let mut operations = if let Some(filter) = &self.operations {
750 parser.filter_operations(filter)?
751 } else {
752 parser.get_operations()
753 };
754
755 if let Some(exclude) = &self.exclude_operations {
757 let before_count = operations.len();
758 operations = parser.exclude_operations(operations, exclude)?;
759 let excluded_count = before_count - operations.len();
760 if excluded_count > 0 {
761 TerminalReporter::print_progress(&format!(
762 "Excluded {} operations matching '{}'",
763 excluded_count, exclude
764 ));
765 }
766 }
767
768 if operations.is_empty() && !self.wafbench_verbatim {
774 return Err(BenchError::Other("No operations found in spec".to_string()));
775 }
776
777 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
778
779 let param_overrides = if let Some(params_file) = &self.params_file {
781 TerminalReporter::print_progress("Loading parameter overrides...");
782 let overrides = ParameterOverrides::from_file(params_file)?;
783 TerminalReporter::print_success(&format!(
784 "Loaded parameter overrides ({} operation-specific, {} defaults)",
785 overrides.operations.len(),
786 if overrides.defaults.is_empty() { 0 } else { 1 }
787 ));
788 Some(overrides)
789 } else {
790 None
791 };
792
793 TerminalReporter::print_progress("Generating request templates...");
795 let templates: Vec<_> = operations
796 .iter()
797 .map(|op| {
798 let op_overrides = param_overrides.as_ref().map(|po| {
799 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
800 });
801 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
802 })
803 .collect::<Result<Vec<_>>>()?;
804 TerminalReporter::print_success("Request templates generated");
805
806 let templates = if self.wafbench_verbatim {
812 let verbatim = self.load_verbatim_templates()?;
813 if verbatim.is_empty() {
814 return Err(BenchError::Other(
815 "--wafbench-verbatim was set but no traffic cases were loaded. Check \
816 --wafbench-dir points at a file, directory or glob containing cases with \
817 a `request.uri`."
818 .to_string(),
819 ));
820 }
821 TerminalReporter::print_success(&format!(
822 "Verbatim mode: {} request(s) will be sent exactly as written (spec endpoints not used)",
823 verbatim.len()
824 ));
825 verbatim
826 } else {
827 templates
828 };
829
830 let custom_headers = self.parse_headers()?;
832
833 let base_path = self.resolve_base_path(&parser);
835 if let Some(ref bp) = base_path {
836 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
837 }
838
839 TerminalReporter::print_progress("Generating k6 load test script...");
841 let scenario =
842 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
843
844 let security_testing_enabled = self.security_testing_enabled();
845
846 let num_ops = operations.len() as u32;
864 if let Some(rps) = self.target_rps {
865 let probe =
866 crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
867
868 let (required_vus, basis) = match probe {
869 Some(p) => (
870 p.required_vus(rps, num_ops),
871 format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
872 ),
873 None => {
874 let fallback = (rps as u64)
876 .saturating_mul(num_ops.max(1) as u64)
877 .div_ceil(10)
878 .min(u32::MAX as u64) as u32;
879 (fallback, "~100ms (default — probe failed)".to_string())
880 }
881 };
882
883 if self.vus < required_vus {
884 const VU_RECOMMENDATION_CAP: u32 = 1000;
890 let recommendation = required_vus.max(self.vus + 1);
891 if recommendation > VU_RECOMMENDATION_CAP {
892 TerminalReporter::print_warning(&format!(
893 "Workload is very large: --rps {} × {} ops/iteration × {} \
894 baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
895 practical to drive. Two ways to fix:\n 1. Reduce \
896 operations per iteration with `--operations 'pattern,…'` \
897 (or `--exclude-operations`) to focus the bench on a \
898 representative subset.\n 2. Drop `--rps` and use \
899 `--vus {}` alone — closed-model load runs as fast as \
900 the VU pool allows, bounded by latency, with no per-\
901 iteration deadline. Expect 1-iteration coverage of ~{} \
902 operations in {}s.",
903 rps,
904 num_ops,
905 basis,
906 recommendation,
907 self.vus.max(5),
908 num_ops,
909 Self::parse_duration(&self.duration).unwrap_or(0),
910 ));
911 } else {
912 TerminalReporter::print_warning(&format!(
913 "--vus {} may be insufficient for --rps {} × {} ops/iteration \
914 (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
915 and each runs every operation in the spec — required ≈ rps × ops × \
916 latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
917 warnings.",
918 self.vus, rps, num_ops, basis, recommendation,
919 ));
920 }
921 } else if probe.is_some() {
922 TerminalReporter::print_progress(&format!(
923 "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
924 is sufficient for --rps {}",
925 basis, num_ops, self.vus, rps,
926 ));
927 }
928 }
929
930 let k6_config = K6Config {
931 target_url: self.target.clone(),
932 base_path,
933 scenario,
934 duration_secs: Self::parse_duration(&self.duration)?,
935 max_vus: self.vus,
936 threshold_percentile: self.threshold_percentile.clone(),
937 threshold_ms: self.threshold_ms,
938 max_error_rate: self.max_error_rate,
939 auth_header: self.auth.clone(),
940 custom_headers,
941 skip_tls_verify: self.skip_tls_verify,
942 security_testing_enabled,
943 chunked_request_bodies: self.chunked_request_bodies,
944 target_rps: self.target_rps,
945 no_keep_alive: self.no_keep_alive,
946 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
952 .into_iter()
953 .map(|ip| ip.to_string())
954 .collect(),
955 geo_source_headers: if self.geo_source_headers.is_empty()
956 && !self.geo_source_ips.is_empty()
957 {
958 crate::conformance::self_test::default_geo_source_headers()
959 } else {
960 self.geo_source_headers.clone()
961 },
962 };
963
964 let generator = K6ScriptGenerator::new(k6_config, templates)
965 .with_abort_valve(self.abort_on_error, self.abort_on_error_rate);
966 let mut script = generator.generate()?;
967 TerminalReporter::print_success("k6 script generated");
968
969 let has_advanced_features = self.data_file.is_some()
971 || self.error_rate.is_some()
972 || self.security_test
973 || self.parallel_create.is_some()
974 || self.wafbench_dir.is_some();
975
976 if has_advanced_features {
978 script = self.generate_enhanced_script(&script)?;
979 }
980
981 if mock_config.is_mock_server {
983 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
984 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
985 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
986
987 if let Some(import_end) = script.find("export const options") {
989 script.insert_str(
990 import_end,
991 &format!(
992 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
993 helper_code, setup_code, teardown_code
994 ),
995 );
996 }
997 }
998
999 TerminalReporter::print_progress("Validating k6 script...");
1001 let validation_errors = K6ScriptGenerator::validate_script(&script);
1002 if !validation_errors.is_empty() {
1003 TerminalReporter::print_error("Script validation failed");
1004 for error in &validation_errors {
1005 eprintln!(" {}", error);
1006 }
1007 return Err(BenchError::Other(format!(
1008 "Generated k6 script has {} validation error(s). Please check the output above.",
1009 validation_errors.len()
1010 )));
1011 }
1012 TerminalReporter::print_success("Script validation passed");
1013
1014 let script_path = if let Some(output) = &self.script_output {
1016 output.clone()
1017 } else {
1018 self.output.join("k6-script.js")
1019 };
1020
1021 if let Some(parent) = script_path.parent() {
1022 std::fs::create_dir_all(parent)?;
1023 }
1024 std::fs::write(&script_path, &script)?;
1025 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
1026
1027 if self.generate_only {
1029 println!("\nScript generated successfully. Run it with:");
1030 println!(" k6 run {}", script_path.display());
1031 return Ok(());
1032 }
1033
1034 TerminalReporter::print_progress("Executing load test...");
1036 let executor = K6Executor::new()?
1040 .with_local_ips(self.source_ips.join(","))
1041 .with_dns_policy(self.dns_policy.clone().unwrap_or_default())
1042 .with_discard_response_bodies(self.discard_response_bodies);
1043
1044 std::fs::create_dir_all(&self.output)?;
1045
1046 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
1047
1048 let duration_secs = Self::parse_duration(&self.duration)?;
1050 TerminalReporter::print_summary_full(
1051 &results,
1052 duration_secs,
1053 self.no_keep_alive,
1054 Some(num_ops),
1055 );
1056
1057 println!("\nResults saved to: {}", self.output.display());
1058
1059 Ok(())
1060 }
1061
1062 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
1064 TerminalReporter::print_progress("Parsing targets file...");
1065 let targets = parse_targets_file(targets_file)?;
1066 let num_targets = targets.len();
1067 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
1068
1069 if targets.is_empty() {
1070 return Err(BenchError::Other("No targets found in file".to_string()));
1071 }
1072
1073 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
1075 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
1079 &self.get_spec_display_name(),
1080 &format!("{} targets", num_targets),
1081 0,
1082 &self.scenario,
1083 Self::parse_duration(&self.duration)?,
1084 );
1085
1086 let executor = ParallelExecutor::new(
1088 BenchCommand {
1089 spec: self.spec.clone(),
1091 spec_dir: self.spec_dir.clone(),
1092 merge_conflicts: self.merge_conflicts.clone(),
1093 spec_mode: self.spec_mode.clone(),
1094 dependency_config: self.dependency_config.clone(),
1095 target: self.target.clone(), base_path: self.base_path.clone(),
1097 duration: self.duration.clone(),
1098 vus: self.vus,
1099 target_rps: self.target_rps,
1100 no_keep_alive: self.no_keep_alive,
1101 scenario: self.scenario.clone(),
1102 operations: self.operations.clone(),
1103 exclude_operations: self.exclude_operations.clone(),
1104 auth: self.auth.clone(),
1105 headers: self.headers.clone(),
1106 output: self.output.clone(),
1107 generate_only: self.generate_only,
1108 script_output: self.script_output.clone(),
1109 threshold_percentile: self.threshold_percentile.clone(),
1110 threshold_ms: self.threshold_ms,
1111 max_error_rate: self.max_error_rate,
1112 abort_on_error: self.abort_on_error,
1113 abort_on_error_rate: self.abort_on_error_rate,
1114 verbose: self.verbose,
1115 skip_tls_verify: self.skip_tls_verify,
1116 chunked_request_bodies: self.chunked_request_bodies,
1117 targets_file: None,
1118 max_concurrency: None,
1119 results_format: self.results_format.clone(),
1120 params_file: self.params_file.clone(),
1121 crud_flow: self.crud_flow,
1122 flow_config: self.flow_config.clone(),
1123 extract_fields: self.extract_fields.clone(),
1124 parallel_create: self.parallel_create,
1125 data_file: self.data_file.clone(),
1126 data_distribution: self.data_distribution.clone(),
1127 data_mappings: self.data_mappings.clone(),
1128 per_uri_control: self.per_uri_control,
1129 error_rate: self.error_rate,
1130 error_types: self.error_types.clone(),
1131 security_test: self.security_test,
1132 security_payloads: self.security_payloads.clone(),
1133 security_categories: self.security_categories.clone(),
1134 security_target_fields: self.security_target_fields.clone(),
1135 wafbench_dir: self.wafbench_dir.clone(),
1136 wafbench_cycle_all: self.wafbench_cycle_all,
1137 wafbench_verbatim: self.wafbench_verbatim,
1138 owasp_api_top10: self.owasp_api_top10,
1139 owasp_categories: self.owasp_categories.clone(),
1140 owasp_auth_header: self.owasp_auth_header.clone(),
1141 owasp_auth_token: self.owasp_auth_token.clone(),
1142 owasp_admin_paths: self.owasp_admin_paths.clone(),
1143 owasp_id_fields: self.owasp_id_fields.clone(),
1144 owasp_report: self.owasp_report.clone(),
1145 owasp_report_format: self.owasp_report_format.clone(),
1146 owasp_iterations: self.owasp_iterations,
1147 conformance: false,
1148 conformance_api_key: self.conformance_api_key.clone(),
1164 conformance_basic_auth: self.conformance_basic_auth.clone(),
1165 conformance_report: PathBuf::from("conformance-report.json"),
1166 conformance_categories: None,
1167 conformance_report_format: "json".to_string(),
1168 conformance_headers: self.conformance_headers.clone(),
1172 conformance_all_operations: false,
1173 conformance_custom: None,
1174 conformance_delay_ms: 0,
1175 use_k6: false,
1176 conformance_custom_filter: None,
1177 export_requests: false,
1178 validate_requests: false,
1179 conformance_self_test: false,
1180 conformance_self_test_capture: false,
1181 conformance_self_test_iterations: 1,
1182 conformance_self_test_duration: None,
1183 validate_response_schemas: false,
1184 source_ips: self.source_ips.clone(),
1189 geo_source_ips: self.geo_source_ips.clone(),
1190 geo_source_headers: self.geo_source_headers.clone(),
1191 report_missed_cap: None,
1192 discard_response_bodies: self.discard_response_bodies,
1196 dns_policy: self.dns_policy.clone(),
1199 },
1200 targets,
1201 max_concurrency,
1202 );
1203
1204 let start_time = std::time::Instant::now();
1206 let aggregated_results = executor.execute_all().await?;
1207 let elapsed = start_time.elapsed();
1208
1209 self.report_multi_target_results(&aggregated_results, elapsed)?;
1211
1212 Ok(())
1213 }
1214
1215 fn report_multi_target_results(
1217 &self,
1218 results: &AggregatedResults,
1219 elapsed: std::time::Duration,
1220 ) -> Result<()> {
1221 TerminalReporter::print_multi_target_summary(results);
1223
1224 let total_secs = elapsed.as_secs();
1226 let hours = total_secs / 3600;
1227 let minutes = (total_secs % 3600) / 60;
1228 let seconds = total_secs % 60;
1229 if hours > 0 {
1230 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
1231 } else if minutes > 0 {
1232 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
1233 } else {
1234 println!("\n Total Elapsed Time: {}s", seconds);
1235 }
1236
1237 if self.results_format == "aggregated" || self.results_format == "both" {
1239 let summary_path = self.output.join("aggregated_summary.json");
1240 let summary_json = serde_json::json!({
1241 "total_elapsed_seconds": elapsed.as_secs(),
1242 "total_targets": results.total_targets,
1243 "successful_targets": results.successful_targets,
1244 "failed_targets": results.failed_targets,
1245 "aggregated_metrics": {
1246 "total_requests": results.aggregated_metrics.total_requests,
1247 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1248 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1249 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1250 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1251 "error_rate": results.aggregated_metrics.error_rate,
1252 "total_rps": results.aggregated_metrics.total_rps,
1253 "avg_rps": results.aggregated_metrics.avg_rps,
1254 "total_vus_max": results.aggregated_metrics.total_vus_max,
1255 },
1256 "target_results": results.target_results.iter().map(|r| {
1257 serde_json::json!({
1258 "target_url": r.target_url,
1259 "target_index": r.target_index,
1260 "success": r.success,
1261 "error": r.error,
1262 "total_requests": r.results.total_requests,
1263 "failed_requests": r.results.failed_requests,
1264 "avg_duration_ms": r.results.avg_duration_ms,
1265 "min_duration_ms": r.results.min_duration_ms,
1266 "med_duration_ms": r.results.med_duration_ms,
1267 "p90_duration_ms": r.results.p90_duration_ms,
1268 "p95_duration_ms": r.results.p95_duration_ms,
1269 "p99_duration_ms": r.results.p99_duration_ms,
1270 "max_duration_ms": r.results.max_duration_ms,
1271 "rps": r.results.rps,
1272 "vus_max": r.results.vus_max,
1273 "output_dir": r.output_dir.to_string_lossy(),
1274 })
1275 }).collect::<Vec<_>>(),
1276 });
1277
1278 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1279 TerminalReporter::print_success(&format!(
1280 "Aggregated summary saved to: {}",
1281 summary_path.display()
1282 ));
1283 }
1284
1285 let csv_path = self.output.join("all_targets.csv");
1287 let mut csv = String::from(
1288 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1289 );
1290 for r in &results.target_results {
1291 csv.push_str(&format!(
1292 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1293 r.target_url,
1294 r.success,
1295 r.results.total_requests,
1296 r.results.failed_requests,
1297 r.results.rps,
1298 r.results.vus_max,
1299 r.results.min_duration_ms,
1300 r.results.avg_duration_ms,
1301 r.results.med_duration_ms,
1302 r.results.p90_duration_ms,
1303 r.results.p95_duration_ms,
1304 r.results.p99_duration_ms,
1305 r.results.max_duration_ms,
1306 r.error.as_deref().unwrap_or(""),
1307 ));
1308 }
1309 let _ = std::fs::write(&csv_path, &csv);
1310
1311 println!("\nResults saved to: {}", self.output.display());
1312 println!(" - Per-target results: {}", self.output.join("target_*").display());
1313 println!(" - All targets CSV: {}", csv_path.display());
1314 if self.results_format == "aggregated" || self.results_format == "both" {
1315 println!(
1316 " - Aggregated summary: {}",
1317 self.output.join("aggregated_summary.json").display()
1318 );
1319 }
1320
1321 Ok(())
1322 }
1323
1324 pub fn parse_duration(duration: &str) -> Result<u64> {
1326 let duration = duration.trim();
1327
1328 if let Some(secs) = duration.strip_suffix('s') {
1329 secs.parse::<u64>()
1330 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1331 } else if let Some(mins) = duration.strip_suffix('m') {
1332 mins.parse::<u64>()
1333 .map(|m| m * 60)
1334 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1335 } else if let Some(hours) = duration.strip_suffix('h') {
1336 hours
1337 .parse::<u64>()
1338 .map(|h| h * 3600)
1339 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1340 } else {
1341 duration
1343 .parse::<u64>()
1344 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1345 }
1346 }
1347
1348 pub(crate) fn load_verbatim_templates(
1355 &self,
1356 ) -> Result<Vec<crate::request_gen::RequestTemplate>> {
1357 let Some(pattern) = self.wafbench_dir.as_ref() else {
1358 return Err(BenchError::Other(
1359 "--wafbench-verbatim requires --wafbench-dir pointing at your traffic file(s)"
1360 .to_string(),
1361 ));
1362 };
1363
1364 let mut loader = WafBenchLoader::new();
1365 loader.load_from_pattern(pattern)?;
1366 Self::print_traffic_file_breakdown(loader.stats());
1367
1368 Ok(crate::wafbench::traffic_cases_to_templates(loader.test_cases()))
1369 }
1370
1371 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1373 let mut headers = parse_header_string(&self.headers)?;
1374
1375 let already_has = |hs: &HashMap<String, String>, name: &str| -> bool {
1386 hs.keys().any(|k| k.eq_ignore_ascii_case(name))
1387 };
1388
1389 if !already_has(&headers, "Authorization") {
1390 if let Some(b) = self.conformance_basic_auth.as_ref().filter(|s| !s.is_empty()) {
1391 use base64::Engine as _;
1392 let encoded = base64::engine::general_purpose::STANDARD.encode(b.as_bytes());
1393 headers.insert("Authorization".to_string(), format!("Basic {}", encoded));
1394 }
1395 }
1396
1397 for line in &self.conformance_headers {
1403 let Some((name, value)) = line.split_once(':') else {
1404 continue;
1405 };
1406 let name = name.trim();
1407 let value = value.trim();
1408 if name.is_empty() || already_has(&headers, name) {
1409 continue;
1410 }
1411 headers.insert(name.to_string(), value.to_string());
1412 }
1413
1414 if !self.conformance && self.conformance_api_key.is_some() {
1420 TerminalReporter::print_warning(
1421 "--conformance-api-key only fires under --conformance. For plain bench use --header 'X-API-Key: ...'.",
1422 );
1423 }
1424
1425 Ok(headers)
1426 }
1427
1428 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1429 let extracted_path = output_dir.join("extracted_values.json");
1430 if !extracted_path.exists() {
1431 return Ok(ExtractedValues::new());
1432 }
1433
1434 let content = std::fs::read_to_string(&extracted_path)
1435 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1436 let parsed: serde_json::Value = serde_json::from_str(&content)
1437 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1438
1439 let mut extracted = ExtractedValues::new();
1440 if let Some(values) = parsed.as_object() {
1441 for (key, value) in values {
1442 extracted.set(key.clone(), value.clone());
1443 }
1444 }
1445
1446 Ok(extracted)
1447 }
1448
1449 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1458 if let Some(cli_base_path) = &self.base_path {
1460 if cli_base_path.is_empty() {
1461 return None;
1463 }
1464 return Some(cli_base_path.clone());
1465 }
1466
1467 parser.get_base_path()
1469 }
1470
1471 async fn build_mock_config(&self) -> MockIntegrationConfig {
1473 if MockServerDetector::looks_like_mock_server(&self.target) {
1475 if let Ok(info) = MockServerDetector::detect(&self.target).await {
1477 if info.is_mockforge {
1478 TerminalReporter::print_success(&format!(
1479 "Detected MockForge server (version: {})",
1480 info.version.as_deref().unwrap_or("unknown")
1481 ));
1482 return MockIntegrationConfig::mock_server();
1483 }
1484 }
1485 }
1486 MockIntegrationConfig::real_api()
1487 }
1488
1489 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1491 if !self.crud_flow {
1492 return None;
1493 }
1494
1495 if let Some(config_path) = &self.flow_config {
1497 match CrudFlowConfig::from_file(config_path) {
1498 Ok(config) => return Some(config),
1499 Err(e) => {
1500 TerminalReporter::print_warning(&format!(
1501 "Failed to load flow config: {}. Using auto-detection.",
1502 e
1503 ));
1504 }
1505 }
1506 }
1507
1508 let extract_fields = self
1510 .extract_fields
1511 .as_ref()
1512 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1513 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1514
1515 Some(CrudFlowConfig {
1516 flows: Vec::new(), default_extract_fields: extract_fields,
1518 })
1519 }
1520
1521 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1523 let data_file = self.data_file.as_ref()?;
1524
1525 let distribution = DataDistribution::from_str(&self.data_distribution)
1526 .unwrap_or(DataDistribution::UniquePerVu);
1527
1528 let mappings = self
1529 .data_mappings
1530 .as_ref()
1531 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1532 .unwrap_or_default();
1533
1534 Some(DataDrivenConfig {
1535 file_path: data_file.to_string_lossy().to_string(),
1536 distribution,
1537 mappings,
1538 csv_has_header: true,
1539 per_uri_control: self.per_uri_control,
1540 per_uri_columns: crate::data_driven::PerUriColumns::default(),
1541 })
1542 }
1543
1544 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1546 let error_rate = self.error_rate?;
1547
1548 let error_types = self
1549 .error_types
1550 .as_ref()
1551 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1552 .unwrap_or_default();
1553
1554 Some(InvalidDataConfig {
1555 error_rate,
1556 error_types,
1557 target_fields: Vec::new(),
1558 })
1559 }
1560
1561 fn build_security_config(&self) -> Option<SecurityTestConfig> {
1563 if !self.security_test {
1564 return None;
1565 }
1566
1567 let categories = self
1568 .security_categories
1569 .as_ref()
1570 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1571 .unwrap_or_else(|| {
1572 let mut default = HashSet::new();
1573 default.insert(SecurityCategory::SqlInjection);
1574 default.insert(SecurityCategory::Xss);
1575 default
1576 });
1577
1578 let target_fields = self
1579 .security_target_fields
1580 .as_ref()
1581 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1582 .unwrap_or_default();
1583
1584 let custom_payloads_file =
1585 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1586
1587 Some(SecurityTestConfig {
1588 enabled: true,
1589 categories,
1590 target_fields,
1591 custom_payloads_file,
1592 include_high_risk: false,
1593 })
1594 }
1595
1596 fn build_parallel_config(&self) -> Option<ParallelConfig> {
1598 let count = self.parallel_create?;
1599
1600 Some(ParallelConfig::new(count))
1601 }
1602
1603 fn print_traffic_file_breakdown(stats: &crate::wafbench::WafBenchStats) {
1607 if stats.per_file.is_empty() {
1608 return;
1609 }
1610 TerminalReporter::print_success("Traffic file breakdown (what to expect in proxy logs):");
1611 for file in &stats.per_file {
1612 let other = if file.other > 0 {
1613 format!(" other={}", file.other)
1614 } else {
1615 String::new()
1616 };
1617 TerminalReporter::print_progress(&format!(
1618 " {}: sent={} attack(expected 403)={} normal(expected 200)={} omitted={}{other}",
1619 file.file, file.sent, file.attack, file.normal, file.omitted
1620 ));
1621 }
1622 }
1623
1624 fn load_wafbench_payloads(&self) -> Result<Vec<SecurityPayload>> {
1631 let Some(ref wafbench_dir) = self.wafbench_dir else {
1632 return Ok(Vec::new());
1633 };
1634
1635 let mut loader = WafBenchLoader::new();
1636 loader.load_from_pattern(wafbench_dir)?;
1637
1638 let stats = loader.stats();
1639
1640 if stats.files_processed == 0 {
1641 let mut msg = format!(
1642 "No WAFBench YAML files found matching '{wafbench_dir}'. \
1643 --wafbench-dir is a file, a directory or a glob. A missing \
1644 file is an error, not an empty payload pool."
1645 );
1646 if !stats.parse_errors.is_empty() {
1647 msg.push_str(" Parse errors:");
1648 for error in &stats.parse_errors {
1649 msg.push_str(&format!("\n - {error}"));
1650 }
1651 }
1652 return Err(BenchError::Other(msg));
1653 }
1654
1655 TerminalReporter::print_progress(&format!(
1656 "Loaded {} WAFBench files, {} test cases, {} payloads",
1657 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1658 ));
1659 Self::print_traffic_file_breakdown(stats);
1660
1661 for (category, count) in &stats.by_category {
1663 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1664 }
1665
1666 for error in &stats.parse_errors {
1668 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1669 }
1670
1671 Ok(loader.to_security_payloads())
1672 }
1673
1674 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1676 let mut enhanced_script = base_script.to_string();
1677 let mut additional_code = String::new();
1678
1679 if let Some(config) = self.build_data_driven_config() {
1681 TerminalReporter::print_progress("Adding data-driven testing support...");
1682 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1683 additional_code.push('\n');
1684 TerminalReporter::print_success("Data-driven testing enabled");
1685 }
1686
1687 if let Some(config) = self.build_invalid_data_config() {
1689 TerminalReporter::print_progress("Adding invalid data testing support...");
1690 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1691 additional_code.push('\n');
1692 additional_code
1693 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1694 additional_code.push('\n');
1695 additional_code
1696 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1697 additional_code.push('\n');
1698 TerminalReporter::print_success(&format!(
1699 "Invalid data testing enabled ({}% error rate)",
1700 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1701 ));
1702 }
1703
1704 let verbatim = self.wafbench_verbatim;
1711 if verbatim && self.security_test {
1712 TerminalReporter::print_warning(
1713 "--security-test is ignored under --wafbench-verbatim: verbatim mode sends your \
1714 traffic cases exactly as written and will not append attack payloads to them. \
1715 Drop --wafbench-verbatim if you want payload injection.",
1716 );
1717 }
1718 let security_config = if verbatim {
1719 None
1720 } else {
1721 self.build_security_config()
1722 };
1723 let wafbench_payloads = if verbatim {
1724 Vec::new()
1725 } else {
1726 self.load_wafbench_payloads()?
1727 };
1728 let security_requested =
1729 !verbatim && (security_config.is_some() || self.wafbench_dir.is_some());
1730
1731 if security_config.is_some() || !wafbench_payloads.is_empty() {
1732 TerminalReporter::print_progress("Adding security testing support...");
1733
1734 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1736
1737 if let Some(ref config) = security_config {
1738 payload_list.extend(SecurityPayloads::get_payloads(config));
1739 }
1740
1741 if !wafbench_payloads.is_empty() {
1743 TerminalReporter::print_progress(&format!(
1744 "Loading {} WAFBench attack patterns...",
1745 wafbench_payloads.len()
1746 ));
1747 payload_list.extend(wafbench_payloads);
1748 }
1749
1750 let target_fields =
1751 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1752
1753 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1754 &payload_list,
1755 self.wafbench_cycle_all,
1756 ));
1757 additional_code.push('\n');
1758 additional_code
1759 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1760 additional_code.push('\n');
1761 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1762 additional_code.push('\n');
1763
1764 let mode = if self.wafbench_cycle_all {
1765 "cycle-all"
1766 } else {
1767 "random"
1768 };
1769 TerminalReporter::print_success(&format!(
1770 "Security testing enabled ({} payloads, {} mode)",
1771 payload_list.len(),
1772 mode
1773 ));
1774 } else if security_requested {
1775 TerminalReporter::print_warning(
1779 "Security testing was requested but no payloads were loaded. \
1780 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1781 );
1782 additional_code
1783 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1784 additional_code.push('\n');
1785 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1786 additional_code.push('\n');
1787 }
1788
1789 if let Some(config) = self.build_parallel_config() {
1791 TerminalReporter::print_progress("Adding parallel execution support...");
1792 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1793 additional_code.push('\n');
1794 TerminalReporter::print_success(&format!(
1795 "Parallel execution enabled (count: {})",
1796 config.count
1797 ));
1798 }
1799
1800 if !additional_code.is_empty() {
1802 if let Some(import_end) = enhanced_script.find("export const options") {
1804 enhanced_script.insert_str(
1805 import_end,
1806 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1807 );
1808 }
1809 }
1810
1811 Ok(enhanced_script)
1812 }
1813
1814 async fn execute_sequential_specs(&self) -> Result<()> {
1816 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1817
1818 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1820
1821 if !self.spec.is_empty() {
1822 let specs = load_specs_from_files(self.spec.clone())
1823 .await
1824 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1825 all_specs.extend(specs);
1826 }
1827
1828 if let Some(spec_dir) = &self.spec_dir {
1829 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1830 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1831 })?;
1832 all_specs.extend(dir_specs);
1833 }
1834
1835 if all_specs.is_empty() {
1836 return Err(BenchError::Other(
1837 "No spec files found for sequential execution".to_string(),
1838 ));
1839 }
1840
1841 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1842
1843 let execution_order = if let Some(config_path) = &self.dependency_config {
1845 TerminalReporter::print_progress("Loading dependency configuration...");
1846 let config = SpecDependencyConfig::from_file(config_path)?;
1847
1848 if !config.disable_auto_detect && config.execution_order.is_empty() {
1849 self.detect_and_sort_specs(&all_specs)?
1851 } else {
1852 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1854 }
1855 } else {
1856 self.detect_and_sort_specs(&all_specs)?
1858 };
1859
1860 TerminalReporter::print_success(&format!(
1861 "Execution order: {}",
1862 execution_order
1863 .iter()
1864 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1865 .collect::<Vec<_>>()
1866 .join(" → ")
1867 ));
1868
1869 let mut extracted_values = ExtractedValues::new();
1871 let total_specs = execution_order.len();
1872
1873 for (index, spec_path) in execution_order.iter().enumerate() {
1874 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1875
1876 TerminalReporter::print_progress(&format!(
1877 "[{}/{}] Executing spec: {}",
1878 index + 1,
1879 total_specs,
1880 spec_name
1881 ));
1882
1883 let spec = all_specs
1885 .iter()
1886 .find(|(p, _)| {
1887 p == spec_path
1888 || p.file_name() == spec_path.file_name()
1889 || p.file_name() == Some(spec_path.as_os_str())
1890 })
1891 .map(|(_, s)| s.clone())
1892 .ok_or_else(|| {
1893 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1894 })?;
1895
1896 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1898
1899 extracted_values.merge(&new_values);
1901
1902 TerminalReporter::print_success(&format!(
1903 "[{}/{}] Completed: {} (extracted {} values)",
1904 index + 1,
1905 total_specs,
1906 spec_name,
1907 new_values.values.len()
1908 ));
1909 }
1910
1911 TerminalReporter::print_success(&format!(
1912 "Sequential execution complete: {} specs executed",
1913 total_specs
1914 ));
1915
1916 Ok(())
1917 }
1918
1919 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1921 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1922
1923 let mut detector = DependencyDetector::new();
1924 let dependencies = detector.detect_dependencies(specs);
1925
1926 if dependencies.is_empty() {
1927 TerminalReporter::print_progress("No dependencies detected, using file order");
1928 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1929 }
1930
1931 TerminalReporter::print_progress(&format!(
1932 "Detected {} cross-spec dependencies",
1933 dependencies.len()
1934 ));
1935
1936 for dep in &dependencies {
1937 TerminalReporter::print_progress(&format!(
1938 " {} → {} (via field '{}')",
1939 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1940 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1941 dep.field_name
1942 ));
1943 }
1944
1945 topological_sort(specs, &dependencies)
1946 }
1947
1948 async fn execute_single_spec(
1950 &self,
1951 spec: &OpenApiSpec,
1952 spec_name: &str,
1953 _external_values: &ExtractedValues,
1954 ) -> Result<ExtractedValues> {
1955 let parser = SpecParser::from_spec(spec.clone());
1956
1957 if self.crud_flow {
1959 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1961 } else {
1962 self.execute_standard_spec(&parser, spec_name).await?;
1964 Ok(ExtractedValues::new())
1965 }
1966 }
1967
1968 async fn execute_crud_flow_with_extraction(
1970 &self,
1971 parser: &SpecParser,
1972 spec_name: &str,
1973 ) -> Result<ExtractedValues> {
1974 let operations = parser.get_operations();
1975 let flows = CrudFlowDetector::detect_flows(&operations);
1976
1977 if flows.is_empty() {
1978 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1979 return Ok(ExtractedValues::new());
1980 }
1981
1982 TerminalReporter::print_progress(&format!(
1983 " {} CRUD flow(s) in {}",
1984 flows.len(),
1985 spec_name
1986 ));
1987
1988 let mut handlebars = handlebars::Handlebars::new();
1990 handlebars.register_helper(
1992 "json",
1993 Box::new(
1994 |h: &handlebars::Helper,
1995 _: &handlebars::Handlebars,
1996 _: &handlebars::Context,
1997 _: &mut handlebars::RenderContext,
1998 out: &mut dyn handlebars::Output|
1999 -> handlebars::HelperResult {
2000 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2001 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2002 Ok(())
2003 },
2004 ),
2005 );
2006 let template = include_str!("templates/k6_crud_flow.hbs");
2007 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2008
2009 let custom_headers = self.parse_headers()?;
2010 let config = self.build_crud_flow_config().unwrap_or_default();
2011
2012 let param_overrides = if let Some(params_file) = &self.params_file {
2014 let overrides = ParameterOverrides::from_file(params_file)?;
2015 Some(overrides)
2016 } else {
2017 None
2018 };
2019
2020 let duration_secs = Self::parse_duration(&self.duration)?;
2022 let scenario =
2023 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2024 let stages = scenario.generate_stages(duration_secs, self.vus);
2025
2026 let api_base_path = self.resolve_base_path(parser);
2028
2029 let mut all_headers = custom_headers.clone();
2031 if let Some(auth) = &self.auth {
2032 all_headers.insert("Authorization".to_string(), auth.clone());
2033 }
2034 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2035
2036 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2038
2039 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2040 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2044 serde_json::json!({
2045 "name": sanitized_name.clone(),
2046 "display_name": f.name,
2047 "base_path": f.base_path,
2048 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2049 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2051 let method_raw = if !parts.is_empty() {
2052 parts[0].to_uppercase()
2053 } else {
2054 "GET".to_string()
2055 };
2056 let method = if !parts.is_empty() {
2057 let m = parts[0].to_lowercase();
2058 if m == "delete" { "del".to_string() } else { m }
2060 } else {
2061 "get".to_string()
2062 };
2063 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2064 let path = if let Some(ref bp) = api_base_path {
2066 format!("{}{}", bp, raw_path)
2067 } else {
2068 raw_path.to_string()
2069 };
2070 let is_get_or_head = method == "get" || method == "head";
2071 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2073
2074 let body_value = if has_body {
2076 param_overrides.as_ref()
2077 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2078 .and_then(|oo| oo.body)
2079 .unwrap_or_else(|| serde_json::json!({}))
2080 } else {
2081 serde_json::json!({})
2082 };
2083
2084 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2086
2087 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2089 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2090
2091 serde_json::json!({
2092 "operation": s.operation,
2093 "method": method,
2094 "path": path,
2095 "extract": s.extract,
2096 "use_values": s.use_values,
2097 "use_body": s.use_body,
2098 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2099 "inject_attacks": s.inject_attacks,
2100 "attack_types": s.attack_types,
2101 "description": s.description,
2102 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2103 "is_get_or_head": is_get_or_head,
2104 "has_body": has_body,
2105 "body": processed_body.value,
2106 "body_is_dynamic": body_is_dynamic,
2107 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2108 })
2109 }).collect::<Vec<_>>(),
2110 })
2111 }).collect();
2112
2113 for flow_data in &flows_data {
2115 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2116 for step in steps {
2117 if let Some(placeholders_arr) =
2118 step.get("_placeholders").and_then(|p| p.as_array())
2119 {
2120 for p_str in placeholders_arr {
2121 if let Some(p_name) = p_str.as_str() {
2122 match p_name {
2123 "VU" => {
2124 all_placeholders.insert(DynamicPlaceholder::VU);
2125 }
2126 "Iteration" => {
2127 all_placeholders.insert(DynamicPlaceholder::Iteration);
2128 }
2129 "Timestamp" => {
2130 all_placeholders.insert(DynamicPlaceholder::Timestamp);
2131 }
2132 "UUID" => {
2133 all_placeholders.insert(DynamicPlaceholder::UUID);
2134 }
2135 "Random" => {
2136 all_placeholders.insert(DynamicPlaceholder::Random);
2137 }
2138 "Counter" => {
2139 all_placeholders.insert(DynamicPlaceholder::Counter);
2140 }
2141 "Date" => {
2142 all_placeholders.insert(DynamicPlaceholder::Date);
2143 }
2144 "VuIter" => {
2145 all_placeholders.insert(DynamicPlaceholder::VuIter);
2146 }
2147 _ => {}
2148 }
2149 }
2150 }
2151 }
2152 }
2153 }
2154 }
2155
2156 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2158 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2159
2160 let security_testing_enabled = self.security_testing_enabled();
2162
2163 let data = serde_json::json!({
2164 "base_url": self.target,
2165 "flows": flows_data,
2166 "extract_fields": config.default_extract_fields,
2167 "duration_secs": duration_secs,
2168 "max_vus": self.vus,
2169 "auth_header": self.auth,
2170 "custom_headers": custom_headers,
2171 "skip_tls_verify": self.skip_tls_verify,
2172 "stages": stages.iter().map(|s| serde_json::json!({
2174 "duration": s.duration,
2175 "target": s.target,
2176 })).collect::<Vec<_>>(),
2177 "threshold_percentile": self.threshold_percentile,
2178 "threshold_ms": self.threshold_ms,
2179 "max_error_rate": self.max_error_rate,
2180 "abort_on_error": self.abort_on_error,
2181 "abort_on_error_rate": self.abort_on_error_rate,
2182 "headers": headers_json,
2183 "dynamic_imports": required_imports,
2184 "dynamic_globals": required_globals,
2185 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
2186 "security_testing_enabled": security_testing_enabled,
2188 "has_custom_headers": !custom_headers.is_empty(),
2189 });
2190
2191 let mut script = handlebars
2192 .render_template(template, &data)
2193 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2194
2195 if security_testing_enabled {
2197 script = self.generate_enhanced_script(&script)?;
2198 }
2199
2200 let script_path =
2202 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
2203
2204 std::fs::create_dir_all(self.output.clone())?;
2205 std::fs::write(&script_path, &script)?;
2206
2207 if !self.generate_only {
2208 let executor = K6Executor::new()?
2209 .with_local_ips(self.source_ips.join(","))
2210 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2211 std::fs::create_dir_all(&output_dir)?;
2212
2213 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2214
2215 let extracted = Self::parse_extracted_values(&output_dir)?;
2216 TerminalReporter::print_progress(&format!(
2217 " Extracted {} value(s) from {}",
2218 extracted.values.len(),
2219 spec_name
2220 ));
2221 return Ok(extracted);
2222 }
2223
2224 Ok(ExtractedValues::new())
2225 }
2226
2227 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
2229 let mut operations = if let Some(filter) = &self.operations {
2230 parser.filter_operations(filter)?
2231 } else {
2232 parser.get_operations()
2233 };
2234
2235 if let Some(exclude) = &self.exclude_operations {
2236 operations = parser.exclude_operations(operations, exclude)?;
2237 }
2238
2239 if operations.is_empty() {
2240 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
2241 return Ok(());
2242 }
2243
2244 TerminalReporter::print_progress(&format!(
2245 " {} operations in {}",
2246 operations.len(),
2247 spec_name
2248 ));
2249
2250 let templates: Vec<_> = operations
2252 .iter()
2253 .map(RequestGenerator::generate_template)
2254 .collect::<Result<Vec<_>>>()?;
2255
2256 let custom_headers = self.parse_headers()?;
2258
2259 let base_path = self.resolve_base_path(parser);
2261
2262 let scenario =
2264 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2265
2266 let security_testing_enabled = self.security_testing_enabled();
2267
2268 let k6_config = K6Config {
2269 target_url: self.target.clone(),
2270 base_path,
2271 scenario,
2272 duration_secs: Self::parse_duration(&self.duration)?,
2273 max_vus: self.vus,
2274 threshold_percentile: self.threshold_percentile.clone(),
2275 threshold_ms: self.threshold_ms,
2276 max_error_rate: self.max_error_rate,
2277 auth_header: self.auth.clone(),
2278 custom_headers,
2279 skip_tls_verify: self.skip_tls_verify,
2280 security_testing_enabled,
2281 chunked_request_bodies: self.chunked_request_bodies,
2282 target_rps: self.target_rps,
2283 no_keep_alive: self.no_keep_alive,
2284 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
2286 .into_iter()
2287 .map(|ip| ip.to_string())
2288 .collect(),
2289 geo_source_headers: if self.geo_source_headers.is_empty()
2290 && !self.geo_source_ips.is_empty()
2291 {
2292 crate::conformance::self_test::default_geo_source_headers()
2293 } else {
2294 self.geo_source_headers.clone()
2295 },
2296 };
2297
2298 let generator = K6ScriptGenerator::new(k6_config, templates)
2299 .with_abort_valve(self.abort_on_error, self.abort_on_error_rate);
2300 let mut script = generator.generate()?;
2301
2302 let has_advanced_features = self.data_file.is_some()
2304 || self.error_rate.is_some()
2305 || self.security_test
2306 || self.parallel_create.is_some()
2307 || self.wafbench_dir.is_some();
2308
2309 if has_advanced_features {
2310 script = self.generate_enhanced_script(&script)?;
2311 }
2312
2313 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
2315
2316 std::fs::create_dir_all(self.output.clone())?;
2317 std::fs::write(&script_path, &script)?;
2318
2319 if !self.generate_only {
2320 let executor = K6Executor::new()?
2323 .with_local_ips(self.source_ips.join(","))
2324 .with_dns_policy(self.dns_policy.clone().unwrap_or_default())
2325 .with_discard_response_bodies(self.discard_response_bodies);
2326 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2327 std::fs::create_dir_all(&output_dir)?;
2328
2329 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2330 }
2331
2332 Ok(())
2333 }
2334
2335 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
2337 let config = self.build_crud_flow_config().unwrap_or_default();
2339
2340 let flows = if !config.flows.is_empty() {
2342 TerminalReporter::print_progress("Using custom flow configuration...");
2343 config.flows.clone()
2344 } else {
2345 TerminalReporter::print_progress("Detecting CRUD operations...");
2346 let operations = parser.get_operations();
2347 CrudFlowDetector::detect_flows(&operations)
2348 };
2349
2350 if flows.is_empty() {
2351 return Err(BenchError::Other(
2352 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
2353 ));
2354 }
2355
2356 if config.flows.is_empty() {
2357 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2358 } else {
2359 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2360 }
2361
2362 for flow in &flows {
2363 TerminalReporter::print_progress(&format!(
2364 " - {}: {} steps",
2365 flow.name,
2366 flow.steps.len()
2367 ));
2368 }
2369
2370 let mut handlebars = handlebars::Handlebars::new();
2372 handlebars.register_helper(
2374 "json",
2375 Box::new(
2376 |h: &handlebars::Helper,
2377 _: &handlebars::Handlebars,
2378 _: &handlebars::Context,
2379 _: &mut handlebars::RenderContext,
2380 out: &mut dyn handlebars::Output|
2381 -> handlebars::HelperResult {
2382 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2383 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2384 Ok(())
2385 },
2386 ),
2387 );
2388 let template = include_str!("templates/k6_crud_flow.hbs");
2389
2390 let custom_headers = self.parse_headers()?;
2391
2392 let param_overrides = if let Some(params_file) = &self.params_file {
2394 TerminalReporter::print_progress("Loading parameter overrides...");
2395 let overrides = ParameterOverrides::from_file(params_file)?;
2396 TerminalReporter::print_success(&format!(
2397 "Loaded parameter overrides ({} operation-specific, {} defaults)",
2398 overrides.operations.len(),
2399 if overrides.defaults.is_empty() { 0 } else { 1 }
2400 ));
2401 Some(overrides)
2402 } else {
2403 None
2404 };
2405
2406 let duration_secs = Self::parse_duration(&self.duration)?;
2408 let scenario =
2409 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2410 let stages = scenario.generate_stages(duration_secs, self.vus);
2411
2412 let api_base_path = self.resolve_base_path(parser);
2414 if let Some(ref bp) = api_base_path {
2415 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2416 }
2417
2418 let mut all_headers = custom_headers.clone();
2420 if let Some(auth) = &self.auth {
2421 all_headers.insert("Authorization".to_string(), auth.clone());
2422 }
2423 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2424
2425 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2427
2428 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2429 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2434 serde_json::json!({
2435 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
2438 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2439 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2441 let method_raw = if !parts.is_empty() {
2442 parts[0].to_uppercase()
2443 } else {
2444 "GET".to_string()
2445 };
2446 let method = if !parts.is_empty() {
2447 let m = parts[0].to_lowercase();
2448 if m == "delete" { "del".to_string() } else { m }
2450 } else {
2451 "get".to_string()
2452 };
2453 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2454 let path = if let Some(ref bp) = api_base_path {
2456 format!("{}{}", bp, raw_path)
2457 } else {
2458 raw_path.to_string()
2459 };
2460 let is_get_or_head = method == "get" || method == "head";
2461 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2463
2464 let body_value = if has_body {
2466 param_overrides.as_ref()
2467 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2468 .and_then(|oo| oo.body)
2469 .unwrap_or_else(|| serde_json::json!({}))
2470 } else {
2471 serde_json::json!({})
2472 };
2473
2474 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2476 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2481 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2482
2483 serde_json::json!({
2484 "operation": s.operation,
2485 "method": method,
2486 "path": path,
2487 "extract": s.extract,
2488 "use_values": s.use_values,
2489 "use_body": s.use_body,
2490 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2491 "inject_attacks": s.inject_attacks,
2492 "attack_types": s.attack_types,
2493 "description": s.description,
2494 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2495 "is_get_or_head": is_get_or_head,
2496 "has_body": has_body,
2497 "body": processed_body.value,
2498 "body_is_dynamic": body_is_dynamic,
2499 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2500 })
2501 }).collect::<Vec<_>>(),
2502 })
2503 }).collect();
2504
2505 for flow_data in &flows_data {
2507 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2508 for step in steps {
2509 if let Some(placeholders_arr) =
2510 step.get("_placeholders").and_then(|p| p.as_array())
2511 {
2512 for p_str in placeholders_arr {
2513 if let Some(p_name) = p_str.as_str() {
2514 match p_name {
2516 "VU" => {
2517 all_placeholders.insert(DynamicPlaceholder::VU);
2518 }
2519 "Iteration" => {
2520 all_placeholders.insert(DynamicPlaceholder::Iteration);
2521 }
2522 "Timestamp" => {
2523 all_placeholders.insert(DynamicPlaceholder::Timestamp);
2524 }
2525 "UUID" => {
2526 all_placeholders.insert(DynamicPlaceholder::UUID);
2527 }
2528 "Random" => {
2529 all_placeholders.insert(DynamicPlaceholder::Random);
2530 }
2531 "Counter" => {
2532 all_placeholders.insert(DynamicPlaceholder::Counter);
2533 }
2534 "Date" => {
2535 all_placeholders.insert(DynamicPlaceholder::Date);
2536 }
2537 "VuIter" => {
2538 all_placeholders.insert(DynamicPlaceholder::VuIter);
2539 }
2540 _ => {}
2541 }
2542 }
2543 }
2544 }
2545 }
2546 }
2547 }
2548
2549 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2551 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2552
2553 let invalid_data_config = self.build_invalid_data_config();
2555 let error_injection_enabled = invalid_data_config.is_some();
2556 let error_rate = self.error_rate.unwrap_or(0.0);
2557 let error_types: Vec<String> = invalid_data_config
2558 .as_ref()
2559 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2560 .unwrap_or_default();
2561
2562 if error_injection_enabled {
2563 TerminalReporter::print_progress(&format!(
2564 "Error injection enabled ({}% rate)",
2565 (error_rate * 100.0) as u32
2566 ));
2567 }
2568
2569 let security_testing_enabled = self.security_testing_enabled();
2571
2572 let data = serde_json::json!({
2573 "base_url": self.target,
2574 "flows": flows_data,
2575 "extract_fields": config.default_extract_fields,
2576 "duration_secs": duration_secs,
2577 "max_vus": self.vus,
2578 "auth_header": self.auth,
2579 "custom_headers": custom_headers,
2580 "skip_tls_verify": self.skip_tls_verify,
2581 "stages": stages.iter().map(|s| serde_json::json!({
2583 "duration": s.duration,
2584 "target": s.target,
2585 })).collect::<Vec<_>>(),
2586 "threshold_percentile": self.threshold_percentile,
2587 "threshold_ms": self.threshold_ms,
2588 "max_error_rate": self.max_error_rate,
2589 "abort_on_error": self.abort_on_error,
2590 "abort_on_error_rate": self.abort_on_error_rate,
2591 "headers": headers_json,
2592 "dynamic_imports": required_imports,
2593 "dynamic_globals": required_globals,
2594 "extracted_values_output_path": self
2595 .output
2596 .join("crud_flow_extracted_values.json")
2597 .to_string_lossy(),
2598 "error_injection_enabled": error_injection_enabled,
2600 "error_rate": error_rate,
2601 "error_types": error_types,
2602 "security_testing_enabled": security_testing_enabled,
2604 "has_custom_headers": !custom_headers.is_empty(),
2605 });
2606
2607 let mut script = handlebars
2608 .render_template(template, &data)
2609 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2610
2611 if security_testing_enabled {
2613 script = self.generate_enhanced_script(&script)?;
2614 }
2615
2616 TerminalReporter::print_progress("Validating CRUD flow script...");
2618 let validation_errors = K6ScriptGenerator::validate_script(&script);
2619 if !validation_errors.is_empty() {
2620 TerminalReporter::print_error("CRUD flow script validation failed");
2621 for error in &validation_errors {
2622 eprintln!(" {}", error);
2623 }
2624 return Err(BenchError::Other(format!(
2625 "CRUD flow script validation failed with {} error(s)",
2626 validation_errors.len()
2627 )));
2628 }
2629
2630 TerminalReporter::print_success("CRUD flow script generated");
2631
2632 let script_path = if let Some(output) = &self.script_output {
2634 output.clone()
2635 } else {
2636 self.output.join("k6-crud-flow-script.js")
2637 };
2638
2639 if let Some(parent) = script_path.parent() {
2640 std::fs::create_dir_all(parent)?;
2641 }
2642 std::fs::write(&script_path, &script)?;
2643 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2644
2645 if self.generate_only {
2646 println!("\nScript generated successfully. Run it with:");
2647 println!(" k6 run {}", script_path.display());
2648 return Ok(());
2649 }
2650
2651 TerminalReporter::print_progress("Executing CRUD flow test...");
2653 let executor = K6Executor::new()?
2654 .with_local_ips(self.source_ips.join(","))
2655 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
2656 std::fs::create_dir_all(&self.output)?;
2657
2658 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2659
2660 let duration_secs = Self::parse_duration(&self.duration)?;
2661 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2662
2663 Ok(())
2664 }
2665
2666 async fn execute_conformance_test(&self) -> Result<()> {
2668 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2669 use crate::conformance::report::ConformanceReport;
2670 use crate::conformance::spec::ConformanceFeature;
2671
2672 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2673
2674 TerminalReporter::print_progress(CONFORMANCE_REPLACES_LOAD_ADVISORY);
2675
2676 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2678 cats_str
2679 .split(',')
2680 .filter_map(|s| {
2681 let trimmed = s.trim();
2682 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2683 Some(canonical.to_string())
2684 } else {
2685 TerminalReporter::print_warning(&format!(
2686 "Unknown conformance category: '{}'. Valid categories: {}",
2687 trimmed,
2688 ConformanceFeature::cli_category_names()
2689 .iter()
2690 .map(|(cli, _)| *cli)
2691 .collect::<Vec<_>>()
2692 .join(", ")
2693 ));
2694 None
2695 }
2696 })
2697 .collect::<Vec<String>>()
2698 });
2699
2700 let custom_headers: Vec<(String, String)> = self
2702 .conformance_headers
2703 .iter()
2704 .filter_map(|h| {
2705 let (name, value) = h.split_once(':')?;
2706 Some((name.trim().to_string(), value.trim().to_string()))
2707 })
2708 .collect();
2709
2710 if !custom_headers.is_empty() {
2711 TerminalReporter::print_progress(&format!(
2712 "Using {} custom header(s) for authentication",
2713 custom_headers.len()
2714 ));
2715 }
2716
2717 if self.conformance_delay_ms > 0 {
2718 TerminalReporter::print_progress(&format!(
2719 "Using {}ms delay between conformance requests",
2720 self.conformance_delay_ms
2721 ));
2722 }
2723
2724 std::fs::create_dir_all(&self.output)?;
2726
2727 let config = ConformanceConfig {
2728 target_url: self.target.clone(),
2729 api_key: self.conformance_api_key.clone(),
2730 basic_auth: self.conformance_basic_auth.clone(),
2731 skip_tls_verify: self.skip_tls_verify,
2732 categories,
2733 base_path: self.base_path.clone(),
2734 custom_headers,
2735 output_dir: Some(self.output.clone()),
2736 all_operations: self.conformance_all_operations,
2737 custom_checks_file: self.conformance_custom.clone(),
2738 request_delay_ms: self.conformance_delay_ms,
2739 custom_filter: self.conformance_custom_filter.clone(),
2740 export_requests: self.export_requests,
2741 validate_requests: self.validate_requests,
2742 };
2743
2744 let mut resolved_base_path: Option<String> = None;
2752 let annotated_ops = if !self.spec.is_empty() {
2753 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2754 let parser = SpecParser::from_file(&self.spec[0]).await?;
2755 resolved_base_path = self.resolve_base_path(&parser);
2756
2757 let mut operations = if let Some(filter) = &self.operations {
2762 parser.filter_operations(filter)?
2763 } else {
2764 parser.get_operations()
2765 };
2766 if let Some(exclude) = &self.exclude_operations {
2767 let before_count = operations.len();
2768 operations = parser.exclude_operations(operations, exclude)?;
2769 let excluded_count = before_count - operations.len();
2770 if excluded_count > 0 {
2771 TerminalReporter::print_progress(&format!(
2772 "Excluded {} operations matching '{}'",
2773 excluded_count, exclude
2774 ));
2775 }
2776 }
2777
2778 let annotated =
2779 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2780 &operations,
2781 parser.spec(),
2782 );
2783 TerminalReporter::print_success(&format!(
2784 "Analyzed {} operations, found {} feature annotations",
2785 operations.len(),
2786 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2787 ));
2788 Some(annotated)
2789 } else {
2790 None
2791 };
2792
2793 if self.conformance_self_test {
2800 let Some(ops) = annotated_ops else {
2801 TerminalReporter::print_error(
2802 "--conformance-self-test requires --spec; no operations to test",
2803 );
2804 return Ok(());
2805 };
2806 let cfg = crate::conformance::self_test::SelfTestConfig {
2807 target_url: self.target.clone(),
2808 skip_tls_verify: self.skip_tls_verify,
2809 timeout: std::time::Duration::from_secs(30),
2810 extra_headers: self
2814 .conformance_headers
2815 .iter()
2816 .filter_map(|h| {
2817 let (n, v) = h.split_once(':')?;
2818 Some((n.trim().to_string(), v.trim().to_string()))
2819 })
2820 .collect(),
2821 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2822 base_path: resolved_base_path.clone(),
2826 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2830 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2831 geo_source_headers: if self.geo_source_headers.is_empty() {
2832 crate::conformance::self_test::default_geo_source_headers()
2833 } else {
2834 self.geo_source_headers.clone()
2835 },
2836 capture: if self.conformance_self_test_capture
2840 || self.validate_response_schemas
2841 || self.validate_requests
2842 {
2843 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2854 } else {
2855 None
2856 },
2857 validate_response_schemas: self.validate_response_schemas,
2858 spec_label: self.spec.first().map(|p| {
2864 p.file_name()
2865 .map(|s| s.to_string_lossy().into_owned())
2866 .unwrap_or_else(|| p.to_string_lossy().into_owned())
2867 }),
2868 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2875 current_iteration: 1,
2876 };
2877 let capture_sink = cfg.capture.clone();
2878 let network_events_sink = cfg.network_events.clone();
2879 TerminalReporter::print_progress(&format!(
2880 "Self-test mode: driving {} operations with positive + per-category negative cases",
2881 ops.len()
2882 ));
2883 let target_iterations = self.conformance_self_test_iterations.max(1);
2890 let duration_budget = self
2891 .conformance_self_test_duration
2892 .as_ref()
2893 .map(|s| Self::parse_duration(s))
2894 .transpose()?
2895 .map(std::time::Duration::from_secs);
2896 let start = std::time::Instant::now();
2897 let deadline = duration_budget.map(|d| start + d);
2906 let mut cfg = cfg;
2910 cfg.current_iteration = 1;
2911 let mut report =
2912 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2913 .await
2914 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2915 let mut iter_done: u32 = 1;
2916 loop {
2917 let by_iter = iter_done >= target_iterations;
2918 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2919 if by_iter && by_dur {
2920 break;
2921 }
2922 cfg.current_iteration = iter_done.saturating_add(1);
2923 let next = crate::conformance::self_test::run_self_test_with_deadline(
2924 &ops, &cfg, deadline,
2925 )
2926 .await
2927 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2928 report.merge_iteration(next);
2929 iter_done = iter_done.saturating_add(1);
2930 }
2931 if iter_done > 1 {
2932 TerminalReporter::print_progress(&format!(
2933 "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2934 iter_done,
2935 start.elapsed(),
2936 ));
2937 }
2938 let per_endpoint_summary: Vec<
2948 crate::conformance::per_endpoint_summary::PerEndpointSummary,
2949 >;
2950 if let Some(sink) = capture_sink {
2951 if let Ok(guard) = sink.lock() {
2952 let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2953 let mut lines = String::with_capacity(guard.len() * 256);
2954 for entry in guard.iter() {
2955 if let Ok(line) = serde_json::to_string(entry) {
2956 lines.push_str(&line);
2957 lines.push('\n');
2958 }
2959 }
2960 let _ = std::fs::write(&jsonl_path, lines);
2961 let html_path = self.output.join("conformance-self-test-requests.html");
2962 let html =
2963 crate::conformance::capture_html::render_capture_html(guard.as_slice());
2964 let _ = std::fs::write(&html_path, html);
2965
2966 per_endpoint_summary =
2970 crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2971 let summary_path = self.output.join("conformance-per-endpoint.json");
2972 if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2973 let _ = std::fs::write(&summary_path, json);
2974 TerminalReporter::print_progress(&format!(
2975 "Self-test request/response capture written to {} ({} entries) + {} + {}",
2976 jsonl_path.display(),
2977 guard.len(),
2978 html_path.display(),
2979 summary_path.display(),
2980 ));
2981 } else {
2982 TerminalReporter::print_progress(&format!(
2983 "Self-test request/response capture written to {} ({} entries) + {}",
2984 jsonl_path.display(),
2985 guard.len(),
2986 html_path.display(),
2987 ));
2988 }
2989 } else {
2990 per_endpoint_summary = Vec::new();
2991 }
2992 } else {
2993 per_endpoint_summary = Vec::new();
2994 }
2995 TerminalReporter::print_progress(&report.render_summary());
2996 if let Some(sink) = network_events_sink {
3003 if let Ok(guard) = sink.lock() {
3004 let path = self.output.join("conformance-network-events.json");
3005 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3006 let _ = std::fs::write(&path, json);
3007 if guard.is_empty() {
3008 TerminalReporter::print_progress(
3009 "No wire-level network failures during self-test (file written empty)",
3010 );
3011 } else {
3012 TerminalReporter::print_warning(&format!(
3013 "Recorded {} wire-level network event(s) to {}",
3014 guard.len(),
3015 path.display()
3016 ));
3017 }
3018 }
3019 }
3020 }
3021 let json_path = self.output.join("conformance-self-test.json");
3025 if let Ok(json) = serde_json::to_string_pretty(&report) {
3026 let _ = std::fs::write(&json_path, json);
3027 TerminalReporter::print_progress(&format!(
3028 "Self-test report written to {}",
3029 json_path.display()
3030 ));
3031 }
3032 let issues = report.definite_issues();
3036 let issues_path = self.output.join("conformance-definite-issues.json");
3037 if let Ok(json) = serde_json::to_string_pretty(&issues) {
3038 if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
3039 TerminalReporter::print_warning(&format!(
3040 "{} definite issue(s) — see {}",
3041 issues.len(),
3042 issues_path.display()
3043 ));
3044 }
3045 }
3046 let owasp_accepted = report.owasp_accepted_probes();
3049 if !owasp_accepted.is_empty() {
3050 let owasp_path = self.output.join("conformance-owasp-accepted.json");
3051 if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
3052 if std::fs::write(&owasp_path, json).is_ok() {
3053 TerminalReporter::print_warning(&format!(
3054 "{} owasp injection probe(s) accepted by the target — see {}",
3055 owasp_accepted.len(),
3056 owasp_path.display()
3057 ));
3058 }
3059 }
3060 }
3061 if let Some(status) = report.detect_target_misconfiguration() {
3070 let hint = match status {
3071 404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
3072 401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
3073 _ => "",
3074 };
3075 TerminalReporter::print_warning(&format!(
3076 "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
3077 ));
3078 } else if !report.all_passed() {
3079 TerminalReporter::print_warning(
3080 "Self-test detected gaps — server let through at least one request that should have been a 4xx",
3081 );
3082 } else {
3083 TerminalReporter::print_success(
3084 "Self-test passed — all positive cases accepted and all negative cases rejected",
3085 );
3086 }
3087 let html_path = self.output.join("conformance-report.html");
3094 let audit_path = self.output.join("conformance-spec-audit.json");
3095 let audit_value = std::fs::read_to_string(&audit_path)
3096 .ok()
3097 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
3098 let render_opts = crate::conformance::report_html::RenderOptions {
3103 missed_cap: match self.report_missed_cap {
3104 Some(0) => None,
3105 Some(n) => Some(n as usize),
3106 None => Some(200),
3107 },
3108 };
3109 let mut html = crate::conformance::report_html::render_html_with_options(
3110 &report,
3111 audit_value.as_ref(),
3112 &render_opts,
3113 );
3114 let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
3120 &per_endpoint_summary,
3121 );
3122 if !summary_section.is_empty() {
3123 if let Some(idx) = html.rfind("</body>") {
3124 html.insert_str(idx, &summary_section);
3125 } else {
3126 html.push_str(&summary_section);
3127 }
3128 }
3129 if std::fs::write(&html_path, html).is_ok() {
3130 TerminalReporter::print_progress(&format!(
3131 "HTML report written to {}",
3132 html_path.display()
3133 ));
3134 }
3135
3136 if self.validate_requests && !self.spec.is_empty() {
3148 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3149 &self.spec,
3150 &self.output,
3151 self.base_path.as_deref(),
3152 )
3153 .await?;
3154 if n > 0 {
3155 TerminalReporter::print_warning(&format!(
3156 "{} emitted request(s) recorded against the spec — see conformance-request-violations.json",
3157 n
3158 ));
3159 }
3160 }
3161 return Ok(());
3162 }
3163
3164 if self.validate_requests && !self.spec.is_empty() {
3166 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
3167 let violation_count = crate::conformance::request_validator::run_request_validation(
3168 &self.spec,
3169 self.conformance_custom.as_deref(),
3170 self.base_path.as_deref(),
3171 &self.output,
3172 )
3173 .await?;
3174 if violation_count > 0 {
3175 TerminalReporter::print_warning(&format!(
3176 "{} request validation violation(s) found — see conformance-request-violations.json",
3177 violation_count
3178 ));
3179 } else {
3180 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
3181 }
3182 }
3183
3184 if self.generate_only || self.use_k6 {
3186 let script = if let Some(annotated) = &annotated_ops {
3187 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3188 config,
3189 annotated.clone(),
3190 );
3191 let op_count = gen.operation_count();
3192 let (script, check_count) = gen.generate()?;
3193 TerminalReporter::print_success(&format!(
3194 "Conformance: {} operations analyzed, {} unique checks generated",
3195 op_count, check_count
3196 ));
3197 script
3198 } else {
3199 let generator = ConformanceGenerator::new(config);
3200 generator.generate()?
3201 };
3202
3203 let script_path = self.output.join("k6-conformance.js");
3204 std::fs::write(&script_path, &script).map_err(|e| {
3205 BenchError::Other(format!("Failed to write conformance script: {}", e))
3206 })?;
3207 TerminalReporter::print_success(&format!(
3208 "Conformance script generated: {}",
3209 script_path.display()
3210 ));
3211
3212 if self.generate_only {
3213 println!("\nScript generated. Run with:");
3214 println!(" k6 run {}", script_path.display());
3215 return Ok(());
3216 }
3217
3218 if !K6Executor::is_k6_installed() {
3220 TerminalReporter::print_error("k6 is not installed");
3221 TerminalReporter::print_warning(
3222 "Install k6 from: https://k6.io/docs/get-started/installation/",
3223 );
3224 return Err(BenchError::K6NotFound);
3225 }
3226
3227 K6Executor::warn_if_pre_v1().await;
3228 TerminalReporter::print_progress("Running conformance tests via k6...");
3229 let executor = K6Executor::new()?
3230 .with_local_ips(self.source_ips.join(","))
3231 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3232 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3233
3234 let report_path = self.output.join("conformance-report.json");
3235 if report_path.exists() {
3236 let report = ConformanceReport::from_file(&report_path)?;
3237 report.print_report_with_options(self.conformance_all_operations);
3238 self.save_conformance_report(&report, &report_path)?;
3239 } else {
3240 TerminalReporter::print_warning(
3241 "Conformance report not generated (k6 handleSummary may not have run)",
3242 );
3243 }
3244
3245 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3257 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3258 &self.spec,
3259 &self.output,
3260 self.base_path.as_deref(),
3261 )
3262 .await?;
3263 if n > 0 {
3264 TerminalReporter::print_warning(&format!(
3265 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3266 n
3267 ));
3268 }
3269 }
3270
3271 return Ok(());
3272 }
3273
3274 TerminalReporter::print_progress("Running conformance tests (native executor)...");
3276
3277 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3278
3279 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3289 executor = if let Some(annotated) = &annotated_ops {
3290 executor.with_spec_driven_checks(annotated)
3291 } else if custom_only {
3292 executor
3293 } else {
3294 executor.with_reference_checks()
3295 };
3296 executor = executor.with_custom_checks()?;
3297
3298 TerminalReporter::print_success(&format!(
3299 "Executing {} conformance checks...",
3300 executor.check_count()
3301 ));
3302
3303 let report = executor.execute().await?;
3304 report.print_report_with_options(self.conformance_all_operations);
3305
3306 let failure_details = report.failure_details();
3308 if !failure_details.is_empty() {
3309 let details_path = self.output.join("conformance-failure-details.json");
3310 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3311 let _ = std::fs::write(&details_path, json);
3312 TerminalReporter::print_success(&format!(
3313 "Failure details saved to: {}",
3314 details_path.display()
3315 ));
3316 }
3317 }
3318
3319 let report_path = self.output.join("conformance-report.json");
3321 let report_json = serde_json::to_string_pretty(&report.to_json())
3322 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3323 std::fs::write(&report_path, &report_json)
3324 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3325 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3326
3327 self.save_conformance_report(&report, &report_path)?;
3328
3329 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3340 let n =
3341 crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3342 &self.spec,
3343 &self.output,
3344 self.base_path.as_deref(),
3345 )
3346 .await?;
3347 if n > 0 {
3348 TerminalReporter::print_warning(&format!(
3349 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3350 n
3351 ));
3352 }
3353 }
3354
3355 Ok(())
3356 }
3357
3358 fn save_conformance_report(
3360 &self,
3361 report: &crate::conformance::report::ConformanceReport,
3362 report_path: &Path,
3363 ) -> Result<()> {
3364 if self.conformance_report_format == "sarif" {
3365 use crate::conformance::sarif::ConformanceSarifReport;
3366 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3367 TerminalReporter::print_success(&format!(
3368 "SARIF report saved to: {}",
3369 self.conformance_report.display()
3370 ));
3371 } else if self.conformance_report != *report_path {
3372 std::fs::copy(report_path, &self.conformance_report)?;
3373 TerminalReporter::print_success(&format!(
3374 "Report saved to: {}",
3375 self.conformance_report.display()
3376 ));
3377 }
3378 Ok(())
3379 }
3380
3381 async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3393 use crate::conformance::self_test::SelfTestConfig;
3394
3395 TerminalReporter::print_progress("Multi-target conformance self-test mode");
3396 let targets = parse_targets_file(targets_file)?;
3397 if targets.is_empty() {
3398 return Err(BenchError::Other("No targets found in file".to_string()));
3399 }
3400 TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3401
3402 let annotated_ops = if !self.spec.is_empty() {
3404 let parser = SpecParser::from_file(&self.spec[0]).await?;
3405 let operations = parser.get_operations();
3406 Some(
3407 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3408 &operations,
3409 parser.spec(),
3410 ),
3411 )
3412 } else {
3413 return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3414 };
3415 let Some(ops) = annotated_ops else {
3416 unreachable!()
3417 };
3418
3419 std::fs::create_dir_all(&self.output)?;
3420 let resolved_base_path = self.base_path.clone();
3421 let target_iterations = self.conformance_self_test_iterations.max(1);
3422 let duration_budget = self
3423 .conformance_self_test_duration
3424 .as_ref()
3425 .map(|s| Self::parse_duration(s))
3426 .transpose()?
3427 .map(std::time::Duration::from_secs);
3428
3429 for (idx, target) in targets.iter().enumerate() {
3430 let target_dir = self.output.join(format!("target_{}", idx));
3431 std::fs::create_dir_all(&target_dir)?;
3432 TerminalReporter::print_progress(&format!(
3433 "[target {}/{}] {}",
3434 idx + 1,
3435 targets.len(),
3436 target.url
3437 ));
3438
3439 let merged_headers: Vec<(String, String)> = self
3440 .conformance_headers
3441 .iter()
3442 .filter_map(|h| {
3443 let (n, v) = h.split_once(':')?;
3444 Some((n.trim().to_string(), v.trim().to_string()))
3445 })
3446 .collect();
3447
3448 let cfg = SelfTestConfig {
3449 target_url: target.url.clone(),
3450 skip_tls_verify: self.skip_tls_verify,
3451 timeout: std::time::Duration::from_secs(30),
3452 extra_headers: merged_headers,
3453 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3454 base_path: resolved_base_path.clone(),
3455 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3456 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3457 geo_source_headers: if self.geo_source_headers.is_empty() {
3458 crate::conformance::self_test::default_geo_source_headers()
3459 } else {
3460 self.geo_source_headers.clone()
3461 },
3462 capture: if self.conformance_self_test_capture
3463 || self.validate_response_schemas
3464 || self.validate_requests
3465 {
3466 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3470 } else {
3471 None
3472 },
3473 validate_response_schemas: self.validate_response_schemas,
3474 spec_label: self.spec.first().map(|p| {
3475 p.file_name()
3476 .map(|s| s.to_string_lossy().into_owned())
3477 .unwrap_or_else(|| p.to_string_lossy().into_owned())
3478 }),
3479 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3480 current_iteration: 1,
3481 };
3482 let capture_sink = cfg.capture.clone();
3483 let network_events_sink = cfg.network_events.clone();
3484
3485 let start = std::time::Instant::now();
3486 let deadline = duration_budget.map(|d| start + d);
3490 let mut cfg = cfg;
3494 cfg.current_iteration = 1;
3495 let mut report =
3496 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3497 .await
3498 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3499 let mut iter_done: u32 = 1;
3500 loop {
3501 let by_iter = iter_done >= target_iterations;
3502 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3503 if by_iter && by_dur {
3504 break;
3505 }
3506 cfg.current_iteration = iter_done.saturating_add(1);
3507 let next = crate::conformance::self_test::run_self_test_with_deadline(
3508 &ops, &cfg, deadline,
3509 )
3510 .await
3511 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3512 report.merge_iteration(next);
3513 iter_done = iter_done.saturating_add(1);
3514 }
3515 if iter_done > 1 {
3516 TerminalReporter::print_progress(&format!(
3517 " ran {} iteration(s) in {:.1?}",
3518 iter_done,
3519 start.elapsed(),
3520 ));
3521 }
3522
3523 if let Some(sink) = capture_sink {
3525 if let Ok(guard) = sink.lock() {
3526 let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3527 let mut lines = String::with_capacity(guard.len() * 256);
3528 for entry in guard.iter() {
3529 if let Ok(line) = serde_json::to_string(entry) {
3530 lines.push_str(&line);
3531 lines.push('\n');
3532 }
3533 }
3534 let _ = std::fs::write(&jsonl, lines);
3535 }
3536 }
3537 if let Some(sink) = network_events_sink {
3538 if let Ok(guard) = sink.lock() {
3539 let path = target_dir.join("conformance-network-events.json");
3540 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3541 let _ = std::fs::write(&path, json);
3542 if !guard.is_empty() {
3543 TerminalReporter::print_warning(&format!(
3544 " recorded {} wire-level network event(s)",
3545 guard.len()
3546 ));
3547 }
3548 }
3549 }
3550 }
3551
3552 let json_path = target_dir.join("conformance-self-test.json");
3553 if let Ok(json) = serde_json::to_string_pretty(&report) {
3554 let _ = std::fs::write(&json_path, json);
3555 }
3556 let issues = report.definite_issues();
3559 if let Ok(json) = serde_json::to_string_pretty(&issues) {
3560 let issues_path = target_dir.join("conformance-definite-issues.json");
3561 if std::fs::write(&issues_path, json).is_ok() && !issues.is_empty() {
3562 TerminalReporter::print_warning(&format!(
3563 " {} definite issue(s) — see {}",
3564 issues.len(),
3565 issues_path.display()
3566 ));
3567 }
3568 }
3569 let owasp_accepted = report.owasp_accepted_probes();
3571 if !owasp_accepted.is_empty() {
3572 if let Ok(json) = serde_json::to_string_pretty(&owasp_accepted) {
3573 let owasp_path = target_dir.join("conformance-owasp-accepted.json");
3574 if std::fs::write(&owasp_path, json).is_ok() {
3575 TerminalReporter::print_warning(&format!(
3576 " {} owasp injection probe(s) accepted by the target — see {}",
3577 owasp_accepted.len(),
3578 owasp_path.display()
3579 ));
3580 }
3581 }
3582 }
3583 TerminalReporter::print_progress(&report.render_summary());
3584
3585 if self.validate_requests {
3594 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3595 &self.spec,
3596 &target_dir,
3597 self.base_path.as_deref(),
3598 )
3599 .await?;
3600 if n > 0 {
3601 TerminalReporter::print_warning(&format!(
3602 " {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3603 n,
3604 target_dir.display(),
3605 ));
3606 }
3607 }
3608 }
3609
3610 Ok(())
3611 }
3612
3613 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3619 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3620 use crate::conformance::report::ConformanceReport;
3621 use crate::conformance::spec::ConformanceFeature;
3622
3623 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3624
3625 TerminalReporter::print_progress("Parsing targets file...");
3627 let targets = parse_targets_file(targets_file)?;
3628 let num_targets = targets.len();
3629 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3630
3631 if targets.is_empty() {
3632 return Err(BenchError::Other("No targets found in file".to_string()));
3633 }
3634
3635 TerminalReporter::print_progress(CONFORMANCE_REPLACES_LOAD_ADVISORY);
3636
3637 let categories = self.conformance_categories.as_ref().map(|cats_str| {
3639 cats_str
3640 .split(',')
3641 .filter_map(|s| {
3642 let trimmed = s.trim();
3643 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3644 Some(canonical.to_string())
3645 } else {
3646 TerminalReporter::print_warning(&format!(
3647 "Unknown conformance category: '{}'. Valid categories: {}",
3648 trimmed,
3649 ConformanceFeature::cli_category_names()
3650 .iter()
3651 .map(|(cli, _)| *cli)
3652 .collect::<Vec<_>>()
3653 .join(", ")
3654 ));
3655 None
3656 }
3657 })
3658 .collect::<Vec<String>>()
3659 });
3660
3661 let base_custom_headers: Vec<(String, String)> = self
3663 .conformance_headers
3664 .iter()
3665 .filter_map(|h| {
3666 let (name, value) = h.split_once(':')?;
3667 Some((name.trim().to_string(), value.trim().to_string()))
3668 })
3669 .collect();
3670
3671 if !base_custom_headers.is_empty() {
3672 TerminalReporter::print_progress(&format!(
3673 "Using {} base custom header(s) for authentication",
3674 base_custom_headers.len()
3675 ));
3676 }
3677
3678 let annotated_ops = if !self.spec.is_empty() {
3680 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3681 let parser = SpecParser::from_file(&self.spec[0]).await?;
3682 let operations = parser.get_operations();
3683 let annotated =
3684 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3685 &operations,
3686 parser.spec(),
3687 );
3688 TerminalReporter::print_success(&format!(
3689 "Analyzed {} operations, found {} feature annotations",
3690 operations.len(),
3691 annotated.iter().map(|a| a.features.len()).sum::<usize>()
3692 ));
3693 Some(annotated)
3694 } else {
3695 None
3696 };
3697
3698 std::fs::create_dir_all(&self.output)?;
3700
3701 struct TargetResult {
3703 url: String,
3704 passed: usize,
3705 failed: usize,
3706 elapsed: std::time::Duration,
3707 report_json: serde_json::Value,
3708 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3709 }
3710
3711 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3712 let total_start = std::time::Instant::now();
3713
3714 for (idx, target) in targets.iter().enumerate() {
3715 tracing::info!(
3716 "Running conformance tests against target {}/{}: {}",
3717 idx + 1,
3718 num_targets,
3719 target.url
3720 );
3721 TerminalReporter::print_progress(&format!(
3722 "\n--- Target {}/{}: {} ---",
3723 idx + 1,
3724 num_targets,
3725 target.url
3726 ));
3727
3728 let mut merged_headers = base_custom_headers.clone();
3730 if let Some(ref target_headers) = target.headers {
3731 for (name, value) in target_headers {
3732 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3734 existing.1 = value.clone();
3735 } else {
3736 merged_headers.push((name.clone(), value.clone()));
3737 }
3738 }
3739 }
3740 if let Some(ref auth) = target.auth {
3742 if let Some(existing) =
3743 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3744 {
3745 existing.1 = auth.clone();
3746 } else {
3747 merged_headers.push(("Authorization".to_string(), auth.clone()));
3748 }
3749 }
3750
3751 let target_dir = self.output.join(format!("target_{}", idx));
3757 std::fs::create_dir_all(&target_dir)?;
3758
3759 let config = ConformanceConfig {
3760 target_url: target.url.clone(),
3761 api_key: self.conformance_api_key.clone(),
3762 basic_auth: self.conformance_basic_auth.clone(),
3763 skip_tls_verify: self.skip_tls_verify,
3764 categories: categories.clone(),
3765 base_path: self.base_path.clone(),
3766 custom_headers: merged_headers,
3767 output_dir: Some(target_dir.clone()),
3768 all_operations: self.conformance_all_operations,
3769 custom_checks_file: self.conformance_custom.clone(),
3770 request_delay_ms: self.conformance_delay_ms,
3771 custom_filter: self.conformance_custom_filter.clone(),
3772 export_requests: self.export_requests,
3773 validate_requests: self.validate_requests,
3774 };
3775
3776 let target_start = std::time::Instant::now();
3777 let report = if self.use_k6 {
3778 if !K6Executor::is_k6_installed() {
3779 TerminalReporter::print_error("k6 is not installed");
3780 TerminalReporter::print_warning(
3781 "Install k6 from: https://k6.io/docs/get-started/installation/",
3782 );
3783 return Err(BenchError::K6NotFound);
3784 }
3785 K6Executor::warn_if_pre_v1().await;
3786
3787 let script = if let Some(ref annotated) = annotated_ops {
3788 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3789 config.clone(),
3790 annotated.clone(),
3791 );
3792 let (script, _check_count) = gen.generate()?;
3793 script
3794 } else {
3795 let generator = ConformanceGenerator::new(config.clone());
3796 generator.generate()?
3797 };
3798
3799 let script_path = target_dir.join("k6-conformance.js");
3800 std::fs::write(&script_path, &script).map_err(|e| {
3801 BenchError::Other(format!("Failed to write conformance script: {}", e))
3802 })?;
3803 TerminalReporter::print_success(&format!(
3804 "Conformance script generated: {}",
3805 script_path.display()
3806 ));
3807
3808 TerminalReporter::print_progress(&format!(
3809 "Running conformance tests via k6 against {}...",
3810 target.url
3811 ));
3812 let k6 = K6Executor::new()?
3813 .with_local_ips(self.source_ips.join(","))
3814 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
3815 let api_port = 6565u16.saturating_add(idx as u16);
3817 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3818 .await?;
3819
3820 let report_path = target_dir.join("conformance-report.json");
3821 if report_path.exists() {
3822 ConformanceReport::from_file(&report_path)?
3823 } else {
3824 TerminalReporter::print_warning(&format!(
3825 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3826 target.url
3827 ));
3828 continue;
3829 }
3830 } else {
3831 let mut executor =
3832 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3833
3834 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3837 executor = if let Some(ref annotated) = annotated_ops {
3838 executor.with_spec_driven_checks(annotated)
3839 } else if custom_only {
3840 executor
3841 } else {
3842 executor.with_reference_checks()
3843 };
3844 executor = executor.with_custom_checks()?;
3845
3846 TerminalReporter::print_success(&format!(
3847 "Executing {} conformance checks against {}...",
3848 executor.check_count(),
3849 target.url
3850 ));
3851
3852 executor.execute().await?
3853 };
3854 let target_elapsed = target_start.elapsed();
3855
3856 let report_json = report.to_json();
3857
3858 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3860 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3861 let total_checks = passed + failed;
3862 let rate = if total_checks == 0 {
3863 0.0
3864 } else {
3865 (passed as f64 / total_checks as f64) * 100.0
3866 };
3867
3868 TerminalReporter::print_success(&format!(
3869 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3870 target.url,
3871 passed,
3872 total_checks,
3873 rate,
3874 target_elapsed.as_secs_f64()
3875 ));
3876
3877 let target_report_path = target_dir.join("conformance-report.json");
3879 let report_str = serde_json::to_string_pretty(&report_json)
3880 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3881 std::fs::write(&target_report_path, &report_str)
3882 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3883
3884 let failure_details = report.failure_details();
3886 if !failure_details.is_empty() {
3887 let details_path = target_dir.join("conformance-failure-details.json");
3888 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3889 let _ = std::fs::write(&details_path, json);
3890 }
3891 }
3892
3893 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3900 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3901 &self.spec,
3902 &target_dir,
3903 self.base_path.as_deref(),
3904 )
3905 .await?;
3906 if n > 0 {
3907 TerminalReporter::print_warning(&format!(
3908 "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3909 target.url,
3910 n,
3911 target_dir.display()
3912 ));
3913 }
3914 }
3915
3916 let owasp_coverage = report.owasp_coverage_data();
3918
3919 target_results.push(TargetResult {
3920 url: target.url.clone(),
3921 passed,
3922 failed,
3923 elapsed: target_elapsed,
3924 report_json,
3925 owasp_coverage,
3926 });
3927 }
3928
3929 let total_elapsed = total_start.elapsed();
3930
3931 println!("\n{}", "=".repeat(80));
3933 println!(" Multi-Target Conformance Summary");
3934 println!("{}", "=".repeat(80));
3935 println!(
3936 " {:<40} {:>8} {:>8} {:>8} {:>8}",
3937 "Target URL", "Passed", "Failed", "Rate", "Time"
3938 );
3939 println!(" {}", "-".repeat(76));
3940
3941 let mut total_passed = 0usize;
3942 let mut total_failed = 0usize;
3943
3944 for result in &target_results {
3945 let total_checks = result.passed + result.failed;
3946 let rate = if total_checks == 0 {
3947 0.0
3948 } else {
3949 (result.passed as f64 / total_checks as f64) * 100.0
3950 };
3951
3952 let display_url = if result.url.len() > 38 {
3954 format!("{}...", &result.url[..35])
3955 } else {
3956 result.url.clone()
3957 };
3958
3959 println!(
3960 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3961 display_url,
3962 result.passed,
3963 result.failed,
3964 rate,
3965 result.elapsed.as_secs_f64()
3966 );
3967
3968 total_passed += result.passed;
3969 total_failed += result.failed;
3970 }
3971
3972 let grand_total = total_passed + total_failed;
3973 let overall_rate = if grand_total == 0 {
3974 0.0
3975 } else {
3976 (total_passed as f64 / grand_total as f64) * 100.0
3977 };
3978
3979 println!(" {}", "-".repeat(76));
3980 println!(
3981 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3982 format!("TOTAL ({} targets)", num_targets),
3983 total_passed,
3984 total_failed,
3985 overall_rate,
3986 total_elapsed.as_secs_f64()
3987 );
3988 println!("{}", "=".repeat(80));
3989
3990 for result in &target_results {
3992 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
3993 for entry in &result.owasp_coverage {
3994 let status = if !entry.tested {
3995 "-"
3996 } else if entry.all_passed {
3997 "pass"
3998 } else {
3999 "FAIL"
4000 };
4001 let via = if entry.via_categories.is_empty() {
4002 String::new()
4003 } else {
4004 format!(" (via {})", entry.via_categories.join(", "))
4005 };
4006 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
4007 }
4008 }
4009
4010 let per_target_summaries: Vec<serde_json::Value> = target_results
4012 .iter()
4013 .enumerate()
4014 .map(|(idx, r)| {
4015 let total_checks = r.passed + r.failed;
4016 let rate = if total_checks == 0 {
4017 0.0
4018 } else {
4019 (r.passed as f64 / total_checks as f64) * 100.0
4020 };
4021 let owasp_json: Vec<serde_json::Value> = r
4022 .owasp_coverage
4023 .iter()
4024 .map(|e| {
4025 serde_json::json!({
4026 "id": e.id,
4027 "name": e.name,
4028 "tested": e.tested,
4029 "all_passed": e.all_passed,
4030 "via_categories": e.via_categories,
4031 })
4032 })
4033 .collect();
4034 serde_json::json!({
4035 "target_url": r.url,
4036 "target_index": idx,
4037 "checks_passed": r.passed,
4038 "checks_failed": r.failed,
4039 "total_checks": total_checks,
4040 "pass_rate": rate,
4041 "elapsed_seconds": r.elapsed.as_secs_f64(),
4042 "report": r.report_json,
4043 "owasp_coverage": owasp_json,
4044 })
4045 })
4046 .collect();
4047
4048 let combined_summary = serde_json::json!({
4049 "total_targets": num_targets,
4050 "total_checks_passed": total_passed,
4051 "total_checks_failed": total_failed,
4052 "overall_pass_rate": overall_rate,
4053 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
4054 "targets": per_target_summaries,
4055 });
4056
4057 let summary_path = self.output.join("multi-target-conformance-summary.json");
4058 let summary_str = serde_json::to_string_pretty(&combined_summary)
4059 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
4060 std::fs::write(&summary_path, &summary_str)
4061 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
4062 TerminalReporter::print_success(&format!(
4063 "Combined summary saved to: {}",
4064 summary_path.display()
4065 ));
4066
4067 Ok(())
4068 }
4069
4070 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
4072 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
4073
4074 let custom_headers = self.parse_headers()?;
4076
4077 let mut config = OwaspApiConfig::new()
4079 .with_auth_header(&self.owasp_auth_header)
4080 .with_verbose(self.verbose)
4081 .with_insecure(self.skip_tls_verify)
4082 .with_concurrency(self.vus as usize)
4083 .with_iterations(self.owasp_iterations as usize)
4084 .with_base_path(self.base_path.clone())
4085 .with_custom_headers(custom_headers);
4086
4087 if let Some(ref token) = self.owasp_auth_token {
4089 config = config.with_valid_auth_token(token);
4090 }
4091
4092 if let Some(ref cats_str) = self.owasp_categories {
4094 let categories: Vec<OwaspCategory> = cats_str
4095 .split(',')
4096 .filter_map(|s| {
4097 let trimmed = s.trim();
4098 match trimmed.parse::<OwaspCategory>() {
4099 Ok(cat) => Some(cat),
4100 Err(e) => {
4101 TerminalReporter::print_warning(&e);
4102 None
4103 }
4104 }
4105 })
4106 .collect();
4107
4108 if !categories.is_empty() {
4109 config = config.with_categories(categories);
4110 }
4111 }
4112
4113 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
4115 config.admin_paths_file = Some(admin_paths_file.clone());
4116 if let Err(e) = config.load_admin_paths() {
4117 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
4118 }
4119 }
4120
4121 if let Some(ref id_fields_str) = self.owasp_id_fields {
4123 let id_fields: Vec<String> = id_fields_str
4124 .split(',')
4125 .map(|s| s.trim().to_string())
4126 .filter(|s| !s.is_empty())
4127 .collect();
4128 if !id_fields.is_empty() {
4129 config = config.with_id_fields(id_fields);
4130 }
4131 }
4132
4133 if let Some(ref report_path) = self.owasp_report {
4135 config = config.with_report_path(report_path);
4136 }
4137 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
4138 config = config.with_report_format(format);
4139 }
4140
4141 let categories = config.categories_to_test();
4143 TerminalReporter::print_success(&format!(
4144 "Testing {} OWASP categories: {}",
4145 categories.len(),
4146 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
4147 ));
4148
4149 if config.valid_auth_token.is_some() {
4150 TerminalReporter::print_progress("Using provided auth token for baseline requests");
4151 }
4152
4153 TerminalReporter::print_progress("Generating OWASP security test script...");
4155 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
4156
4157 let script = generator.generate()?;
4159 TerminalReporter::print_success("OWASP security test script generated");
4160
4161 let script_path = if let Some(output) = &self.script_output {
4163 output.clone()
4164 } else {
4165 self.output.join("k6-owasp-security-test.js")
4166 };
4167
4168 if let Some(parent) = script_path.parent() {
4169 std::fs::create_dir_all(parent)?;
4170 }
4171 std::fs::write(&script_path, &script)?;
4172 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
4173
4174 if self.generate_only {
4176 println!("\nOWASP security test script generated. Run it with:");
4177 println!(" k6 run {}", script_path.display());
4178 return Ok(());
4179 }
4180
4181 TerminalReporter::print_progress("Executing OWASP security tests...");
4183 let executor = K6Executor::new()?
4184 .with_local_ips(self.source_ips.join(","))
4185 .with_dns_policy(self.dns_policy.clone().unwrap_or_default());
4186 std::fs::create_dir_all(&self.output)?;
4187
4188 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
4189
4190 let duration_secs = Self::parse_duration(&self.duration)?;
4191 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
4192
4193 println!("\nOWASP security test results saved to: {}", self.output.display());
4194
4195 Ok(())
4196 }
4197}
4198
4199#[cfg(test)]
4200mod tests {
4201 use super::*;
4202 use tempfile::tempdir;
4203
4204 #[test]
4205 fn test_parse_duration() {
4206 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
4207 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
4208 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
4209 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
4210 }
4211
4212 #[test]
4216 fn parse_ip_list_ipv4_range_inclusive() {
4217 let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
4218 assert_eq!(v.len(), 23);
4219 assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
4220 assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
4221 }
4222
4223 #[test]
4226 fn parse_ip_list_range_rejects_backwards() {
4227 let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
4228 assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
4229 }
4230
4231 #[test]
4235 fn parse_ip_list_rejects_ipv6_range_syntax() {
4236 let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
4237 assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
4238 }
4239
4240 #[test]
4242 fn parse_ip_list_range_capped_at_256() {
4243 let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
4244 assert_eq!(v.len(), 256);
4245 assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
4246 }
4247
4248 #[test]
4251 fn parse_ip_list_plain_and_comma() {
4252 let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
4253 assert_eq!(v.len(), 3);
4254 assert_eq!(v[0].to_string(), "10.0.0.5");
4255 assert_eq!(v[2].to_string(), "10.0.0.7");
4256 }
4257
4258 #[test]
4261 fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
4262 let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
4263 assert_eq!(v.len(), 8);
4264 assert_eq!(v[0].to_string(), "10.0.0.0");
4265 assert_eq!(v[7].to_string(), "10.0.0.7");
4266 }
4267
4268 #[test]
4271 fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
4272 let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
4273 assert_eq!(v.len(), 256);
4274 assert_eq!(v[0].to_string(), "10.0.0.0");
4275 assert_eq!(v[255].to_string(), "10.0.0.255");
4276 }
4277
4278 #[test]
4280 fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
4281 let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
4282 assert_eq!(v.len(), 4);
4283 assert!(v[0].is_ipv6());
4284 assert_eq!(v[0].to_string(), "2001:db8::");
4285 assert_eq!(v[3].to_string(), "2001:db8::3");
4286 }
4287
4288 #[test]
4290 fn parse_ip_list_mixed_v4_v6_cidr() {
4291 let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
4292 assert_eq!(v.len(), 6); assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
4294 assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
4295 }
4296
4297 #[test]
4300 fn parse_ip_list_skips_malformed() {
4301 let v = parse_ip_list(
4302 &[
4303 "10.0.0.5".into(),
4304 "not-an-ip".into(),
4305 "10.0.0.6".into(),
4306 "/24".into(),
4307 "1.2.3.4/200".into(),
4308 ],
4309 "source-ip",
4310 );
4311 assert_eq!(v.len(), 2);
4312 assert_eq!(v[0].to_string(), "10.0.0.5");
4313 assert_eq!(v[1].to_string(), "10.0.0.6");
4314 }
4315
4316 #[test]
4317 fn test_parse_duration_invalid() {
4318 assert!(BenchCommand::parse_duration("invalid").is_err());
4319 assert!(BenchCommand::parse_duration("30x").is_err());
4320 }
4321
4322 #[test]
4323 fn test_parse_headers() {
4324 let cmd = BenchCommand {
4325 spec: vec![PathBuf::from("test.yaml")],
4326 spec_dir: None,
4327 merge_conflicts: "error".to_string(),
4328 spec_mode: "merge".to_string(),
4329 dependency_config: None,
4330 target: "http://localhost".to_string(),
4331 base_path: None,
4332 duration: "1m".to_string(),
4333 vus: 10,
4334 scenario: "ramp-up".to_string(),
4335 operations: None,
4336 exclude_operations: None,
4337 auth: None,
4338 headers: vec![
4339 "X-API-Key:test123".to_string(),
4340 "X-Client-ID:client456".to_string(),
4341 ],
4342 output: PathBuf::from("output"),
4343 generate_only: false,
4344 script_output: None,
4345 threshold_percentile: "p(95)".to_string(),
4346 threshold_ms: 500,
4347 max_error_rate: 0.05,
4348 abort_on_error: true,
4349 abort_on_error_rate: 0.95,
4350 verbose: false,
4351 skip_tls_verify: false,
4352 chunked_request_bodies: false,
4353 target_rps: None,
4354 no_keep_alive: false,
4355 targets_file: None,
4356 max_concurrency: None,
4357 results_format: "both".to_string(),
4358 params_file: None,
4359 crud_flow: false,
4360 flow_config: None,
4361 extract_fields: None,
4362 parallel_create: None,
4363 data_file: None,
4364 data_distribution: "unique-per-vu".to_string(),
4365 data_mappings: None,
4366 per_uri_control: false,
4367 error_rate: None,
4368 error_types: None,
4369 security_test: false,
4370 security_payloads: None,
4371 security_categories: None,
4372 security_target_fields: None,
4373 wafbench_dir: None,
4374 wafbench_cycle_all: false,
4375 wafbench_verbatim: false,
4376 owasp_api_top10: false,
4377 owasp_categories: None,
4378 owasp_auth_header: "Authorization".to_string(),
4379 owasp_auth_token: None,
4380 owasp_admin_paths: None,
4381 owasp_id_fields: None,
4382 owasp_report: None,
4383 owasp_report_format: "json".to_string(),
4384 owasp_iterations: 1,
4385 conformance: false,
4386 conformance_api_key: None,
4387 conformance_basic_auth: None,
4388 conformance_report: PathBuf::from("conformance-report.json"),
4389 conformance_categories: None,
4390 conformance_report_format: "json".to_string(),
4391 conformance_headers: vec![],
4392 conformance_all_operations: false,
4393 conformance_custom: None,
4394 conformance_delay_ms: 0,
4395 use_k6: false,
4396 conformance_custom_filter: None,
4397 export_requests: false,
4398 validate_requests: false,
4399 conformance_self_test: false,
4400 conformance_self_test_capture: false,
4401 conformance_self_test_iterations: 1,
4402 conformance_self_test_duration: None,
4403 validate_response_schemas: false,
4404 source_ips: Vec::new(),
4405 geo_source_ips: Vec::new(),
4406 geo_source_headers: Vec::new(),
4407 report_missed_cap: None,
4408 discard_response_bodies: false,
4409 dns_policy: None,
4410 };
4411
4412 let headers = cmd.parse_headers().unwrap();
4413 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4414 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4415 }
4416
4417 #[test]
4418 fn test_parse_header_string_preserves_comma_in_value() {
4419 let inputs = vec![
4422 "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4423 "X-Trace:1".to_string(),
4424 ];
4425 let headers = parse_header_string(&inputs).unwrap();
4426 assert_eq!(
4427 headers.get("Cookie"),
4428 Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4429 );
4430 assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4431 }
4432
4433 #[test]
4441 fn conformance_advisory_names_every_discarded_flag() {
4442 let msg = CONFORMANCE_REPLACES_LOAD_ADVISORY;
4443 for flag in ["--vus", "--rps", "-d"] {
4444 assert!(
4445 msg.contains(flag),
4446 "conformance advisory must name `{flag}` as ignored; it is discarded on that \
4447 path and silently dropping it is how users end up tuning a knob that does \
4448 nothing (#980). Message was: {msg}"
4449 );
4450 }
4451 assert!(
4452 msg.contains("REPLACES"),
4453 "conformance advisory must say the load run is REPLACED, not merely that some \
4454 flags are ignored — `--conformance` returns before the load path runs, so no \
4455 load traffic is generated at all (#980). Message was: {msg}"
4456 );
4457 }
4458
4459 #[test]
4473 fn multi_target_clone_preserves_fields_parse_headers_reads() {
4474 let src = include_str!("command.rs");
4475
4476 let fn_start = src
4477 .find("async fn execute_multi_target(")
4478 .expect("execute_multi_target should exist");
4479 let block_start = src[fn_start..]
4480 .find("ParallelExecutor::new(")
4481 .map(|i| i + fn_start)
4482 .expect("multi-target path should build a ParallelExecutor");
4483 let block_end = src[block_start..]
4485 .find("\n );")
4486 .map(|i| i + block_start)
4487 .expect("ParallelExecutor::new(..) should be closed");
4488 let block = &src[block_start..block_end];
4489
4490 for field in ["conformance_basic_auth", "conformance_headers"] {
4493 for zeroed in [format!("{field}: None"), format!("{field}: vec![]")] {
4494 assert!(
4495 !block.contains(&zeroed),
4496 "execute_multi_target zeroes `{zeroed}`. parse_headers() folds `{field}` \
4497 into the header map, so zeroing it here strips auth from every \
4498 multi-target run while single-target keeps working (#79 round 64)."
4499 );
4500 }
4501 let passthrough = format!("{field}: self.{field}.clone()");
4502 assert!(
4503 block.contains(&passthrough),
4504 "execute_multi_target must carry `{field}` through as `{passthrough}` so \
4505 parse_headers() can fold it (#79 round 64)."
4506 );
4507 }
4508 }
4509
4510 #[test]
4511 fn test_get_spec_display_name() {
4512 let cmd = BenchCommand {
4513 spec: vec![PathBuf::from("test.yaml")],
4514 spec_dir: None,
4515 merge_conflicts: "error".to_string(),
4516 spec_mode: "merge".to_string(),
4517 dependency_config: None,
4518 target: "http://localhost".to_string(),
4519 base_path: None,
4520 duration: "1m".to_string(),
4521 vus: 10,
4522 scenario: "ramp-up".to_string(),
4523 operations: None,
4524 exclude_operations: None,
4525 auth: None,
4526 headers: Vec::new(),
4527 output: PathBuf::from("output"),
4528 generate_only: false,
4529 script_output: None,
4530 threshold_percentile: "p(95)".to_string(),
4531 threshold_ms: 500,
4532 max_error_rate: 0.05,
4533 abort_on_error: true,
4534 abort_on_error_rate: 0.95,
4535 verbose: false,
4536 skip_tls_verify: false,
4537 chunked_request_bodies: false,
4538 target_rps: None,
4539 no_keep_alive: false,
4540 targets_file: None,
4541 max_concurrency: None,
4542 results_format: "both".to_string(),
4543 params_file: None,
4544 crud_flow: false,
4545 flow_config: None,
4546 extract_fields: None,
4547 parallel_create: None,
4548 data_file: None,
4549 data_distribution: "unique-per-vu".to_string(),
4550 data_mappings: None,
4551 per_uri_control: false,
4552 error_rate: None,
4553 error_types: None,
4554 security_test: false,
4555 security_payloads: None,
4556 security_categories: None,
4557 security_target_fields: None,
4558 wafbench_dir: None,
4559 wafbench_cycle_all: false,
4560 wafbench_verbatim: false,
4561 owasp_api_top10: false,
4562 owasp_categories: None,
4563 owasp_auth_header: "Authorization".to_string(),
4564 owasp_auth_token: None,
4565 owasp_admin_paths: None,
4566 owasp_id_fields: None,
4567 owasp_report: None,
4568 owasp_report_format: "json".to_string(),
4569 owasp_iterations: 1,
4570 conformance: false,
4571 conformance_api_key: None,
4572 conformance_basic_auth: None,
4573 conformance_report: PathBuf::from("conformance-report.json"),
4574 conformance_categories: None,
4575 conformance_report_format: "json".to_string(),
4576 conformance_headers: vec![],
4577 conformance_all_operations: false,
4578 conformance_custom: None,
4579 conformance_delay_ms: 0,
4580 use_k6: false,
4581 conformance_custom_filter: None,
4582 export_requests: false,
4583 validate_requests: false,
4584 conformance_self_test: false,
4585 conformance_self_test_capture: false,
4586 conformance_self_test_iterations: 1,
4587 conformance_self_test_duration: None,
4588 validate_response_schemas: false,
4589 source_ips: Vec::new(),
4590 geo_source_ips: Vec::new(),
4591 geo_source_headers: Vec::new(),
4592 report_missed_cap: None,
4593 discard_response_bodies: false,
4594 dns_policy: None,
4595 };
4596
4597 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4598
4599 let cmd_multi = BenchCommand {
4601 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4602 spec_dir: None,
4603 merge_conflicts: "error".to_string(),
4604 spec_mode: "merge".to_string(),
4605 dependency_config: None,
4606 target: "http://localhost".to_string(),
4607 base_path: None,
4608 duration: "1m".to_string(),
4609 vus: 10,
4610 scenario: "ramp-up".to_string(),
4611 operations: None,
4612 exclude_operations: None,
4613 auth: None,
4614 headers: Vec::new(),
4615 output: PathBuf::from("output"),
4616 generate_only: false,
4617 script_output: None,
4618 threshold_percentile: "p(95)".to_string(),
4619 threshold_ms: 500,
4620 max_error_rate: 0.05,
4621 abort_on_error: true,
4622 abort_on_error_rate: 0.95,
4623 verbose: false,
4624 skip_tls_verify: false,
4625 chunked_request_bodies: false,
4626 target_rps: None,
4627 no_keep_alive: false,
4628 targets_file: None,
4629 max_concurrency: None,
4630 results_format: "both".to_string(),
4631 params_file: None,
4632 crud_flow: false,
4633 flow_config: None,
4634 extract_fields: None,
4635 parallel_create: None,
4636 data_file: None,
4637 data_distribution: "unique-per-vu".to_string(),
4638 data_mappings: None,
4639 per_uri_control: false,
4640 error_rate: None,
4641 error_types: None,
4642 security_test: false,
4643 security_payloads: None,
4644 security_categories: None,
4645 security_target_fields: None,
4646 wafbench_dir: None,
4647 wafbench_cycle_all: false,
4648 wafbench_verbatim: false,
4649 owasp_api_top10: false,
4650 owasp_categories: None,
4651 owasp_auth_header: "Authorization".to_string(),
4652 owasp_auth_token: None,
4653 owasp_admin_paths: None,
4654 owasp_id_fields: None,
4655 owasp_report: None,
4656 owasp_report_format: "json".to_string(),
4657 owasp_iterations: 1,
4658 conformance: false,
4659 conformance_api_key: None,
4660 conformance_basic_auth: None,
4661 conformance_report: PathBuf::from("conformance-report.json"),
4662 conformance_categories: None,
4663 conformance_report_format: "json".to_string(),
4664 conformance_headers: vec![],
4665 conformance_all_operations: false,
4666 conformance_custom: None,
4667 conformance_delay_ms: 0,
4668 use_k6: false,
4669 conformance_custom_filter: None,
4670 export_requests: false,
4671 validate_requests: false,
4672 conformance_self_test: false,
4673 conformance_self_test_capture: false,
4674 conformance_self_test_iterations: 1,
4675 conformance_self_test_duration: None,
4676 validate_response_schemas: false,
4677 source_ips: Vec::new(),
4678 geo_source_ips: Vec::new(),
4679 geo_source_headers: Vec::new(),
4680 report_missed_cap: None,
4681 discard_response_bodies: false,
4682 dns_policy: None,
4683 };
4684
4685 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4686 }
4687
4688 #[test]
4689 fn test_parse_extracted_values_from_output_dir() {
4690 let dir = tempdir().unwrap();
4691 let path = dir.path().join("extracted_values.json");
4692 std::fs::write(
4693 &path,
4694 r#"{
4695 "pool_id": "abc123",
4696 "count": 0,
4697 "enabled": false,
4698 "metadata": { "owner": "team-a" }
4699}"#,
4700 )
4701 .unwrap();
4702
4703 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4704 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4705 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4706 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4707 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4708 }
4709
4710 #[test]
4711 fn test_parse_extracted_values_missing_file() {
4712 let dir = tempdir().unwrap();
4713 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4714 assert!(extracted.values.is_empty());
4715 }
4716
4717 fn sample_bench_command() -> BenchCommand {
4720 BenchCommand {
4721 spec: vec![PathBuf::from("test.yaml")],
4722 spec_dir: None,
4723 merge_conflicts: "error".to_string(),
4724 spec_mode: "merge".to_string(),
4725 dependency_config: None,
4726 target: "http://localhost".to_string(),
4727 base_path: None,
4728 duration: "1m".to_string(),
4729 vus: 10,
4730 scenario: "ramp-up".to_string(),
4731 operations: None,
4732 exclude_operations: None,
4733 auth: None,
4734 headers: vec![
4735 "X-API-Key:test123".to_string(),
4736 "X-Client-ID:client456".to_string(),
4737 ],
4738 output: PathBuf::from("output"),
4739 generate_only: false,
4740 script_output: None,
4741 threshold_percentile: "p(95)".to_string(),
4742 threshold_ms: 500,
4743 max_error_rate: 0.05,
4744 abort_on_error: true,
4745 abort_on_error_rate: 0.95,
4746 verbose: false,
4747 skip_tls_verify: false,
4748 chunked_request_bodies: false,
4749 target_rps: None,
4750 no_keep_alive: false,
4751 targets_file: None,
4752 max_concurrency: None,
4753 results_format: "both".to_string(),
4754 params_file: None,
4755 crud_flow: false,
4756 flow_config: None,
4757 extract_fields: None,
4758 parallel_create: None,
4759 data_file: None,
4760 data_distribution: "unique-per-vu".to_string(),
4761 data_mappings: None,
4762 per_uri_control: false,
4763 error_rate: None,
4764 error_types: None,
4765 security_test: false,
4766 security_payloads: None,
4767 security_categories: None,
4768 security_target_fields: None,
4769 wafbench_dir: None,
4770 wafbench_cycle_all: false,
4771 wafbench_verbatim: false,
4772 owasp_api_top10: false,
4773 owasp_categories: None,
4774 owasp_auth_header: "Authorization".to_string(),
4775 owasp_auth_token: None,
4776 owasp_admin_paths: None,
4777 owasp_id_fields: None,
4778 owasp_report: None,
4779 owasp_report_format: "json".to_string(),
4780 owasp_iterations: 1,
4781 conformance: false,
4782 conformance_api_key: None,
4783 conformance_basic_auth: None,
4784 conformance_report: PathBuf::from("conformance-report.json"),
4785 conformance_categories: None,
4786 conformance_report_format: "json".to_string(),
4787 conformance_headers: vec![],
4788 conformance_all_operations: false,
4789 conformance_custom: None,
4790 conformance_delay_ms: 0,
4791 use_k6: false,
4792 conformance_custom_filter: None,
4793 export_requests: false,
4794 validate_requests: false,
4795 conformance_self_test: false,
4796 conformance_self_test_capture: false,
4797 conformance_self_test_iterations: 1,
4798 conformance_self_test_duration: None,
4799 validate_response_schemas: false,
4800 source_ips: Vec::new(),
4801 geo_source_ips: Vec::new(),
4802 geo_source_headers: Vec::new(),
4803 report_missed_cap: None,
4804 discard_response_bodies: false,
4805 dns_policy: None,
4806 }
4807 }
4808
4809 #[test]
4817 fn verbatim_disables_security_payload_injection() {
4818 let mut cmd = sample_bench_command();
4819 cmd.wafbench_dir = Some("traffic.yaml".to_string());
4820
4821 assert!(
4822 cmd.security_testing_enabled(),
4823 "--wafbench-dir alone must still enable payload injection"
4824 );
4825
4826 cmd.wafbench_verbatim = true;
4827 assert!(
4828 !cmd.security_testing_enabled(),
4829 "verbatim mode must not inject payloads into requests sent as written"
4830 );
4831
4832 cmd.security_test = true;
4835 assert!(
4836 !cmd.security_testing_enabled(),
4837 "--security-test must not re-enable injection under --wafbench-verbatim"
4838 );
4839 }
4840
4841 #[test]
4846 fn security_testing_enabled_has_a_single_definition() {
4847 let src = include_str!("command.rs");
4848 let parallel = include_str!("parallel_executor.rs");
4849 let a = format!("self.{} || self.{}.is_some()", "security_test", "wafbench_dir");
4851 let b = format!("self.{}.is_some() || self.{}", "wafbench_dir", "security_test");
4852 let inline = src.matches(a.as_str()).count() + src.matches(b.as_str()).count();
4853 assert_eq!(
4854 inline, 1,
4855 "expected the security_testing_enabled() method to be the only place this is \
4856 computed, found {inline} inline copies -- collapse them or the render paths drift"
4857 );
4858
4859 let parallel_inline = format!(
4864 "{}.{} || {}.{}.is_some()",
4865 "base_command", "security_test", "self.base_command", "wafbench_dir"
4866 );
4867 assert!(
4868 !parallel.contains(¶llel_inline),
4869 "ParallelExecutor must not recompute the security flag inline"
4870 );
4871 assert!(
4872 parallel.contains("security_testing_enabled()"),
4873 "ParallelExecutor must call security_testing_enabled() so --wafbench-verbatim \
4874 turns injection off on --targets-file runs too"
4875 );
4876 }
4877
4878 #[test]
4882 fn missing_wafbench_dir_is_not_swallowed() {
4883 let src = include_str!("command.rs");
4884 let swallowed = format!("Failed to {} WAFBench tests", "load");
4886 let impl_line = src
4887 .lines()
4888 .filter(|l| !l.trim_start().starts_with("//"))
4889 .any(|l| l.contains(&swallowed));
4890 assert!(!impl_line, "missing --wafbench-dir must not be downgraded to a warning");
4891 assert!(
4892 src.contains("self.load_wafbench_payloads()?"),
4893 "payload load errors must reach generate_enhanced_script"
4894 );
4895 }
4896
4897 #[test]
4902 fn multi_target_path_honors_verbatim_templates() {
4903 let src = include_str!("parallel_executor.rs");
4904 assert!(
4905 src.contains("load_verbatim_templates"),
4906 "ParallelExecutor must load traffic-file requests under --wafbench-verbatim. \
4907 Requiring a spec and generating templates from its operations is how \
4908 --targets-file ignored the flag and fuzzed spec URLs (#79)."
4909 );
4910 }
4911}