1use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11 MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21 SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24 topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30 load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37pub fn parse_header_string(inputs: &[String]) -> Result<HashMap<String, String>> {
45 let mut headers = HashMap::new();
46
47 for pair in inputs {
48 let pair = pair.trim();
49 if pair.is_empty() {
50 continue;
51 }
52 let parts: Vec<&str> = pair.splitn(2, ':').collect();
53 if parts.len() != 2 {
54 return Err(BenchError::Other(format!(
55 "Invalid header format: '{}'. Expected 'Key:Value'",
56 pair
57 )));
58 }
59 headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
60 }
61
62 Ok(headers)
63}
64
65pub struct BenchCommand {
67 pub spec: Vec<PathBuf>,
69 pub spec_dir: Option<PathBuf>,
71 pub merge_conflicts: String,
73 pub spec_mode: String,
75 pub dependency_config: Option<PathBuf>,
77 pub target: String,
78 pub base_path: Option<String>,
81 pub duration: String,
82 pub vus: u32,
83 pub target_rps: Option<u32>,
89 pub no_keep_alive: bool,
94 pub scenario: String,
95 pub operations: Option<String>,
96 pub exclude_operations: Option<String>,
100 pub auth: Option<String>,
101 pub headers: Vec<String>,
104 pub output: PathBuf,
105 pub generate_only: bool,
106 pub script_output: Option<PathBuf>,
107 pub threshold_percentile: String,
108 pub threshold_ms: u64,
109 pub max_error_rate: f64,
110 pub verbose: bool,
111 pub skip_tls_verify: bool,
112 pub chunked_request_bodies: bool,
117 pub targets_file: Option<PathBuf>,
119 pub max_concurrency: Option<u32>,
121 pub results_format: String,
123 pub params_file: Option<PathBuf>,
128
129 pub crud_flow: bool,
132 pub flow_config: Option<PathBuf>,
134 pub extract_fields: Option<String>,
136
137 pub parallel_create: Option<u32>,
140
141 pub data_file: Option<PathBuf>,
144 pub data_distribution: String,
146 pub data_mappings: Option<String>,
148 pub per_uri_control: bool,
150
151 pub error_rate: Option<f64>,
154 pub error_types: Option<String>,
156
157 pub security_test: bool,
160 pub security_payloads: Option<PathBuf>,
162 pub security_categories: Option<String>,
164 pub security_target_fields: Option<String>,
166
167 pub wafbench_dir: Option<String>,
170 pub wafbench_cycle_all: bool,
172
173 pub conformance: bool,
176 pub conformance_api_key: Option<String>,
178 pub conformance_basic_auth: Option<String>,
180 pub conformance_report: PathBuf,
182 pub conformance_categories: Option<String>,
184 pub conformance_report_format: String,
186 pub conformance_headers: Vec<String>,
189 pub conformance_all_operations: bool,
192 pub conformance_custom: Option<PathBuf>,
194 pub conformance_delay_ms: u64,
197 pub use_k6: bool,
199 pub conformance_custom_filter: Option<String>,
203 pub export_requests: bool,
206 pub validate_requests: bool,
209 pub conformance_self_test: bool,
216 pub conformance_self_test_capture: bool,
220 pub validate_response_schemas: bool,
226 pub conformance_self_test_iterations: u32,
231 pub conformance_self_test_duration: Option<String>,
236
237 pub source_ips: Vec<String>,
242 pub geo_source_ips: Vec<String>,
246 pub geo_source_headers: Vec<String>,
250
251 pub report_missed_cap: Option<u32>,
258
259 pub owasp_api_top10: bool,
262 pub owasp_categories: Option<String>,
264 pub owasp_auth_header: String,
266 pub owasp_auth_token: Option<String>,
268 pub owasp_admin_paths: Option<PathBuf>,
270 pub owasp_id_fields: Option<String>,
272 pub owasp_report: Option<PathBuf>,
274 pub owasp_report_format: String,
276 pub owasp_iterations: u32,
278}
279
280fn parse_ip_list(raw: &[String], flag_name: &str) -> Vec<std::net::IpAddr> {
294 use std::net::IpAddr;
295 const MAX_CIDR_EXPANSION: usize = 256;
296 let mut out = Vec::new();
297 for entry in raw {
298 for piece in entry.split(',') {
299 let s = piece.trim();
300 if s.is_empty() {
301 continue;
302 }
303 if let Some((addr_part, prefix_part)) = s.split_once('/') {
305 let prefix: u32 = match prefix_part.parse() {
306 Ok(p) => p,
307 Err(e) => {
308 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR prefix: {e}");
309 continue;
310 }
311 };
312 let net_addr: IpAddr = match addr_part.parse() {
313 Ok(a) => a,
314 Err(e) => {
315 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad CIDR address: {e}");
316 continue;
317 }
318 };
319 expand_cidr(net_addr, prefix, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
320 continue;
321 }
322 if let Some((start_str, end_str)) = s.split_once('-') {
328 let start_s = start_str.trim();
329 let end_s = end_str.trim();
330 if start_s.contains(':') || end_s.contains(':') {
334 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{s}': IPv6 range syntax not supported (use CIDR like 2001:db8::/126 instead)");
335 continue;
336 }
337 let start: IpAddr = match start_s.parse() {
338 Ok(a) => a,
339 Err(e) => {
340 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range start: {e}");
341 continue;
342 }
343 };
344 let end: IpAddr = match end_s.parse() {
345 Ok(a) => a,
346 Err(e) => {
347 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{s}': bad range end: {e}");
348 continue;
349 }
350 };
351 expand_range(start, end, MAX_CIDR_EXPANSION, flag_name, s, &mut out);
352 continue;
353 }
354 match s.parse::<IpAddr>() {
356 Ok(ip) => out.push(ip),
357 Err(e) => {
358 tracing::warn!(target: "mockforge::bench", "ignoring malformed --{flag_name} value '{s}': {e}");
359 }
360 }
361 }
362 }
363 out
364}
365
366fn expand_range(
370 start: std::net::IpAddr,
371 end: std::net::IpAddr,
372 cap: usize,
373 flag_name: &str,
374 raw: &str,
375 out: &mut Vec<std::net::IpAddr>,
376) {
377 use std::net::{IpAddr, Ipv4Addr};
378 let (start_v4, end_v4) = match (start, end) {
379 (IpAddr::V4(a), IpAddr::V4(b)) => (a, b),
380 _ => {
381 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range start/end must both be IPv4");
382 return;
383 }
384 };
385 let start_u32 = u32::from(start_v4);
386 let end_u32 = u32::from(end_v4);
387 if end_u32 < start_u32 {
388 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range end {end_v4} is before start {start_v4}");
389 return;
390 }
391 let total = (end_u32 - start_u32).saturating_add(1) as usize;
392 let take = total.min(cap);
393 if total > cap {
394 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': range has {total} addresses, capping at {cap}");
395 }
396 for i in 0..take as u32 {
397 out.push(IpAddr::V4(Ipv4Addr::from(start_u32 + i)));
398 }
399}
400
401fn expand_cidr(
405 net: std::net::IpAddr,
406 prefix: u32,
407 cap: usize,
408 flag_name: &str,
409 raw: &str,
410 out: &mut Vec<std::net::IpAddr>,
411) {
412 use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
413 match net {
414 IpAddr::V4(ipv4) => {
415 if prefix > 32 {
416 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv4 prefix must be <= 32");
417 return;
418 }
419 let total: u64 = 1u64 << (32 - prefix);
420 let take = total.min(cap as u64) as u32;
421 if total > cap as u64 {
422 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': CIDR has {total} addresses, capping at {cap}");
423 }
424 let mask: u32 = if prefix == 0 {
425 0
426 } else {
427 !0u32 << (32 - prefix)
428 };
429 let net_u32 = u32::from(ipv4) & mask;
430 for i in 0..take {
431 out.push(IpAddr::V4(Ipv4Addr::from(net_u32.wrapping_add(i))));
432 }
433 }
434 IpAddr::V6(ipv6) => {
435 if prefix > 128 {
436 tracing::warn!(target: "mockforge::bench", "ignoring --{flag_name} '{raw}': IPv6 prefix must be <= 128");
437 return;
438 }
439 let mask: u128 = if prefix == 0 {
443 0
444 } else {
445 !0u128 << (128 - prefix)
446 };
447 let net_u128 = u128::from(ipv6) & mask;
448 let remaining_bits = 128 - prefix;
449 let total_capped = if remaining_bits >= 64 {
452 cap as u128
453 } else {
454 (1u128 << remaining_bits).min(cap as u128)
455 };
456 if remaining_bits < 128 && (1u128 << remaining_bits) > cap as u128 {
457 tracing::warn!(target: "mockforge::bench", "--{flag_name} '{raw}': IPv6 CIDR exceeds {cap} addresses, capping");
458 }
459 for i in 0..total_capped {
460 out.push(IpAddr::V6(Ipv6Addr::from(net_u128.wrapping_add(i))));
461 }
462 }
463 }
464}
465
466impl BenchCommand {
467 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
469 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
470
471 if !self.spec.is_empty() {
473 let specs = load_specs_from_files(self.spec.clone())
474 .await
475 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
476 all_specs.extend(specs);
477 }
478
479 if let Some(spec_dir) = &self.spec_dir {
481 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
482 BenchError::Other(format!("Failed to load specs from directory: {}", e))
483 })?;
484 all_specs.extend(dir_specs);
485 }
486
487 if all_specs.is_empty() {
488 return Err(BenchError::Other(
489 "No spec files provided. Use --spec or --spec-dir.".to_string(),
490 ));
491 }
492
493 if all_specs.len() == 1 {
495 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
497 }
498
499 let conflict_strategy = match self.merge_conflicts.as_str() {
501 "first" => ConflictStrategy::First,
502 "last" => ConflictStrategy::Last,
503 _ => ConflictStrategy::Error,
504 };
505
506 merge_specs(all_specs, conflict_strategy)
507 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
508 }
509
510 fn get_spec_display_name(&self) -> String {
512 if self.spec.len() == 1 {
513 self.spec[0].to_string_lossy().to_string()
514 } else if !self.spec.is_empty() {
515 format!("{} spec files", self.spec.len())
516 } else if let Some(dir) = &self.spec_dir {
517 format!("specs from {}", dir.display())
518 } else {
519 "no specs".to_string()
520 }
521 }
522
523 fn advise_capacity(&self) {
530 let target_count = self
531 .targets_file
532 .as_ref()
533 .and_then(|p| std::fs::read_to_string(p).ok())
534 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
535 .and_then(|v| v.as_array().map(|a| a.len()))
536 .unwrap_or(1);
537 let vus = self.vus.max(1);
538 let rps_total = self.target_rps.unwrap_or(0) as usize * target_count.max(1);
539 let load_product = target_count * vus as usize;
543 if load_product >= 150 {
544 let est_ram_gb =
545 (vus as usize * 50) / 1024 + (target_count * 10 * 2) / 1024 + target_count / 2;
546 let est_cores = ((vus as usize) / 50).max(2);
547 TerminalReporter::print_warning(&format!(
548 "Capacity advisory: targets={target_count}, VUs={vus}, RPS-total≈{rps_total}. \
549 Single-client estimate: ~{est_cores} CPU cores, ~{est_ram_gb} GB RAM. \
550 If your machine is below that, expect OOM hangs partway through the run. \
551 See https://docs.mockforge.dev/reference/bench-capacity-sizing.html \
552 for the sizing table and sharding guide."
553 ));
554 }
555 }
556
557 pub async fn execute(&self) -> Result<()> {
559 if self.conformance_self_test && self.use_k6 {
566 TerminalReporter::print_warning(
567 "--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.",
568 );
569 }
570
571 self.advise_capacity();
577
578 if let Some(targets_file) = &self.targets_file {
580 if self.conformance && self.conformance_self_test {
589 return self.execute_multi_target_self_test(targets_file).await;
590 }
591 if self.conformance {
592 return self.execute_multi_target_conformance(targets_file).await;
593 }
594 return self.execute_multi_target(targets_file).await;
595 }
596
597 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
599 return self.execute_sequential_specs().await;
600 }
601
602 TerminalReporter::print_header(
605 &self.get_spec_display_name(),
606 &self.target,
607 0, &self.scenario,
609 Self::parse_duration(&self.duration)?,
610 );
611
612 if !K6Executor::is_k6_installed() {
614 TerminalReporter::print_error("k6 is not installed");
615 TerminalReporter::print_warning(
616 "Install k6 from: https://k6.io/docs/get-started/installation/",
617 );
618 return Err(BenchError::K6NotFound);
619 }
620
621 if self.conformance {
623 return self.execute_conformance_test().await;
624 }
625
626 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
628 let merged_spec = self.load_and_merge_specs().await?;
629 let parser = SpecParser::from_spec(merged_spec);
630 if self.spec.len() > 1 || self.spec_dir.is_some() {
631 TerminalReporter::print_success(&format!(
632 "Loaded and merged {} specification(s)",
633 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
634 ));
635 } else {
636 TerminalReporter::print_success("Specification loaded");
637 }
638
639 let mock_config = self.build_mock_config().await;
641 if mock_config.is_mock_server {
642 TerminalReporter::print_progress("Mock server integration enabled");
643 }
644
645 if self.crud_flow {
647 return self.execute_crud_flow(&parser).await;
648 }
649
650 if self.owasp_api_top10 {
652 return self.execute_owasp_test(&parser).await;
653 }
654
655 TerminalReporter::print_progress("Extracting API operations...");
657 let mut operations = if let Some(filter) = &self.operations {
658 parser.filter_operations(filter)?
659 } else {
660 parser.get_operations()
661 };
662
663 if let Some(exclude) = &self.exclude_operations {
665 let before_count = operations.len();
666 operations = parser.exclude_operations(operations, exclude)?;
667 let excluded_count = before_count - operations.len();
668 if excluded_count > 0 {
669 TerminalReporter::print_progress(&format!(
670 "Excluded {} operations matching '{}'",
671 excluded_count, exclude
672 ));
673 }
674 }
675
676 if operations.is_empty() {
677 return Err(BenchError::Other("No operations found in spec".to_string()));
678 }
679
680 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
681
682 let param_overrides = if let Some(params_file) = &self.params_file {
684 TerminalReporter::print_progress("Loading parameter overrides...");
685 let overrides = ParameterOverrides::from_file(params_file)?;
686 TerminalReporter::print_success(&format!(
687 "Loaded parameter overrides ({} operation-specific, {} defaults)",
688 overrides.operations.len(),
689 if overrides.defaults.is_empty() { 0 } else { 1 }
690 ));
691 Some(overrides)
692 } else {
693 None
694 };
695
696 TerminalReporter::print_progress("Generating request templates...");
698 let templates: Vec<_> = operations
699 .iter()
700 .map(|op| {
701 let op_overrides = param_overrides.as_ref().map(|po| {
702 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
703 });
704 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
705 })
706 .collect::<Result<Vec<_>>>()?;
707 TerminalReporter::print_success("Request templates generated");
708
709 let custom_headers = self.parse_headers()?;
711
712 let base_path = self.resolve_base_path(&parser);
714 if let Some(ref bp) = base_path {
715 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
716 }
717
718 TerminalReporter::print_progress("Generating k6 load test script...");
720 let scenario =
721 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
722
723 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
724
725 let num_ops = operations.len() as u32;
743 if let Some(rps) = self.target_rps {
744 let probe =
745 crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
746
747 let (required_vus, basis) = match probe {
748 Some(p) => (
749 p.required_vus(rps, num_ops),
750 format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
751 ),
752 None => {
753 let fallback = (rps as u64)
755 .saturating_mul(num_ops.max(1) as u64)
756 .div_ceil(10)
757 .min(u32::MAX as u64) as u32;
758 (fallback, "~100ms (default — probe failed)".to_string())
759 }
760 };
761
762 if self.vus < required_vus {
763 const VU_RECOMMENDATION_CAP: u32 = 1000;
769 let recommendation = required_vus.max(self.vus + 1);
770 if recommendation > VU_RECOMMENDATION_CAP {
771 TerminalReporter::print_warning(&format!(
772 "Workload is very large: --rps {} × {} ops/iteration × {} \
773 baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
774 practical to drive. Two ways to fix:\n 1. Reduce \
775 operations per iteration with `--operations 'pattern,…'` \
776 (or `--exclude-operations`) to focus the bench on a \
777 representative subset.\n 2. Drop `--rps` and use \
778 `--vus {}` alone — closed-model load runs as fast as \
779 the VU pool allows, bounded by latency, with no per-\
780 iteration deadline. Expect 1-iteration coverage of ~{} \
781 operations in {}s.",
782 rps,
783 num_ops,
784 basis,
785 recommendation,
786 self.vus.max(5),
787 num_ops,
788 Self::parse_duration(&self.duration).unwrap_or(0),
789 ));
790 } else {
791 TerminalReporter::print_warning(&format!(
792 "--vus {} may be insufficient for --rps {} × {} ops/iteration \
793 (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
794 and each runs every operation in the spec — required ≈ rps × ops × \
795 latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
796 warnings.",
797 self.vus, rps, num_ops, basis, recommendation,
798 ));
799 }
800 } else if probe.is_some() {
801 TerminalReporter::print_progress(&format!(
802 "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
803 is sufficient for --rps {}",
804 basis, num_ops, self.vus, rps,
805 ));
806 }
807 }
808
809 let k6_config = K6Config {
810 target_url: self.target.clone(),
811 base_path,
812 scenario,
813 duration_secs: Self::parse_duration(&self.duration)?,
814 max_vus: self.vus,
815 threshold_percentile: self.threshold_percentile.clone(),
816 threshold_ms: self.threshold_ms,
817 max_error_rate: self.max_error_rate,
818 auth_header: self.auth.clone(),
819 custom_headers,
820 skip_tls_verify: self.skip_tls_verify,
821 security_testing_enabled,
822 chunked_request_bodies: self.chunked_request_bodies,
823 target_rps: self.target_rps,
824 no_keep_alive: self.no_keep_alive,
825 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
831 .into_iter()
832 .map(|ip| ip.to_string())
833 .collect(),
834 geo_source_headers: if self.geo_source_headers.is_empty()
835 && !self.geo_source_ips.is_empty()
836 {
837 crate::conformance::self_test::default_geo_source_headers()
838 } else {
839 self.geo_source_headers.clone()
840 },
841 };
842
843 let generator = K6ScriptGenerator::new(k6_config, templates);
844 let mut script = generator.generate()?;
845 TerminalReporter::print_success("k6 script generated");
846
847 let has_advanced_features = self.data_file.is_some()
849 || self.error_rate.is_some()
850 || self.security_test
851 || self.parallel_create.is_some()
852 || self.wafbench_dir.is_some();
853
854 if has_advanced_features {
856 script = self.generate_enhanced_script(&script)?;
857 }
858
859 if mock_config.is_mock_server {
861 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
862 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
863 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
864
865 if let Some(import_end) = script.find("export const options") {
867 script.insert_str(
868 import_end,
869 &format!(
870 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
871 helper_code, setup_code, teardown_code
872 ),
873 );
874 }
875 }
876
877 TerminalReporter::print_progress("Validating k6 script...");
879 let validation_errors = K6ScriptGenerator::validate_script(&script);
880 if !validation_errors.is_empty() {
881 TerminalReporter::print_error("Script validation failed");
882 for error in &validation_errors {
883 eprintln!(" {}", error);
884 }
885 return Err(BenchError::Other(format!(
886 "Generated k6 script has {} validation error(s). Please check the output above.",
887 validation_errors.len()
888 )));
889 }
890 TerminalReporter::print_success("Script validation passed");
891
892 let script_path = if let Some(output) = &self.script_output {
894 output.clone()
895 } else {
896 self.output.join("k6-script.js")
897 };
898
899 if let Some(parent) = script_path.parent() {
900 std::fs::create_dir_all(parent)?;
901 }
902 std::fs::write(&script_path, &script)?;
903 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
904
905 if self.generate_only {
907 println!("\nScript generated successfully. Run it with:");
908 println!(" k6 run {}", script_path.display());
909 return Ok(());
910 }
911
912 TerminalReporter::print_progress("Executing load test...");
914 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
915
916 std::fs::create_dir_all(&self.output)?;
917
918 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
919
920 let duration_secs = Self::parse_duration(&self.duration)?;
922 TerminalReporter::print_summary_full(
923 &results,
924 duration_secs,
925 self.no_keep_alive,
926 Some(num_ops),
927 );
928
929 println!("\nResults saved to: {}", self.output.display());
930
931 Ok(())
932 }
933
934 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
936 TerminalReporter::print_progress("Parsing targets file...");
937 let targets = parse_targets_file(targets_file)?;
938 let num_targets = targets.len();
939 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
940
941 if targets.is_empty() {
942 return Err(BenchError::Other("No targets found in file".to_string()));
943 }
944
945 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
947 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
951 &self.get_spec_display_name(),
952 &format!("{} targets", num_targets),
953 0,
954 &self.scenario,
955 Self::parse_duration(&self.duration)?,
956 );
957
958 let executor = ParallelExecutor::new(
960 BenchCommand {
961 spec: self.spec.clone(),
963 spec_dir: self.spec_dir.clone(),
964 merge_conflicts: self.merge_conflicts.clone(),
965 spec_mode: self.spec_mode.clone(),
966 dependency_config: self.dependency_config.clone(),
967 target: self.target.clone(), base_path: self.base_path.clone(),
969 duration: self.duration.clone(),
970 vus: self.vus,
971 target_rps: self.target_rps,
972 no_keep_alive: self.no_keep_alive,
973 scenario: self.scenario.clone(),
974 operations: self.operations.clone(),
975 exclude_operations: self.exclude_operations.clone(),
976 auth: self.auth.clone(),
977 headers: self.headers.clone(),
978 output: self.output.clone(),
979 generate_only: self.generate_only,
980 script_output: self.script_output.clone(),
981 threshold_percentile: self.threshold_percentile.clone(),
982 threshold_ms: self.threshold_ms,
983 max_error_rate: self.max_error_rate,
984 verbose: self.verbose,
985 skip_tls_verify: self.skip_tls_verify,
986 chunked_request_bodies: self.chunked_request_bodies,
987 targets_file: None,
988 max_concurrency: None,
989 results_format: self.results_format.clone(),
990 params_file: self.params_file.clone(),
991 crud_flow: self.crud_flow,
992 flow_config: self.flow_config.clone(),
993 extract_fields: self.extract_fields.clone(),
994 parallel_create: self.parallel_create,
995 data_file: self.data_file.clone(),
996 data_distribution: self.data_distribution.clone(),
997 data_mappings: self.data_mappings.clone(),
998 per_uri_control: self.per_uri_control,
999 error_rate: self.error_rate,
1000 error_types: self.error_types.clone(),
1001 security_test: self.security_test,
1002 security_payloads: self.security_payloads.clone(),
1003 security_categories: self.security_categories.clone(),
1004 security_target_fields: self.security_target_fields.clone(),
1005 wafbench_dir: self.wafbench_dir.clone(),
1006 wafbench_cycle_all: self.wafbench_cycle_all,
1007 owasp_api_top10: self.owasp_api_top10,
1008 owasp_categories: self.owasp_categories.clone(),
1009 owasp_auth_header: self.owasp_auth_header.clone(),
1010 owasp_auth_token: self.owasp_auth_token.clone(),
1011 owasp_admin_paths: self.owasp_admin_paths.clone(),
1012 owasp_id_fields: self.owasp_id_fields.clone(),
1013 owasp_report: self.owasp_report.clone(),
1014 owasp_report_format: self.owasp_report_format.clone(),
1015 owasp_iterations: self.owasp_iterations,
1016 conformance: false,
1017 conformance_api_key: None,
1018 conformance_basic_auth: None,
1019 conformance_report: PathBuf::from("conformance-report.json"),
1020 conformance_categories: None,
1021 conformance_report_format: "json".to_string(),
1022 conformance_headers: vec![],
1023 conformance_all_operations: false,
1024 conformance_custom: None,
1025 conformance_delay_ms: 0,
1026 use_k6: false,
1027 conformance_custom_filter: None,
1028 export_requests: false,
1029 validate_requests: false,
1030 conformance_self_test: false,
1031 conformance_self_test_capture: false,
1032 conformance_self_test_iterations: 1,
1033 conformance_self_test_duration: None,
1034 validate_response_schemas: false,
1035 source_ips: self.source_ips.clone(),
1040 geo_source_ips: self.geo_source_ips.clone(),
1041 geo_source_headers: self.geo_source_headers.clone(),
1042 report_missed_cap: None,
1043 },
1044 targets,
1045 max_concurrency,
1046 );
1047
1048 let start_time = std::time::Instant::now();
1050 let aggregated_results = executor.execute_all().await?;
1051 let elapsed = start_time.elapsed();
1052
1053 self.report_multi_target_results(&aggregated_results, elapsed)?;
1055
1056 Ok(())
1057 }
1058
1059 fn report_multi_target_results(
1061 &self,
1062 results: &AggregatedResults,
1063 elapsed: std::time::Duration,
1064 ) -> Result<()> {
1065 TerminalReporter::print_multi_target_summary(results);
1067
1068 let total_secs = elapsed.as_secs();
1070 let hours = total_secs / 3600;
1071 let minutes = (total_secs % 3600) / 60;
1072 let seconds = total_secs % 60;
1073 if hours > 0 {
1074 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
1075 } else if minutes > 0 {
1076 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
1077 } else {
1078 println!("\n Total Elapsed Time: {}s", seconds);
1079 }
1080
1081 if self.results_format == "aggregated" || self.results_format == "both" {
1083 let summary_path = self.output.join("aggregated_summary.json");
1084 let summary_json = serde_json::json!({
1085 "total_elapsed_seconds": elapsed.as_secs(),
1086 "total_targets": results.total_targets,
1087 "successful_targets": results.successful_targets,
1088 "failed_targets": results.failed_targets,
1089 "aggregated_metrics": {
1090 "total_requests": results.aggregated_metrics.total_requests,
1091 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
1092 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
1093 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
1094 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
1095 "error_rate": results.aggregated_metrics.error_rate,
1096 "total_rps": results.aggregated_metrics.total_rps,
1097 "avg_rps": results.aggregated_metrics.avg_rps,
1098 "total_vus_max": results.aggregated_metrics.total_vus_max,
1099 },
1100 "target_results": results.target_results.iter().map(|r| {
1101 serde_json::json!({
1102 "target_url": r.target_url,
1103 "target_index": r.target_index,
1104 "success": r.success,
1105 "error": r.error,
1106 "total_requests": r.results.total_requests,
1107 "failed_requests": r.results.failed_requests,
1108 "avg_duration_ms": r.results.avg_duration_ms,
1109 "min_duration_ms": r.results.min_duration_ms,
1110 "med_duration_ms": r.results.med_duration_ms,
1111 "p90_duration_ms": r.results.p90_duration_ms,
1112 "p95_duration_ms": r.results.p95_duration_ms,
1113 "p99_duration_ms": r.results.p99_duration_ms,
1114 "max_duration_ms": r.results.max_duration_ms,
1115 "rps": r.results.rps,
1116 "vus_max": r.results.vus_max,
1117 "output_dir": r.output_dir.to_string_lossy(),
1118 })
1119 }).collect::<Vec<_>>(),
1120 });
1121
1122 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
1123 TerminalReporter::print_success(&format!(
1124 "Aggregated summary saved to: {}",
1125 summary_path.display()
1126 ));
1127 }
1128
1129 let csv_path = self.output.join("all_targets.csv");
1131 let mut csv = String::from(
1132 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
1133 );
1134 for r in &results.target_results {
1135 csv.push_str(&format!(
1136 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
1137 r.target_url,
1138 r.success,
1139 r.results.total_requests,
1140 r.results.failed_requests,
1141 r.results.rps,
1142 r.results.vus_max,
1143 r.results.min_duration_ms,
1144 r.results.avg_duration_ms,
1145 r.results.med_duration_ms,
1146 r.results.p90_duration_ms,
1147 r.results.p95_duration_ms,
1148 r.results.p99_duration_ms,
1149 r.results.max_duration_ms,
1150 r.error.as_deref().unwrap_or(""),
1151 ));
1152 }
1153 let _ = std::fs::write(&csv_path, &csv);
1154
1155 println!("\nResults saved to: {}", self.output.display());
1156 println!(" - Per-target results: {}", self.output.join("target_*").display());
1157 println!(" - All targets CSV: {}", csv_path.display());
1158 if self.results_format == "aggregated" || self.results_format == "both" {
1159 println!(
1160 " - Aggregated summary: {}",
1161 self.output.join("aggregated_summary.json").display()
1162 );
1163 }
1164
1165 Ok(())
1166 }
1167
1168 pub fn parse_duration(duration: &str) -> Result<u64> {
1170 let duration = duration.trim();
1171
1172 if let Some(secs) = duration.strip_suffix('s') {
1173 secs.parse::<u64>()
1174 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1175 } else if let Some(mins) = duration.strip_suffix('m') {
1176 mins.parse::<u64>()
1177 .map(|m| m * 60)
1178 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1179 } else if let Some(hours) = duration.strip_suffix('h') {
1180 hours
1181 .parse::<u64>()
1182 .map(|h| h * 3600)
1183 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1184 } else {
1185 duration
1187 .parse::<u64>()
1188 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
1189 }
1190 }
1191
1192 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
1194 let mut headers = parse_header_string(&self.headers)?;
1195
1196 let already_has = |hs: &HashMap<String, String>, name: &str| -> bool {
1207 hs.keys().any(|k| k.eq_ignore_ascii_case(name))
1208 };
1209
1210 if !already_has(&headers, "Authorization") {
1211 if let Some(b) = self.conformance_basic_auth.as_ref().filter(|s| !s.is_empty()) {
1212 use base64::Engine as _;
1213 let encoded = base64::engine::general_purpose::STANDARD.encode(b.as_bytes());
1214 headers.insert("Authorization".to_string(), format!("Basic {}", encoded));
1215 }
1216 }
1217
1218 for line in &self.conformance_headers {
1224 let Some((name, value)) = line.split_once(':') else {
1225 continue;
1226 };
1227 let name = name.trim();
1228 let value = value.trim();
1229 if name.is_empty() || already_has(&headers, name) {
1230 continue;
1231 }
1232 headers.insert(name.to_string(), value.to_string());
1233 }
1234
1235 if !self.conformance && self.conformance_api_key.is_some() {
1241 TerminalReporter::print_warning(
1242 "--conformance-api-key only fires under --conformance. For plain bench use --header 'X-API-Key: ...'.",
1243 );
1244 }
1245
1246 Ok(headers)
1247 }
1248
1249 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
1250 let extracted_path = output_dir.join("extracted_values.json");
1251 if !extracted_path.exists() {
1252 return Ok(ExtractedValues::new());
1253 }
1254
1255 let content = std::fs::read_to_string(&extracted_path)
1256 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1257 let parsed: serde_json::Value = serde_json::from_str(&content)
1258 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
1259
1260 let mut extracted = ExtractedValues::new();
1261 if let Some(values) = parsed.as_object() {
1262 for (key, value) in values {
1263 extracted.set(key.clone(), value.clone());
1264 }
1265 }
1266
1267 Ok(extracted)
1268 }
1269
1270 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
1279 if let Some(cli_base_path) = &self.base_path {
1281 if cli_base_path.is_empty() {
1282 return None;
1284 }
1285 return Some(cli_base_path.clone());
1286 }
1287
1288 parser.get_base_path()
1290 }
1291
1292 async fn build_mock_config(&self) -> MockIntegrationConfig {
1294 if MockServerDetector::looks_like_mock_server(&self.target) {
1296 if let Ok(info) = MockServerDetector::detect(&self.target).await {
1298 if info.is_mockforge {
1299 TerminalReporter::print_success(&format!(
1300 "Detected MockForge server (version: {})",
1301 info.version.as_deref().unwrap_or("unknown")
1302 ));
1303 return MockIntegrationConfig::mock_server();
1304 }
1305 }
1306 }
1307 MockIntegrationConfig::real_api()
1308 }
1309
1310 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
1312 if !self.crud_flow {
1313 return None;
1314 }
1315
1316 if let Some(config_path) = &self.flow_config {
1318 match CrudFlowConfig::from_file(config_path) {
1319 Ok(config) => return Some(config),
1320 Err(e) => {
1321 TerminalReporter::print_warning(&format!(
1322 "Failed to load flow config: {}. Using auto-detection.",
1323 e
1324 ));
1325 }
1326 }
1327 }
1328
1329 let extract_fields = self
1331 .extract_fields
1332 .as_ref()
1333 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
1334 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
1335
1336 Some(CrudFlowConfig {
1337 flows: Vec::new(), default_extract_fields: extract_fields,
1339 })
1340 }
1341
1342 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
1344 let data_file = self.data_file.as_ref()?;
1345
1346 let distribution = DataDistribution::from_str(&self.data_distribution)
1347 .unwrap_or(DataDistribution::UniquePerVu);
1348
1349 let mappings = self
1350 .data_mappings
1351 .as_ref()
1352 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
1353 .unwrap_or_default();
1354
1355 Some(DataDrivenConfig {
1356 file_path: data_file.to_string_lossy().to_string(),
1357 distribution,
1358 mappings,
1359 csv_has_header: true,
1360 per_uri_control: self.per_uri_control,
1361 per_uri_columns: crate::data_driven::PerUriColumns::default(),
1362 })
1363 }
1364
1365 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
1367 let error_rate = self.error_rate?;
1368
1369 let error_types = self
1370 .error_types
1371 .as_ref()
1372 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
1373 .unwrap_or_default();
1374
1375 Some(InvalidDataConfig {
1376 error_rate,
1377 error_types,
1378 target_fields: Vec::new(),
1379 })
1380 }
1381
1382 fn build_security_config(&self) -> Option<SecurityTestConfig> {
1384 if !self.security_test {
1385 return None;
1386 }
1387
1388 let categories = self
1389 .security_categories
1390 .as_ref()
1391 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1392 .unwrap_or_else(|| {
1393 let mut default = HashSet::new();
1394 default.insert(SecurityCategory::SqlInjection);
1395 default.insert(SecurityCategory::Xss);
1396 default
1397 });
1398
1399 let target_fields = self
1400 .security_target_fields
1401 .as_ref()
1402 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1403 .unwrap_or_default();
1404
1405 let custom_payloads_file =
1406 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1407
1408 Some(SecurityTestConfig {
1409 enabled: true,
1410 categories,
1411 target_fields,
1412 custom_payloads_file,
1413 include_high_risk: false,
1414 })
1415 }
1416
1417 fn build_parallel_config(&self) -> Option<ParallelConfig> {
1419 let count = self.parallel_create?;
1420
1421 Some(ParallelConfig::new(count))
1422 }
1423
1424 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1426 let Some(ref wafbench_dir) = self.wafbench_dir else {
1427 return Vec::new();
1428 };
1429
1430 let mut loader = WafBenchLoader::new();
1431
1432 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1433 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1434 return Vec::new();
1435 }
1436
1437 let stats = loader.stats();
1438
1439 if stats.files_processed == 0 {
1440 TerminalReporter::print_warning(&format!(
1441 "No WAFBench YAML files found matching '{}'",
1442 wafbench_dir
1443 ));
1444 if !stats.parse_errors.is_empty() {
1446 TerminalReporter::print_warning("Some files were found but failed to parse:");
1447 for error in &stats.parse_errors {
1448 TerminalReporter::print_warning(&format!(" - {}", error));
1449 }
1450 }
1451 return Vec::new();
1452 }
1453
1454 TerminalReporter::print_progress(&format!(
1455 "Loaded {} WAFBench files, {} test cases, {} payloads",
1456 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1457 ));
1458
1459 for (category, count) in &stats.by_category {
1461 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1462 }
1463
1464 for error in &stats.parse_errors {
1466 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1467 }
1468
1469 loader.to_security_payloads()
1470 }
1471
1472 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1474 let mut enhanced_script = base_script.to_string();
1475 let mut additional_code = String::new();
1476
1477 if let Some(config) = self.build_data_driven_config() {
1479 TerminalReporter::print_progress("Adding data-driven testing support...");
1480 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1481 additional_code.push('\n');
1482 TerminalReporter::print_success("Data-driven testing enabled");
1483 }
1484
1485 if let Some(config) = self.build_invalid_data_config() {
1487 TerminalReporter::print_progress("Adding invalid data testing support...");
1488 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1489 additional_code.push('\n');
1490 additional_code
1491 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1492 additional_code.push('\n');
1493 additional_code
1494 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1495 additional_code.push('\n');
1496 TerminalReporter::print_success(&format!(
1497 "Invalid data testing enabled ({}% error rate)",
1498 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1499 ));
1500 }
1501
1502 let security_config = self.build_security_config();
1504 let wafbench_payloads = self.load_wafbench_payloads();
1505 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1506
1507 if security_config.is_some() || !wafbench_payloads.is_empty() {
1508 TerminalReporter::print_progress("Adding security testing support...");
1509
1510 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1512
1513 if let Some(ref config) = security_config {
1514 payload_list.extend(SecurityPayloads::get_payloads(config));
1515 }
1516
1517 if !wafbench_payloads.is_empty() {
1519 TerminalReporter::print_progress(&format!(
1520 "Loading {} WAFBench attack patterns...",
1521 wafbench_payloads.len()
1522 ));
1523 payload_list.extend(wafbench_payloads);
1524 }
1525
1526 let target_fields =
1527 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1528
1529 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1530 &payload_list,
1531 self.wafbench_cycle_all,
1532 ));
1533 additional_code.push('\n');
1534 additional_code
1535 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1536 additional_code.push('\n');
1537 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1538 additional_code.push('\n');
1539
1540 let mode = if self.wafbench_cycle_all {
1541 "cycle-all"
1542 } else {
1543 "random"
1544 };
1545 TerminalReporter::print_success(&format!(
1546 "Security testing enabled ({} payloads, {} mode)",
1547 payload_list.len(),
1548 mode
1549 ));
1550 } else if security_requested {
1551 TerminalReporter::print_warning(
1555 "Security testing was requested but no payloads were loaded. \
1556 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1557 );
1558 additional_code
1559 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1560 additional_code.push('\n');
1561 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1562 additional_code.push('\n');
1563 }
1564
1565 if let Some(config) = self.build_parallel_config() {
1567 TerminalReporter::print_progress("Adding parallel execution support...");
1568 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1569 additional_code.push('\n');
1570 TerminalReporter::print_success(&format!(
1571 "Parallel execution enabled (count: {})",
1572 config.count
1573 ));
1574 }
1575
1576 if !additional_code.is_empty() {
1578 if let Some(import_end) = enhanced_script.find("export const options") {
1580 enhanced_script.insert_str(
1581 import_end,
1582 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1583 );
1584 }
1585 }
1586
1587 Ok(enhanced_script)
1588 }
1589
1590 async fn execute_sequential_specs(&self) -> Result<()> {
1592 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1593
1594 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1596
1597 if !self.spec.is_empty() {
1598 let specs = load_specs_from_files(self.spec.clone())
1599 .await
1600 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1601 all_specs.extend(specs);
1602 }
1603
1604 if let Some(spec_dir) = &self.spec_dir {
1605 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1606 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1607 })?;
1608 all_specs.extend(dir_specs);
1609 }
1610
1611 if all_specs.is_empty() {
1612 return Err(BenchError::Other(
1613 "No spec files found for sequential execution".to_string(),
1614 ));
1615 }
1616
1617 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1618
1619 let execution_order = if let Some(config_path) = &self.dependency_config {
1621 TerminalReporter::print_progress("Loading dependency configuration...");
1622 let config = SpecDependencyConfig::from_file(config_path)?;
1623
1624 if !config.disable_auto_detect && config.execution_order.is_empty() {
1625 self.detect_and_sort_specs(&all_specs)?
1627 } else {
1628 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1630 }
1631 } else {
1632 self.detect_and_sort_specs(&all_specs)?
1634 };
1635
1636 TerminalReporter::print_success(&format!(
1637 "Execution order: {}",
1638 execution_order
1639 .iter()
1640 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1641 .collect::<Vec<_>>()
1642 .join(" → ")
1643 ));
1644
1645 let mut extracted_values = ExtractedValues::new();
1647 let total_specs = execution_order.len();
1648
1649 for (index, spec_path) in execution_order.iter().enumerate() {
1650 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1651
1652 TerminalReporter::print_progress(&format!(
1653 "[{}/{}] Executing spec: {}",
1654 index + 1,
1655 total_specs,
1656 spec_name
1657 ));
1658
1659 let spec = all_specs
1661 .iter()
1662 .find(|(p, _)| {
1663 p == spec_path
1664 || p.file_name() == spec_path.file_name()
1665 || p.file_name() == Some(spec_path.as_os_str())
1666 })
1667 .map(|(_, s)| s.clone())
1668 .ok_or_else(|| {
1669 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1670 })?;
1671
1672 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1674
1675 extracted_values.merge(&new_values);
1677
1678 TerminalReporter::print_success(&format!(
1679 "[{}/{}] Completed: {} (extracted {} values)",
1680 index + 1,
1681 total_specs,
1682 spec_name,
1683 new_values.values.len()
1684 ));
1685 }
1686
1687 TerminalReporter::print_success(&format!(
1688 "Sequential execution complete: {} specs executed",
1689 total_specs
1690 ));
1691
1692 Ok(())
1693 }
1694
1695 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1697 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1698
1699 let mut detector = DependencyDetector::new();
1700 let dependencies = detector.detect_dependencies(specs);
1701
1702 if dependencies.is_empty() {
1703 TerminalReporter::print_progress("No dependencies detected, using file order");
1704 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1705 }
1706
1707 TerminalReporter::print_progress(&format!(
1708 "Detected {} cross-spec dependencies",
1709 dependencies.len()
1710 ));
1711
1712 for dep in &dependencies {
1713 TerminalReporter::print_progress(&format!(
1714 " {} → {} (via field '{}')",
1715 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1716 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1717 dep.field_name
1718 ));
1719 }
1720
1721 topological_sort(specs, &dependencies)
1722 }
1723
1724 async fn execute_single_spec(
1726 &self,
1727 spec: &OpenApiSpec,
1728 spec_name: &str,
1729 _external_values: &ExtractedValues,
1730 ) -> Result<ExtractedValues> {
1731 let parser = SpecParser::from_spec(spec.clone());
1732
1733 if self.crud_flow {
1735 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1737 } else {
1738 self.execute_standard_spec(&parser, spec_name).await?;
1740 Ok(ExtractedValues::new())
1741 }
1742 }
1743
1744 async fn execute_crud_flow_with_extraction(
1746 &self,
1747 parser: &SpecParser,
1748 spec_name: &str,
1749 ) -> Result<ExtractedValues> {
1750 let operations = parser.get_operations();
1751 let flows = CrudFlowDetector::detect_flows(&operations);
1752
1753 if flows.is_empty() {
1754 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1755 return Ok(ExtractedValues::new());
1756 }
1757
1758 TerminalReporter::print_progress(&format!(
1759 " {} CRUD flow(s) in {}",
1760 flows.len(),
1761 spec_name
1762 ));
1763
1764 let mut handlebars = handlebars::Handlebars::new();
1766 handlebars.register_helper(
1768 "json",
1769 Box::new(
1770 |h: &handlebars::Helper,
1771 _: &handlebars::Handlebars,
1772 _: &handlebars::Context,
1773 _: &mut handlebars::RenderContext,
1774 out: &mut dyn handlebars::Output|
1775 -> handlebars::HelperResult {
1776 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1777 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1778 Ok(())
1779 },
1780 ),
1781 );
1782 let template = include_str!("templates/k6_crud_flow.hbs");
1783 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1784
1785 let custom_headers = self.parse_headers()?;
1786 let config = self.build_crud_flow_config().unwrap_or_default();
1787
1788 let param_overrides = if let Some(params_file) = &self.params_file {
1790 let overrides = ParameterOverrides::from_file(params_file)?;
1791 Some(overrides)
1792 } else {
1793 None
1794 };
1795
1796 let duration_secs = Self::parse_duration(&self.duration)?;
1798 let scenario =
1799 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1800 let stages = scenario.generate_stages(duration_secs, self.vus);
1801
1802 let api_base_path = self.resolve_base_path(parser);
1804
1805 let mut all_headers = custom_headers.clone();
1807 if let Some(auth) = &self.auth {
1808 all_headers.insert("Authorization".to_string(), auth.clone());
1809 }
1810 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1811
1812 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1814
1815 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1816 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1820 serde_json::json!({
1821 "name": sanitized_name.clone(),
1822 "display_name": f.name,
1823 "base_path": f.base_path,
1824 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1825 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1827 let method_raw = if !parts.is_empty() {
1828 parts[0].to_uppercase()
1829 } else {
1830 "GET".to_string()
1831 };
1832 let method = if !parts.is_empty() {
1833 let m = parts[0].to_lowercase();
1834 if m == "delete" { "del".to_string() } else { m }
1836 } else {
1837 "get".to_string()
1838 };
1839 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1840 let path = if let Some(ref bp) = api_base_path {
1842 format!("{}{}", bp, raw_path)
1843 } else {
1844 raw_path.to_string()
1845 };
1846 let is_get_or_head = method == "get" || method == "head";
1847 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1849
1850 let body_value = if has_body {
1852 param_overrides.as_ref()
1853 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1854 .and_then(|oo| oo.body)
1855 .unwrap_or_else(|| serde_json::json!({}))
1856 } else {
1857 serde_json::json!({})
1858 };
1859
1860 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1862
1863 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1865 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1866
1867 serde_json::json!({
1868 "operation": s.operation,
1869 "method": method,
1870 "path": path,
1871 "extract": s.extract,
1872 "use_values": s.use_values,
1873 "use_body": s.use_body,
1874 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1875 "inject_attacks": s.inject_attacks,
1876 "attack_types": s.attack_types,
1877 "description": s.description,
1878 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1879 "is_get_or_head": is_get_or_head,
1880 "has_body": has_body,
1881 "body": processed_body.value,
1882 "body_is_dynamic": body_is_dynamic,
1883 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1884 })
1885 }).collect::<Vec<_>>(),
1886 })
1887 }).collect();
1888
1889 for flow_data in &flows_data {
1891 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1892 for step in steps {
1893 if let Some(placeholders_arr) =
1894 step.get("_placeholders").and_then(|p| p.as_array())
1895 {
1896 for p_str in placeholders_arr {
1897 if let Some(p_name) = p_str.as_str() {
1898 match p_name {
1899 "VU" => {
1900 all_placeholders.insert(DynamicPlaceholder::VU);
1901 }
1902 "Iteration" => {
1903 all_placeholders.insert(DynamicPlaceholder::Iteration);
1904 }
1905 "Timestamp" => {
1906 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1907 }
1908 "UUID" => {
1909 all_placeholders.insert(DynamicPlaceholder::UUID);
1910 }
1911 "Random" => {
1912 all_placeholders.insert(DynamicPlaceholder::Random);
1913 }
1914 "Counter" => {
1915 all_placeholders.insert(DynamicPlaceholder::Counter);
1916 }
1917 "Date" => {
1918 all_placeholders.insert(DynamicPlaceholder::Date);
1919 }
1920 "VuIter" => {
1921 all_placeholders.insert(DynamicPlaceholder::VuIter);
1922 }
1923 _ => {}
1924 }
1925 }
1926 }
1927 }
1928 }
1929 }
1930 }
1931
1932 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1934 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1935
1936 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1938
1939 let data = serde_json::json!({
1940 "base_url": self.target,
1941 "flows": flows_data,
1942 "extract_fields": config.default_extract_fields,
1943 "duration_secs": duration_secs,
1944 "max_vus": self.vus,
1945 "auth_header": self.auth,
1946 "custom_headers": custom_headers,
1947 "skip_tls_verify": self.skip_tls_verify,
1948 "stages": stages.iter().map(|s| serde_json::json!({
1950 "duration": s.duration,
1951 "target": s.target,
1952 })).collect::<Vec<_>>(),
1953 "threshold_percentile": self.threshold_percentile,
1954 "threshold_ms": self.threshold_ms,
1955 "max_error_rate": self.max_error_rate,
1956 "headers": headers_json,
1957 "dynamic_imports": required_imports,
1958 "dynamic_globals": required_globals,
1959 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1960 "security_testing_enabled": security_testing_enabled,
1962 "has_custom_headers": !custom_headers.is_empty(),
1963 });
1964
1965 let mut script = handlebars
1966 .render_template(template, &data)
1967 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1968
1969 if security_testing_enabled {
1971 script = self.generate_enhanced_script(&script)?;
1972 }
1973
1974 let script_path =
1976 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1977
1978 std::fs::create_dir_all(self.output.clone())?;
1979 std::fs::write(&script_path, &script)?;
1980
1981 if !self.generate_only {
1982 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
1983 std::fs::create_dir_all(&output_dir)?;
1984
1985 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1986
1987 let extracted = Self::parse_extracted_values(&output_dir)?;
1988 TerminalReporter::print_progress(&format!(
1989 " Extracted {} value(s) from {}",
1990 extracted.values.len(),
1991 spec_name
1992 ));
1993 return Ok(extracted);
1994 }
1995
1996 Ok(ExtractedValues::new())
1997 }
1998
1999 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
2001 let mut operations = if let Some(filter) = &self.operations {
2002 parser.filter_operations(filter)?
2003 } else {
2004 parser.get_operations()
2005 };
2006
2007 if let Some(exclude) = &self.exclude_operations {
2008 operations = parser.exclude_operations(operations, exclude)?;
2009 }
2010
2011 if operations.is_empty() {
2012 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
2013 return Ok(());
2014 }
2015
2016 TerminalReporter::print_progress(&format!(
2017 " {} operations in {}",
2018 operations.len(),
2019 spec_name
2020 ));
2021
2022 let templates: Vec<_> = operations
2024 .iter()
2025 .map(RequestGenerator::generate_template)
2026 .collect::<Result<Vec<_>>>()?;
2027
2028 let custom_headers = self.parse_headers()?;
2030
2031 let base_path = self.resolve_base_path(parser);
2033
2034 let scenario =
2036 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2037
2038 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
2039
2040 let k6_config = K6Config {
2041 target_url: self.target.clone(),
2042 base_path,
2043 scenario,
2044 duration_secs: Self::parse_duration(&self.duration)?,
2045 max_vus: self.vus,
2046 threshold_percentile: self.threshold_percentile.clone(),
2047 threshold_ms: self.threshold_ms,
2048 max_error_rate: self.max_error_rate,
2049 auth_header: self.auth.clone(),
2050 custom_headers,
2051 skip_tls_verify: self.skip_tls_verify,
2052 security_testing_enabled,
2053 chunked_request_bodies: self.chunked_request_bodies,
2054 target_rps: self.target_rps,
2055 no_keep_alive: self.no_keep_alive,
2056 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip")
2058 .into_iter()
2059 .map(|ip| ip.to_string())
2060 .collect(),
2061 geo_source_headers: if self.geo_source_headers.is_empty()
2062 && !self.geo_source_ips.is_empty()
2063 {
2064 crate::conformance::self_test::default_geo_source_headers()
2065 } else {
2066 self.geo_source_headers.clone()
2067 },
2068 };
2069
2070 let generator = K6ScriptGenerator::new(k6_config, templates);
2071 let mut script = generator.generate()?;
2072
2073 let has_advanced_features = self.data_file.is_some()
2075 || self.error_rate.is_some()
2076 || self.security_test
2077 || self.parallel_create.is_some()
2078 || self.wafbench_dir.is_some();
2079
2080 if has_advanced_features {
2081 script = self.generate_enhanced_script(&script)?;
2082 }
2083
2084 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
2086
2087 std::fs::create_dir_all(self.output.clone())?;
2088 std::fs::write(&script_path, &script)?;
2089
2090 if !self.generate_only {
2091 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2092 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
2093 std::fs::create_dir_all(&output_dir)?;
2094
2095 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
2096 }
2097
2098 Ok(())
2099 }
2100
2101 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
2103 let config = self.build_crud_flow_config().unwrap_or_default();
2105
2106 let flows = if !config.flows.is_empty() {
2108 TerminalReporter::print_progress("Using custom flow configuration...");
2109 config.flows.clone()
2110 } else {
2111 TerminalReporter::print_progress("Detecting CRUD operations...");
2112 let operations = parser.get_operations();
2113 CrudFlowDetector::detect_flows(&operations)
2114 };
2115
2116 if flows.is_empty() {
2117 return Err(BenchError::Other(
2118 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
2119 ));
2120 }
2121
2122 if config.flows.is_empty() {
2123 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
2124 } else {
2125 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
2126 }
2127
2128 for flow in &flows {
2129 TerminalReporter::print_progress(&format!(
2130 " - {}: {} steps",
2131 flow.name,
2132 flow.steps.len()
2133 ));
2134 }
2135
2136 let mut handlebars = handlebars::Handlebars::new();
2138 handlebars.register_helper(
2140 "json",
2141 Box::new(
2142 |h: &handlebars::Helper,
2143 _: &handlebars::Handlebars,
2144 _: &handlebars::Context,
2145 _: &mut handlebars::RenderContext,
2146 out: &mut dyn handlebars::Output|
2147 -> handlebars::HelperResult {
2148 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
2149 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
2150 Ok(())
2151 },
2152 ),
2153 );
2154 let template = include_str!("templates/k6_crud_flow.hbs");
2155
2156 let custom_headers = self.parse_headers()?;
2157
2158 let param_overrides = if let Some(params_file) = &self.params_file {
2160 TerminalReporter::print_progress("Loading parameter overrides...");
2161 let overrides = ParameterOverrides::from_file(params_file)?;
2162 TerminalReporter::print_success(&format!(
2163 "Loaded parameter overrides ({} operation-specific, {} defaults)",
2164 overrides.operations.len(),
2165 if overrides.defaults.is_empty() { 0 } else { 1 }
2166 ));
2167 Some(overrides)
2168 } else {
2169 None
2170 };
2171
2172 let duration_secs = Self::parse_duration(&self.duration)?;
2174 let scenario =
2175 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
2176 let stages = scenario.generate_stages(duration_secs, self.vus);
2177
2178 let api_base_path = self.resolve_base_path(parser);
2180 if let Some(ref bp) = api_base_path {
2181 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
2182 }
2183
2184 let mut all_headers = custom_headers.clone();
2186 if let Some(auth) = &self.auth {
2187 all_headers.insert("Authorization".to_string(), auth.clone());
2188 }
2189 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
2190
2191 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
2193
2194 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
2195 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
2200 serde_json::json!({
2201 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
2204 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
2205 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
2207 let method_raw = if !parts.is_empty() {
2208 parts[0].to_uppercase()
2209 } else {
2210 "GET".to_string()
2211 };
2212 let method = if !parts.is_empty() {
2213 let m = parts[0].to_lowercase();
2214 if m == "delete" { "del".to_string() } else { m }
2216 } else {
2217 "get".to_string()
2218 };
2219 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
2220 let path = if let Some(ref bp) = api_base_path {
2222 format!("{}{}", bp, raw_path)
2223 } else {
2224 raw_path.to_string()
2225 };
2226 let is_get_or_head = method == "get" || method == "head";
2227 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
2229
2230 let body_value = if has_body {
2232 param_overrides.as_ref()
2233 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
2234 .and_then(|oo| oo.body)
2235 .unwrap_or_else(|| serde_json::json!({}))
2236 } else {
2237 serde_json::json!({})
2238 };
2239
2240 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
2242 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
2247 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
2248
2249 serde_json::json!({
2250 "operation": s.operation,
2251 "method": method,
2252 "path": path,
2253 "extract": s.extract,
2254 "use_values": s.use_values,
2255 "use_body": s.use_body,
2256 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
2257 "inject_attacks": s.inject_attacks,
2258 "attack_types": s.attack_types,
2259 "description": s.description,
2260 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
2261 "is_get_or_head": is_get_or_head,
2262 "has_body": has_body,
2263 "body": processed_body.value,
2264 "body_is_dynamic": body_is_dynamic,
2265 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
2266 })
2267 }).collect::<Vec<_>>(),
2268 })
2269 }).collect();
2270
2271 for flow_data in &flows_data {
2273 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
2274 for step in steps {
2275 if let Some(placeholders_arr) =
2276 step.get("_placeholders").and_then(|p| p.as_array())
2277 {
2278 for p_str in placeholders_arr {
2279 if let Some(p_name) = p_str.as_str() {
2280 match p_name {
2282 "VU" => {
2283 all_placeholders.insert(DynamicPlaceholder::VU);
2284 }
2285 "Iteration" => {
2286 all_placeholders.insert(DynamicPlaceholder::Iteration);
2287 }
2288 "Timestamp" => {
2289 all_placeholders.insert(DynamicPlaceholder::Timestamp);
2290 }
2291 "UUID" => {
2292 all_placeholders.insert(DynamicPlaceholder::UUID);
2293 }
2294 "Random" => {
2295 all_placeholders.insert(DynamicPlaceholder::Random);
2296 }
2297 "Counter" => {
2298 all_placeholders.insert(DynamicPlaceholder::Counter);
2299 }
2300 "Date" => {
2301 all_placeholders.insert(DynamicPlaceholder::Date);
2302 }
2303 "VuIter" => {
2304 all_placeholders.insert(DynamicPlaceholder::VuIter);
2305 }
2306 _ => {}
2307 }
2308 }
2309 }
2310 }
2311 }
2312 }
2313 }
2314
2315 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
2317 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
2318
2319 let invalid_data_config = self.build_invalid_data_config();
2321 let error_injection_enabled = invalid_data_config.is_some();
2322 let error_rate = self.error_rate.unwrap_or(0.0);
2323 let error_types: Vec<String> = invalid_data_config
2324 .as_ref()
2325 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
2326 .unwrap_or_default();
2327
2328 if error_injection_enabled {
2329 TerminalReporter::print_progress(&format!(
2330 "Error injection enabled ({}% rate)",
2331 (error_rate * 100.0) as u32
2332 ));
2333 }
2334
2335 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
2337
2338 let data = serde_json::json!({
2339 "base_url": self.target,
2340 "flows": flows_data,
2341 "extract_fields": config.default_extract_fields,
2342 "duration_secs": duration_secs,
2343 "max_vus": self.vus,
2344 "auth_header": self.auth,
2345 "custom_headers": custom_headers,
2346 "skip_tls_verify": self.skip_tls_verify,
2347 "stages": stages.iter().map(|s| serde_json::json!({
2349 "duration": s.duration,
2350 "target": s.target,
2351 })).collect::<Vec<_>>(),
2352 "threshold_percentile": self.threshold_percentile,
2353 "threshold_ms": self.threshold_ms,
2354 "max_error_rate": self.max_error_rate,
2355 "headers": headers_json,
2356 "dynamic_imports": required_imports,
2357 "dynamic_globals": required_globals,
2358 "extracted_values_output_path": self
2359 .output
2360 .join("crud_flow_extracted_values.json")
2361 .to_string_lossy(),
2362 "error_injection_enabled": error_injection_enabled,
2364 "error_rate": error_rate,
2365 "error_types": error_types,
2366 "security_testing_enabled": security_testing_enabled,
2368 "has_custom_headers": !custom_headers.is_empty(),
2369 });
2370
2371 let mut script = handlebars
2372 .render_template(template, &data)
2373 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
2374
2375 if security_testing_enabled {
2377 script = self.generate_enhanced_script(&script)?;
2378 }
2379
2380 TerminalReporter::print_progress("Validating CRUD flow script...");
2382 let validation_errors = K6ScriptGenerator::validate_script(&script);
2383 if !validation_errors.is_empty() {
2384 TerminalReporter::print_error("CRUD flow script validation failed");
2385 for error in &validation_errors {
2386 eprintln!(" {}", error);
2387 }
2388 return Err(BenchError::Other(format!(
2389 "CRUD flow script validation failed with {} error(s)",
2390 validation_errors.len()
2391 )));
2392 }
2393
2394 TerminalReporter::print_success("CRUD flow script generated");
2395
2396 let script_path = if let Some(output) = &self.script_output {
2398 output.clone()
2399 } else {
2400 self.output.join("k6-crud-flow-script.js")
2401 };
2402
2403 if let Some(parent) = script_path.parent() {
2404 std::fs::create_dir_all(parent)?;
2405 }
2406 std::fs::write(&script_path, &script)?;
2407 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2408
2409 if self.generate_only {
2410 println!("\nScript generated successfully. Run it with:");
2411 println!(" k6 run {}", script_path.display());
2412 return Ok(());
2413 }
2414
2415 TerminalReporter::print_progress("Executing CRUD flow test...");
2417 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2418 std::fs::create_dir_all(&self.output)?;
2419
2420 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2421
2422 let duration_secs = Self::parse_duration(&self.duration)?;
2423 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2424
2425 Ok(())
2426 }
2427
2428 async fn execute_conformance_test(&self) -> Result<()> {
2430 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2431 use crate::conformance::report::ConformanceReport;
2432 use crate::conformance::spec::ConformanceFeature;
2433
2434 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2435
2436 TerminalReporter::print_progress(
2439 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2440 );
2441
2442 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2444 cats_str
2445 .split(',')
2446 .filter_map(|s| {
2447 let trimmed = s.trim();
2448 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2449 Some(canonical.to_string())
2450 } else {
2451 TerminalReporter::print_warning(&format!(
2452 "Unknown conformance category: '{}'. Valid categories: {}",
2453 trimmed,
2454 ConformanceFeature::cli_category_names()
2455 .iter()
2456 .map(|(cli, _)| *cli)
2457 .collect::<Vec<_>>()
2458 .join(", ")
2459 ));
2460 None
2461 }
2462 })
2463 .collect::<Vec<String>>()
2464 });
2465
2466 let custom_headers: Vec<(String, String)> = self
2468 .conformance_headers
2469 .iter()
2470 .filter_map(|h| {
2471 let (name, value) = h.split_once(':')?;
2472 Some((name.trim().to_string(), value.trim().to_string()))
2473 })
2474 .collect();
2475
2476 if !custom_headers.is_empty() {
2477 TerminalReporter::print_progress(&format!(
2478 "Using {} custom header(s) for authentication",
2479 custom_headers.len()
2480 ));
2481 }
2482
2483 if self.conformance_delay_ms > 0 {
2484 TerminalReporter::print_progress(&format!(
2485 "Using {}ms delay between conformance requests",
2486 self.conformance_delay_ms
2487 ));
2488 }
2489
2490 std::fs::create_dir_all(&self.output)?;
2492
2493 let config = ConformanceConfig {
2494 target_url: self.target.clone(),
2495 api_key: self.conformance_api_key.clone(),
2496 basic_auth: self.conformance_basic_auth.clone(),
2497 skip_tls_verify: self.skip_tls_verify,
2498 categories,
2499 base_path: self.base_path.clone(),
2500 custom_headers,
2501 output_dir: Some(self.output.clone()),
2502 all_operations: self.conformance_all_operations,
2503 custom_checks_file: self.conformance_custom.clone(),
2504 request_delay_ms: self.conformance_delay_ms,
2505 custom_filter: self.conformance_custom_filter.clone(),
2506 export_requests: self.export_requests,
2507 validate_requests: self.validate_requests,
2508 };
2509
2510 let mut resolved_base_path: Option<String> = None;
2518 let annotated_ops = if !self.spec.is_empty() {
2519 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2520 let parser = SpecParser::from_file(&self.spec[0]).await?;
2521 resolved_base_path = self.resolve_base_path(&parser);
2522
2523 let mut operations = if let Some(filter) = &self.operations {
2528 parser.filter_operations(filter)?
2529 } else {
2530 parser.get_operations()
2531 };
2532 if let Some(exclude) = &self.exclude_operations {
2533 let before_count = operations.len();
2534 operations = parser.exclude_operations(operations, exclude)?;
2535 let excluded_count = before_count - operations.len();
2536 if excluded_count > 0 {
2537 TerminalReporter::print_progress(&format!(
2538 "Excluded {} operations matching '{}'",
2539 excluded_count, exclude
2540 ));
2541 }
2542 }
2543
2544 let annotated =
2545 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2546 &operations,
2547 parser.spec(),
2548 );
2549 TerminalReporter::print_success(&format!(
2550 "Analyzed {} operations, found {} feature annotations",
2551 operations.len(),
2552 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2553 ));
2554 Some(annotated)
2555 } else {
2556 None
2557 };
2558
2559 if self.conformance_self_test {
2566 let Some(ops) = annotated_ops else {
2567 TerminalReporter::print_error(
2568 "--conformance-self-test requires --spec; no operations to test",
2569 );
2570 return Ok(());
2571 };
2572 let cfg = crate::conformance::self_test::SelfTestConfig {
2573 target_url: self.target.clone(),
2574 skip_tls_verify: self.skip_tls_verify,
2575 timeout: std::time::Duration::from_secs(30),
2576 extra_headers: self
2580 .conformance_headers
2581 .iter()
2582 .filter_map(|h| {
2583 let (n, v) = h.split_once(':')?;
2584 Some((n.trim().to_string(), v.trim().to_string()))
2585 })
2586 .collect(),
2587 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
2588 base_path: resolved_base_path.clone(),
2592 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
2596 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
2597 geo_source_headers: if self.geo_source_headers.is_empty() {
2598 crate::conformance::self_test::default_geo_source_headers()
2599 } else {
2600 self.geo_source_headers.clone()
2601 },
2602 capture: if self.conformance_self_test_capture || self.validate_response_schemas {
2606 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
2612 } else {
2613 None
2614 },
2615 validate_response_schemas: self.validate_response_schemas,
2616 spec_label: self.spec.first().map(|p| {
2622 p.file_name()
2623 .map(|s| s.to_string_lossy().into_owned())
2624 .unwrap_or_else(|| p.to_string_lossy().into_owned())
2625 }),
2626 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
2633 current_iteration: 1,
2634 };
2635 let capture_sink = cfg.capture.clone();
2636 let network_events_sink = cfg.network_events.clone();
2637 TerminalReporter::print_progress(&format!(
2638 "Self-test mode: driving {} operations with positive + per-category negative cases",
2639 ops.len()
2640 ));
2641 let target_iterations = self.conformance_self_test_iterations.max(1);
2648 let duration_budget = self
2649 .conformance_self_test_duration
2650 .as_ref()
2651 .map(|s| Self::parse_duration(s))
2652 .transpose()?
2653 .map(std::time::Duration::from_secs);
2654 let start = std::time::Instant::now();
2655 let deadline = duration_budget.map(|d| start + d);
2664 let mut cfg = cfg;
2668 cfg.current_iteration = 1;
2669 let mut report =
2670 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
2671 .await
2672 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2673 let mut iter_done: u32 = 1;
2674 loop {
2675 let by_iter = iter_done >= target_iterations;
2676 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
2677 if by_iter && by_dur {
2678 break;
2679 }
2680 cfg.current_iteration = iter_done.saturating_add(1);
2681 let next = crate::conformance::self_test::run_self_test_with_deadline(
2682 &ops, &cfg, deadline,
2683 )
2684 .await
2685 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
2686 report.merge_iteration(next);
2687 iter_done = iter_done.saturating_add(1);
2688 }
2689 if iter_done > 1 {
2690 TerminalReporter::print_progress(&format!(
2691 "Self-test repeated {} iteration(s) ({:.1?} elapsed)",
2692 iter_done,
2693 start.elapsed(),
2694 ));
2695 }
2696 let per_endpoint_summary: Vec<
2706 crate::conformance::per_endpoint_summary::PerEndpointSummary,
2707 >;
2708 if let Some(sink) = capture_sink {
2709 if let Ok(guard) = sink.lock() {
2710 let jsonl_path = self.output.join("conformance-self-test-requests.jsonl");
2711 let mut lines = String::with_capacity(guard.len() * 256);
2712 for entry in guard.iter() {
2713 if let Ok(line) = serde_json::to_string(entry) {
2714 lines.push_str(&line);
2715 lines.push('\n');
2716 }
2717 }
2718 let _ = std::fs::write(&jsonl_path, lines);
2719 let html_path = self.output.join("conformance-self-test-requests.html");
2720 let html =
2721 crate::conformance::capture_html::render_capture_html(guard.as_slice());
2722 let _ = std::fs::write(&html_path, html);
2723
2724 per_endpoint_summary =
2728 crate::conformance::per_endpoint_summary::build_summary(guard.as_slice());
2729 let summary_path = self.output.join("conformance-per-endpoint.json");
2730 if let Ok(json) = serde_json::to_string_pretty(&per_endpoint_summary) {
2731 let _ = std::fs::write(&summary_path, json);
2732 TerminalReporter::print_progress(&format!(
2733 "Self-test request/response capture written to {} ({} entries) + {} + {}",
2734 jsonl_path.display(),
2735 guard.len(),
2736 html_path.display(),
2737 summary_path.display(),
2738 ));
2739 } else {
2740 TerminalReporter::print_progress(&format!(
2741 "Self-test request/response capture written to {} ({} entries) + {}",
2742 jsonl_path.display(),
2743 guard.len(),
2744 html_path.display(),
2745 ));
2746 }
2747 } else {
2748 per_endpoint_summary = Vec::new();
2749 }
2750 } else {
2751 per_endpoint_summary = Vec::new();
2752 }
2753 TerminalReporter::print_progress(&report.render_summary());
2754 if let Some(sink) = network_events_sink {
2761 if let Ok(guard) = sink.lock() {
2762 let path = self.output.join("conformance-network-events.json");
2763 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
2764 let _ = std::fs::write(&path, json);
2765 if guard.is_empty() {
2766 TerminalReporter::print_progress(
2767 "No wire-level network failures during self-test (file written empty)",
2768 );
2769 } else {
2770 TerminalReporter::print_warning(&format!(
2771 "Recorded {} wire-level network event(s) to {}",
2772 guard.len(),
2773 path.display()
2774 ));
2775 }
2776 }
2777 }
2778 }
2779 let json_path = self.output.join("conformance-self-test.json");
2783 if let Ok(json) = serde_json::to_string_pretty(&report) {
2784 let _ = std::fs::write(&json_path, json);
2785 TerminalReporter::print_progress(&format!(
2786 "Self-test report written to {}",
2787 json_path.display()
2788 ));
2789 }
2790 if let Some(status) = report.detect_target_misconfiguration() {
2799 let hint = match status {
2800 404 => " Likely cause: spec paths don't match deployed routes — check --base-path and the spec's `servers` block.",
2801 401 | 403 => " Likely cause: authentication header is missing or invalid — check --conformance-header.",
2802 _ => "",
2803 };
2804 TerminalReporter::print_warning(&format!(
2805 "Self-test misconfiguration: every positive case returned {status}.{hint} Negative results below are meaningless under this condition."
2806 ));
2807 } else if !report.all_passed() {
2808 TerminalReporter::print_warning(
2809 "Self-test detected gaps — server let through at least one request that should have been a 4xx",
2810 );
2811 } else {
2812 TerminalReporter::print_success(
2813 "Self-test passed — all positive cases accepted and all negative cases rejected",
2814 );
2815 }
2816 let html_path = self.output.join("conformance-report.html");
2823 let audit_path = self.output.join("conformance-spec-audit.json");
2824 let audit_value = std::fs::read_to_string(&audit_path)
2825 .ok()
2826 .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok());
2827 let render_opts = crate::conformance::report_html::RenderOptions {
2832 missed_cap: match self.report_missed_cap {
2833 Some(0) => None,
2834 Some(n) => Some(n as usize),
2835 None => Some(200),
2836 },
2837 };
2838 let mut html = crate::conformance::report_html::render_html_with_options(
2839 &report,
2840 audit_value.as_ref(),
2841 &render_opts,
2842 );
2843 let summary_section = crate::conformance::per_endpoint_summary::render_html_section(
2849 &per_endpoint_summary,
2850 );
2851 if !summary_section.is_empty() {
2852 if let Some(idx) = html.rfind("</body>") {
2853 html.insert_str(idx, &summary_section);
2854 } else {
2855 html.push_str(&summary_section);
2856 }
2857 }
2858 if std::fs::write(&html_path, html).is_ok() {
2859 TerminalReporter::print_progress(&format!(
2860 "HTML report written to {}",
2861 html_path.display()
2862 ));
2863 }
2864 return Ok(());
2865 }
2866
2867 if self.validate_requests && !self.spec.is_empty() {
2869 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2870 let violation_count = crate::conformance::request_validator::run_request_validation(
2871 &self.spec,
2872 self.conformance_custom.as_deref(),
2873 self.base_path.as_deref(),
2874 &self.output,
2875 )
2876 .await?;
2877 if violation_count > 0 {
2878 TerminalReporter::print_warning(&format!(
2879 "{} request validation violation(s) found — see conformance-request-violations.json",
2880 violation_count
2881 ));
2882 } else {
2883 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2884 }
2885 }
2886
2887 if self.generate_only || self.use_k6 {
2889 let script = if let Some(annotated) = &annotated_ops {
2890 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2891 config,
2892 annotated.clone(),
2893 );
2894 let op_count = gen.operation_count();
2895 let (script, check_count) = gen.generate()?;
2896 TerminalReporter::print_success(&format!(
2897 "Conformance: {} operations analyzed, {} unique checks generated",
2898 op_count, check_count
2899 ));
2900 script
2901 } else {
2902 let generator = ConformanceGenerator::new(config);
2903 generator.generate()?
2904 };
2905
2906 let script_path = self.output.join("k6-conformance.js");
2907 std::fs::write(&script_path, &script).map_err(|e| {
2908 BenchError::Other(format!("Failed to write conformance script: {}", e))
2909 })?;
2910 TerminalReporter::print_success(&format!(
2911 "Conformance script generated: {}",
2912 script_path.display()
2913 ));
2914
2915 if self.generate_only {
2916 println!("\nScript generated. Run with:");
2917 println!(" k6 run {}", script_path.display());
2918 return Ok(());
2919 }
2920
2921 if !K6Executor::is_k6_installed() {
2923 TerminalReporter::print_error("k6 is not installed");
2924 TerminalReporter::print_warning(
2925 "Install k6 from: https://k6.io/docs/get-started/installation/",
2926 );
2927 return Err(BenchError::K6NotFound);
2928 }
2929
2930 TerminalReporter::print_progress("Running conformance tests via k6...");
2931 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
2932 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2933
2934 let report_path = self.output.join("conformance-report.json");
2935 if report_path.exists() {
2936 let report = ConformanceReport::from_file(&report_path)?;
2937 report.print_report_with_options(self.conformance_all_operations);
2938 self.save_conformance_report(&report, &report_path)?;
2939 } else {
2940 TerminalReporter::print_warning(
2941 "Conformance report not generated (k6 handleSummary may not have run)",
2942 );
2943 }
2944
2945 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
2957 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
2958 &self.spec,
2959 &self.output,
2960 self.base_path.as_deref(),
2961 )
2962 .await?;
2963 if n > 0 {
2964 TerminalReporter::print_warning(&format!(
2965 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
2966 n
2967 ));
2968 }
2969 }
2970
2971 return Ok(());
2972 }
2973
2974 TerminalReporter::print_progress("Running conformance tests (native executor)...");
2976
2977 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2978
2979 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
2989 executor = if let Some(annotated) = &annotated_ops {
2990 executor.with_spec_driven_checks(annotated)
2991 } else if custom_only {
2992 executor
2993 } else {
2994 executor.with_reference_checks()
2995 };
2996 executor = executor.with_custom_checks()?;
2997
2998 TerminalReporter::print_success(&format!(
2999 "Executing {} conformance checks...",
3000 executor.check_count()
3001 ));
3002
3003 let report = executor.execute().await?;
3004 report.print_report_with_options(self.conformance_all_operations);
3005
3006 let failure_details = report.failure_details();
3008 if !failure_details.is_empty() {
3009 let details_path = self.output.join("conformance-failure-details.json");
3010 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3011 let _ = std::fs::write(&details_path, json);
3012 TerminalReporter::print_success(&format!(
3013 "Failure details saved to: {}",
3014 details_path.display()
3015 ));
3016 }
3017 }
3018
3019 let report_path = self.output.join("conformance-report.json");
3021 let report_json = serde_json::to_string_pretty(&report.to_json())
3022 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3023 std::fs::write(&report_path, &report_json)
3024 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3025 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
3026
3027 self.save_conformance_report(&report, &report_path)?;
3028
3029 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3040 let n =
3041 crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3042 &self.spec,
3043 &self.output,
3044 self.base_path.as_deref(),
3045 )
3046 .await?;
3047 if n > 0 {
3048 TerminalReporter::print_warning(&format!(
3049 "{} emitted request(s) violated the spec — see conformance-request-violations.json",
3050 n
3051 ));
3052 }
3053 }
3054
3055 Ok(())
3056 }
3057
3058 fn save_conformance_report(
3060 &self,
3061 report: &crate::conformance::report::ConformanceReport,
3062 report_path: &Path,
3063 ) -> Result<()> {
3064 if self.conformance_report_format == "sarif" {
3065 use crate::conformance::sarif::ConformanceSarifReport;
3066 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
3067 TerminalReporter::print_success(&format!(
3068 "SARIF report saved to: {}",
3069 self.conformance_report.display()
3070 ));
3071 } else if self.conformance_report != *report_path {
3072 std::fs::copy(report_path, &self.conformance_report)?;
3073 TerminalReporter::print_success(&format!(
3074 "Report saved to: {}",
3075 self.conformance_report.display()
3076 ));
3077 }
3078 Ok(())
3079 }
3080
3081 async fn execute_multi_target_self_test(&self, targets_file: &Path) -> Result<()> {
3093 use crate::conformance::self_test::SelfTestConfig;
3094
3095 TerminalReporter::print_progress("Multi-target conformance self-test mode");
3096 let targets = parse_targets_file(targets_file)?;
3097 if targets.is_empty() {
3098 return Err(BenchError::Other("No targets found in file".to_string()));
3099 }
3100 TerminalReporter::print_success(&format!("Loaded {} target(s)", targets.len()));
3101
3102 let annotated_ops = if !self.spec.is_empty() {
3104 let parser = SpecParser::from_file(&self.spec[0]).await?;
3105 let operations = parser.get_operations();
3106 Some(
3107 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3108 &operations,
3109 parser.spec(),
3110 ),
3111 )
3112 } else {
3113 return Err(BenchError::Other("--conformance-self-test requires --spec".to_string()));
3114 };
3115 let Some(ops) = annotated_ops else {
3116 unreachable!()
3117 };
3118
3119 std::fs::create_dir_all(&self.output)?;
3120 let resolved_base_path = self.base_path.clone();
3121 let target_iterations = self.conformance_self_test_iterations.max(1);
3122 let duration_budget = self
3123 .conformance_self_test_duration
3124 .as_ref()
3125 .map(|s| Self::parse_duration(s))
3126 .transpose()?
3127 .map(std::time::Duration::from_secs);
3128
3129 for (idx, target) in targets.iter().enumerate() {
3130 let target_dir = self.output.join(format!("target_{}", idx));
3131 std::fs::create_dir_all(&target_dir)?;
3132 TerminalReporter::print_progress(&format!(
3133 "[target {}/{}] {}",
3134 idx + 1,
3135 targets.len(),
3136 target.url
3137 ));
3138
3139 let merged_headers: Vec<(String, String)> = self
3140 .conformance_headers
3141 .iter()
3142 .filter_map(|h| {
3143 let (n, v) = h.split_once(':')?;
3144 Some((n.trim().to_string(), v.trim().to_string()))
3145 })
3146 .collect();
3147
3148 let cfg = SelfTestConfig {
3149 target_url: target.url.clone(),
3150 skip_tls_verify: self.skip_tls_verify,
3151 timeout: std::time::Duration::from_secs(30),
3152 extra_headers: merged_headers,
3153 delay_between_requests: std::time::Duration::from_millis(self.conformance_delay_ms),
3154 base_path: resolved_base_path.clone(),
3155 source_ips: parse_ip_list(&self.source_ips, "source-ip"),
3156 geo_source_ips: parse_ip_list(&self.geo_source_ips, "geo-source-ip"),
3157 geo_source_headers: if self.geo_source_headers.is_empty() {
3158 crate::conformance::self_test::default_geo_source_headers()
3159 } else {
3160 self.geo_source_headers.clone()
3161 },
3162 capture: if self.conformance_self_test_capture || self.validate_response_schemas {
3163 Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new())))
3164 } else {
3165 None
3166 },
3167 validate_response_schemas: self.validate_response_schemas,
3168 spec_label: self.spec.first().map(|p| {
3169 p.file_name()
3170 .map(|s| s.to_string_lossy().into_owned())
3171 .unwrap_or_else(|| p.to_string_lossy().into_owned())
3172 }),
3173 network_events: Some(std::sync::Arc::new(std::sync::Mutex::new(Vec::new()))),
3174 current_iteration: 1,
3175 };
3176 let capture_sink = cfg.capture.clone();
3177 let network_events_sink = cfg.network_events.clone();
3178
3179 let start = std::time::Instant::now();
3180 let deadline = duration_budget.map(|d| start + d);
3184 let mut cfg = cfg;
3188 cfg.current_iteration = 1;
3189 let mut report =
3190 crate::conformance::self_test::run_self_test_with_deadline(&ops, &cfg, deadline)
3191 .await
3192 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3193 let mut iter_done: u32 = 1;
3194 loop {
3195 let by_iter = iter_done >= target_iterations;
3196 let by_dur = duration_budget.map(|d| start.elapsed() >= d).unwrap_or(true);
3197 if by_iter && by_dur {
3198 break;
3199 }
3200 cfg.current_iteration = iter_done.saturating_add(1);
3201 let next = crate::conformance::self_test::run_self_test_with_deadline(
3202 &ops, &cfg, deadline,
3203 )
3204 .await
3205 .map_err(|e| BenchError::Other(format!("self-test client error: {e}")))?;
3206 report.merge_iteration(next);
3207 iter_done = iter_done.saturating_add(1);
3208 }
3209 if iter_done > 1 {
3210 TerminalReporter::print_progress(&format!(
3211 " ran {} iteration(s) in {:.1?}",
3212 iter_done,
3213 start.elapsed(),
3214 ));
3215 }
3216
3217 if let Some(sink) = capture_sink {
3219 if let Ok(guard) = sink.lock() {
3220 let jsonl = target_dir.join("conformance-self-test-requests.jsonl");
3221 let mut lines = String::with_capacity(guard.len() * 256);
3222 for entry in guard.iter() {
3223 if let Ok(line) = serde_json::to_string(entry) {
3224 lines.push_str(&line);
3225 lines.push('\n');
3226 }
3227 }
3228 let _ = std::fs::write(&jsonl, lines);
3229 }
3230 }
3231 if let Some(sink) = network_events_sink {
3232 if let Ok(guard) = sink.lock() {
3233 let path = target_dir.join("conformance-network-events.json");
3234 if let Ok(json) = serde_json::to_string_pretty(&*guard) {
3235 let _ = std::fs::write(&path, json);
3236 if !guard.is_empty() {
3237 TerminalReporter::print_warning(&format!(
3238 " recorded {} wire-level network event(s)",
3239 guard.len()
3240 ));
3241 }
3242 }
3243 }
3244 }
3245
3246 let json_path = target_dir.join("conformance-self-test.json");
3247 if let Ok(json) = serde_json::to_string_pretty(&report) {
3248 let _ = std::fs::write(&json_path, json);
3249 }
3250 TerminalReporter::print_progress(&report.render_summary());
3251
3252 if self.validate_requests {
3261 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3262 &self.spec,
3263 &target_dir,
3264 self.base_path.as_deref(),
3265 )
3266 .await?;
3267 if n > 0 {
3268 TerminalReporter::print_warning(&format!(
3269 " {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3270 n,
3271 target_dir.display(),
3272 ));
3273 }
3274 }
3275 }
3276
3277 Ok(())
3278 }
3279
3280 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
3286 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
3287 use crate::conformance::report::ConformanceReport;
3288 use crate::conformance::spec::ConformanceFeature;
3289
3290 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
3291
3292 TerminalReporter::print_progress("Parsing targets file...");
3294 let targets = parse_targets_file(targets_file)?;
3295 let num_targets = targets.len();
3296 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
3297
3298 if targets.is_empty() {
3299 return Err(BenchError::Other("No targets found in file".to_string()));
3300 }
3301
3302 TerminalReporter::print_progress(
3303 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
3304 );
3305
3306 let categories = self.conformance_categories.as_ref().map(|cats_str| {
3308 cats_str
3309 .split(',')
3310 .filter_map(|s| {
3311 let trimmed = s.trim();
3312 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
3313 Some(canonical.to_string())
3314 } else {
3315 TerminalReporter::print_warning(&format!(
3316 "Unknown conformance category: '{}'. Valid categories: {}",
3317 trimmed,
3318 ConformanceFeature::cli_category_names()
3319 .iter()
3320 .map(|(cli, _)| *cli)
3321 .collect::<Vec<_>>()
3322 .join(", ")
3323 ));
3324 None
3325 }
3326 })
3327 .collect::<Vec<String>>()
3328 });
3329
3330 let base_custom_headers: Vec<(String, String)> = self
3332 .conformance_headers
3333 .iter()
3334 .filter_map(|h| {
3335 let (name, value) = h.split_once(':')?;
3336 Some((name.trim().to_string(), value.trim().to_string()))
3337 })
3338 .collect();
3339
3340 if !base_custom_headers.is_empty() {
3341 TerminalReporter::print_progress(&format!(
3342 "Using {} base custom header(s) for authentication",
3343 base_custom_headers.len()
3344 ));
3345 }
3346
3347 let annotated_ops = if !self.spec.is_empty() {
3349 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
3350 let parser = SpecParser::from_file(&self.spec[0]).await?;
3351 let operations = parser.get_operations();
3352 let annotated =
3353 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
3354 &operations,
3355 parser.spec(),
3356 );
3357 TerminalReporter::print_success(&format!(
3358 "Analyzed {} operations, found {} feature annotations",
3359 operations.len(),
3360 annotated.iter().map(|a| a.features.len()).sum::<usize>()
3361 ));
3362 Some(annotated)
3363 } else {
3364 None
3365 };
3366
3367 std::fs::create_dir_all(&self.output)?;
3369
3370 struct TargetResult {
3372 url: String,
3373 passed: usize,
3374 failed: usize,
3375 elapsed: std::time::Duration,
3376 report_json: serde_json::Value,
3377 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
3378 }
3379
3380 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
3381 let total_start = std::time::Instant::now();
3382
3383 for (idx, target) in targets.iter().enumerate() {
3384 tracing::info!(
3385 "Running conformance tests against target {}/{}: {}",
3386 idx + 1,
3387 num_targets,
3388 target.url
3389 );
3390 TerminalReporter::print_progress(&format!(
3391 "\n--- Target {}/{}: {} ---",
3392 idx + 1,
3393 num_targets,
3394 target.url
3395 ));
3396
3397 let mut merged_headers = base_custom_headers.clone();
3399 if let Some(ref target_headers) = target.headers {
3400 for (name, value) in target_headers {
3401 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
3403 existing.1 = value.clone();
3404 } else {
3405 merged_headers.push((name.clone(), value.clone()));
3406 }
3407 }
3408 }
3409 if let Some(ref auth) = target.auth {
3411 if let Some(existing) =
3412 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
3413 {
3414 existing.1 = auth.clone();
3415 } else {
3416 merged_headers.push(("Authorization".to_string(), auth.clone()));
3417 }
3418 }
3419
3420 let target_dir = self.output.join(format!("target_{}", idx));
3426 std::fs::create_dir_all(&target_dir)?;
3427
3428 let config = ConformanceConfig {
3429 target_url: target.url.clone(),
3430 api_key: self.conformance_api_key.clone(),
3431 basic_auth: self.conformance_basic_auth.clone(),
3432 skip_tls_verify: self.skip_tls_verify,
3433 categories: categories.clone(),
3434 base_path: self.base_path.clone(),
3435 custom_headers: merged_headers,
3436 output_dir: Some(target_dir.clone()),
3437 all_operations: self.conformance_all_operations,
3438 custom_checks_file: self.conformance_custom.clone(),
3439 request_delay_ms: self.conformance_delay_ms,
3440 custom_filter: self.conformance_custom_filter.clone(),
3441 export_requests: self.export_requests,
3442 validate_requests: self.validate_requests,
3443 };
3444
3445 let target_start = std::time::Instant::now();
3446 let report = if self.use_k6 {
3447 if !K6Executor::is_k6_installed() {
3448 TerminalReporter::print_error("k6 is not installed");
3449 TerminalReporter::print_warning(
3450 "Install k6 from: https://k6.io/docs/get-started/installation/",
3451 );
3452 return Err(BenchError::K6NotFound);
3453 }
3454
3455 let script = if let Some(ref annotated) = annotated_ops {
3456 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
3457 config.clone(),
3458 annotated.clone(),
3459 );
3460 let (script, _check_count) = gen.generate()?;
3461 script
3462 } else {
3463 let generator = ConformanceGenerator::new(config.clone());
3464 generator.generate()?
3465 };
3466
3467 let script_path = target_dir.join("k6-conformance.js");
3468 std::fs::write(&script_path, &script).map_err(|e| {
3469 BenchError::Other(format!("Failed to write conformance script: {}", e))
3470 })?;
3471 TerminalReporter::print_success(&format!(
3472 "Conformance script generated: {}",
3473 script_path.display()
3474 ));
3475
3476 TerminalReporter::print_progress(&format!(
3477 "Running conformance tests via k6 against {}...",
3478 target.url
3479 ));
3480 let k6 = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3481 let api_port = 6565u16.saturating_add(idx as u16);
3483 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
3484 .await?;
3485
3486 let report_path = target_dir.join("conformance-report.json");
3487 if report_path.exists() {
3488 ConformanceReport::from_file(&report_path)?
3489 } else {
3490 TerminalReporter::print_warning(&format!(
3491 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
3492 target.url
3493 ));
3494 continue;
3495 }
3496 } else {
3497 let mut executor =
3498 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
3499
3500 let custom_only = annotated_ops.is_none() && self.conformance_custom.is_some();
3503 executor = if let Some(ref annotated) = annotated_ops {
3504 executor.with_spec_driven_checks(annotated)
3505 } else if custom_only {
3506 executor
3507 } else {
3508 executor.with_reference_checks()
3509 };
3510 executor = executor.with_custom_checks()?;
3511
3512 TerminalReporter::print_success(&format!(
3513 "Executing {} conformance checks against {}...",
3514 executor.check_count(),
3515 target.url
3516 ));
3517
3518 executor.execute().await?
3519 };
3520 let target_elapsed = target_start.elapsed();
3521
3522 let report_json = report.to_json();
3523
3524 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
3526 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
3527 let total_checks = passed + failed;
3528 let rate = if total_checks == 0 {
3529 0.0
3530 } else {
3531 (passed as f64 / total_checks as f64) * 100.0
3532 };
3533
3534 TerminalReporter::print_success(&format!(
3535 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
3536 target.url,
3537 passed,
3538 total_checks,
3539 rate,
3540 target_elapsed.as_secs_f64()
3541 ));
3542
3543 let target_report_path = target_dir.join("conformance-report.json");
3545 let report_str = serde_json::to_string_pretty(&report_json)
3546 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
3547 std::fs::write(&target_report_path, &report_str)
3548 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
3549
3550 let failure_details = report.failure_details();
3552 if !failure_details.is_empty() {
3553 let details_path = target_dir.join("conformance-failure-details.json");
3554 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
3555 let _ = std::fs::write(&details_path, json);
3556 }
3557 }
3558
3559 if self.validate_requests && self.export_requests && !self.spec.is_empty() {
3566 let n = crate::conformance::request_validator::validate_emitted_requests_with_base_path(
3567 &self.spec,
3568 &target_dir,
3569 self.base_path.as_deref(),
3570 )
3571 .await?;
3572 if n > 0 {
3573 TerminalReporter::print_warning(&format!(
3574 "Target {}: {} emitted request(s) violated the spec — see {}/conformance-request-violations.json",
3575 target.url,
3576 n,
3577 target_dir.display()
3578 ));
3579 }
3580 }
3581
3582 let owasp_coverage = report.owasp_coverage_data();
3584
3585 target_results.push(TargetResult {
3586 url: target.url.clone(),
3587 passed,
3588 failed,
3589 elapsed: target_elapsed,
3590 report_json,
3591 owasp_coverage,
3592 });
3593 }
3594
3595 let total_elapsed = total_start.elapsed();
3596
3597 println!("\n{}", "=".repeat(80));
3599 println!(" Multi-Target Conformance Summary");
3600 println!("{}", "=".repeat(80));
3601 println!(
3602 " {:<40} {:>8} {:>8} {:>8} {:>8}",
3603 "Target URL", "Passed", "Failed", "Rate", "Time"
3604 );
3605 println!(" {}", "-".repeat(76));
3606
3607 let mut total_passed = 0usize;
3608 let mut total_failed = 0usize;
3609
3610 for result in &target_results {
3611 let total_checks = result.passed + result.failed;
3612 let rate = if total_checks == 0 {
3613 0.0
3614 } else {
3615 (result.passed as f64 / total_checks as f64) * 100.0
3616 };
3617
3618 let display_url = if result.url.len() > 38 {
3620 format!("{}...", &result.url[..35])
3621 } else {
3622 result.url.clone()
3623 };
3624
3625 println!(
3626 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3627 display_url,
3628 result.passed,
3629 result.failed,
3630 rate,
3631 result.elapsed.as_secs_f64()
3632 );
3633
3634 total_passed += result.passed;
3635 total_failed += result.failed;
3636 }
3637
3638 let grand_total = total_passed + total_failed;
3639 let overall_rate = if grand_total == 0 {
3640 0.0
3641 } else {
3642 (total_passed as f64 / grand_total as f64) * 100.0
3643 };
3644
3645 println!(" {}", "-".repeat(76));
3646 println!(
3647 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
3648 format!("TOTAL ({} targets)", num_targets),
3649 total_passed,
3650 total_failed,
3651 overall_rate,
3652 total_elapsed.as_secs_f64()
3653 );
3654 println!("{}", "=".repeat(80));
3655
3656 for result in &target_results {
3658 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
3659 for entry in &result.owasp_coverage {
3660 let status = if !entry.tested {
3661 "-"
3662 } else if entry.all_passed {
3663 "pass"
3664 } else {
3665 "FAIL"
3666 };
3667 let via = if entry.via_categories.is_empty() {
3668 String::new()
3669 } else {
3670 format!(" (via {})", entry.via_categories.join(", "))
3671 };
3672 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
3673 }
3674 }
3675
3676 let per_target_summaries: Vec<serde_json::Value> = target_results
3678 .iter()
3679 .enumerate()
3680 .map(|(idx, r)| {
3681 let total_checks = r.passed + r.failed;
3682 let rate = if total_checks == 0 {
3683 0.0
3684 } else {
3685 (r.passed as f64 / total_checks as f64) * 100.0
3686 };
3687 let owasp_json: Vec<serde_json::Value> = r
3688 .owasp_coverage
3689 .iter()
3690 .map(|e| {
3691 serde_json::json!({
3692 "id": e.id,
3693 "name": e.name,
3694 "tested": e.tested,
3695 "all_passed": e.all_passed,
3696 "via_categories": e.via_categories,
3697 })
3698 })
3699 .collect();
3700 serde_json::json!({
3701 "target_url": r.url,
3702 "target_index": idx,
3703 "checks_passed": r.passed,
3704 "checks_failed": r.failed,
3705 "total_checks": total_checks,
3706 "pass_rate": rate,
3707 "elapsed_seconds": r.elapsed.as_secs_f64(),
3708 "report": r.report_json,
3709 "owasp_coverage": owasp_json,
3710 })
3711 })
3712 .collect();
3713
3714 let combined_summary = serde_json::json!({
3715 "total_targets": num_targets,
3716 "total_checks_passed": total_passed,
3717 "total_checks_failed": total_failed,
3718 "overall_pass_rate": overall_rate,
3719 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
3720 "targets": per_target_summaries,
3721 });
3722
3723 let summary_path = self.output.join("multi-target-conformance-summary.json");
3724 let summary_str = serde_json::to_string_pretty(&combined_summary)
3725 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
3726 std::fs::write(&summary_path, &summary_str)
3727 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
3728 TerminalReporter::print_success(&format!(
3729 "Combined summary saved to: {}",
3730 summary_path.display()
3731 ));
3732
3733 Ok(())
3734 }
3735
3736 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
3738 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
3739
3740 let custom_headers = self.parse_headers()?;
3742
3743 let mut config = OwaspApiConfig::new()
3745 .with_auth_header(&self.owasp_auth_header)
3746 .with_verbose(self.verbose)
3747 .with_insecure(self.skip_tls_verify)
3748 .with_concurrency(self.vus as usize)
3749 .with_iterations(self.owasp_iterations as usize)
3750 .with_base_path(self.base_path.clone())
3751 .with_custom_headers(custom_headers);
3752
3753 if let Some(ref token) = self.owasp_auth_token {
3755 config = config.with_valid_auth_token(token);
3756 }
3757
3758 if let Some(ref cats_str) = self.owasp_categories {
3760 let categories: Vec<OwaspCategory> = cats_str
3761 .split(',')
3762 .filter_map(|s| {
3763 let trimmed = s.trim();
3764 match trimmed.parse::<OwaspCategory>() {
3765 Ok(cat) => Some(cat),
3766 Err(e) => {
3767 TerminalReporter::print_warning(&e);
3768 None
3769 }
3770 }
3771 })
3772 .collect();
3773
3774 if !categories.is_empty() {
3775 config = config.with_categories(categories);
3776 }
3777 }
3778
3779 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
3781 config.admin_paths_file = Some(admin_paths_file.clone());
3782 if let Err(e) = config.load_admin_paths() {
3783 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
3784 }
3785 }
3786
3787 if let Some(ref id_fields_str) = self.owasp_id_fields {
3789 let id_fields: Vec<String> = id_fields_str
3790 .split(',')
3791 .map(|s| s.trim().to_string())
3792 .filter(|s| !s.is_empty())
3793 .collect();
3794 if !id_fields.is_empty() {
3795 config = config.with_id_fields(id_fields);
3796 }
3797 }
3798
3799 if let Some(ref report_path) = self.owasp_report {
3801 config = config.with_report_path(report_path);
3802 }
3803 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
3804 config = config.with_report_format(format);
3805 }
3806
3807 let categories = config.categories_to_test();
3809 TerminalReporter::print_success(&format!(
3810 "Testing {} OWASP categories: {}",
3811 categories.len(),
3812 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
3813 ));
3814
3815 if config.valid_auth_token.is_some() {
3816 TerminalReporter::print_progress("Using provided auth token for baseline requests");
3817 }
3818
3819 TerminalReporter::print_progress("Generating OWASP security test script...");
3821 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
3822
3823 let script = generator.generate()?;
3825 TerminalReporter::print_success("OWASP security test script generated");
3826
3827 let script_path = if let Some(output) = &self.script_output {
3829 output.clone()
3830 } else {
3831 self.output.join("k6-owasp-security-test.js")
3832 };
3833
3834 if let Some(parent) = script_path.parent() {
3835 std::fs::create_dir_all(parent)?;
3836 }
3837 std::fs::write(&script_path, &script)?;
3838 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
3839
3840 if self.generate_only {
3842 println!("\nOWASP security test script generated. Run it with:");
3843 println!(" k6 run {}", script_path.display());
3844 return Ok(());
3845 }
3846
3847 TerminalReporter::print_progress("Executing OWASP security tests...");
3849 let executor = K6Executor::new()?.with_local_ips(self.source_ips.join(","));
3850 std::fs::create_dir_all(&self.output)?;
3851
3852 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
3853
3854 let duration_secs = Self::parse_duration(&self.duration)?;
3855 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
3856
3857 println!("\nOWASP security test results saved to: {}", self.output.display());
3858
3859 Ok(())
3860 }
3861}
3862
3863#[cfg(test)]
3864mod tests {
3865 use super::*;
3866 use tempfile::tempdir;
3867
3868 #[test]
3869 fn test_parse_duration() {
3870 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
3871 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
3872 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
3873 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
3874 }
3875
3876 #[test]
3880 fn parse_ip_list_ipv4_range_inclusive() {
3881 let v = parse_ip_list(&["10.0.0.5-10.0.0.27".into()], "source-ip");
3882 assert_eq!(v.len(), 23);
3883 assert_eq!(v.first().unwrap().to_string(), "10.0.0.5");
3884 assert_eq!(v.last().unwrap().to_string(), "10.0.0.27");
3885 }
3886
3887 #[test]
3890 fn parse_ip_list_range_rejects_backwards() {
3891 let v = parse_ip_list(&["10.0.0.10-10.0.0.5".into()], "source-ip");
3892 assert!(v.is_empty(), "backwards range should produce no IPs; got {v:?}");
3893 }
3894
3895 #[test]
3899 fn parse_ip_list_rejects_ipv6_range_syntax() {
3900 let v = parse_ip_list(&["2001:db8::1-2001:db8::5".into()], "geo-source-ip");
3901 assert!(v.is_empty(), "IPv6 range should be rejected; got {v:?}");
3902 }
3903
3904 #[test]
3906 fn parse_ip_list_range_capped_at_256() {
3907 let v = parse_ip_list(&["10.0.0.0-10.0.5.0".into()], "source-ip");
3908 assert_eq!(v.len(), 256);
3909 assert_eq!(v.first().unwrap().to_string(), "10.0.0.0");
3910 }
3911
3912 #[test]
3915 fn parse_ip_list_plain_and_comma() {
3916 let v = parse_ip_list(&["10.0.0.5".into(), "10.0.0.6,10.0.0.7".into()], "source-ip");
3917 assert_eq!(v.len(), 3);
3918 assert_eq!(v[0].to_string(), "10.0.0.5");
3919 assert_eq!(v[2].to_string(), "10.0.0.7");
3920 }
3921
3922 #[test]
3925 fn parse_ip_list_ipv4_cidr_29_expands_to_8() {
3926 let v = parse_ip_list(&["10.0.0.0/29".into()], "source-ip");
3927 assert_eq!(v.len(), 8);
3928 assert_eq!(v[0].to_string(), "10.0.0.0");
3929 assert_eq!(v[7].to_string(), "10.0.0.7");
3930 }
3931
3932 #[test]
3935 fn parse_ip_list_ipv4_cidr_8_capped_at_256() {
3936 let v = parse_ip_list(&["10.0.0.0/8".into()], "source-ip");
3937 assert_eq!(v.len(), 256);
3938 assert_eq!(v[0].to_string(), "10.0.0.0");
3939 assert_eq!(v[255].to_string(), "10.0.0.255");
3940 }
3941
3942 #[test]
3944 fn parse_ip_list_ipv6_cidr_126_expands_to_4() {
3945 let v = parse_ip_list(&["2001:db8::/126".into()], "geo-source-ip");
3946 assert_eq!(v.len(), 4);
3947 assert!(v[0].is_ipv6());
3948 assert_eq!(v[0].to_string(), "2001:db8::");
3949 assert_eq!(v[3].to_string(), "2001:db8::3");
3950 }
3951
3952 #[test]
3954 fn parse_ip_list_mixed_v4_v6_cidr() {
3955 let v = parse_ip_list(&["10.0.0.0/30,2001:db8::1,203.0.113.42".into()], "geo-source-ip");
3956 assert_eq!(v.len(), 6); assert!(v.iter().any(|ip| ip.to_string() == "2001:db8::1"));
3958 assert!(v.iter().any(|ip| ip.to_string() == "203.0.113.42"));
3959 }
3960
3961 #[test]
3964 fn parse_ip_list_skips_malformed() {
3965 let v = parse_ip_list(
3966 &[
3967 "10.0.0.5".into(),
3968 "not-an-ip".into(),
3969 "10.0.0.6".into(),
3970 "/24".into(),
3971 "1.2.3.4/200".into(),
3972 ],
3973 "source-ip",
3974 );
3975 assert_eq!(v.len(), 2);
3976 assert_eq!(v[0].to_string(), "10.0.0.5");
3977 assert_eq!(v[1].to_string(), "10.0.0.6");
3978 }
3979
3980 #[test]
3981 fn test_parse_duration_invalid() {
3982 assert!(BenchCommand::parse_duration("invalid").is_err());
3983 assert!(BenchCommand::parse_duration("30x").is_err());
3984 }
3985
3986 #[test]
3987 fn test_parse_headers() {
3988 let cmd = BenchCommand {
3989 spec: vec![PathBuf::from("test.yaml")],
3990 spec_dir: None,
3991 merge_conflicts: "error".to_string(),
3992 spec_mode: "merge".to_string(),
3993 dependency_config: None,
3994 target: "http://localhost".to_string(),
3995 base_path: None,
3996 duration: "1m".to_string(),
3997 vus: 10,
3998 scenario: "ramp-up".to_string(),
3999 operations: None,
4000 exclude_operations: None,
4001 auth: None,
4002 headers: vec![
4003 "X-API-Key:test123".to_string(),
4004 "X-Client-ID:client456".to_string(),
4005 ],
4006 output: PathBuf::from("output"),
4007 generate_only: false,
4008 script_output: None,
4009 threshold_percentile: "p(95)".to_string(),
4010 threshold_ms: 500,
4011 max_error_rate: 0.05,
4012 verbose: false,
4013 skip_tls_verify: false,
4014 chunked_request_bodies: false,
4015 target_rps: None,
4016 no_keep_alive: false,
4017 targets_file: None,
4018 max_concurrency: None,
4019 results_format: "both".to_string(),
4020 params_file: None,
4021 crud_flow: false,
4022 flow_config: None,
4023 extract_fields: None,
4024 parallel_create: None,
4025 data_file: None,
4026 data_distribution: "unique-per-vu".to_string(),
4027 data_mappings: None,
4028 per_uri_control: false,
4029 error_rate: None,
4030 error_types: None,
4031 security_test: false,
4032 security_payloads: None,
4033 security_categories: None,
4034 security_target_fields: None,
4035 wafbench_dir: None,
4036 wafbench_cycle_all: false,
4037 owasp_api_top10: false,
4038 owasp_categories: None,
4039 owasp_auth_header: "Authorization".to_string(),
4040 owasp_auth_token: None,
4041 owasp_admin_paths: None,
4042 owasp_id_fields: None,
4043 owasp_report: None,
4044 owasp_report_format: "json".to_string(),
4045 owasp_iterations: 1,
4046 conformance: false,
4047 conformance_api_key: None,
4048 conformance_basic_auth: None,
4049 conformance_report: PathBuf::from("conformance-report.json"),
4050 conformance_categories: None,
4051 conformance_report_format: "json".to_string(),
4052 conformance_headers: vec![],
4053 conformance_all_operations: false,
4054 conformance_custom: None,
4055 conformance_delay_ms: 0,
4056 use_k6: false,
4057 conformance_custom_filter: None,
4058 export_requests: false,
4059 validate_requests: false,
4060 conformance_self_test: false,
4061 conformance_self_test_capture: false,
4062 conformance_self_test_iterations: 1,
4063 conformance_self_test_duration: None,
4064 validate_response_schemas: false,
4065 source_ips: Vec::new(),
4066 geo_source_ips: Vec::new(),
4067 geo_source_headers: Vec::new(),
4068 report_missed_cap: None,
4069 };
4070
4071 let headers = cmd.parse_headers().unwrap();
4072 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
4073 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
4074 }
4075
4076 #[test]
4077 fn test_parse_header_string_preserves_comma_in_value() {
4078 let inputs = vec![
4081 "Cookie:session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string(),
4082 "X-Trace:1".to_string(),
4083 ];
4084 let headers = parse_header_string(&inputs).unwrap();
4085 assert_eq!(
4086 headers.get("Cookie"),
4087 Some(&"session=abc; expires=Thu, 01 Jan 2099 00:00:00 GMT".to_string())
4088 );
4089 assert_eq!(headers.get("X-Trace"), Some(&"1".to_string()));
4090 }
4091
4092 #[test]
4093 fn test_get_spec_display_name() {
4094 let cmd = BenchCommand {
4095 spec: vec![PathBuf::from("test.yaml")],
4096 spec_dir: None,
4097 merge_conflicts: "error".to_string(),
4098 spec_mode: "merge".to_string(),
4099 dependency_config: None,
4100 target: "http://localhost".to_string(),
4101 base_path: None,
4102 duration: "1m".to_string(),
4103 vus: 10,
4104 scenario: "ramp-up".to_string(),
4105 operations: None,
4106 exclude_operations: None,
4107 auth: None,
4108 headers: Vec::new(),
4109 output: PathBuf::from("output"),
4110 generate_only: false,
4111 script_output: None,
4112 threshold_percentile: "p(95)".to_string(),
4113 threshold_ms: 500,
4114 max_error_rate: 0.05,
4115 verbose: false,
4116 skip_tls_verify: false,
4117 chunked_request_bodies: false,
4118 target_rps: None,
4119 no_keep_alive: false,
4120 targets_file: None,
4121 max_concurrency: None,
4122 results_format: "both".to_string(),
4123 params_file: None,
4124 crud_flow: false,
4125 flow_config: None,
4126 extract_fields: None,
4127 parallel_create: None,
4128 data_file: None,
4129 data_distribution: "unique-per-vu".to_string(),
4130 data_mappings: None,
4131 per_uri_control: false,
4132 error_rate: None,
4133 error_types: None,
4134 security_test: false,
4135 security_payloads: None,
4136 security_categories: None,
4137 security_target_fields: None,
4138 wafbench_dir: None,
4139 wafbench_cycle_all: false,
4140 owasp_api_top10: false,
4141 owasp_categories: None,
4142 owasp_auth_header: "Authorization".to_string(),
4143 owasp_auth_token: None,
4144 owasp_admin_paths: None,
4145 owasp_id_fields: None,
4146 owasp_report: None,
4147 owasp_report_format: "json".to_string(),
4148 owasp_iterations: 1,
4149 conformance: false,
4150 conformance_api_key: None,
4151 conformance_basic_auth: None,
4152 conformance_report: PathBuf::from("conformance-report.json"),
4153 conformance_categories: None,
4154 conformance_report_format: "json".to_string(),
4155 conformance_headers: vec![],
4156 conformance_all_operations: false,
4157 conformance_custom: None,
4158 conformance_delay_ms: 0,
4159 use_k6: false,
4160 conformance_custom_filter: None,
4161 export_requests: false,
4162 validate_requests: false,
4163 conformance_self_test: false,
4164 conformance_self_test_capture: false,
4165 conformance_self_test_iterations: 1,
4166 conformance_self_test_duration: None,
4167 validate_response_schemas: false,
4168 source_ips: Vec::new(),
4169 geo_source_ips: Vec::new(),
4170 geo_source_headers: Vec::new(),
4171 report_missed_cap: None,
4172 };
4173
4174 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
4175
4176 let cmd_multi = BenchCommand {
4178 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
4179 spec_dir: None,
4180 merge_conflicts: "error".to_string(),
4181 spec_mode: "merge".to_string(),
4182 dependency_config: None,
4183 target: "http://localhost".to_string(),
4184 base_path: None,
4185 duration: "1m".to_string(),
4186 vus: 10,
4187 scenario: "ramp-up".to_string(),
4188 operations: None,
4189 exclude_operations: None,
4190 auth: None,
4191 headers: Vec::new(),
4192 output: PathBuf::from("output"),
4193 generate_only: false,
4194 script_output: None,
4195 threshold_percentile: "p(95)".to_string(),
4196 threshold_ms: 500,
4197 max_error_rate: 0.05,
4198 verbose: false,
4199 skip_tls_verify: false,
4200 chunked_request_bodies: false,
4201 target_rps: None,
4202 no_keep_alive: false,
4203 targets_file: None,
4204 max_concurrency: None,
4205 results_format: "both".to_string(),
4206 params_file: None,
4207 crud_flow: false,
4208 flow_config: None,
4209 extract_fields: None,
4210 parallel_create: None,
4211 data_file: None,
4212 data_distribution: "unique-per-vu".to_string(),
4213 data_mappings: None,
4214 per_uri_control: false,
4215 error_rate: None,
4216 error_types: None,
4217 security_test: false,
4218 security_payloads: None,
4219 security_categories: None,
4220 security_target_fields: None,
4221 wafbench_dir: None,
4222 wafbench_cycle_all: false,
4223 owasp_api_top10: false,
4224 owasp_categories: None,
4225 owasp_auth_header: "Authorization".to_string(),
4226 owasp_auth_token: None,
4227 owasp_admin_paths: None,
4228 owasp_id_fields: None,
4229 owasp_report: None,
4230 owasp_report_format: "json".to_string(),
4231 owasp_iterations: 1,
4232 conformance: false,
4233 conformance_api_key: None,
4234 conformance_basic_auth: None,
4235 conformance_report: PathBuf::from("conformance-report.json"),
4236 conformance_categories: None,
4237 conformance_report_format: "json".to_string(),
4238 conformance_headers: vec![],
4239 conformance_all_operations: false,
4240 conformance_custom: None,
4241 conformance_delay_ms: 0,
4242 use_k6: false,
4243 conformance_custom_filter: None,
4244 export_requests: false,
4245 validate_requests: false,
4246 conformance_self_test: false,
4247 conformance_self_test_capture: false,
4248 conformance_self_test_iterations: 1,
4249 conformance_self_test_duration: None,
4250 validate_response_schemas: false,
4251 source_ips: Vec::new(),
4252 geo_source_ips: Vec::new(),
4253 geo_source_headers: Vec::new(),
4254 report_missed_cap: None,
4255 };
4256
4257 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
4258 }
4259
4260 #[test]
4261 fn test_parse_extracted_values_from_output_dir() {
4262 let dir = tempdir().unwrap();
4263 let path = dir.path().join("extracted_values.json");
4264 std::fs::write(
4265 &path,
4266 r#"{
4267 "pool_id": "abc123",
4268 "count": 0,
4269 "enabled": false,
4270 "metadata": { "owner": "team-a" }
4271}"#,
4272 )
4273 .unwrap();
4274
4275 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4276 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
4277 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
4278 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
4279 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
4280 }
4281
4282 #[test]
4283 fn test_parse_extracted_values_missing_file() {
4284 let dir = tempdir().unwrap();
4285 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
4286 assert!(extracted.values.is_empty());
4287 }
4288}