1use crate::crud_flow::{CrudFlowConfig, CrudFlowDetector};
4use crate::data_driven::{DataDistribution, DataDrivenConfig, DataDrivenGenerator, DataMapping};
5use crate::dynamic_params::{DynamicParamProcessor, DynamicPlaceholder};
6use crate::error::{BenchError, Result};
7use crate::executor::K6Executor;
8use crate::invalid_data::{InvalidDataConfig, InvalidDataGenerator};
9use crate::k6_gen::{K6Config, K6ScriptGenerator};
10use crate::mock_integration::{
11 MockIntegrationConfig, MockIntegrationGenerator, MockServerDetector,
12};
13use crate::owasp_api::{OwaspApiConfig, OwaspApiGenerator, OwaspCategory, ReportFormat};
14use crate::parallel_executor::{AggregatedResults, ParallelExecutor};
15use crate::parallel_requests::{ParallelConfig, ParallelRequestGenerator};
16use crate::param_overrides::ParameterOverrides;
17use crate::reporter::TerminalReporter;
18use crate::request_gen::RequestGenerator;
19use crate::scenarios::LoadScenario;
20use crate::security_payloads::{
21 SecurityCategory, SecurityPayload, SecurityPayloads, SecurityTestConfig, SecurityTestGenerator,
22};
23use crate::spec_dependencies::{
24 topological_sort, DependencyDetector, ExtractedValues, SpecDependencyConfig,
25};
26use crate::spec_parser::SpecParser;
27use crate::target_parser::parse_targets_file;
28use crate::wafbench::WafBenchLoader;
29use mockforge_openapi::multi_spec::{
30 load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_openapi::spec::OpenApiSpec;
33use std::collections::{HashMap, HashSet};
34use std::path::{Path, PathBuf};
35use std::str::FromStr;
36
37pub fn parse_header_string(input: &str) -> Result<HashMap<String, String>> {
45 let mut headers = HashMap::new();
46
47 for pair in input.split(',') {
48 let parts: Vec<&str> = pair.splitn(2, ':').collect();
49 if parts.len() != 2 {
50 return Err(BenchError::Other(format!(
51 "Invalid header format: '{}'. Expected 'Key:Value'",
52 pair
53 )));
54 }
55 headers.insert(parts[0].trim().to_string(), parts[1].trim().to_string());
56 }
57
58 Ok(headers)
59}
60
61pub struct BenchCommand {
63 pub spec: Vec<PathBuf>,
65 pub spec_dir: Option<PathBuf>,
67 pub merge_conflicts: String,
69 pub spec_mode: String,
71 pub dependency_config: Option<PathBuf>,
73 pub target: String,
74 pub base_path: Option<String>,
77 pub duration: String,
78 pub vus: u32,
79 pub target_rps: Option<u32>,
85 pub no_keep_alive: bool,
90 pub scenario: String,
91 pub operations: Option<String>,
92 pub exclude_operations: Option<String>,
96 pub auth: Option<String>,
97 pub headers: Option<String>,
98 pub output: PathBuf,
99 pub generate_only: bool,
100 pub script_output: Option<PathBuf>,
101 pub threshold_percentile: String,
102 pub threshold_ms: u64,
103 pub max_error_rate: f64,
104 pub verbose: bool,
105 pub skip_tls_verify: bool,
106 pub chunked_request_bodies: bool,
111 pub targets_file: Option<PathBuf>,
113 pub max_concurrency: Option<u32>,
115 pub results_format: String,
117 pub params_file: Option<PathBuf>,
122
123 pub crud_flow: bool,
126 pub flow_config: Option<PathBuf>,
128 pub extract_fields: Option<String>,
130
131 pub parallel_create: Option<u32>,
134
135 pub data_file: Option<PathBuf>,
138 pub data_distribution: String,
140 pub data_mappings: Option<String>,
142 pub per_uri_control: bool,
144
145 pub error_rate: Option<f64>,
148 pub error_types: Option<String>,
150
151 pub security_test: bool,
154 pub security_payloads: Option<PathBuf>,
156 pub security_categories: Option<String>,
158 pub security_target_fields: Option<String>,
160
161 pub wafbench_dir: Option<String>,
164 pub wafbench_cycle_all: bool,
166
167 pub conformance: bool,
170 pub conformance_api_key: Option<String>,
172 pub conformance_basic_auth: Option<String>,
174 pub conformance_report: PathBuf,
176 pub conformance_categories: Option<String>,
178 pub conformance_report_format: String,
180 pub conformance_headers: Vec<String>,
183 pub conformance_all_operations: bool,
186 pub conformance_custom: Option<PathBuf>,
188 pub conformance_delay_ms: u64,
191 pub use_k6: bool,
193 pub conformance_custom_filter: Option<String>,
197 pub export_requests: bool,
200 pub validate_requests: bool,
203
204 pub owasp_api_top10: bool,
207 pub owasp_categories: Option<String>,
209 pub owasp_auth_header: String,
211 pub owasp_auth_token: Option<String>,
213 pub owasp_admin_paths: Option<PathBuf>,
215 pub owasp_id_fields: Option<String>,
217 pub owasp_report: Option<PathBuf>,
219 pub owasp_report_format: String,
221 pub owasp_iterations: u32,
223}
224
225impl BenchCommand {
226 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
228 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
229
230 if !self.spec.is_empty() {
232 let specs = load_specs_from_files(self.spec.clone())
233 .await
234 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
235 all_specs.extend(specs);
236 }
237
238 if let Some(spec_dir) = &self.spec_dir {
240 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
241 BenchError::Other(format!("Failed to load specs from directory: {}", e))
242 })?;
243 all_specs.extend(dir_specs);
244 }
245
246 if all_specs.is_empty() {
247 return Err(BenchError::Other(
248 "No spec files provided. Use --spec or --spec-dir.".to_string(),
249 ));
250 }
251
252 if all_specs.len() == 1 {
254 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
256 }
257
258 let conflict_strategy = match self.merge_conflicts.as_str() {
260 "first" => ConflictStrategy::First,
261 "last" => ConflictStrategy::Last,
262 _ => ConflictStrategy::Error,
263 };
264
265 merge_specs(all_specs, conflict_strategy)
266 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
267 }
268
269 fn get_spec_display_name(&self) -> String {
271 if self.spec.len() == 1 {
272 self.spec[0].to_string_lossy().to_string()
273 } else if !self.spec.is_empty() {
274 format!("{} spec files", self.spec.len())
275 } else if let Some(dir) = &self.spec_dir {
276 format!("specs from {}", dir.display())
277 } else {
278 "no specs".to_string()
279 }
280 }
281
282 pub async fn execute(&self) -> Result<()> {
284 if let Some(targets_file) = &self.targets_file {
286 if self.conformance {
287 return self.execute_multi_target_conformance(targets_file).await;
288 }
289 return self.execute_multi_target(targets_file).await;
290 }
291
292 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
294 return self.execute_sequential_specs().await;
295 }
296
297 TerminalReporter::print_header(
300 &self.get_spec_display_name(),
301 &self.target,
302 0, &self.scenario,
304 Self::parse_duration(&self.duration)?,
305 );
306
307 if !K6Executor::is_k6_installed() {
309 TerminalReporter::print_error("k6 is not installed");
310 TerminalReporter::print_warning(
311 "Install k6 from: https://k6.io/docs/get-started/installation/",
312 );
313 return Err(BenchError::K6NotFound);
314 }
315
316 if self.conformance {
318 return self.execute_conformance_test().await;
319 }
320
321 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
323 let merged_spec = self.load_and_merge_specs().await?;
324 let parser = SpecParser::from_spec(merged_spec);
325 if self.spec.len() > 1 || self.spec_dir.is_some() {
326 TerminalReporter::print_success(&format!(
327 "Loaded and merged {} specification(s)",
328 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
329 ));
330 } else {
331 TerminalReporter::print_success("Specification loaded");
332 }
333
334 let mock_config = self.build_mock_config().await;
336 if mock_config.is_mock_server {
337 TerminalReporter::print_progress("Mock server integration enabled");
338 }
339
340 if self.crud_flow {
342 return self.execute_crud_flow(&parser).await;
343 }
344
345 if self.owasp_api_top10 {
347 return self.execute_owasp_test(&parser).await;
348 }
349
350 TerminalReporter::print_progress("Extracting API operations...");
352 let mut operations = if let Some(filter) = &self.operations {
353 parser.filter_operations(filter)?
354 } else {
355 parser.get_operations()
356 };
357
358 if let Some(exclude) = &self.exclude_operations {
360 let before_count = operations.len();
361 operations = parser.exclude_operations(operations, exclude)?;
362 let excluded_count = before_count - operations.len();
363 if excluded_count > 0 {
364 TerminalReporter::print_progress(&format!(
365 "Excluded {} operations matching '{}'",
366 excluded_count, exclude
367 ));
368 }
369 }
370
371 if operations.is_empty() {
372 return Err(BenchError::Other("No operations found in spec".to_string()));
373 }
374
375 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
376
377 let param_overrides = if let Some(params_file) = &self.params_file {
379 TerminalReporter::print_progress("Loading parameter overrides...");
380 let overrides = ParameterOverrides::from_file(params_file)?;
381 TerminalReporter::print_success(&format!(
382 "Loaded parameter overrides ({} operation-specific, {} defaults)",
383 overrides.operations.len(),
384 if overrides.defaults.is_empty() { 0 } else { 1 }
385 ));
386 Some(overrides)
387 } else {
388 None
389 };
390
391 TerminalReporter::print_progress("Generating request templates...");
393 let templates: Vec<_> = operations
394 .iter()
395 .map(|op| {
396 let op_overrides = param_overrides.as_ref().map(|po| {
397 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
398 });
399 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
400 })
401 .collect::<Result<Vec<_>>>()?;
402 TerminalReporter::print_success("Request templates generated");
403
404 let custom_headers = self.parse_headers()?;
406
407 let base_path = self.resolve_base_path(&parser);
409 if let Some(ref bp) = base_path {
410 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
411 }
412
413 TerminalReporter::print_progress("Generating k6 load test script...");
415 let scenario =
416 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
417
418 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
419
420 let num_ops = operations.len() as u32;
438 if let Some(rps) = self.target_rps {
439 let probe =
440 crate::preflight::probe_target_latency(&self.target, 3, self.skip_tls_verify).await;
441
442 let (required_vus, basis) = match probe {
443 Some(p) => (
444 p.required_vus(rps, num_ops),
445 format!("avg {:.1}ms (measured)", p.avg_latency.as_secs_f64() * 1000.0),
446 ),
447 None => {
448 let fallback = (rps as u64)
450 .saturating_mul(num_ops.max(1) as u64)
451 .div_ceil(10)
452 .min(u32::MAX as u64) as u32;
453 (fallback, "~100ms (default — probe failed)".to_string())
454 }
455 };
456
457 if self.vus < required_vus {
458 const VU_RECOMMENDATION_CAP: u32 = 1000;
464 let recommendation = required_vus.max(self.vus + 1);
465 if recommendation > VU_RECOMMENDATION_CAP {
466 TerminalReporter::print_warning(&format!(
467 "Workload is very large: --rps {} × {} ops/iteration × {} \
468 baseline ⇒ ~{} VUs needed end-to-end, far beyond what's \
469 practical to drive. Two ways to fix:\n 1. Reduce \
470 operations per iteration with `--operations 'pattern,…'` \
471 (or `--exclude-operations`) to focus the bench on a \
472 representative subset.\n 2. Drop `--rps` and use \
473 `--vus {}` alone — closed-model load runs as fast as \
474 the VU pool allows, bounded by latency, with no per-\
475 iteration deadline. Expect 1-iteration coverage of ~{} \
476 operations in {}s.",
477 rps,
478 num_ops,
479 basis,
480 recommendation,
481 self.vus.max(5),
482 num_ops,
483 Self::parse_duration(&self.duration).unwrap_or(0),
484 ));
485 } else {
486 TerminalReporter::print_warning(&format!(
487 "--vus {} may be insufficient for --rps {} × {} ops/iteration \
488 (baseline latency {}). k6's constant-arrival-rate counts ITERATIONS \
489 and each runs every operation in the spec — required ≈ rps × ops × \
490 latency_secs VUs. Bump --vus to ~{} if you see \"Insufficient VUs\" \
491 warnings.",
492 self.vus, rps, num_ops, basis, recommendation,
493 ));
494 }
495 } else if probe.is_some() {
496 TerminalReporter::print_progress(&format!(
497 "Pre-flight probe: target latency {}, {} ops/iteration — --vus {} \
498 is sufficient for --rps {}",
499 basis, num_ops, self.vus, rps,
500 ));
501 }
502 }
503
504 let k6_config = K6Config {
505 target_url: self.target.clone(),
506 base_path,
507 scenario,
508 duration_secs: Self::parse_duration(&self.duration)?,
509 max_vus: self.vus,
510 threshold_percentile: self.threshold_percentile.clone(),
511 threshold_ms: self.threshold_ms,
512 max_error_rate: self.max_error_rate,
513 auth_header: self.auth.clone(),
514 custom_headers,
515 skip_tls_verify: self.skip_tls_verify,
516 security_testing_enabled,
517 chunked_request_bodies: self.chunked_request_bodies,
518 target_rps: self.target_rps,
519 no_keep_alive: self.no_keep_alive,
520 };
521
522 let generator = K6ScriptGenerator::new(k6_config, templates);
523 let mut script = generator.generate()?;
524 TerminalReporter::print_success("k6 script generated");
525
526 let has_advanced_features = self.data_file.is_some()
528 || self.error_rate.is_some()
529 || self.security_test
530 || self.parallel_create.is_some()
531 || self.wafbench_dir.is_some();
532
533 if has_advanced_features {
535 script = self.generate_enhanced_script(&script)?;
536 }
537
538 if mock_config.is_mock_server {
540 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
541 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
542 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
543
544 if let Some(import_end) = script.find("export const options") {
546 script.insert_str(
547 import_end,
548 &format!(
549 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
550 helper_code, setup_code, teardown_code
551 ),
552 );
553 }
554 }
555
556 TerminalReporter::print_progress("Validating k6 script...");
558 let validation_errors = K6ScriptGenerator::validate_script(&script);
559 if !validation_errors.is_empty() {
560 TerminalReporter::print_error("Script validation failed");
561 for error in &validation_errors {
562 eprintln!(" {}", error);
563 }
564 return Err(BenchError::Other(format!(
565 "Generated k6 script has {} validation error(s). Please check the output above.",
566 validation_errors.len()
567 )));
568 }
569 TerminalReporter::print_success("Script validation passed");
570
571 let script_path = if let Some(output) = &self.script_output {
573 output.clone()
574 } else {
575 self.output.join("k6-script.js")
576 };
577
578 if let Some(parent) = script_path.parent() {
579 std::fs::create_dir_all(parent)?;
580 }
581 std::fs::write(&script_path, &script)?;
582 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
583
584 if self.generate_only {
586 println!("\nScript generated successfully. Run it with:");
587 println!(" k6 run {}", script_path.display());
588 return Ok(());
589 }
590
591 TerminalReporter::print_progress("Executing load test...");
593 let executor = K6Executor::new()?;
594
595 std::fs::create_dir_all(&self.output)?;
596
597 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
598
599 let duration_secs = Self::parse_duration(&self.duration)?;
601 TerminalReporter::print_summary_full(
602 &results,
603 duration_secs,
604 self.no_keep_alive,
605 Some(num_ops),
606 );
607
608 println!("\nResults saved to: {}", self.output.display());
609
610 Ok(())
611 }
612
613 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
615 TerminalReporter::print_progress("Parsing targets file...");
616 let targets = parse_targets_file(targets_file)?;
617 let num_targets = targets.len();
618 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
619
620 if targets.is_empty() {
621 return Err(BenchError::Other("No targets found in file".to_string()));
622 }
623
624 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
626 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
630 &self.get_spec_display_name(),
631 &format!("{} targets", num_targets),
632 0,
633 &self.scenario,
634 Self::parse_duration(&self.duration)?,
635 );
636
637 let executor = ParallelExecutor::new(
639 BenchCommand {
640 spec: self.spec.clone(),
642 spec_dir: self.spec_dir.clone(),
643 merge_conflicts: self.merge_conflicts.clone(),
644 spec_mode: self.spec_mode.clone(),
645 dependency_config: self.dependency_config.clone(),
646 target: self.target.clone(), base_path: self.base_path.clone(),
648 duration: self.duration.clone(),
649 vus: self.vus,
650 target_rps: self.target_rps,
651 no_keep_alive: self.no_keep_alive,
652 scenario: self.scenario.clone(),
653 operations: self.operations.clone(),
654 exclude_operations: self.exclude_operations.clone(),
655 auth: self.auth.clone(),
656 headers: self.headers.clone(),
657 output: self.output.clone(),
658 generate_only: self.generate_only,
659 script_output: self.script_output.clone(),
660 threshold_percentile: self.threshold_percentile.clone(),
661 threshold_ms: self.threshold_ms,
662 max_error_rate: self.max_error_rate,
663 verbose: self.verbose,
664 skip_tls_verify: self.skip_tls_verify,
665 chunked_request_bodies: self.chunked_request_bodies,
666 targets_file: None,
667 max_concurrency: None,
668 results_format: self.results_format.clone(),
669 params_file: self.params_file.clone(),
670 crud_flow: self.crud_flow,
671 flow_config: self.flow_config.clone(),
672 extract_fields: self.extract_fields.clone(),
673 parallel_create: self.parallel_create,
674 data_file: self.data_file.clone(),
675 data_distribution: self.data_distribution.clone(),
676 data_mappings: self.data_mappings.clone(),
677 per_uri_control: self.per_uri_control,
678 error_rate: self.error_rate,
679 error_types: self.error_types.clone(),
680 security_test: self.security_test,
681 security_payloads: self.security_payloads.clone(),
682 security_categories: self.security_categories.clone(),
683 security_target_fields: self.security_target_fields.clone(),
684 wafbench_dir: self.wafbench_dir.clone(),
685 wafbench_cycle_all: self.wafbench_cycle_all,
686 owasp_api_top10: self.owasp_api_top10,
687 owasp_categories: self.owasp_categories.clone(),
688 owasp_auth_header: self.owasp_auth_header.clone(),
689 owasp_auth_token: self.owasp_auth_token.clone(),
690 owasp_admin_paths: self.owasp_admin_paths.clone(),
691 owasp_id_fields: self.owasp_id_fields.clone(),
692 owasp_report: self.owasp_report.clone(),
693 owasp_report_format: self.owasp_report_format.clone(),
694 owasp_iterations: self.owasp_iterations,
695 conformance: false,
696 conformance_api_key: None,
697 conformance_basic_auth: None,
698 conformance_report: PathBuf::from("conformance-report.json"),
699 conformance_categories: None,
700 conformance_report_format: "json".to_string(),
701 conformance_headers: vec![],
702 conformance_all_operations: false,
703 conformance_custom: None,
704 conformance_delay_ms: 0,
705 use_k6: false,
706 conformance_custom_filter: None,
707 export_requests: false,
708 validate_requests: false,
709 },
710 targets,
711 max_concurrency,
712 );
713
714 let start_time = std::time::Instant::now();
716 let aggregated_results = executor.execute_all().await?;
717 let elapsed = start_time.elapsed();
718
719 self.report_multi_target_results(&aggregated_results, elapsed)?;
721
722 Ok(())
723 }
724
725 fn report_multi_target_results(
727 &self,
728 results: &AggregatedResults,
729 elapsed: std::time::Duration,
730 ) -> Result<()> {
731 TerminalReporter::print_multi_target_summary(results);
733
734 let total_secs = elapsed.as_secs();
736 let hours = total_secs / 3600;
737 let minutes = (total_secs % 3600) / 60;
738 let seconds = total_secs % 60;
739 if hours > 0 {
740 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
741 } else if minutes > 0 {
742 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
743 } else {
744 println!("\n Total Elapsed Time: {}s", seconds);
745 }
746
747 if self.results_format == "aggregated" || self.results_format == "both" {
749 let summary_path = self.output.join("aggregated_summary.json");
750 let summary_json = serde_json::json!({
751 "total_elapsed_seconds": elapsed.as_secs(),
752 "total_targets": results.total_targets,
753 "successful_targets": results.successful_targets,
754 "failed_targets": results.failed_targets,
755 "aggregated_metrics": {
756 "total_requests": results.aggregated_metrics.total_requests,
757 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
758 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
759 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
760 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
761 "error_rate": results.aggregated_metrics.error_rate,
762 "total_rps": results.aggregated_metrics.total_rps,
763 "avg_rps": results.aggregated_metrics.avg_rps,
764 "total_vus_max": results.aggregated_metrics.total_vus_max,
765 },
766 "target_results": results.target_results.iter().map(|r| {
767 serde_json::json!({
768 "target_url": r.target_url,
769 "target_index": r.target_index,
770 "success": r.success,
771 "error": r.error,
772 "total_requests": r.results.total_requests,
773 "failed_requests": r.results.failed_requests,
774 "avg_duration_ms": r.results.avg_duration_ms,
775 "min_duration_ms": r.results.min_duration_ms,
776 "med_duration_ms": r.results.med_duration_ms,
777 "p90_duration_ms": r.results.p90_duration_ms,
778 "p95_duration_ms": r.results.p95_duration_ms,
779 "p99_duration_ms": r.results.p99_duration_ms,
780 "max_duration_ms": r.results.max_duration_ms,
781 "rps": r.results.rps,
782 "vus_max": r.results.vus_max,
783 "output_dir": r.output_dir.to_string_lossy(),
784 })
785 }).collect::<Vec<_>>(),
786 });
787
788 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
789 TerminalReporter::print_success(&format!(
790 "Aggregated summary saved to: {}",
791 summary_path.display()
792 ));
793 }
794
795 let csv_path = self.output.join("all_targets.csv");
797 let mut csv = String::from(
798 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
799 );
800 for r in &results.target_results {
801 csv.push_str(&format!(
802 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
803 r.target_url,
804 r.success,
805 r.results.total_requests,
806 r.results.failed_requests,
807 r.results.rps,
808 r.results.vus_max,
809 r.results.min_duration_ms,
810 r.results.avg_duration_ms,
811 r.results.med_duration_ms,
812 r.results.p90_duration_ms,
813 r.results.p95_duration_ms,
814 r.results.p99_duration_ms,
815 r.results.max_duration_ms,
816 r.error.as_deref().unwrap_or(""),
817 ));
818 }
819 let _ = std::fs::write(&csv_path, &csv);
820
821 println!("\nResults saved to: {}", self.output.display());
822 println!(" - Per-target results: {}", self.output.join("target_*").display());
823 println!(" - All targets CSV: {}", csv_path.display());
824 if self.results_format == "aggregated" || self.results_format == "both" {
825 println!(
826 " - Aggregated summary: {}",
827 self.output.join("aggregated_summary.json").display()
828 );
829 }
830
831 Ok(())
832 }
833
834 pub fn parse_duration(duration: &str) -> Result<u64> {
836 let duration = duration.trim();
837
838 if let Some(secs) = duration.strip_suffix('s') {
839 secs.parse::<u64>()
840 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
841 } else if let Some(mins) = duration.strip_suffix('m') {
842 mins.parse::<u64>()
843 .map(|m| m * 60)
844 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
845 } else if let Some(hours) = duration.strip_suffix('h') {
846 hours
847 .parse::<u64>()
848 .map(|h| h * 3600)
849 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
850 } else {
851 duration
853 .parse::<u64>()
854 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
855 }
856 }
857
858 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
860 match &self.headers {
861 Some(s) => parse_header_string(s),
862 None => Ok(HashMap::new()),
863 }
864 }
865
866 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
867 let extracted_path = output_dir.join("extracted_values.json");
868 if !extracted_path.exists() {
869 return Ok(ExtractedValues::new());
870 }
871
872 let content = std::fs::read_to_string(&extracted_path)
873 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
874 let parsed: serde_json::Value = serde_json::from_str(&content)
875 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
876
877 let mut extracted = ExtractedValues::new();
878 if let Some(values) = parsed.as_object() {
879 for (key, value) in values {
880 extracted.set(key.clone(), value.clone());
881 }
882 }
883
884 Ok(extracted)
885 }
886
887 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
896 if let Some(cli_base_path) = &self.base_path {
898 if cli_base_path.is_empty() {
899 return None;
901 }
902 return Some(cli_base_path.clone());
903 }
904
905 parser.get_base_path()
907 }
908
909 async fn build_mock_config(&self) -> MockIntegrationConfig {
911 if MockServerDetector::looks_like_mock_server(&self.target) {
913 if let Ok(info) = MockServerDetector::detect(&self.target).await {
915 if info.is_mockforge {
916 TerminalReporter::print_success(&format!(
917 "Detected MockForge server (version: {})",
918 info.version.as_deref().unwrap_or("unknown")
919 ));
920 return MockIntegrationConfig::mock_server();
921 }
922 }
923 }
924 MockIntegrationConfig::real_api()
925 }
926
927 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
929 if !self.crud_flow {
930 return None;
931 }
932
933 if let Some(config_path) = &self.flow_config {
935 match CrudFlowConfig::from_file(config_path) {
936 Ok(config) => return Some(config),
937 Err(e) => {
938 TerminalReporter::print_warning(&format!(
939 "Failed to load flow config: {}. Using auto-detection.",
940 e
941 ));
942 }
943 }
944 }
945
946 let extract_fields = self
948 .extract_fields
949 .as_ref()
950 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
951 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
952
953 Some(CrudFlowConfig {
954 flows: Vec::new(), default_extract_fields: extract_fields,
956 })
957 }
958
959 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
961 let data_file = self.data_file.as_ref()?;
962
963 let distribution = DataDistribution::from_str(&self.data_distribution)
964 .unwrap_or(DataDistribution::UniquePerVu);
965
966 let mappings = self
967 .data_mappings
968 .as_ref()
969 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
970 .unwrap_or_default();
971
972 Some(DataDrivenConfig {
973 file_path: data_file.to_string_lossy().to_string(),
974 distribution,
975 mappings,
976 csv_has_header: true,
977 per_uri_control: self.per_uri_control,
978 per_uri_columns: crate::data_driven::PerUriColumns::default(),
979 })
980 }
981
982 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
984 let error_rate = self.error_rate?;
985
986 let error_types = self
987 .error_types
988 .as_ref()
989 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
990 .unwrap_or_default();
991
992 Some(InvalidDataConfig {
993 error_rate,
994 error_types,
995 target_fields: Vec::new(),
996 })
997 }
998
999 fn build_security_config(&self) -> Option<SecurityTestConfig> {
1001 if !self.security_test {
1002 return None;
1003 }
1004
1005 let categories = self
1006 .security_categories
1007 .as_ref()
1008 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
1009 .unwrap_or_else(|| {
1010 let mut default = HashSet::new();
1011 default.insert(SecurityCategory::SqlInjection);
1012 default.insert(SecurityCategory::Xss);
1013 default
1014 });
1015
1016 let target_fields = self
1017 .security_target_fields
1018 .as_ref()
1019 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
1020 .unwrap_or_default();
1021
1022 let custom_payloads_file =
1023 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
1024
1025 Some(SecurityTestConfig {
1026 enabled: true,
1027 categories,
1028 target_fields,
1029 custom_payloads_file,
1030 include_high_risk: false,
1031 })
1032 }
1033
1034 fn build_parallel_config(&self) -> Option<ParallelConfig> {
1036 let count = self.parallel_create?;
1037
1038 Some(ParallelConfig::new(count))
1039 }
1040
1041 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
1043 let Some(ref wafbench_dir) = self.wafbench_dir else {
1044 return Vec::new();
1045 };
1046
1047 let mut loader = WafBenchLoader::new();
1048
1049 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
1050 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
1051 return Vec::new();
1052 }
1053
1054 let stats = loader.stats();
1055
1056 if stats.files_processed == 0 {
1057 TerminalReporter::print_warning(&format!(
1058 "No WAFBench YAML files found matching '{}'",
1059 wafbench_dir
1060 ));
1061 if !stats.parse_errors.is_empty() {
1063 TerminalReporter::print_warning("Some files were found but failed to parse:");
1064 for error in &stats.parse_errors {
1065 TerminalReporter::print_warning(&format!(" - {}", error));
1066 }
1067 }
1068 return Vec::new();
1069 }
1070
1071 TerminalReporter::print_progress(&format!(
1072 "Loaded {} WAFBench files, {} test cases, {} payloads",
1073 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
1074 ));
1075
1076 for (category, count) in &stats.by_category {
1078 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
1079 }
1080
1081 for error in &stats.parse_errors {
1083 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
1084 }
1085
1086 loader.to_security_payloads()
1087 }
1088
1089 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
1091 let mut enhanced_script = base_script.to_string();
1092 let mut additional_code = String::new();
1093
1094 if let Some(config) = self.build_data_driven_config() {
1096 TerminalReporter::print_progress("Adding data-driven testing support...");
1097 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
1098 additional_code.push('\n');
1099 TerminalReporter::print_success("Data-driven testing enabled");
1100 }
1101
1102 if let Some(config) = self.build_invalid_data_config() {
1104 TerminalReporter::print_progress("Adding invalid data testing support...");
1105 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
1106 additional_code.push('\n');
1107 additional_code
1108 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
1109 additional_code.push('\n');
1110 additional_code
1111 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
1112 additional_code.push('\n');
1113 TerminalReporter::print_success(&format!(
1114 "Invalid data testing enabled ({}% error rate)",
1115 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
1116 ));
1117 }
1118
1119 let security_config = self.build_security_config();
1121 let wafbench_payloads = self.load_wafbench_payloads();
1122 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
1123
1124 if security_config.is_some() || !wafbench_payloads.is_empty() {
1125 TerminalReporter::print_progress("Adding security testing support...");
1126
1127 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1129
1130 if let Some(ref config) = security_config {
1131 payload_list.extend(SecurityPayloads::get_payloads(config));
1132 }
1133
1134 if !wafbench_payloads.is_empty() {
1136 TerminalReporter::print_progress(&format!(
1137 "Loading {} WAFBench attack patterns...",
1138 wafbench_payloads.len()
1139 ));
1140 payload_list.extend(wafbench_payloads);
1141 }
1142
1143 let target_fields =
1144 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1145
1146 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1147 &payload_list,
1148 self.wafbench_cycle_all,
1149 ));
1150 additional_code.push('\n');
1151 additional_code
1152 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1153 additional_code.push('\n');
1154 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1155 additional_code.push('\n');
1156
1157 let mode = if self.wafbench_cycle_all {
1158 "cycle-all"
1159 } else {
1160 "random"
1161 };
1162 TerminalReporter::print_success(&format!(
1163 "Security testing enabled ({} payloads, {} mode)",
1164 payload_list.len(),
1165 mode
1166 ));
1167 } else if security_requested {
1168 TerminalReporter::print_warning(
1172 "Security testing was requested but no payloads were loaded. \
1173 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1174 );
1175 additional_code
1176 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1177 additional_code.push('\n');
1178 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1179 additional_code.push('\n');
1180 }
1181
1182 if let Some(config) = self.build_parallel_config() {
1184 TerminalReporter::print_progress("Adding parallel execution support...");
1185 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1186 additional_code.push('\n');
1187 TerminalReporter::print_success(&format!(
1188 "Parallel execution enabled (count: {})",
1189 config.count
1190 ));
1191 }
1192
1193 if !additional_code.is_empty() {
1195 if let Some(import_end) = enhanced_script.find("export const options") {
1197 enhanced_script.insert_str(
1198 import_end,
1199 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1200 );
1201 }
1202 }
1203
1204 Ok(enhanced_script)
1205 }
1206
1207 async fn execute_sequential_specs(&self) -> Result<()> {
1209 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1210
1211 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1213
1214 if !self.spec.is_empty() {
1215 let specs = load_specs_from_files(self.spec.clone())
1216 .await
1217 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1218 all_specs.extend(specs);
1219 }
1220
1221 if let Some(spec_dir) = &self.spec_dir {
1222 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1223 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1224 })?;
1225 all_specs.extend(dir_specs);
1226 }
1227
1228 if all_specs.is_empty() {
1229 return Err(BenchError::Other(
1230 "No spec files found for sequential execution".to_string(),
1231 ));
1232 }
1233
1234 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1235
1236 let execution_order = if let Some(config_path) = &self.dependency_config {
1238 TerminalReporter::print_progress("Loading dependency configuration...");
1239 let config = SpecDependencyConfig::from_file(config_path)?;
1240
1241 if !config.disable_auto_detect && config.execution_order.is_empty() {
1242 self.detect_and_sort_specs(&all_specs)?
1244 } else {
1245 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1247 }
1248 } else {
1249 self.detect_and_sort_specs(&all_specs)?
1251 };
1252
1253 TerminalReporter::print_success(&format!(
1254 "Execution order: {}",
1255 execution_order
1256 .iter()
1257 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1258 .collect::<Vec<_>>()
1259 .join(" → ")
1260 ));
1261
1262 let mut extracted_values = ExtractedValues::new();
1264 let total_specs = execution_order.len();
1265
1266 for (index, spec_path) in execution_order.iter().enumerate() {
1267 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1268
1269 TerminalReporter::print_progress(&format!(
1270 "[{}/{}] Executing spec: {}",
1271 index + 1,
1272 total_specs,
1273 spec_name
1274 ));
1275
1276 let spec = all_specs
1278 .iter()
1279 .find(|(p, _)| {
1280 p == spec_path
1281 || p.file_name() == spec_path.file_name()
1282 || p.file_name() == Some(spec_path.as_os_str())
1283 })
1284 .map(|(_, s)| s.clone())
1285 .ok_or_else(|| {
1286 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1287 })?;
1288
1289 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1291
1292 extracted_values.merge(&new_values);
1294
1295 TerminalReporter::print_success(&format!(
1296 "[{}/{}] Completed: {} (extracted {} values)",
1297 index + 1,
1298 total_specs,
1299 spec_name,
1300 new_values.values.len()
1301 ));
1302 }
1303
1304 TerminalReporter::print_success(&format!(
1305 "Sequential execution complete: {} specs executed",
1306 total_specs
1307 ));
1308
1309 Ok(())
1310 }
1311
1312 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1314 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1315
1316 let mut detector = DependencyDetector::new();
1317 let dependencies = detector.detect_dependencies(specs);
1318
1319 if dependencies.is_empty() {
1320 TerminalReporter::print_progress("No dependencies detected, using file order");
1321 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1322 }
1323
1324 TerminalReporter::print_progress(&format!(
1325 "Detected {} cross-spec dependencies",
1326 dependencies.len()
1327 ));
1328
1329 for dep in &dependencies {
1330 TerminalReporter::print_progress(&format!(
1331 " {} → {} (via field '{}')",
1332 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1333 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1334 dep.field_name
1335 ));
1336 }
1337
1338 topological_sort(specs, &dependencies)
1339 }
1340
1341 async fn execute_single_spec(
1343 &self,
1344 spec: &OpenApiSpec,
1345 spec_name: &str,
1346 _external_values: &ExtractedValues,
1347 ) -> Result<ExtractedValues> {
1348 let parser = SpecParser::from_spec(spec.clone());
1349
1350 if self.crud_flow {
1352 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1354 } else {
1355 self.execute_standard_spec(&parser, spec_name).await?;
1357 Ok(ExtractedValues::new())
1358 }
1359 }
1360
1361 async fn execute_crud_flow_with_extraction(
1363 &self,
1364 parser: &SpecParser,
1365 spec_name: &str,
1366 ) -> Result<ExtractedValues> {
1367 let operations = parser.get_operations();
1368 let flows = CrudFlowDetector::detect_flows(&operations);
1369
1370 if flows.is_empty() {
1371 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1372 return Ok(ExtractedValues::new());
1373 }
1374
1375 TerminalReporter::print_progress(&format!(
1376 " {} CRUD flow(s) in {}",
1377 flows.len(),
1378 spec_name
1379 ));
1380
1381 let mut handlebars = handlebars::Handlebars::new();
1383 handlebars.register_helper(
1385 "json",
1386 Box::new(
1387 |h: &handlebars::Helper,
1388 _: &handlebars::Handlebars,
1389 _: &handlebars::Context,
1390 _: &mut handlebars::RenderContext,
1391 out: &mut dyn handlebars::Output|
1392 -> handlebars::HelperResult {
1393 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1394 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1395 Ok(())
1396 },
1397 ),
1398 );
1399 let template = include_str!("templates/k6_crud_flow.hbs");
1400 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1401
1402 let custom_headers = self.parse_headers()?;
1403 let config = self.build_crud_flow_config().unwrap_or_default();
1404
1405 let param_overrides = if let Some(params_file) = &self.params_file {
1407 let overrides = ParameterOverrides::from_file(params_file)?;
1408 Some(overrides)
1409 } else {
1410 None
1411 };
1412
1413 let duration_secs = Self::parse_duration(&self.duration)?;
1415 let scenario =
1416 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1417 let stages = scenario.generate_stages(duration_secs, self.vus);
1418
1419 let api_base_path = self.resolve_base_path(parser);
1421
1422 let mut all_headers = custom_headers.clone();
1424 if let Some(auth) = &self.auth {
1425 all_headers.insert("Authorization".to_string(), auth.clone());
1426 }
1427 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1428
1429 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1431
1432 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1433 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1437 serde_json::json!({
1438 "name": sanitized_name.clone(),
1439 "display_name": f.name,
1440 "base_path": f.base_path,
1441 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1442 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1444 let method_raw = if !parts.is_empty() {
1445 parts[0].to_uppercase()
1446 } else {
1447 "GET".to_string()
1448 };
1449 let method = if !parts.is_empty() {
1450 let m = parts[0].to_lowercase();
1451 if m == "delete" { "del".to_string() } else { m }
1453 } else {
1454 "get".to_string()
1455 };
1456 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1457 let path = if let Some(ref bp) = api_base_path {
1459 format!("{}{}", bp, raw_path)
1460 } else {
1461 raw_path.to_string()
1462 };
1463 let is_get_or_head = method == "get" || method == "head";
1464 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1466
1467 let body_value = if has_body {
1469 param_overrides.as_ref()
1470 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1471 .and_then(|oo| oo.body)
1472 .unwrap_or_else(|| serde_json::json!({}))
1473 } else {
1474 serde_json::json!({})
1475 };
1476
1477 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1479
1480 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1482 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1483
1484 serde_json::json!({
1485 "operation": s.operation,
1486 "method": method,
1487 "path": path,
1488 "extract": s.extract,
1489 "use_values": s.use_values,
1490 "use_body": s.use_body,
1491 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1492 "inject_attacks": s.inject_attacks,
1493 "attack_types": s.attack_types,
1494 "description": s.description,
1495 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1496 "is_get_or_head": is_get_or_head,
1497 "has_body": has_body,
1498 "body": processed_body.value,
1499 "body_is_dynamic": body_is_dynamic,
1500 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1501 })
1502 }).collect::<Vec<_>>(),
1503 })
1504 }).collect();
1505
1506 for flow_data in &flows_data {
1508 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1509 for step in steps {
1510 if let Some(placeholders_arr) =
1511 step.get("_placeholders").and_then(|p| p.as_array())
1512 {
1513 for p_str in placeholders_arr {
1514 if let Some(p_name) = p_str.as_str() {
1515 match p_name {
1516 "VU" => {
1517 all_placeholders.insert(DynamicPlaceholder::VU);
1518 }
1519 "Iteration" => {
1520 all_placeholders.insert(DynamicPlaceholder::Iteration);
1521 }
1522 "Timestamp" => {
1523 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1524 }
1525 "UUID" => {
1526 all_placeholders.insert(DynamicPlaceholder::UUID);
1527 }
1528 "Random" => {
1529 all_placeholders.insert(DynamicPlaceholder::Random);
1530 }
1531 "Counter" => {
1532 all_placeholders.insert(DynamicPlaceholder::Counter);
1533 }
1534 "Date" => {
1535 all_placeholders.insert(DynamicPlaceholder::Date);
1536 }
1537 "VuIter" => {
1538 all_placeholders.insert(DynamicPlaceholder::VuIter);
1539 }
1540 _ => {}
1541 }
1542 }
1543 }
1544 }
1545 }
1546 }
1547 }
1548
1549 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1551 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1552
1553 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1555
1556 let data = serde_json::json!({
1557 "base_url": self.target,
1558 "flows": flows_data,
1559 "extract_fields": config.default_extract_fields,
1560 "duration_secs": duration_secs,
1561 "max_vus": self.vus,
1562 "auth_header": self.auth,
1563 "custom_headers": custom_headers,
1564 "skip_tls_verify": self.skip_tls_verify,
1565 "stages": stages.iter().map(|s| serde_json::json!({
1567 "duration": s.duration,
1568 "target": s.target,
1569 })).collect::<Vec<_>>(),
1570 "threshold_percentile": self.threshold_percentile,
1571 "threshold_ms": self.threshold_ms,
1572 "max_error_rate": self.max_error_rate,
1573 "headers": headers_json,
1574 "dynamic_imports": required_imports,
1575 "dynamic_globals": required_globals,
1576 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1577 "security_testing_enabled": security_testing_enabled,
1579 "has_custom_headers": !custom_headers.is_empty(),
1580 });
1581
1582 let mut script = handlebars
1583 .render_template(template, &data)
1584 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1585
1586 if security_testing_enabled {
1588 script = self.generate_enhanced_script(&script)?;
1589 }
1590
1591 let script_path =
1593 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1594
1595 std::fs::create_dir_all(self.output.clone())?;
1596 std::fs::write(&script_path, &script)?;
1597
1598 if !self.generate_only {
1599 let executor = K6Executor::new()?;
1600 std::fs::create_dir_all(&output_dir)?;
1601
1602 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1603
1604 let extracted = Self::parse_extracted_values(&output_dir)?;
1605 TerminalReporter::print_progress(&format!(
1606 " Extracted {} value(s) from {}",
1607 extracted.values.len(),
1608 spec_name
1609 ));
1610 return Ok(extracted);
1611 }
1612
1613 Ok(ExtractedValues::new())
1614 }
1615
1616 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1618 let mut operations = if let Some(filter) = &self.operations {
1619 parser.filter_operations(filter)?
1620 } else {
1621 parser.get_operations()
1622 };
1623
1624 if let Some(exclude) = &self.exclude_operations {
1625 operations = parser.exclude_operations(operations, exclude)?;
1626 }
1627
1628 if operations.is_empty() {
1629 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1630 return Ok(());
1631 }
1632
1633 TerminalReporter::print_progress(&format!(
1634 " {} operations in {}",
1635 operations.len(),
1636 spec_name
1637 ));
1638
1639 let templates: Vec<_> = operations
1641 .iter()
1642 .map(RequestGenerator::generate_template)
1643 .collect::<Result<Vec<_>>>()?;
1644
1645 let custom_headers = self.parse_headers()?;
1647
1648 let base_path = self.resolve_base_path(parser);
1650
1651 let scenario =
1653 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1654
1655 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1656
1657 let k6_config = K6Config {
1658 target_url: self.target.clone(),
1659 base_path,
1660 scenario,
1661 duration_secs: Self::parse_duration(&self.duration)?,
1662 max_vus: self.vus,
1663 threshold_percentile: self.threshold_percentile.clone(),
1664 threshold_ms: self.threshold_ms,
1665 max_error_rate: self.max_error_rate,
1666 auth_header: self.auth.clone(),
1667 custom_headers,
1668 skip_tls_verify: self.skip_tls_verify,
1669 security_testing_enabled,
1670 chunked_request_bodies: self.chunked_request_bodies,
1671 target_rps: self.target_rps,
1672 no_keep_alive: self.no_keep_alive,
1673 };
1674
1675 let generator = K6ScriptGenerator::new(k6_config, templates);
1676 let mut script = generator.generate()?;
1677
1678 let has_advanced_features = self.data_file.is_some()
1680 || self.error_rate.is_some()
1681 || self.security_test
1682 || self.parallel_create.is_some()
1683 || self.wafbench_dir.is_some();
1684
1685 if has_advanced_features {
1686 script = self.generate_enhanced_script(&script)?;
1687 }
1688
1689 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1691
1692 std::fs::create_dir_all(self.output.clone())?;
1693 std::fs::write(&script_path, &script)?;
1694
1695 if !self.generate_only {
1696 let executor = K6Executor::new()?;
1697 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1698 std::fs::create_dir_all(&output_dir)?;
1699
1700 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1701 }
1702
1703 Ok(())
1704 }
1705
1706 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1708 let config = self.build_crud_flow_config().unwrap_or_default();
1710
1711 let flows = if !config.flows.is_empty() {
1713 TerminalReporter::print_progress("Using custom flow configuration...");
1714 config.flows.clone()
1715 } else {
1716 TerminalReporter::print_progress("Detecting CRUD operations...");
1717 let operations = parser.get_operations();
1718 CrudFlowDetector::detect_flows(&operations)
1719 };
1720
1721 if flows.is_empty() {
1722 return Err(BenchError::Other(
1723 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1724 ));
1725 }
1726
1727 if config.flows.is_empty() {
1728 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
1729 } else {
1730 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
1731 }
1732
1733 for flow in &flows {
1734 TerminalReporter::print_progress(&format!(
1735 " - {}: {} steps",
1736 flow.name,
1737 flow.steps.len()
1738 ));
1739 }
1740
1741 let mut handlebars = handlebars::Handlebars::new();
1743 handlebars.register_helper(
1745 "json",
1746 Box::new(
1747 |h: &handlebars::Helper,
1748 _: &handlebars::Handlebars,
1749 _: &handlebars::Context,
1750 _: &mut handlebars::RenderContext,
1751 out: &mut dyn handlebars::Output|
1752 -> handlebars::HelperResult {
1753 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1754 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1755 Ok(())
1756 },
1757 ),
1758 );
1759 let template = include_str!("templates/k6_crud_flow.hbs");
1760
1761 let custom_headers = self.parse_headers()?;
1762
1763 let param_overrides = if let Some(params_file) = &self.params_file {
1765 TerminalReporter::print_progress("Loading parameter overrides...");
1766 let overrides = ParameterOverrides::from_file(params_file)?;
1767 TerminalReporter::print_success(&format!(
1768 "Loaded parameter overrides ({} operation-specific, {} defaults)",
1769 overrides.operations.len(),
1770 if overrides.defaults.is_empty() { 0 } else { 1 }
1771 ));
1772 Some(overrides)
1773 } else {
1774 None
1775 };
1776
1777 let duration_secs = Self::parse_duration(&self.duration)?;
1779 let scenario =
1780 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1781 let stages = scenario.generate_stages(duration_secs, self.vus);
1782
1783 let api_base_path = self.resolve_base_path(parser);
1785 if let Some(ref bp) = api_base_path {
1786 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
1787 }
1788
1789 let mut all_headers = custom_headers.clone();
1791 if let Some(auth) = &self.auth {
1792 all_headers.insert("Authorization".to_string(), auth.clone());
1793 }
1794 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1795
1796 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1798
1799 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1800 let sanitized_name = K6ScriptGenerator::sanitize_k6_metric_name(&f.name);
1805 serde_json::json!({
1806 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
1809 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1810 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1812 let method_raw = if !parts.is_empty() {
1813 parts[0].to_uppercase()
1814 } else {
1815 "GET".to_string()
1816 };
1817 let method = if !parts.is_empty() {
1818 let m = parts[0].to_lowercase();
1819 if m == "delete" { "del".to_string() } else { m }
1821 } else {
1822 "get".to_string()
1823 };
1824 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1825 let path = if let Some(ref bp) = api_base_path {
1827 format!("{}{}", bp, raw_path)
1828 } else {
1829 raw_path.to_string()
1830 };
1831 let is_get_or_head = method == "get" || method == "head";
1832 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1834
1835 let body_value = if has_body {
1837 param_overrides.as_ref()
1838 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1839 .and_then(|oo| oo.body)
1840 .unwrap_or_else(|| serde_json::json!({}))
1841 } else {
1842 serde_json::json!({})
1843 };
1844
1845 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1847 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1852 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1853
1854 serde_json::json!({
1855 "operation": s.operation,
1856 "method": method,
1857 "path": path,
1858 "extract": s.extract,
1859 "use_values": s.use_values,
1860 "use_body": s.use_body,
1861 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1862 "inject_attacks": s.inject_attacks,
1863 "attack_types": s.attack_types,
1864 "description": s.description,
1865 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1866 "is_get_or_head": is_get_or_head,
1867 "has_body": has_body,
1868 "body": processed_body.value,
1869 "body_is_dynamic": body_is_dynamic,
1870 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1871 })
1872 }).collect::<Vec<_>>(),
1873 })
1874 }).collect();
1875
1876 for flow_data in &flows_data {
1878 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1879 for step in steps {
1880 if let Some(placeholders_arr) =
1881 step.get("_placeholders").and_then(|p| p.as_array())
1882 {
1883 for p_str in placeholders_arr {
1884 if let Some(p_name) = p_str.as_str() {
1885 match p_name {
1887 "VU" => {
1888 all_placeholders.insert(DynamicPlaceholder::VU);
1889 }
1890 "Iteration" => {
1891 all_placeholders.insert(DynamicPlaceholder::Iteration);
1892 }
1893 "Timestamp" => {
1894 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1895 }
1896 "UUID" => {
1897 all_placeholders.insert(DynamicPlaceholder::UUID);
1898 }
1899 "Random" => {
1900 all_placeholders.insert(DynamicPlaceholder::Random);
1901 }
1902 "Counter" => {
1903 all_placeholders.insert(DynamicPlaceholder::Counter);
1904 }
1905 "Date" => {
1906 all_placeholders.insert(DynamicPlaceholder::Date);
1907 }
1908 "VuIter" => {
1909 all_placeholders.insert(DynamicPlaceholder::VuIter);
1910 }
1911 _ => {}
1912 }
1913 }
1914 }
1915 }
1916 }
1917 }
1918 }
1919
1920 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1922 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1923
1924 let invalid_data_config = self.build_invalid_data_config();
1926 let error_injection_enabled = invalid_data_config.is_some();
1927 let error_rate = self.error_rate.unwrap_or(0.0);
1928 let error_types: Vec<String> = invalid_data_config
1929 .as_ref()
1930 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
1931 .unwrap_or_default();
1932
1933 if error_injection_enabled {
1934 TerminalReporter::print_progress(&format!(
1935 "Error injection enabled ({}% rate)",
1936 (error_rate * 100.0) as u32
1937 ));
1938 }
1939
1940 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1942
1943 let data = serde_json::json!({
1944 "base_url": self.target,
1945 "flows": flows_data,
1946 "extract_fields": config.default_extract_fields,
1947 "duration_secs": duration_secs,
1948 "max_vus": self.vus,
1949 "auth_header": self.auth,
1950 "custom_headers": custom_headers,
1951 "skip_tls_verify": self.skip_tls_verify,
1952 "stages": stages.iter().map(|s| serde_json::json!({
1954 "duration": s.duration,
1955 "target": s.target,
1956 })).collect::<Vec<_>>(),
1957 "threshold_percentile": self.threshold_percentile,
1958 "threshold_ms": self.threshold_ms,
1959 "max_error_rate": self.max_error_rate,
1960 "headers": headers_json,
1961 "dynamic_imports": required_imports,
1962 "dynamic_globals": required_globals,
1963 "extracted_values_output_path": self
1964 .output
1965 .join("crud_flow_extracted_values.json")
1966 .to_string_lossy(),
1967 "error_injection_enabled": error_injection_enabled,
1969 "error_rate": error_rate,
1970 "error_types": error_types,
1971 "security_testing_enabled": security_testing_enabled,
1973 "has_custom_headers": !custom_headers.is_empty(),
1974 });
1975
1976 let mut script = handlebars
1977 .render_template(template, &data)
1978 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1979
1980 if security_testing_enabled {
1982 script = self.generate_enhanced_script(&script)?;
1983 }
1984
1985 TerminalReporter::print_progress("Validating CRUD flow script...");
1987 let validation_errors = K6ScriptGenerator::validate_script(&script);
1988 if !validation_errors.is_empty() {
1989 TerminalReporter::print_error("CRUD flow script validation failed");
1990 for error in &validation_errors {
1991 eprintln!(" {}", error);
1992 }
1993 return Err(BenchError::Other(format!(
1994 "CRUD flow script validation failed with {} error(s)",
1995 validation_errors.len()
1996 )));
1997 }
1998
1999 TerminalReporter::print_success("CRUD flow script generated");
2000
2001 let script_path = if let Some(output) = &self.script_output {
2003 output.clone()
2004 } else {
2005 self.output.join("k6-crud-flow-script.js")
2006 };
2007
2008 if let Some(parent) = script_path.parent() {
2009 std::fs::create_dir_all(parent)?;
2010 }
2011 std::fs::write(&script_path, &script)?;
2012 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2013
2014 if self.generate_only {
2015 println!("\nScript generated successfully. Run it with:");
2016 println!(" k6 run {}", script_path.display());
2017 return Ok(());
2018 }
2019
2020 TerminalReporter::print_progress("Executing CRUD flow test...");
2022 let executor = K6Executor::new()?;
2023 std::fs::create_dir_all(&self.output)?;
2024
2025 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2026
2027 let duration_secs = Self::parse_duration(&self.duration)?;
2028 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2029
2030 Ok(())
2031 }
2032
2033 async fn execute_conformance_test(&self) -> Result<()> {
2035 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2036 use crate::conformance::report::ConformanceReport;
2037 use crate::conformance::spec::ConformanceFeature;
2038
2039 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
2040
2041 TerminalReporter::print_progress(
2044 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2045 );
2046
2047 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2049 cats_str
2050 .split(',')
2051 .filter_map(|s| {
2052 let trimmed = s.trim();
2053 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2054 Some(canonical.to_string())
2055 } else {
2056 TerminalReporter::print_warning(&format!(
2057 "Unknown conformance category: '{}'. Valid categories: {}",
2058 trimmed,
2059 ConformanceFeature::cli_category_names()
2060 .iter()
2061 .map(|(cli, _)| *cli)
2062 .collect::<Vec<_>>()
2063 .join(", ")
2064 ));
2065 None
2066 }
2067 })
2068 .collect::<Vec<String>>()
2069 });
2070
2071 let custom_headers: Vec<(String, String)> = self
2073 .conformance_headers
2074 .iter()
2075 .filter_map(|h| {
2076 let (name, value) = h.split_once(':')?;
2077 Some((name.trim().to_string(), value.trim().to_string()))
2078 })
2079 .collect();
2080
2081 if !custom_headers.is_empty() {
2082 TerminalReporter::print_progress(&format!(
2083 "Using {} custom header(s) for authentication",
2084 custom_headers.len()
2085 ));
2086 }
2087
2088 if self.conformance_delay_ms > 0 {
2089 TerminalReporter::print_progress(&format!(
2090 "Using {}ms delay between conformance requests",
2091 self.conformance_delay_ms
2092 ));
2093 }
2094
2095 std::fs::create_dir_all(&self.output)?;
2097
2098 let config = ConformanceConfig {
2099 target_url: self.target.clone(),
2100 api_key: self.conformance_api_key.clone(),
2101 basic_auth: self.conformance_basic_auth.clone(),
2102 skip_tls_verify: self.skip_tls_verify,
2103 categories,
2104 base_path: self.base_path.clone(),
2105 custom_headers,
2106 output_dir: Some(self.output.clone()),
2107 all_operations: self.conformance_all_operations,
2108 custom_checks_file: self.conformance_custom.clone(),
2109 request_delay_ms: self.conformance_delay_ms,
2110 custom_filter: self.conformance_custom_filter.clone(),
2111 export_requests: self.export_requests,
2112 validate_requests: self.validate_requests,
2113 };
2114
2115 let annotated_ops = if !self.spec.is_empty() {
2118 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2119 let parser = SpecParser::from_file(&self.spec[0]).await?;
2120 let operations = parser.get_operations();
2121
2122 let annotated =
2123 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2124 &operations,
2125 parser.spec(),
2126 );
2127 TerminalReporter::print_success(&format!(
2128 "Analyzed {} operations, found {} feature annotations",
2129 operations.len(),
2130 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2131 ));
2132 Some(annotated)
2133 } else {
2134 None
2135 };
2136
2137 if self.validate_requests && !self.spec.is_empty() {
2139 TerminalReporter::print_progress("Validating requests against OpenAPI spec...");
2140 let violation_count = crate::conformance::request_validator::run_request_validation(
2141 &self.spec,
2142 self.conformance_custom.as_deref(),
2143 self.base_path.as_deref(),
2144 &self.output,
2145 )
2146 .await?;
2147 if violation_count > 0 {
2148 TerminalReporter::print_warning(&format!(
2149 "{} request validation violation(s) found — see conformance-request-violations.json",
2150 violation_count
2151 ));
2152 } else {
2153 TerminalReporter::print_success("All requests conform to the OpenAPI spec");
2154 }
2155 }
2156
2157 if self.generate_only || self.use_k6 {
2159 let script = if let Some(annotated) = &annotated_ops {
2160 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2161 config,
2162 annotated.clone(),
2163 );
2164 let op_count = gen.operation_count();
2165 let (script, check_count) = gen.generate()?;
2166 TerminalReporter::print_success(&format!(
2167 "Conformance: {} operations analyzed, {} unique checks generated",
2168 op_count, check_count
2169 ));
2170 script
2171 } else {
2172 let generator = ConformanceGenerator::new(config);
2173 generator.generate()?
2174 };
2175
2176 let script_path = self.output.join("k6-conformance.js");
2177 std::fs::write(&script_path, &script).map_err(|e| {
2178 BenchError::Other(format!("Failed to write conformance script: {}", e))
2179 })?;
2180 TerminalReporter::print_success(&format!(
2181 "Conformance script generated: {}",
2182 script_path.display()
2183 ));
2184
2185 if self.generate_only {
2186 println!("\nScript generated. Run with:");
2187 println!(" k6 run {}", script_path.display());
2188 return Ok(());
2189 }
2190
2191 if !K6Executor::is_k6_installed() {
2193 TerminalReporter::print_error("k6 is not installed");
2194 TerminalReporter::print_warning(
2195 "Install k6 from: https://k6.io/docs/get-started/installation/",
2196 );
2197 return Err(BenchError::K6NotFound);
2198 }
2199
2200 TerminalReporter::print_progress("Running conformance tests via k6...");
2201 let executor = K6Executor::new()?;
2202 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2203
2204 let report_path = self.output.join("conformance-report.json");
2205 if report_path.exists() {
2206 let report = ConformanceReport::from_file(&report_path)?;
2207 report.print_report_with_options(self.conformance_all_operations);
2208 self.save_conformance_report(&report, &report_path)?;
2209 } else {
2210 TerminalReporter::print_warning(
2211 "Conformance report not generated (k6 handleSummary may not have run)",
2212 );
2213 }
2214
2215 return Ok(());
2216 }
2217
2218 TerminalReporter::print_progress("Running conformance tests (native executor)...");
2220
2221 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2222
2223 executor = if let Some(annotated) = &annotated_ops {
2224 executor.with_spec_driven_checks(annotated)
2225 } else {
2226 executor.with_reference_checks()
2227 };
2228 executor = executor.with_custom_checks()?;
2229
2230 TerminalReporter::print_success(&format!(
2231 "Executing {} conformance checks...",
2232 executor.check_count()
2233 ));
2234
2235 let report = executor.execute().await?;
2236 report.print_report_with_options(self.conformance_all_operations);
2237
2238 let failure_details = report.failure_details();
2240 if !failure_details.is_empty() {
2241 let details_path = self.output.join("conformance-failure-details.json");
2242 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2243 let _ = std::fs::write(&details_path, json);
2244 TerminalReporter::print_success(&format!(
2245 "Failure details saved to: {}",
2246 details_path.display()
2247 ));
2248 }
2249 }
2250
2251 let report_path = self.output.join("conformance-report.json");
2253 let report_json = serde_json::to_string_pretty(&report.to_json())
2254 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2255 std::fs::write(&report_path, &report_json)
2256 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2257 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2258
2259 self.save_conformance_report(&report, &report_path)?;
2260
2261 Ok(())
2262 }
2263
2264 fn save_conformance_report(
2266 &self,
2267 report: &crate::conformance::report::ConformanceReport,
2268 report_path: &Path,
2269 ) -> Result<()> {
2270 if self.conformance_report_format == "sarif" {
2271 use crate::conformance::sarif::ConformanceSarifReport;
2272 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2273 TerminalReporter::print_success(&format!(
2274 "SARIF report saved to: {}",
2275 self.conformance_report.display()
2276 ));
2277 } else if self.conformance_report != *report_path {
2278 std::fs::copy(report_path, &self.conformance_report)?;
2279 TerminalReporter::print_success(&format!(
2280 "Report saved to: {}",
2281 self.conformance_report.display()
2282 ));
2283 }
2284 Ok(())
2285 }
2286
2287 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2293 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
2294 use crate::conformance::report::ConformanceReport;
2295 use crate::conformance::spec::ConformanceFeature;
2296
2297 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2298
2299 TerminalReporter::print_progress("Parsing targets file...");
2301 let targets = parse_targets_file(targets_file)?;
2302 let num_targets = targets.len();
2303 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2304
2305 if targets.is_empty() {
2306 return Err(BenchError::Other("No targets found in file".to_string()));
2307 }
2308
2309 TerminalReporter::print_progress(
2310 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2311 );
2312
2313 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2315 cats_str
2316 .split(',')
2317 .filter_map(|s| {
2318 let trimmed = s.trim();
2319 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2320 Some(canonical.to_string())
2321 } else {
2322 TerminalReporter::print_warning(&format!(
2323 "Unknown conformance category: '{}'. Valid categories: {}",
2324 trimmed,
2325 ConformanceFeature::cli_category_names()
2326 .iter()
2327 .map(|(cli, _)| *cli)
2328 .collect::<Vec<_>>()
2329 .join(", ")
2330 ));
2331 None
2332 }
2333 })
2334 .collect::<Vec<String>>()
2335 });
2336
2337 let base_custom_headers: Vec<(String, String)> = self
2339 .conformance_headers
2340 .iter()
2341 .filter_map(|h| {
2342 let (name, value) = h.split_once(':')?;
2343 Some((name.trim().to_string(), value.trim().to_string()))
2344 })
2345 .collect();
2346
2347 if !base_custom_headers.is_empty() {
2348 TerminalReporter::print_progress(&format!(
2349 "Using {} base custom header(s) for authentication",
2350 base_custom_headers.len()
2351 ));
2352 }
2353
2354 let annotated_ops = if !self.spec.is_empty() {
2356 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2357 let parser = SpecParser::from_file(&self.spec[0]).await?;
2358 let operations = parser.get_operations();
2359 let annotated =
2360 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2361 &operations,
2362 parser.spec(),
2363 );
2364 TerminalReporter::print_success(&format!(
2365 "Analyzed {} operations, found {} feature annotations",
2366 operations.len(),
2367 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2368 ));
2369 Some(annotated)
2370 } else {
2371 None
2372 };
2373
2374 std::fs::create_dir_all(&self.output)?;
2376
2377 struct TargetResult {
2379 url: String,
2380 passed: usize,
2381 failed: usize,
2382 elapsed: std::time::Duration,
2383 report_json: serde_json::Value,
2384 owasp_coverage: Vec<crate::conformance::report::OwaspCoverageEntry>,
2385 }
2386
2387 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2388 let total_start = std::time::Instant::now();
2389
2390 for (idx, target) in targets.iter().enumerate() {
2391 tracing::info!(
2392 "Running conformance tests against target {}/{}: {}",
2393 idx + 1,
2394 num_targets,
2395 target.url
2396 );
2397 TerminalReporter::print_progress(&format!(
2398 "\n--- Target {}/{}: {} ---",
2399 idx + 1,
2400 num_targets,
2401 target.url
2402 ));
2403
2404 let mut merged_headers = base_custom_headers.clone();
2406 if let Some(ref target_headers) = target.headers {
2407 for (name, value) in target_headers {
2408 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2410 existing.1 = value.clone();
2411 } else {
2412 merged_headers.push((name.clone(), value.clone()));
2413 }
2414 }
2415 }
2416 if let Some(ref auth) = target.auth {
2418 if let Some(existing) =
2419 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2420 {
2421 existing.1 = auth.clone();
2422 } else {
2423 merged_headers.push(("Authorization".to_string(), auth.clone()));
2424 }
2425 }
2426
2427 let target_dir = self.output.join(format!("target_{}", idx));
2433 std::fs::create_dir_all(&target_dir)?;
2434
2435 let config = ConformanceConfig {
2436 target_url: target.url.clone(),
2437 api_key: self.conformance_api_key.clone(),
2438 basic_auth: self.conformance_basic_auth.clone(),
2439 skip_tls_verify: self.skip_tls_verify,
2440 categories: categories.clone(),
2441 base_path: self.base_path.clone(),
2442 custom_headers: merged_headers,
2443 output_dir: Some(target_dir.clone()),
2444 all_operations: self.conformance_all_operations,
2445 custom_checks_file: self.conformance_custom.clone(),
2446 request_delay_ms: self.conformance_delay_ms,
2447 custom_filter: self.conformance_custom_filter.clone(),
2448 export_requests: self.export_requests,
2449 validate_requests: self.validate_requests,
2450 };
2451
2452 let target_start = std::time::Instant::now();
2453 let report = if self.use_k6 {
2454 if !K6Executor::is_k6_installed() {
2455 TerminalReporter::print_error("k6 is not installed");
2456 TerminalReporter::print_warning(
2457 "Install k6 from: https://k6.io/docs/get-started/installation/",
2458 );
2459 return Err(BenchError::K6NotFound);
2460 }
2461
2462 let script = if let Some(ref annotated) = annotated_ops {
2463 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2464 config.clone(),
2465 annotated.clone(),
2466 );
2467 let (script, _check_count) = gen.generate()?;
2468 script
2469 } else {
2470 let generator = ConformanceGenerator::new(config.clone());
2471 generator.generate()?
2472 };
2473
2474 let script_path = target_dir.join("k6-conformance.js");
2475 std::fs::write(&script_path, &script).map_err(|e| {
2476 BenchError::Other(format!("Failed to write conformance script: {}", e))
2477 })?;
2478 TerminalReporter::print_success(&format!(
2479 "Conformance script generated: {}",
2480 script_path.display()
2481 ));
2482
2483 TerminalReporter::print_progress(&format!(
2484 "Running conformance tests via k6 against {}...",
2485 target.url
2486 ));
2487 let k6 = K6Executor::new()?;
2488 let api_port = 6565u16.saturating_add(idx as u16);
2490 k6.execute_with_port(&script_path, Some(&target_dir), self.verbose, Some(api_port))
2491 .await?;
2492
2493 let report_path = target_dir.join("conformance-report.json");
2494 if report_path.exists() {
2495 ConformanceReport::from_file(&report_path)?
2496 } else {
2497 TerminalReporter::print_warning(&format!(
2498 "Conformance report not generated for target {} (k6 handleSummary may not have run)",
2499 target.url
2500 ));
2501 continue;
2502 }
2503 } else {
2504 let mut executor =
2505 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2506
2507 executor = if let Some(ref annotated) = annotated_ops {
2508 executor.with_spec_driven_checks(annotated)
2509 } else {
2510 executor.with_reference_checks()
2511 };
2512 executor = executor.with_custom_checks()?;
2513
2514 TerminalReporter::print_success(&format!(
2515 "Executing {} conformance checks against {}...",
2516 executor.check_count(),
2517 target.url
2518 ));
2519
2520 executor.execute().await?
2521 };
2522 let target_elapsed = target_start.elapsed();
2523
2524 let report_json = report.to_json();
2525
2526 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2528 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2529 let total_checks = passed + failed;
2530 let rate = if total_checks == 0 {
2531 0.0
2532 } else {
2533 (passed as f64 / total_checks as f64) * 100.0
2534 };
2535
2536 TerminalReporter::print_success(&format!(
2537 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
2538 target.url,
2539 passed,
2540 total_checks,
2541 rate,
2542 target_elapsed.as_secs_f64()
2543 ));
2544
2545 let target_report_path = target_dir.join("conformance-report.json");
2547 let report_str = serde_json::to_string_pretty(&report_json)
2548 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2549 std::fs::write(&target_report_path, &report_str)
2550 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2551
2552 let failure_details = report.failure_details();
2554 if !failure_details.is_empty() {
2555 let details_path = target_dir.join("conformance-failure-details.json");
2556 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2557 let _ = std::fs::write(&details_path, json);
2558 }
2559 }
2560
2561 let owasp_coverage = report.owasp_coverage_data();
2563
2564 target_results.push(TargetResult {
2565 url: target.url.clone(),
2566 passed,
2567 failed,
2568 elapsed: target_elapsed,
2569 report_json,
2570 owasp_coverage,
2571 });
2572 }
2573
2574 let total_elapsed = total_start.elapsed();
2575
2576 println!("\n{}", "=".repeat(80));
2578 println!(" Multi-Target Conformance Summary");
2579 println!("{}", "=".repeat(80));
2580 println!(
2581 " {:<40} {:>8} {:>8} {:>8} {:>8}",
2582 "Target URL", "Passed", "Failed", "Rate", "Time"
2583 );
2584 println!(" {}", "-".repeat(76));
2585
2586 let mut total_passed = 0usize;
2587 let mut total_failed = 0usize;
2588
2589 for result in &target_results {
2590 let total_checks = result.passed + result.failed;
2591 let rate = if total_checks == 0 {
2592 0.0
2593 } else {
2594 (result.passed as f64 / total_checks as f64) * 100.0
2595 };
2596
2597 let display_url = if result.url.len() > 38 {
2599 format!("{}...", &result.url[..35])
2600 } else {
2601 result.url.clone()
2602 };
2603
2604 println!(
2605 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2606 display_url,
2607 result.passed,
2608 result.failed,
2609 rate,
2610 result.elapsed.as_secs_f64()
2611 );
2612
2613 total_passed += result.passed;
2614 total_failed += result.failed;
2615 }
2616
2617 let grand_total = total_passed + total_failed;
2618 let overall_rate = if grand_total == 0 {
2619 0.0
2620 } else {
2621 (total_passed as f64 / grand_total as f64) * 100.0
2622 };
2623
2624 println!(" {}", "-".repeat(76));
2625 println!(
2626 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2627 format!("TOTAL ({} targets)", num_targets),
2628 total_passed,
2629 total_failed,
2630 overall_rate,
2631 total_elapsed.as_secs_f64()
2632 );
2633 println!("{}", "=".repeat(80));
2634
2635 for result in &target_results {
2637 println!("\n OWASP API Security Top 10 Coverage for {}:", result.url);
2638 for entry in &result.owasp_coverage {
2639 let status = if !entry.tested {
2640 "-"
2641 } else if entry.all_passed {
2642 "pass"
2643 } else {
2644 "FAIL"
2645 };
2646 let via = if entry.via_categories.is_empty() {
2647 String::new()
2648 } else {
2649 format!(" (via {})", entry.via_categories.join(", "))
2650 };
2651 println!(" {:<12} {:<40} {}{}", entry.id, entry.name, status, via);
2652 }
2653 }
2654
2655 let per_target_summaries: Vec<serde_json::Value> = target_results
2657 .iter()
2658 .enumerate()
2659 .map(|(idx, r)| {
2660 let total_checks = r.passed + r.failed;
2661 let rate = if total_checks == 0 {
2662 0.0
2663 } else {
2664 (r.passed as f64 / total_checks as f64) * 100.0
2665 };
2666 let owasp_json: Vec<serde_json::Value> = r
2667 .owasp_coverage
2668 .iter()
2669 .map(|e| {
2670 serde_json::json!({
2671 "id": e.id,
2672 "name": e.name,
2673 "tested": e.tested,
2674 "all_passed": e.all_passed,
2675 "via_categories": e.via_categories,
2676 })
2677 })
2678 .collect();
2679 serde_json::json!({
2680 "target_url": r.url,
2681 "target_index": idx,
2682 "checks_passed": r.passed,
2683 "checks_failed": r.failed,
2684 "total_checks": total_checks,
2685 "pass_rate": rate,
2686 "elapsed_seconds": r.elapsed.as_secs_f64(),
2687 "report": r.report_json,
2688 "owasp_coverage": owasp_json,
2689 })
2690 })
2691 .collect();
2692
2693 let combined_summary = serde_json::json!({
2694 "total_targets": num_targets,
2695 "total_checks_passed": total_passed,
2696 "total_checks_failed": total_failed,
2697 "overall_pass_rate": overall_rate,
2698 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
2699 "targets": per_target_summaries,
2700 });
2701
2702 let summary_path = self.output.join("multi-target-conformance-summary.json");
2703 let summary_str = serde_json::to_string_pretty(&combined_summary)
2704 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
2705 std::fs::write(&summary_path, &summary_str)
2706 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
2707 TerminalReporter::print_success(&format!(
2708 "Combined summary saved to: {}",
2709 summary_path.display()
2710 ));
2711
2712 Ok(())
2713 }
2714
2715 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
2717 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
2718
2719 let custom_headers = self.parse_headers()?;
2721
2722 let mut config = OwaspApiConfig::new()
2724 .with_auth_header(&self.owasp_auth_header)
2725 .with_verbose(self.verbose)
2726 .with_insecure(self.skip_tls_verify)
2727 .with_concurrency(self.vus as usize)
2728 .with_iterations(self.owasp_iterations as usize)
2729 .with_base_path(self.base_path.clone())
2730 .with_custom_headers(custom_headers);
2731
2732 if let Some(ref token) = self.owasp_auth_token {
2734 config = config.with_valid_auth_token(token);
2735 }
2736
2737 if let Some(ref cats_str) = self.owasp_categories {
2739 let categories: Vec<OwaspCategory> = cats_str
2740 .split(',')
2741 .filter_map(|s| {
2742 let trimmed = s.trim();
2743 match trimmed.parse::<OwaspCategory>() {
2744 Ok(cat) => Some(cat),
2745 Err(e) => {
2746 TerminalReporter::print_warning(&e);
2747 None
2748 }
2749 }
2750 })
2751 .collect();
2752
2753 if !categories.is_empty() {
2754 config = config.with_categories(categories);
2755 }
2756 }
2757
2758 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
2760 config.admin_paths_file = Some(admin_paths_file.clone());
2761 if let Err(e) = config.load_admin_paths() {
2762 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
2763 }
2764 }
2765
2766 if let Some(ref id_fields_str) = self.owasp_id_fields {
2768 let id_fields: Vec<String> = id_fields_str
2769 .split(',')
2770 .map(|s| s.trim().to_string())
2771 .filter(|s| !s.is_empty())
2772 .collect();
2773 if !id_fields.is_empty() {
2774 config = config.with_id_fields(id_fields);
2775 }
2776 }
2777
2778 if let Some(ref report_path) = self.owasp_report {
2780 config = config.with_report_path(report_path);
2781 }
2782 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
2783 config = config.with_report_format(format);
2784 }
2785
2786 let categories = config.categories_to_test();
2788 TerminalReporter::print_success(&format!(
2789 "Testing {} OWASP categories: {}",
2790 categories.len(),
2791 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
2792 ));
2793
2794 if config.valid_auth_token.is_some() {
2795 TerminalReporter::print_progress("Using provided auth token for baseline requests");
2796 }
2797
2798 TerminalReporter::print_progress("Generating OWASP security test script...");
2800 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
2801
2802 let script = generator.generate()?;
2804 TerminalReporter::print_success("OWASP security test script generated");
2805
2806 let script_path = if let Some(output) = &self.script_output {
2808 output.clone()
2809 } else {
2810 self.output.join("k6-owasp-security-test.js")
2811 };
2812
2813 if let Some(parent) = script_path.parent() {
2814 std::fs::create_dir_all(parent)?;
2815 }
2816 std::fs::write(&script_path, &script)?;
2817 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2818
2819 if self.generate_only {
2821 println!("\nOWASP security test script generated. Run it with:");
2822 println!(" k6 run {}", script_path.display());
2823 return Ok(());
2824 }
2825
2826 TerminalReporter::print_progress("Executing OWASP security tests...");
2828 let executor = K6Executor::new()?;
2829 std::fs::create_dir_all(&self.output)?;
2830
2831 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2832
2833 let duration_secs = Self::parse_duration(&self.duration)?;
2834 TerminalReporter::print_summary_with_mode(&results, duration_secs, self.no_keep_alive);
2835
2836 println!("\nOWASP security test results saved to: {}", self.output.display());
2837
2838 Ok(())
2839 }
2840}
2841
2842#[cfg(test)]
2843mod tests {
2844 use super::*;
2845 use tempfile::tempdir;
2846
2847 #[test]
2848 fn test_parse_duration() {
2849 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
2850 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
2851 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
2852 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
2853 }
2854
2855 #[test]
2856 fn test_parse_duration_invalid() {
2857 assert!(BenchCommand::parse_duration("invalid").is_err());
2858 assert!(BenchCommand::parse_duration("30x").is_err());
2859 }
2860
2861 #[test]
2862 fn test_parse_headers() {
2863 let cmd = BenchCommand {
2864 spec: vec![PathBuf::from("test.yaml")],
2865 spec_dir: None,
2866 merge_conflicts: "error".to_string(),
2867 spec_mode: "merge".to_string(),
2868 dependency_config: None,
2869 target: "http://localhost".to_string(),
2870 base_path: None,
2871 duration: "1m".to_string(),
2872 vus: 10,
2873 scenario: "ramp-up".to_string(),
2874 operations: None,
2875 exclude_operations: None,
2876 auth: None,
2877 headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
2878 output: PathBuf::from("output"),
2879 generate_only: false,
2880 script_output: None,
2881 threshold_percentile: "p(95)".to_string(),
2882 threshold_ms: 500,
2883 max_error_rate: 0.05,
2884 verbose: false,
2885 skip_tls_verify: false,
2886 chunked_request_bodies: false,
2887 target_rps: None,
2888 no_keep_alive: false,
2889 targets_file: None,
2890 max_concurrency: None,
2891 results_format: "both".to_string(),
2892 params_file: None,
2893 crud_flow: false,
2894 flow_config: None,
2895 extract_fields: None,
2896 parallel_create: None,
2897 data_file: None,
2898 data_distribution: "unique-per-vu".to_string(),
2899 data_mappings: None,
2900 per_uri_control: false,
2901 error_rate: None,
2902 error_types: None,
2903 security_test: false,
2904 security_payloads: None,
2905 security_categories: None,
2906 security_target_fields: None,
2907 wafbench_dir: None,
2908 wafbench_cycle_all: false,
2909 owasp_api_top10: false,
2910 owasp_categories: None,
2911 owasp_auth_header: "Authorization".to_string(),
2912 owasp_auth_token: None,
2913 owasp_admin_paths: None,
2914 owasp_id_fields: None,
2915 owasp_report: None,
2916 owasp_report_format: "json".to_string(),
2917 owasp_iterations: 1,
2918 conformance: false,
2919 conformance_api_key: None,
2920 conformance_basic_auth: None,
2921 conformance_report: PathBuf::from("conformance-report.json"),
2922 conformance_categories: None,
2923 conformance_report_format: "json".to_string(),
2924 conformance_headers: vec![],
2925 conformance_all_operations: false,
2926 conformance_custom: None,
2927 conformance_delay_ms: 0,
2928 use_k6: false,
2929 conformance_custom_filter: None,
2930 export_requests: false,
2931 validate_requests: false,
2932 };
2933
2934 let headers = cmd.parse_headers().unwrap();
2935 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
2936 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
2937 }
2938
2939 #[test]
2940 fn test_get_spec_display_name() {
2941 let cmd = BenchCommand {
2942 spec: vec![PathBuf::from("test.yaml")],
2943 spec_dir: None,
2944 merge_conflicts: "error".to_string(),
2945 spec_mode: "merge".to_string(),
2946 dependency_config: None,
2947 target: "http://localhost".to_string(),
2948 base_path: None,
2949 duration: "1m".to_string(),
2950 vus: 10,
2951 scenario: "ramp-up".to_string(),
2952 operations: None,
2953 exclude_operations: None,
2954 auth: None,
2955 headers: None,
2956 output: PathBuf::from("output"),
2957 generate_only: false,
2958 script_output: None,
2959 threshold_percentile: "p(95)".to_string(),
2960 threshold_ms: 500,
2961 max_error_rate: 0.05,
2962 verbose: false,
2963 skip_tls_verify: false,
2964 chunked_request_bodies: false,
2965 target_rps: None,
2966 no_keep_alive: false,
2967 targets_file: None,
2968 max_concurrency: None,
2969 results_format: "both".to_string(),
2970 params_file: None,
2971 crud_flow: false,
2972 flow_config: None,
2973 extract_fields: None,
2974 parallel_create: None,
2975 data_file: None,
2976 data_distribution: "unique-per-vu".to_string(),
2977 data_mappings: None,
2978 per_uri_control: false,
2979 error_rate: None,
2980 error_types: None,
2981 security_test: false,
2982 security_payloads: None,
2983 security_categories: None,
2984 security_target_fields: None,
2985 wafbench_dir: None,
2986 wafbench_cycle_all: false,
2987 owasp_api_top10: false,
2988 owasp_categories: None,
2989 owasp_auth_header: "Authorization".to_string(),
2990 owasp_auth_token: None,
2991 owasp_admin_paths: None,
2992 owasp_id_fields: None,
2993 owasp_report: None,
2994 owasp_report_format: "json".to_string(),
2995 owasp_iterations: 1,
2996 conformance: false,
2997 conformance_api_key: None,
2998 conformance_basic_auth: None,
2999 conformance_report: PathBuf::from("conformance-report.json"),
3000 conformance_categories: None,
3001 conformance_report_format: "json".to_string(),
3002 conformance_headers: vec![],
3003 conformance_all_operations: false,
3004 conformance_custom: None,
3005 conformance_delay_ms: 0,
3006 use_k6: false,
3007 conformance_custom_filter: None,
3008 export_requests: false,
3009 validate_requests: false,
3010 };
3011
3012 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
3013
3014 let cmd_multi = BenchCommand {
3016 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
3017 spec_dir: None,
3018 merge_conflicts: "error".to_string(),
3019 spec_mode: "merge".to_string(),
3020 dependency_config: None,
3021 target: "http://localhost".to_string(),
3022 base_path: None,
3023 duration: "1m".to_string(),
3024 vus: 10,
3025 scenario: "ramp-up".to_string(),
3026 operations: None,
3027 exclude_operations: None,
3028 auth: None,
3029 headers: None,
3030 output: PathBuf::from("output"),
3031 generate_only: false,
3032 script_output: None,
3033 threshold_percentile: "p(95)".to_string(),
3034 threshold_ms: 500,
3035 max_error_rate: 0.05,
3036 verbose: false,
3037 skip_tls_verify: false,
3038 chunked_request_bodies: false,
3039 target_rps: None,
3040 no_keep_alive: false,
3041 targets_file: None,
3042 max_concurrency: None,
3043 results_format: "both".to_string(),
3044 params_file: None,
3045 crud_flow: false,
3046 flow_config: None,
3047 extract_fields: None,
3048 parallel_create: None,
3049 data_file: None,
3050 data_distribution: "unique-per-vu".to_string(),
3051 data_mappings: None,
3052 per_uri_control: false,
3053 error_rate: None,
3054 error_types: None,
3055 security_test: false,
3056 security_payloads: None,
3057 security_categories: None,
3058 security_target_fields: None,
3059 wafbench_dir: None,
3060 wafbench_cycle_all: false,
3061 owasp_api_top10: false,
3062 owasp_categories: None,
3063 owasp_auth_header: "Authorization".to_string(),
3064 owasp_auth_token: None,
3065 owasp_admin_paths: None,
3066 owasp_id_fields: None,
3067 owasp_report: None,
3068 owasp_report_format: "json".to_string(),
3069 owasp_iterations: 1,
3070 conformance: false,
3071 conformance_api_key: None,
3072 conformance_basic_auth: None,
3073 conformance_report: PathBuf::from("conformance-report.json"),
3074 conformance_categories: None,
3075 conformance_report_format: "json".to_string(),
3076 conformance_headers: vec![],
3077 conformance_all_operations: false,
3078 conformance_custom: None,
3079 conformance_delay_ms: 0,
3080 use_k6: false,
3081 conformance_custom_filter: None,
3082 export_requests: false,
3083 validate_requests: false,
3084 };
3085
3086 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
3087 }
3088
3089 #[test]
3090 fn test_parse_extracted_values_from_output_dir() {
3091 let dir = tempdir().unwrap();
3092 let path = dir.path().join("extracted_values.json");
3093 std::fs::write(
3094 &path,
3095 r#"{
3096 "pool_id": "abc123",
3097 "count": 0,
3098 "enabled": false,
3099 "metadata": { "owner": "team-a" }
3100}"#,
3101 )
3102 .unwrap();
3103
3104 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3105 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
3106 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
3107 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
3108 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
3109 }
3110
3111 #[test]
3112 fn test_parse_extracted_values_missing_file() {
3113 let dir = tempdir().unwrap();
3114 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
3115 assert!(extracted.values.is_empty());
3116 }
3117}