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_core::openapi::multi_spec::{
30 load_specs_from_directory, load_specs_from_files, merge_specs, ConflictStrategy,
31};
32use mockforge_core::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 scenario: String,
80 pub operations: Option<String>,
81 pub exclude_operations: Option<String>,
85 pub auth: Option<String>,
86 pub headers: Option<String>,
87 pub output: PathBuf,
88 pub generate_only: bool,
89 pub script_output: Option<PathBuf>,
90 pub threshold_percentile: String,
91 pub threshold_ms: u64,
92 pub max_error_rate: f64,
93 pub verbose: bool,
94 pub skip_tls_verify: bool,
95 pub targets_file: Option<PathBuf>,
97 pub max_concurrency: Option<u32>,
99 pub results_format: String,
101 pub params_file: Option<PathBuf>,
106
107 pub crud_flow: bool,
110 pub flow_config: Option<PathBuf>,
112 pub extract_fields: Option<String>,
114
115 pub parallel_create: Option<u32>,
118
119 pub data_file: Option<PathBuf>,
122 pub data_distribution: String,
124 pub data_mappings: Option<String>,
126 pub per_uri_control: bool,
128
129 pub error_rate: Option<f64>,
132 pub error_types: Option<String>,
134
135 pub security_test: bool,
138 pub security_payloads: Option<PathBuf>,
140 pub security_categories: Option<String>,
142 pub security_target_fields: Option<String>,
144
145 pub wafbench_dir: Option<String>,
148 pub wafbench_cycle_all: bool,
150
151 pub conformance: bool,
154 pub conformance_api_key: Option<String>,
156 pub conformance_basic_auth: Option<String>,
158 pub conformance_report: PathBuf,
160 pub conformance_categories: Option<String>,
162 pub conformance_report_format: String,
164 pub conformance_headers: Vec<String>,
167 pub conformance_all_operations: bool,
170 pub conformance_custom: Option<PathBuf>,
172 pub conformance_delay_ms: u64,
175 pub use_k6: bool,
177
178 pub owasp_api_top10: bool,
181 pub owasp_categories: Option<String>,
183 pub owasp_auth_header: String,
185 pub owasp_auth_token: Option<String>,
187 pub owasp_admin_paths: Option<PathBuf>,
189 pub owasp_id_fields: Option<String>,
191 pub owasp_report: Option<PathBuf>,
193 pub owasp_report_format: String,
195 pub owasp_iterations: u32,
197}
198
199impl BenchCommand {
200 pub async fn load_and_merge_specs(&self) -> Result<OpenApiSpec> {
202 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
203
204 if !self.spec.is_empty() {
206 let specs = load_specs_from_files(self.spec.clone())
207 .await
208 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
209 all_specs.extend(specs);
210 }
211
212 if let Some(spec_dir) = &self.spec_dir {
214 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
215 BenchError::Other(format!("Failed to load specs from directory: {}", e))
216 })?;
217 all_specs.extend(dir_specs);
218 }
219
220 if all_specs.is_empty() {
221 return Err(BenchError::Other(
222 "No spec files provided. Use --spec or --spec-dir.".to_string(),
223 ));
224 }
225
226 if all_specs.len() == 1 {
228 return Ok(all_specs.into_iter().next().expect("checked len() == 1 above").1);
230 }
231
232 let conflict_strategy = match self.merge_conflicts.as_str() {
234 "first" => ConflictStrategy::First,
235 "last" => ConflictStrategy::Last,
236 _ => ConflictStrategy::Error,
237 };
238
239 merge_specs(all_specs, conflict_strategy)
240 .map_err(|e| BenchError::Other(format!("Failed to merge specs: {}", e)))
241 }
242
243 fn get_spec_display_name(&self) -> String {
245 if self.spec.len() == 1 {
246 self.spec[0].to_string_lossy().to_string()
247 } else if !self.spec.is_empty() {
248 format!("{} spec files", self.spec.len())
249 } else if let Some(dir) = &self.spec_dir {
250 format!("specs from {}", dir.display())
251 } else {
252 "no specs".to_string()
253 }
254 }
255
256 pub async fn execute(&self) -> Result<()> {
258 if let Some(targets_file) = &self.targets_file {
260 if self.conformance {
261 return self.execute_multi_target_conformance(targets_file).await;
262 }
263 return self.execute_multi_target(targets_file).await;
264 }
265
266 if self.spec_mode == "sequential" && (self.spec.len() > 1 || self.spec_dir.is_some()) {
268 return self.execute_sequential_specs().await;
269 }
270
271 TerminalReporter::print_header(
274 &self.get_spec_display_name(),
275 &self.target,
276 0, &self.scenario,
278 Self::parse_duration(&self.duration)?,
279 );
280
281 if !K6Executor::is_k6_installed() {
283 TerminalReporter::print_error("k6 is not installed");
284 TerminalReporter::print_warning(
285 "Install k6 from: https://k6.io/docs/get-started/installation/",
286 );
287 return Err(BenchError::K6NotFound);
288 }
289
290 if self.conformance {
292 return self.execute_conformance_test().await;
293 }
294
295 TerminalReporter::print_progress("Loading OpenAPI specification(s)...");
297 let merged_spec = self.load_and_merge_specs().await?;
298 let parser = SpecParser::from_spec(merged_spec);
299 if self.spec.len() > 1 || self.spec_dir.is_some() {
300 TerminalReporter::print_success(&format!(
301 "Loaded and merged {} specification(s)",
302 self.spec.len() + self.spec_dir.as_ref().map(|_| 1).unwrap_or(0)
303 ));
304 } else {
305 TerminalReporter::print_success("Specification loaded");
306 }
307
308 let mock_config = self.build_mock_config().await;
310 if mock_config.is_mock_server {
311 TerminalReporter::print_progress("Mock server integration enabled");
312 }
313
314 if self.crud_flow {
316 return self.execute_crud_flow(&parser).await;
317 }
318
319 if self.owasp_api_top10 {
321 return self.execute_owasp_test(&parser).await;
322 }
323
324 TerminalReporter::print_progress("Extracting API operations...");
326 let mut operations = if let Some(filter) = &self.operations {
327 parser.filter_operations(filter)?
328 } else {
329 parser.get_operations()
330 };
331
332 if let Some(exclude) = &self.exclude_operations {
334 let before_count = operations.len();
335 operations = parser.exclude_operations(operations, exclude)?;
336 let excluded_count = before_count - operations.len();
337 if excluded_count > 0 {
338 TerminalReporter::print_progress(&format!(
339 "Excluded {} operations matching '{}'",
340 excluded_count, exclude
341 ));
342 }
343 }
344
345 if operations.is_empty() {
346 return Err(BenchError::Other("No operations found in spec".to_string()));
347 }
348
349 TerminalReporter::print_success(&format!("Found {} operations", operations.len()));
350
351 let param_overrides = if let Some(params_file) = &self.params_file {
353 TerminalReporter::print_progress("Loading parameter overrides...");
354 let overrides = ParameterOverrides::from_file(params_file)?;
355 TerminalReporter::print_success(&format!(
356 "Loaded parameter overrides ({} operation-specific, {} defaults)",
357 overrides.operations.len(),
358 if overrides.defaults.is_empty() { 0 } else { 1 }
359 ));
360 Some(overrides)
361 } else {
362 None
363 };
364
365 TerminalReporter::print_progress("Generating request templates...");
367 let templates: Vec<_> = operations
368 .iter()
369 .map(|op| {
370 let op_overrides = param_overrides.as_ref().map(|po| {
371 po.get_for_operation(op.operation_id.as_deref(), &op.method, &op.path)
372 });
373 RequestGenerator::generate_template_with_overrides(op, op_overrides.as_ref())
374 })
375 .collect::<Result<Vec<_>>>()?;
376 TerminalReporter::print_success("Request templates generated");
377
378 let custom_headers = self.parse_headers()?;
380
381 let base_path = self.resolve_base_path(&parser);
383 if let Some(ref bp) = base_path {
384 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
385 }
386
387 TerminalReporter::print_progress("Generating k6 load test script...");
389 let scenario =
390 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
391
392 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
393
394 let k6_config = K6Config {
395 target_url: self.target.clone(),
396 base_path,
397 scenario,
398 duration_secs: Self::parse_duration(&self.duration)?,
399 max_vus: self.vus,
400 threshold_percentile: self.threshold_percentile.clone(),
401 threshold_ms: self.threshold_ms,
402 max_error_rate: self.max_error_rate,
403 auth_header: self.auth.clone(),
404 custom_headers,
405 skip_tls_verify: self.skip_tls_verify,
406 security_testing_enabled,
407 };
408
409 let generator = K6ScriptGenerator::new(k6_config, templates);
410 let mut script = generator.generate()?;
411 TerminalReporter::print_success("k6 script generated");
412
413 let has_advanced_features = self.data_file.is_some()
415 || self.error_rate.is_some()
416 || self.security_test
417 || self.parallel_create.is_some()
418 || self.wafbench_dir.is_some();
419
420 if has_advanced_features {
422 script = self.generate_enhanced_script(&script)?;
423 }
424
425 if mock_config.is_mock_server {
427 let setup_code = MockIntegrationGenerator::generate_setup(&mock_config);
428 let teardown_code = MockIntegrationGenerator::generate_teardown(&mock_config);
429 let helper_code = MockIntegrationGenerator::generate_vu_id_helper();
430
431 if let Some(import_end) = script.find("export const options") {
433 script.insert_str(
434 import_end,
435 &format!(
436 "\n// === Mock Server Integration ===\n{}\n{}\n{}\n",
437 helper_code, setup_code, teardown_code
438 ),
439 );
440 }
441 }
442
443 TerminalReporter::print_progress("Validating k6 script...");
445 let validation_errors = K6ScriptGenerator::validate_script(&script);
446 if !validation_errors.is_empty() {
447 TerminalReporter::print_error("Script validation failed");
448 for error in &validation_errors {
449 eprintln!(" {}", error);
450 }
451 return Err(BenchError::Other(format!(
452 "Generated k6 script has {} validation error(s). Please check the output above.",
453 validation_errors.len()
454 )));
455 }
456 TerminalReporter::print_success("Script validation passed");
457
458 let script_path = if let Some(output) = &self.script_output {
460 output.clone()
461 } else {
462 self.output.join("k6-script.js")
463 };
464
465 if let Some(parent) = script_path.parent() {
466 std::fs::create_dir_all(parent)?;
467 }
468 std::fs::write(&script_path, &script)?;
469 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
470
471 if self.generate_only {
473 println!("\nScript generated successfully. Run it with:");
474 println!(" k6 run {}", script_path.display());
475 return Ok(());
476 }
477
478 TerminalReporter::print_progress("Executing load test...");
480 let executor = K6Executor::new()?;
481
482 std::fs::create_dir_all(&self.output)?;
483
484 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
485
486 let duration_secs = Self::parse_duration(&self.duration)?;
488 TerminalReporter::print_summary(&results, duration_secs);
489
490 println!("\nResults saved to: {}", self.output.display());
491
492 Ok(())
493 }
494
495 async fn execute_multi_target(&self, targets_file: &Path) -> Result<()> {
497 TerminalReporter::print_progress("Parsing targets file...");
498 let targets = parse_targets_file(targets_file)?;
499 let num_targets = targets.len();
500 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
501
502 if targets.is_empty() {
503 return Err(BenchError::Other("No targets found in file".to_string()));
504 }
505
506 let max_concurrency = self.max_concurrency.unwrap_or(10) as usize;
508 let max_concurrency = max_concurrency.min(num_targets); TerminalReporter::print_header(
512 &self.get_spec_display_name(),
513 &format!("{} targets", num_targets),
514 0,
515 &self.scenario,
516 Self::parse_duration(&self.duration)?,
517 );
518
519 let executor = ParallelExecutor::new(
521 BenchCommand {
522 spec: self.spec.clone(),
524 spec_dir: self.spec_dir.clone(),
525 merge_conflicts: self.merge_conflicts.clone(),
526 spec_mode: self.spec_mode.clone(),
527 dependency_config: self.dependency_config.clone(),
528 target: self.target.clone(), base_path: self.base_path.clone(),
530 duration: self.duration.clone(),
531 vus: self.vus,
532 scenario: self.scenario.clone(),
533 operations: self.operations.clone(),
534 exclude_operations: self.exclude_operations.clone(),
535 auth: self.auth.clone(),
536 headers: self.headers.clone(),
537 output: self.output.clone(),
538 generate_only: self.generate_only,
539 script_output: self.script_output.clone(),
540 threshold_percentile: self.threshold_percentile.clone(),
541 threshold_ms: self.threshold_ms,
542 max_error_rate: self.max_error_rate,
543 verbose: self.verbose,
544 skip_tls_verify: self.skip_tls_verify,
545 targets_file: None,
546 max_concurrency: None,
547 results_format: self.results_format.clone(),
548 params_file: self.params_file.clone(),
549 crud_flow: self.crud_flow,
550 flow_config: self.flow_config.clone(),
551 extract_fields: self.extract_fields.clone(),
552 parallel_create: self.parallel_create,
553 data_file: self.data_file.clone(),
554 data_distribution: self.data_distribution.clone(),
555 data_mappings: self.data_mappings.clone(),
556 per_uri_control: self.per_uri_control,
557 error_rate: self.error_rate,
558 error_types: self.error_types.clone(),
559 security_test: self.security_test,
560 security_payloads: self.security_payloads.clone(),
561 security_categories: self.security_categories.clone(),
562 security_target_fields: self.security_target_fields.clone(),
563 wafbench_dir: self.wafbench_dir.clone(),
564 wafbench_cycle_all: self.wafbench_cycle_all,
565 owasp_api_top10: self.owasp_api_top10,
566 owasp_categories: self.owasp_categories.clone(),
567 owasp_auth_header: self.owasp_auth_header.clone(),
568 owasp_auth_token: self.owasp_auth_token.clone(),
569 owasp_admin_paths: self.owasp_admin_paths.clone(),
570 owasp_id_fields: self.owasp_id_fields.clone(),
571 owasp_report: self.owasp_report.clone(),
572 owasp_report_format: self.owasp_report_format.clone(),
573 owasp_iterations: self.owasp_iterations,
574 conformance: false,
575 conformance_api_key: None,
576 conformance_basic_auth: None,
577 conformance_report: PathBuf::from("conformance-report.json"),
578 conformance_categories: None,
579 conformance_report_format: "json".to_string(),
580 conformance_headers: vec![],
581 conformance_all_operations: false,
582 conformance_custom: None,
583 conformance_delay_ms: 0,
584 use_k6: false,
585 },
586 targets,
587 max_concurrency,
588 );
589
590 let start_time = std::time::Instant::now();
592 let aggregated_results = executor.execute_all().await?;
593 let elapsed = start_time.elapsed();
594
595 self.report_multi_target_results(&aggregated_results, elapsed)?;
597
598 Ok(())
599 }
600
601 fn report_multi_target_results(
603 &self,
604 results: &AggregatedResults,
605 elapsed: std::time::Duration,
606 ) -> Result<()> {
607 TerminalReporter::print_multi_target_summary(results);
609
610 let total_secs = elapsed.as_secs();
612 let hours = total_secs / 3600;
613 let minutes = (total_secs % 3600) / 60;
614 let seconds = total_secs % 60;
615 if hours > 0 {
616 println!("\n Total Elapsed Time: {}h {}m {}s", hours, minutes, seconds);
617 } else if minutes > 0 {
618 println!("\n Total Elapsed Time: {}m {}s", minutes, seconds);
619 } else {
620 println!("\n Total Elapsed Time: {}s", seconds);
621 }
622
623 if self.results_format == "aggregated" || self.results_format == "both" {
625 let summary_path = self.output.join("aggregated_summary.json");
626 let summary_json = serde_json::json!({
627 "total_elapsed_seconds": elapsed.as_secs(),
628 "total_targets": results.total_targets,
629 "successful_targets": results.successful_targets,
630 "failed_targets": results.failed_targets,
631 "aggregated_metrics": {
632 "total_requests": results.aggregated_metrics.total_requests,
633 "total_failed_requests": results.aggregated_metrics.total_failed_requests,
634 "avg_duration_ms": results.aggregated_metrics.avg_duration_ms,
635 "p95_duration_ms": results.aggregated_metrics.p95_duration_ms,
636 "p99_duration_ms": results.aggregated_metrics.p99_duration_ms,
637 "error_rate": results.aggregated_metrics.error_rate,
638 "total_rps": results.aggregated_metrics.total_rps,
639 "avg_rps": results.aggregated_metrics.avg_rps,
640 "total_vus_max": results.aggregated_metrics.total_vus_max,
641 },
642 "target_results": results.target_results.iter().map(|r| {
643 serde_json::json!({
644 "target_url": r.target_url,
645 "target_index": r.target_index,
646 "success": r.success,
647 "error": r.error,
648 "total_requests": r.results.total_requests,
649 "failed_requests": r.results.failed_requests,
650 "avg_duration_ms": r.results.avg_duration_ms,
651 "min_duration_ms": r.results.min_duration_ms,
652 "med_duration_ms": r.results.med_duration_ms,
653 "p90_duration_ms": r.results.p90_duration_ms,
654 "p95_duration_ms": r.results.p95_duration_ms,
655 "p99_duration_ms": r.results.p99_duration_ms,
656 "max_duration_ms": r.results.max_duration_ms,
657 "rps": r.results.rps,
658 "vus_max": r.results.vus_max,
659 "output_dir": r.output_dir.to_string_lossy(),
660 })
661 }).collect::<Vec<_>>(),
662 });
663
664 std::fs::write(&summary_path, serde_json::to_string_pretty(&summary_json)?)?;
665 TerminalReporter::print_success(&format!(
666 "Aggregated summary saved to: {}",
667 summary_path.display()
668 ));
669 }
670
671 let csv_path = self.output.join("all_targets.csv");
673 let mut csv = String::from(
674 "target_url,success,requests,failed,rps,vus,min_ms,avg_ms,med_ms,p90_ms,p95_ms,p99_ms,max_ms,error\n",
675 );
676 for r in &results.target_results {
677 csv.push_str(&format!(
678 "{},{},{},{},{:.1},{},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{:.1},{}\n",
679 r.target_url,
680 r.success,
681 r.results.total_requests,
682 r.results.failed_requests,
683 r.results.rps,
684 r.results.vus_max,
685 r.results.min_duration_ms,
686 r.results.avg_duration_ms,
687 r.results.med_duration_ms,
688 r.results.p90_duration_ms,
689 r.results.p95_duration_ms,
690 r.results.p99_duration_ms,
691 r.results.max_duration_ms,
692 r.error.as_deref().unwrap_or(""),
693 ));
694 }
695 let _ = std::fs::write(&csv_path, &csv);
696
697 println!("\nResults saved to: {}", self.output.display());
698 println!(" - Per-target results: {}", self.output.join("target_*").display());
699 println!(" - All targets CSV: {}", csv_path.display());
700 if self.results_format == "aggregated" || self.results_format == "both" {
701 println!(
702 " - Aggregated summary: {}",
703 self.output.join("aggregated_summary.json").display()
704 );
705 }
706
707 Ok(())
708 }
709
710 pub fn parse_duration(duration: &str) -> Result<u64> {
712 let duration = duration.trim();
713
714 if let Some(secs) = duration.strip_suffix('s') {
715 secs.parse::<u64>()
716 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
717 } else if let Some(mins) = duration.strip_suffix('m') {
718 mins.parse::<u64>()
719 .map(|m| m * 60)
720 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
721 } else if let Some(hours) = duration.strip_suffix('h') {
722 hours
723 .parse::<u64>()
724 .map(|h| h * 3600)
725 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
726 } else {
727 duration
729 .parse::<u64>()
730 .map_err(|_| BenchError::Other(format!("Invalid duration: {}", duration)))
731 }
732 }
733
734 pub fn parse_headers(&self) -> Result<HashMap<String, String>> {
736 match &self.headers {
737 Some(s) => parse_header_string(s),
738 None => Ok(HashMap::new()),
739 }
740 }
741
742 fn parse_extracted_values(output_dir: &Path) -> Result<ExtractedValues> {
743 let extracted_path = output_dir.join("extracted_values.json");
744 if !extracted_path.exists() {
745 return Ok(ExtractedValues::new());
746 }
747
748 let content = std::fs::read_to_string(&extracted_path)
749 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
750 let parsed: serde_json::Value = serde_json::from_str(&content)
751 .map_err(|e| BenchError::ResultsParseError(e.to_string()))?;
752
753 let mut extracted = ExtractedValues::new();
754 if let Some(values) = parsed.as_object() {
755 for (key, value) in values {
756 extracted.set(key.clone(), value.clone());
757 }
758 }
759
760 Ok(extracted)
761 }
762
763 fn resolve_base_path(&self, parser: &SpecParser) -> Option<String> {
772 if let Some(cli_base_path) = &self.base_path {
774 if cli_base_path.is_empty() {
775 return None;
777 }
778 return Some(cli_base_path.clone());
779 }
780
781 parser.get_base_path()
783 }
784
785 async fn build_mock_config(&self) -> MockIntegrationConfig {
787 if MockServerDetector::looks_like_mock_server(&self.target) {
789 if let Ok(info) = MockServerDetector::detect(&self.target).await {
791 if info.is_mockforge {
792 TerminalReporter::print_success(&format!(
793 "Detected MockForge server (version: {})",
794 info.version.as_deref().unwrap_or("unknown")
795 ));
796 return MockIntegrationConfig::mock_server();
797 }
798 }
799 }
800 MockIntegrationConfig::real_api()
801 }
802
803 fn build_crud_flow_config(&self) -> Option<CrudFlowConfig> {
805 if !self.crud_flow {
806 return None;
807 }
808
809 if let Some(config_path) = &self.flow_config {
811 match CrudFlowConfig::from_file(config_path) {
812 Ok(config) => return Some(config),
813 Err(e) => {
814 TerminalReporter::print_warning(&format!(
815 "Failed to load flow config: {}. Using auto-detection.",
816 e
817 ));
818 }
819 }
820 }
821
822 let extract_fields = self
824 .extract_fields
825 .as_ref()
826 .map(|f| f.split(',').map(|s| s.trim().to_string()).collect())
827 .unwrap_or_else(|| vec!["id".to_string(), "uuid".to_string()]);
828
829 Some(CrudFlowConfig {
830 flows: Vec::new(), default_extract_fields: extract_fields,
832 })
833 }
834
835 fn build_data_driven_config(&self) -> Option<DataDrivenConfig> {
837 let data_file = self.data_file.as_ref()?;
838
839 let distribution = DataDistribution::from_str(&self.data_distribution)
840 .unwrap_or(DataDistribution::UniquePerVu);
841
842 let mappings = self
843 .data_mappings
844 .as_ref()
845 .map(|m| DataMapping::parse_mappings(m).unwrap_or_default())
846 .unwrap_or_default();
847
848 Some(DataDrivenConfig {
849 file_path: data_file.to_string_lossy().to_string(),
850 distribution,
851 mappings,
852 csv_has_header: true,
853 per_uri_control: self.per_uri_control,
854 per_uri_columns: crate::data_driven::PerUriColumns::default(),
855 })
856 }
857
858 fn build_invalid_data_config(&self) -> Option<InvalidDataConfig> {
860 let error_rate = self.error_rate?;
861
862 let error_types = self
863 .error_types
864 .as_ref()
865 .map(|types| InvalidDataConfig::parse_error_types(types).unwrap_or_default())
866 .unwrap_or_default();
867
868 Some(InvalidDataConfig {
869 error_rate,
870 error_types,
871 target_fields: Vec::new(),
872 })
873 }
874
875 fn build_security_config(&self) -> Option<SecurityTestConfig> {
877 if !self.security_test {
878 return None;
879 }
880
881 let categories = self
882 .security_categories
883 .as_ref()
884 .map(|cats| SecurityTestConfig::parse_categories(cats).unwrap_or_default())
885 .unwrap_or_else(|| {
886 let mut default = HashSet::new();
887 default.insert(SecurityCategory::SqlInjection);
888 default.insert(SecurityCategory::Xss);
889 default
890 });
891
892 let target_fields = self
893 .security_target_fields
894 .as_ref()
895 .map(|fields| fields.split(',').map(|f| f.trim().to_string()).collect())
896 .unwrap_or_default();
897
898 let custom_payloads_file =
899 self.security_payloads.as_ref().map(|p| p.to_string_lossy().to_string());
900
901 Some(SecurityTestConfig {
902 enabled: true,
903 categories,
904 target_fields,
905 custom_payloads_file,
906 include_high_risk: false,
907 })
908 }
909
910 fn build_parallel_config(&self) -> Option<ParallelConfig> {
912 let count = self.parallel_create?;
913
914 Some(ParallelConfig::new(count))
915 }
916
917 fn load_wafbench_payloads(&self) -> Vec<SecurityPayload> {
919 let Some(ref wafbench_dir) = self.wafbench_dir else {
920 return Vec::new();
921 };
922
923 let mut loader = WafBenchLoader::new();
924
925 if let Err(e) = loader.load_from_pattern(wafbench_dir) {
926 TerminalReporter::print_warning(&format!("Failed to load WAFBench tests: {}", e));
927 return Vec::new();
928 }
929
930 let stats = loader.stats();
931
932 if stats.files_processed == 0 {
933 TerminalReporter::print_warning(&format!(
934 "No WAFBench YAML files found matching '{}'",
935 wafbench_dir
936 ));
937 if !stats.parse_errors.is_empty() {
939 TerminalReporter::print_warning("Some files were found but failed to parse:");
940 for error in &stats.parse_errors {
941 TerminalReporter::print_warning(&format!(" - {}", error));
942 }
943 }
944 return Vec::new();
945 }
946
947 TerminalReporter::print_progress(&format!(
948 "Loaded {} WAFBench files, {} test cases, {} payloads",
949 stats.files_processed, stats.test_cases_loaded, stats.payloads_extracted
950 ));
951
952 for (category, count) in &stats.by_category {
954 TerminalReporter::print_progress(&format!(" - {}: {} tests", category, count));
955 }
956
957 for error in &stats.parse_errors {
959 TerminalReporter::print_warning(&format!(" Parse error: {}", error));
960 }
961
962 loader.to_security_payloads()
963 }
964
965 pub(crate) fn generate_enhanced_script(&self, base_script: &str) -> Result<String> {
967 let mut enhanced_script = base_script.to_string();
968 let mut additional_code = String::new();
969
970 if let Some(config) = self.build_data_driven_config() {
972 TerminalReporter::print_progress("Adding data-driven testing support...");
973 additional_code.push_str(&DataDrivenGenerator::generate_setup(&config));
974 additional_code.push('\n');
975 TerminalReporter::print_success("Data-driven testing enabled");
976 }
977
978 if let Some(config) = self.build_invalid_data_config() {
980 TerminalReporter::print_progress("Adding invalid data testing support...");
981 additional_code.push_str(&InvalidDataGenerator::generate_invalidation_logic());
982 additional_code.push('\n');
983 additional_code
984 .push_str(&InvalidDataGenerator::generate_should_invalidate(config.error_rate));
985 additional_code.push('\n');
986 additional_code
987 .push_str(&InvalidDataGenerator::generate_type_selection(&config.error_types));
988 additional_code.push('\n');
989 TerminalReporter::print_success(&format!(
990 "Invalid data testing enabled ({}% error rate)",
991 (self.error_rate.unwrap_or(0.0) * 100.0) as u32
992 ));
993 }
994
995 let security_config = self.build_security_config();
997 let wafbench_payloads = self.load_wafbench_payloads();
998 let security_requested = security_config.is_some() || self.wafbench_dir.is_some();
999
1000 if security_config.is_some() || !wafbench_payloads.is_empty() {
1001 TerminalReporter::print_progress("Adding security testing support...");
1002
1003 let mut payload_list: Vec<SecurityPayload> = Vec::new();
1005
1006 if let Some(ref config) = security_config {
1007 payload_list.extend(SecurityPayloads::get_payloads(config));
1008 }
1009
1010 if !wafbench_payloads.is_empty() {
1012 TerminalReporter::print_progress(&format!(
1013 "Loading {} WAFBench attack patterns...",
1014 wafbench_payloads.len()
1015 ));
1016 payload_list.extend(wafbench_payloads);
1017 }
1018
1019 let target_fields =
1020 security_config.as_ref().map(|c| c.target_fields.clone()).unwrap_or_default();
1021
1022 additional_code.push_str(&SecurityTestGenerator::generate_payload_selection(
1023 &payload_list,
1024 self.wafbench_cycle_all,
1025 ));
1026 additional_code.push('\n');
1027 additional_code
1028 .push_str(&SecurityTestGenerator::generate_apply_payload(&target_fields));
1029 additional_code.push('\n');
1030 additional_code.push_str(&SecurityTestGenerator::generate_security_checks());
1031 additional_code.push('\n');
1032
1033 let mode = if self.wafbench_cycle_all {
1034 "cycle-all"
1035 } else {
1036 "random"
1037 };
1038 TerminalReporter::print_success(&format!(
1039 "Security testing enabled ({} payloads, {} mode)",
1040 payload_list.len(),
1041 mode
1042 ));
1043 } else if security_requested {
1044 TerminalReporter::print_warning(
1048 "Security testing was requested but no payloads were loaded. \
1049 Ensure --wafbench-dir points to valid CRS YAML files or add --security-test.",
1050 );
1051 additional_code
1052 .push_str(&SecurityTestGenerator::generate_payload_selection(&[], false));
1053 additional_code.push('\n');
1054 additional_code.push_str(&SecurityTestGenerator::generate_apply_payload(&[]));
1055 additional_code.push('\n');
1056 }
1057
1058 if let Some(config) = self.build_parallel_config() {
1060 TerminalReporter::print_progress("Adding parallel execution support...");
1061 additional_code.push_str(&ParallelRequestGenerator::generate_batch_helper(&config));
1062 additional_code.push('\n');
1063 TerminalReporter::print_success(&format!(
1064 "Parallel execution enabled (count: {})",
1065 config.count
1066 ));
1067 }
1068
1069 if !additional_code.is_empty() {
1071 if let Some(import_end) = enhanced_script.find("export const options") {
1073 enhanced_script.insert_str(
1074 import_end,
1075 &format!("\n// === Advanced Testing Features ===\n{}\n", additional_code),
1076 );
1077 }
1078 }
1079
1080 Ok(enhanced_script)
1081 }
1082
1083 async fn execute_sequential_specs(&self) -> Result<()> {
1085 TerminalReporter::print_progress("Sequential spec mode: Loading specs individually...");
1086
1087 let mut all_specs: Vec<(PathBuf, OpenApiSpec)> = Vec::new();
1089
1090 if !self.spec.is_empty() {
1091 let specs = load_specs_from_files(self.spec.clone())
1092 .await
1093 .map_err(|e| BenchError::Other(format!("Failed to load spec files: {}", e)))?;
1094 all_specs.extend(specs);
1095 }
1096
1097 if let Some(spec_dir) = &self.spec_dir {
1098 let dir_specs = load_specs_from_directory(spec_dir).await.map_err(|e| {
1099 BenchError::Other(format!("Failed to load specs from directory: {}", e))
1100 })?;
1101 all_specs.extend(dir_specs);
1102 }
1103
1104 if all_specs.is_empty() {
1105 return Err(BenchError::Other(
1106 "No spec files found for sequential execution".to_string(),
1107 ));
1108 }
1109
1110 TerminalReporter::print_success(&format!("Loaded {} spec(s)", all_specs.len()));
1111
1112 let execution_order = if let Some(config_path) = &self.dependency_config {
1114 TerminalReporter::print_progress("Loading dependency configuration...");
1115 let config = SpecDependencyConfig::from_file(config_path)?;
1116
1117 if !config.disable_auto_detect && config.execution_order.is_empty() {
1118 self.detect_and_sort_specs(&all_specs)?
1120 } else {
1121 config.execution_order.iter().flat_map(|g| g.specs.clone()).collect()
1123 }
1124 } else {
1125 self.detect_and_sort_specs(&all_specs)?
1127 };
1128
1129 TerminalReporter::print_success(&format!(
1130 "Execution order: {}",
1131 execution_order
1132 .iter()
1133 .map(|p| p.file_name().unwrap_or_default().to_string_lossy().to_string())
1134 .collect::<Vec<_>>()
1135 .join(" → ")
1136 ));
1137
1138 let mut extracted_values = ExtractedValues::new();
1140 let total_specs = execution_order.len();
1141
1142 for (index, spec_path) in execution_order.iter().enumerate() {
1143 let spec_name = spec_path.file_name().unwrap_or_default().to_string_lossy().to_string();
1144
1145 TerminalReporter::print_progress(&format!(
1146 "[{}/{}] Executing spec: {}",
1147 index + 1,
1148 total_specs,
1149 spec_name
1150 ));
1151
1152 let spec = all_specs
1154 .iter()
1155 .find(|(p, _)| {
1156 p == spec_path
1157 || p.file_name() == spec_path.file_name()
1158 || p.file_name() == Some(spec_path.as_os_str())
1159 })
1160 .map(|(_, s)| s.clone())
1161 .ok_or_else(|| {
1162 BenchError::Other(format!("Spec not found: {}", spec_path.display()))
1163 })?;
1164
1165 let new_values = self.execute_single_spec(&spec, &spec_name, &extracted_values).await?;
1167
1168 extracted_values.merge(&new_values);
1170
1171 TerminalReporter::print_success(&format!(
1172 "[{}/{}] Completed: {} (extracted {} values)",
1173 index + 1,
1174 total_specs,
1175 spec_name,
1176 new_values.values.len()
1177 ));
1178 }
1179
1180 TerminalReporter::print_success(&format!(
1181 "Sequential execution complete: {} specs executed",
1182 total_specs
1183 ));
1184
1185 Ok(())
1186 }
1187
1188 fn detect_and_sort_specs(&self, specs: &[(PathBuf, OpenApiSpec)]) -> Result<Vec<PathBuf>> {
1190 TerminalReporter::print_progress("Auto-detecting spec dependencies...");
1191
1192 let mut detector = DependencyDetector::new();
1193 let dependencies = detector.detect_dependencies(specs);
1194
1195 if dependencies.is_empty() {
1196 TerminalReporter::print_progress("No dependencies detected, using file order");
1197 return Ok(specs.iter().map(|(p, _)| p.clone()).collect());
1198 }
1199
1200 TerminalReporter::print_progress(&format!(
1201 "Detected {} cross-spec dependencies",
1202 dependencies.len()
1203 ));
1204
1205 for dep in &dependencies {
1206 TerminalReporter::print_progress(&format!(
1207 " {} → {} (via field '{}')",
1208 dep.dependency_spec.file_name().unwrap_or_default().to_string_lossy(),
1209 dep.dependent_spec.file_name().unwrap_or_default().to_string_lossy(),
1210 dep.field_name
1211 ));
1212 }
1213
1214 topological_sort(specs, &dependencies)
1215 }
1216
1217 async fn execute_single_spec(
1219 &self,
1220 spec: &OpenApiSpec,
1221 spec_name: &str,
1222 _external_values: &ExtractedValues,
1223 ) -> Result<ExtractedValues> {
1224 let parser = SpecParser::from_spec(spec.clone());
1225
1226 if self.crud_flow {
1228 self.execute_crud_flow_with_extraction(&parser, spec_name).await
1230 } else {
1231 self.execute_standard_spec(&parser, spec_name).await?;
1233 Ok(ExtractedValues::new())
1234 }
1235 }
1236
1237 async fn execute_crud_flow_with_extraction(
1239 &self,
1240 parser: &SpecParser,
1241 spec_name: &str,
1242 ) -> Result<ExtractedValues> {
1243 let operations = parser.get_operations();
1244 let flows = CrudFlowDetector::detect_flows(&operations);
1245
1246 if flows.is_empty() {
1247 TerminalReporter::print_warning(&format!("No CRUD flows detected in {}", spec_name));
1248 return Ok(ExtractedValues::new());
1249 }
1250
1251 TerminalReporter::print_progress(&format!(
1252 " {} CRUD flow(s) in {}",
1253 flows.len(),
1254 spec_name
1255 ));
1256
1257 let mut handlebars = handlebars::Handlebars::new();
1259 handlebars.register_helper(
1261 "json",
1262 Box::new(
1263 |h: &handlebars::Helper,
1264 _: &handlebars::Handlebars,
1265 _: &handlebars::Context,
1266 _: &mut handlebars::RenderContext,
1267 out: &mut dyn handlebars::Output|
1268 -> handlebars::HelperResult {
1269 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1270 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1271 Ok(())
1272 },
1273 ),
1274 );
1275 let template = include_str!("templates/k6_crud_flow.hbs");
1276 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1277
1278 let custom_headers = self.parse_headers()?;
1279 let config = self.build_crud_flow_config().unwrap_or_default();
1280
1281 let param_overrides = if let Some(params_file) = &self.params_file {
1283 let overrides = ParameterOverrides::from_file(params_file)?;
1284 Some(overrides)
1285 } else {
1286 None
1287 };
1288
1289 let duration_secs = Self::parse_duration(&self.duration)?;
1291 let scenario =
1292 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1293 let stages = scenario.generate_stages(duration_secs, self.vus);
1294
1295 let api_base_path = self.resolve_base_path(parser);
1297
1298 let mut all_headers = custom_headers.clone();
1300 if let Some(auth) = &self.auth {
1301 all_headers.insert("Authorization".to_string(), auth.clone());
1302 }
1303 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1304
1305 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1307
1308 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1309 let sanitized_name = K6ScriptGenerator::sanitize_js_identifier(&f.name);
1310 serde_json::json!({
1311 "name": sanitized_name.clone(),
1312 "display_name": f.name,
1313 "base_path": f.base_path,
1314 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1315 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1317 let method_raw = if !parts.is_empty() {
1318 parts[0].to_uppercase()
1319 } else {
1320 "GET".to_string()
1321 };
1322 let method = if !parts.is_empty() {
1323 let m = parts[0].to_lowercase();
1324 if m == "delete" { "del".to_string() } else { m }
1326 } else {
1327 "get".to_string()
1328 };
1329 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1330 let path = if let Some(ref bp) = api_base_path {
1332 format!("{}{}", bp, raw_path)
1333 } else {
1334 raw_path.to_string()
1335 };
1336 let is_get_or_head = method == "get" || method == "head";
1337 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1339
1340 let body_value = if has_body {
1342 param_overrides.as_ref()
1343 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1344 .and_then(|oo| oo.body)
1345 .unwrap_or_else(|| serde_json::json!({}))
1346 } else {
1347 serde_json::json!({})
1348 };
1349
1350 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1352
1353 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1355 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1356
1357 serde_json::json!({
1358 "operation": s.operation,
1359 "method": method,
1360 "path": path,
1361 "extract": s.extract,
1362 "use_values": s.use_values,
1363 "use_body": s.use_body,
1364 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1365 "inject_attacks": s.inject_attacks,
1366 "attack_types": s.attack_types,
1367 "description": s.description,
1368 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1369 "is_get_or_head": is_get_or_head,
1370 "has_body": has_body,
1371 "body": processed_body.value,
1372 "body_is_dynamic": body_is_dynamic,
1373 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1374 })
1375 }).collect::<Vec<_>>(),
1376 })
1377 }).collect();
1378
1379 for flow_data in &flows_data {
1381 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1382 for step in steps {
1383 if let Some(placeholders_arr) =
1384 step.get("_placeholders").and_then(|p| p.as_array())
1385 {
1386 for p_str in placeholders_arr {
1387 if let Some(p_name) = p_str.as_str() {
1388 match p_name {
1389 "VU" => {
1390 all_placeholders.insert(DynamicPlaceholder::VU);
1391 }
1392 "Iteration" => {
1393 all_placeholders.insert(DynamicPlaceholder::Iteration);
1394 }
1395 "Timestamp" => {
1396 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1397 }
1398 "UUID" => {
1399 all_placeholders.insert(DynamicPlaceholder::UUID);
1400 }
1401 "Random" => {
1402 all_placeholders.insert(DynamicPlaceholder::Random);
1403 }
1404 "Counter" => {
1405 all_placeholders.insert(DynamicPlaceholder::Counter);
1406 }
1407 "Date" => {
1408 all_placeholders.insert(DynamicPlaceholder::Date);
1409 }
1410 "VuIter" => {
1411 all_placeholders.insert(DynamicPlaceholder::VuIter);
1412 }
1413 _ => {}
1414 }
1415 }
1416 }
1417 }
1418 }
1419 }
1420 }
1421
1422 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1424 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1425
1426 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1428
1429 let data = serde_json::json!({
1430 "base_url": self.target,
1431 "flows": flows_data,
1432 "extract_fields": config.default_extract_fields,
1433 "duration_secs": duration_secs,
1434 "max_vus": self.vus,
1435 "auth_header": self.auth,
1436 "custom_headers": custom_headers,
1437 "skip_tls_verify": self.skip_tls_verify,
1438 "stages": stages.iter().map(|s| serde_json::json!({
1440 "duration": s.duration,
1441 "target": s.target,
1442 })).collect::<Vec<_>>(),
1443 "threshold_percentile": self.threshold_percentile,
1444 "threshold_ms": self.threshold_ms,
1445 "max_error_rate": self.max_error_rate,
1446 "headers": headers_json,
1447 "dynamic_imports": required_imports,
1448 "dynamic_globals": required_globals,
1449 "extracted_values_output_path": output_dir.join("extracted_values.json").to_string_lossy(),
1450 "security_testing_enabled": security_testing_enabled,
1452 "has_custom_headers": !custom_headers.is_empty(),
1453 });
1454
1455 let mut script = handlebars
1456 .render_template(template, &data)
1457 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1458
1459 if security_testing_enabled {
1461 script = self.generate_enhanced_script(&script)?;
1462 }
1463
1464 let script_path =
1466 self.output.join(format!("k6-{}-crud-flow.js", spec_name.replace('.', "_")));
1467
1468 std::fs::create_dir_all(self.output.clone())?;
1469 std::fs::write(&script_path, &script)?;
1470
1471 if !self.generate_only {
1472 let executor = K6Executor::new()?;
1473 std::fs::create_dir_all(&output_dir)?;
1474
1475 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1476
1477 let extracted = Self::parse_extracted_values(&output_dir)?;
1478 TerminalReporter::print_progress(&format!(
1479 " Extracted {} value(s) from {}",
1480 extracted.values.len(),
1481 spec_name
1482 ));
1483 return Ok(extracted);
1484 }
1485
1486 Ok(ExtractedValues::new())
1487 }
1488
1489 async fn execute_standard_spec(&self, parser: &SpecParser, spec_name: &str) -> Result<()> {
1491 let mut operations = if let Some(filter) = &self.operations {
1492 parser.filter_operations(filter)?
1493 } else {
1494 parser.get_operations()
1495 };
1496
1497 if let Some(exclude) = &self.exclude_operations {
1498 operations = parser.exclude_operations(operations, exclude)?;
1499 }
1500
1501 if operations.is_empty() {
1502 TerminalReporter::print_warning(&format!("No operations found in {}", spec_name));
1503 return Ok(());
1504 }
1505
1506 TerminalReporter::print_progress(&format!(
1507 " {} operations in {}",
1508 operations.len(),
1509 spec_name
1510 ));
1511
1512 let templates: Vec<_> = operations
1514 .iter()
1515 .map(RequestGenerator::generate_template)
1516 .collect::<Result<Vec<_>>>()?;
1517
1518 let custom_headers = self.parse_headers()?;
1520
1521 let base_path = self.resolve_base_path(parser);
1523
1524 let scenario =
1526 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1527
1528 let security_testing_enabled = self.security_test || self.wafbench_dir.is_some();
1529
1530 let k6_config = K6Config {
1531 target_url: self.target.clone(),
1532 base_path,
1533 scenario,
1534 duration_secs: Self::parse_duration(&self.duration)?,
1535 max_vus: self.vus,
1536 threshold_percentile: self.threshold_percentile.clone(),
1537 threshold_ms: self.threshold_ms,
1538 max_error_rate: self.max_error_rate,
1539 auth_header: self.auth.clone(),
1540 custom_headers,
1541 skip_tls_verify: self.skip_tls_verify,
1542 security_testing_enabled,
1543 };
1544
1545 let generator = K6ScriptGenerator::new(k6_config, templates);
1546 let mut script = generator.generate()?;
1547
1548 let has_advanced_features = self.data_file.is_some()
1550 || self.error_rate.is_some()
1551 || self.security_test
1552 || self.parallel_create.is_some()
1553 || self.wafbench_dir.is_some();
1554
1555 if has_advanced_features {
1556 script = self.generate_enhanced_script(&script)?;
1557 }
1558
1559 let script_path = self.output.join(format!("k6-{}.js", spec_name.replace('.', "_")));
1561
1562 std::fs::create_dir_all(self.output.clone())?;
1563 std::fs::write(&script_path, &script)?;
1564
1565 if !self.generate_only {
1566 let executor = K6Executor::new()?;
1567 let output_dir = self.output.join(format!("{}_results", spec_name.replace('.', "_")));
1568 std::fs::create_dir_all(&output_dir)?;
1569
1570 executor.execute(&script_path, Some(&output_dir), self.verbose).await?;
1571 }
1572
1573 Ok(())
1574 }
1575
1576 async fn execute_crud_flow(&self, parser: &SpecParser) -> Result<()> {
1578 let config = self.build_crud_flow_config().unwrap_or_default();
1580
1581 let flows = if !config.flows.is_empty() {
1583 TerminalReporter::print_progress("Using custom flow configuration...");
1584 config.flows.clone()
1585 } else {
1586 TerminalReporter::print_progress("Detecting CRUD operations...");
1587 let operations = parser.get_operations();
1588 CrudFlowDetector::detect_flows(&operations)
1589 };
1590
1591 if flows.is_empty() {
1592 return Err(BenchError::Other(
1593 "No CRUD flows detected in spec. Ensure spec has POST/GET/PUT/DELETE operations on related paths.".to_string(),
1594 ));
1595 }
1596
1597 if config.flows.is_empty() {
1598 TerminalReporter::print_success(&format!("Detected {} CRUD flow(s)", flows.len()));
1599 } else {
1600 TerminalReporter::print_success(&format!("Loaded {} custom flow(s)", flows.len()));
1601 }
1602
1603 for flow in &flows {
1604 TerminalReporter::print_progress(&format!(
1605 " - {}: {} steps",
1606 flow.name,
1607 flow.steps.len()
1608 ));
1609 }
1610
1611 let mut handlebars = handlebars::Handlebars::new();
1613 handlebars.register_helper(
1615 "json",
1616 Box::new(
1617 |h: &handlebars::Helper,
1618 _: &handlebars::Handlebars,
1619 _: &handlebars::Context,
1620 _: &mut handlebars::RenderContext,
1621 out: &mut dyn handlebars::Output|
1622 -> handlebars::HelperResult {
1623 let param = h.param(0).map(|v| v.value()).unwrap_or(&serde_json::Value::Null);
1624 out.write(&serde_json::to_string(param).unwrap_or_else(|_| "[]".to_string()))?;
1625 Ok(())
1626 },
1627 ),
1628 );
1629 let template = include_str!("templates/k6_crud_flow.hbs");
1630
1631 let custom_headers = self.parse_headers()?;
1632
1633 let param_overrides = if let Some(params_file) = &self.params_file {
1635 TerminalReporter::print_progress("Loading parameter overrides...");
1636 let overrides = ParameterOverrides::from_file(params_file)?;
1637 TerminalReporter::print_success(&format!(
1638 "Loaded parameter overrides ({} operation-specific, {} defaults)",
1639 overrides.operations.len(),
1640 if overrides.defaults.is_empty() { 0 } else { 1 }
1641 ));
1642 Some(overrides)
1643 } else {
1644 None
1645 };
1646
1647 let duration_secs = Self::parse_duration(&self.duration)?;
1649 let scenario =
1650 LoadScenario::from_str(&self.scenario).map_err(BenchError::InvalidScenario)?;
1651 let stages = scenario.generate_stages(duration_secs, self.vus);
1652
1653 let api_base_path = self.resolve_base_path(parser);
1655 if let Some(ref bp) = api_base_path {
1656 TerminalReporter::print_progress(&format!("Using base path: {}", bp));
1657 }
1658
1659 let mut all_headers = custom_headers.clone();
1661 if let Some(auth) = &self.auth {
1662 all_headers.insert("Authorization".to_string(), auth.clone());
1663 }
1664 let headers_json = serde_json::to_string(&all_headers).unwrap_or_else(|_| "{}".to_string());
1665
1666 let mut all_placeholders: HashSet<DynamicPlaceholder> = HashSet::new();
1668
1669 let flows_data: Vec<serde_json::Value> = flows.iter().map(|f| {
1670 let sanitized_name = K6ScriptGenerator::sanitize_js_identifier(&f.name);
1672 serde_json::json!({
1673 "name": sanitized_name.clone(), "display_name": f.name, "base_path": f.base_path,
1676 "steps": f.steps.iter().enumerate().map(|(idx, s)| {
1677 let parts: Vec<&str> = s.operation.splitn(2, ' ').collect();
1679 let method_raw = if !parts.is_empty() {
1680 parts[0].to_uppercase()
1681 } else {
1682 "GET".to_string()
1683 };
1684 let method = if !parts.is_empty() {
1685 let m = parts[0].to_lowercase();
1686 if m == "delete" { "del".to_string() } else { m }
1688 } else {
1689 "get".to_string()
1690 };
1691 let raw_path = if parts.len() >= 2 { parts[1] } else { "/" };
1692 let path = if let Some(ref bp) = api_base_path {
1694 format!("{}{}", bp, raw_path)
1695 } else {
1696 raw_path.to_string()
1697 };
1698 let is_get_or_head = method == "get" || method == "head";
1699 let has_body = matches!(method.as_str(), "post" | "put" | "patch");
1701
1702 let body_value = if has_body {
1704 param_overrides.as_ref()
1705 .map(|po| po.get_for_operation(None, &method_raw, raw_path))
1706 .and_then(|oo| oo.body)
1707 .unwrap_or_else(|| serde_json::json!({}))
1708 } else {
1709 serde_json::json!({})
1710 };
1711
1712 let processed_body = DynamicParamProcessor::process_json_body(&body_value);
1714 let body_has_extracted_placeholders = processed_body.value.contains("${extracted.");
1719 let body_is_dynamic = processed_body.is_dynamic || body_has_extracted_placeholders;
1720
1721 serde_json::json!({
1722 "operation": s.operation,
1723 "method": method,
1724 "path": path,
1725 "extract": s.extract,
1726 "use_values": s.use_values,
1727 "use_body": s.use_body,
1728 "merge_body": if s.merge_body.is_empty() { None } else { Some(&s.merge_body) },
1729 "inject_attacks": s.inject_attacks,
1730 "attack_types": s.attack_types,
1731 "description": s.description,
1732 "display_name": s.description.clone().unwrap_or_else(|| format!("Step {}", idx)),
1733 "is_get_or_head": is_get_or_head,
1734 "has_body": has_body,
1735 "body": processed_body.value,
1736 "body_is_dynamic": body_is_dynamic,
1737 "_placeholders": processed_body.placeholders.iter().map(|p| format!("{:?}", p)).collect::<Vec<_>>(),
1738 })
1739 }).collect::<Vec<_>>(),
1740 })
1741 }).collect();
1742
1743 for flow_data in &flows_data {
1745 if let Some(steps) = flow_data.get("steps").and_then(|s| s.as_array()) {
1746 for step in steps {
1747 if let Some(placeholders_arr) =
1748 step.get("_placeholders").and_then(|p| p.as_array())
1749 {
1750 for p_str in placeholders_arr {
1751 if let Some(p_name) = p_str.as_str() {
1752 match p_name {
1754 "VU" => {
1755 all_placeholders.insert(DynamicPlaceholder::VU);
1756 }
1757 "Iteration" => {
1758 all_placeholders.insert(DynamicPlaceholder::Iteration);
1759 }
1760 "Timestamp" => {
1761 all_placeholders.insert(DynamicPlaceholder::Timestamp);
1762 }
1763 "UUID" => {
1764 all_placeholders.insert(DynamicPlaceholder::UUID);
1765 }
1766 "Random" => {
1767 all_placeholders.insert(DynamicPlaceholder::Random);
1768 }
1769 "Counter" => {
1770 all_placeholders.insert(DynamicPlaceholder::Counter);
1771 }
1772 "Date" => {
1773 all_placeholders.insert(DynamicPlaceholder::Date);
1774 }
1775 "VuIter" => {
1776 all_placeholders.insert(DynamicPlaceholder::VuIter);
1777 }
1778 _ => {}
1779 }
1780 }
1781 }
1782 }
1783 }
1784 }
1785 }
1786
1787 let required_imports = DynamicParamProcessor::get_required_imports(&all_placeholders);
1789 let required_globals = DynamicParamProcessor::get_required_globals(&all_placeholders);
1790
1791 let invalid_data_config = self.build_invalid_data_config();
1793 let error_injection_enabled = invalid_data_config.is_some();
1794 let error_rate = self.error_rate.unwrap_or(0.0);
1795 let error_types: Vec<String> = invalid_data_config
1796 .as_ref()
1797 .map(|c| c.error_types.iter().map(|t| format!("{:?}", t)).collect())
1798 .unwrap_or_default();
1799
1800 if error_injection_enabled {
1801 TerminalReporter::print_progress(&format!(
1802 "Error injection enabled ({}% rate)",
1803 (error_rate * 100.0) as u32
1804 ));
1805 }
1806
1807 let security_testing_enabled = self.wafbench_dir.is_some() || self.security_test;
1809
1810 let data = serde_json::json!({
1811 "base_url": self.target,
1812 "flows": flows_data,
1813 "extract_fields": config.default_extract_fields,
1814 "duration_secs": duration_secs,
1815 "max_vus": self.vus,
1816 "auth_header": self.auth,
1817 "custom_headers": custom_headers,
1818 "skip_tls_verify": self.skip_tls_verify,
1819 "stages": stages.iter().map(|s| serde_json::json!({
1821 "duration": s.duration,
1822 "target": s.target,
1823 })).collect::<Vec<_>>(),
1824 "threshold_percentile": self.threshold_percentile,
1825 "threshold_ms": self.threshold_ms,
1826 "max_error_rate": self.max_error_rate,
1827 "headers": headers_json,
1828 "dynamic_imports": required_imports,
1829 "dynamic_globals": required_globals,
1830 "extracted_values_output_path": self
1831 .output
1832 .join("crud_flow_extracted_values.json")
1833 .to_string_lossy(),
1834 "error_injection_enabled": error_injection_enabled,
1836 "error_rate": error_rate,
1837 "error_types": error_types,
1838 "security_testing_enabled": security_testing_enabled,
1840 "has_custom_headers": !custom_headers.is_empty(),
1841 });
1842
1843 let mut script = handlebars
1844 .render_template(template, &data)
1845 .map_err(|e| BenchError::ScriptGenerationFailed(e.to_string()))?;
1846
1847 if security_testing_enabled {
1849 script = self.generate_enhanced_script(&script)?;
1850 }
1851
1852 TerminalReporter::print_progress("Validating CRUD flow script...");
1854 let validation_errors = K6ScriptGenerator::validate_script(&script);
1855 if !validation_errors.is_empty() {
1856 TerminalReporter::print_error("CRUD flow script validation failed");
1857 for error in &validation_errors {
1858 eprintln!(" {}", error);
1859 }
1860 return Err(BenchError::Other(format!(
1861 "CRUD flow script validation failed with {} error(s)",
1862 validation_errors.len()
1863 )));
1864 }
1865
1866 TerminalReporter::print_success("CRUD flow script generated");
1867
1868 let script_path = if let Some(output) = &self.script_output {
1870 output.clone()
1871 } else {
1872 self.output.join("k6-crud-flow-script.js")
1873 };
1874
1875 if let Some(parent) = script_path.parent() {
1876 std::fs::create_dir_all(parent)?;
1877 }
1878 std::fs::write(&script_path, &script)?;
1879 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
1880
1881 if self.generate_only {
1882 println!("\nScript generated successfully. Run it with:");
1883 println!(" k6 run {}", script_path.display());
1884 return Ok(());
1885 }
1886
1887 TerminalReporter::print_progress("Executing CRUD flow test...");
1889 let executor = K6Executor::new()?;
1890 std::fs::create_dir_all(&self.output)?;
1891
1892 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
1893
1894 let duration_secs = Self::parse_duration(&self.duration)?;
1895 TerminalReporter::print_summary(&results, duration_secs);
1896
1897 Ok(())
1898 }
1899
1900 async fn execute_conformance_test(&self) -> Result<()> {
1902 use crate::conformance::generator::{ConformanceConfig, ConformanceGenerator};
1903 use crate::conformance::report::ConformanceReport;
1904 use crate::conformance::spec::ConformanceFeature;
1905
1906 TerminalReporter::print_progress("OpenAPI 3.0.0 Conformance Testing Mode");
1907
1908 TerminalReporter::print_progress(
1911 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
1912 );
1913
1914 let categories = self.conformance_categories.as_ref().map(|cats_str| {
1916 cats_str
1917 .split(',')
1918 .filter_map(|s| {
1919 let trimmed = s.trim();
1920 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
1921 Some(canonical.to_string())
1922 } else {
1923 TerminalReporter::print_warning(&format!(
1924 "Unknown conformance category: '{}'. Valid categories: {}",
1925 trimmed,
1926 ConformanceFeature::cli_category_names()
1927 .iter()
1928 .map(|(cli, _)| *cli)
1929 .collect::<Vec<_>>()
1930 .join(", ")
1931 ));
1932 None
1933 }
1934 })
1935 .collect::<Vec<String>>()
1936 });
1937
1938 let custom_headers: Vec<(String, String)> = self
1940 .conformance_headers
1941 .iter()
1942 .filter_map(|h| {
1943 let (name, value) = h.split_once(':')?;
1944 Some((name.trim().to_string(), value.trim().to_string()))
1945 })
1946 .collect();
1947
1948 if !custom_headers.is_empty() {
1949 TerminalReporter::print_progress(&format!(
1950 "Using {} custom header(s) for authentication",
1951 custom_headers.len()
1952 ));
1953 }
1954
1955 if self.conformance_delay_ms > 0 {
1956 TerminalReporter::print_progress(&format!(
1957 "Using {}ms delay between conformance requests",
1958 self.conformance_delay_ms
1959 ));
1960 }
1961
1962 std::fs::create_dir_all(&self.output)?;
1964
1965 let config = ConformanceConfig {
1966 target_url: self.target.clone(),
1967 api_key: self.conformance_api_key.clone(),
1968 basic_auth: self.conformance_basic_auth.clone(),
1969 skip_tls_verify: self.skip_tls_verify,
1970 categories,
1971 base_path: self.base_path.clone(),
1972 custom_headers,
1973 output_dir: Some(self.output.clone()),
1974 all_operations: self.conformance_all_operations,
1975 custom_checks_file: self.conformance_custom.clone(),
1976 request_delay_ms: self.conformance_delay_ms,
1977 };
1978
1979 let annotated_ops = if !self.spec.is_empty() {
1982 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
1983 let parser = SpecParser::from_file(&self.spec[0]).await?;
1984 let operations = parser.get_operations();
1985
1986 let annotated =
1987 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
1988 &operations,
1989 parser.spec(),
1990 );
1991 TerminalReporter::print_success(&format!(
1992 "Analyzed {} operations, found {} feature annotations",
1993 operations.len(),
1994 annotated.iter().map(|a| a.features.len()).sum::<usize>()
1995 ));
1996 Some(annotated)
1997 } else {
1998 None
1999 };
2000
2001 if self.generate_only || self.use_k6 {
2003 let script = if let Some(annotated) = &annotated_ops {
2004 let gen = crate::conformance::spec_driven::SpecDrivenConformanceGenerator::new(
2005 config,
2006 annotated.clone(),
2007 );
2008 let op_count = gen.operation_count();
2009 let (script, check_count) = gen.generate()?;
2010 TerminalReporter::print_success(&format!(
2011 "Conformance: {} operations analyzed, {} unique checks generated",
2012 op_count, check_count
2013 ));
2014 script
2015 } else {
2016 let generator = ConformanceGenerator::new(config);
2017 generator.generate()?
2018 };
2019
2020 let script_path = self.output.join("k6-conformance.js");
2021 std::fs::write(&script_path, &script).map_err(|e| {
2022 BenchError::Other(format!("Failed to write conformance script: {}", e))
2023 })?;
2024 TerminalReporter::print_success(&format!(
2025 "Conformance script generated: {}",
2026 script_path.display()
2027 ));
2028
2029 if self.generate_only {
2030 println!("\nScript generated. Run with:");
2031 println!(" k6 run {}", script_path.display());
2032 return Ok(());
2033 }
2034
2035 if !K6Executor::is_k6_installed() {
2037 TerminalReporter::print_error("k6 is not installed");
2038 TerminalReporter::print_warning(
2039 "Install k6 from: https://k6.io/docs/get-started/installation/",
2040 );
2041 return Err(BenchError::K6NotFound);
2042 }
2043
2044 TerminalReporter::print_progress("Running conformance tests via k6...");
2045 let executor = K6Executor::new()?;
2046 executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2047
2048 let report_path = self.output.join("conformance-report.json");
2049 if report_path.exists() {
2050 let report = ConformanceReport::from_file(&report_path)?;
2051 report.print_report_with_options(self.conformance_all_operations);
2052 self.save_conformance_report(&report, &report_path)?;
2053 } else {
2054 TerminalReporter::print_warning(
2055 "Conformance report not generated (k6 handleSummary may not have run)",
2056 );
2057 }
2058
2059 return Ok(());
2060 }
2061
2062 TerminalReporter::print_progress("Running conformance tests (native executor)...");
2064
2065 let mut executor = crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2066
2067 executor = if let Some(annotated) = &annotated_ops {
2068 executor.with_spec_driven_checks(annotated)
2069 } else {
2070 executor.with_reference_checks()
2071 };
2072 executor = executor.with_custom_checks()?;
2073
2074 TerminalReporter::print_success(&format!(
2075 "Executing {} conformance checks...",
2076 executor.check_count()
2077 ));
2078
2079 let report = executor.execute().await?;
2080 report.print_report_with_options(self.conformance_all_operations);
2081
2082 let failure_details = report.failure_details();
2084 if !failure_details.is_empty() {
2085 let details_path = self.output.join("conformance-failure-details.json");
2086 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2087 let _ = std::fs::write(&details_path, json);
2088 TerminalReporter::print_success(&format!(
2089 "Failure details saved to: {}",
2090 details_path.display()
2091 ));
2092 }
2093 }
2094
2095 let report_path = self.output.join("conformance-report.json");
2097 let report_json = serde_json::to_string_pretty(&report.to_json())
2098 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2099 std::fs::write(&report_path, &report_json)
2100 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2101 TerminalReporter::print_success(&format!("Report saved to: {}", report_path.display()));
2102
2103 self.save_conformance_report(&report, &report_path)?;
2104
2105 Ok(())
2106 }
2107
2108 fn save_conformance_report(
2110 &self,
2111 report: &crate::conformance::report::ConformanceReport,
2112 report_path: &Path,
2113 ) -> Result<()> {
2114 if self.conformance_report_format == "sarif" {
2115 use crate::conformance::sarif::ConformanceSarifReport;
2116 ConformanceSarifReport::write(report, &self.target, &self.conformance_report)?;
2117 TerminalReporter::print_success(&format!(
2118 "SARIF report saved to: {}",
2119 self.conformance_report.display()
2120 ));
2121 } else if self.conformance_report != *report_path {
2122 std::fs::copy(report_path, &self.conformance_report)?;
2123 TerminalReporter::print_success(&format!(
2124 "Report saved to: {}",
2125 self.conformance_report.display()
2126 ));
2127 }
2128 Ok(())
2129 }
2130
2131 async fn execute_multi_target_conformance(&self, targets_file: &Path) -> Result<()> {
2137 use crate::conformance::generator::ConformanceConfig;
2138 use crate::conformance::spec::ConformanceFeature;
2139
2140 TerminalReporter::print_progress("Multi-target OpenAPI 3.0.0 Conformance Testing Mode");
2141
2142 TerminalReporter::print_progress("Parsing targets file...");
2144 let targets = parse_targets_file(targets_file)?;
2145 let num_targets = targets.len();
2146 TerminalReporter::print_success(&format!("Loaded {} targets", num_targets));
2147
2148 if targets.is_empty() {
2149 return Err(BenchError::Other("No targets found in file".to_string()));
2150 }
2151
2152 TerminalReporter::print_progress(
2153 "Conformance mode runs 1 VU, 1 iteration per endpoint (--vus and -d are ignored)",
2154 );
2155
2156 let categories = self.conformance_categories.as_ref().map(|cats_str| {
2158 cats_str
2159 .split(',')
2160 .filter_map(|s| {
2161 let trimmed = s.trim();
2162 if let Some(canonical) = ConformanceFeature::category_from_cli_name(trimmed) {
2163 Some(canonical.to_string())
2164 } else {
2165 TerminalReporter::print_warning(&format!(
2166 "Unknown conformance category: '{}'. Valid categories: {}",
2167 trimmed,
2168 ConformanceFeature::cli_category_names()
2169 .iter()
2170 .map(|(cli, _)| *cli)
2171 .collect::<Vec<_>>()
2172 .join(", ")
2173 ));
2174 None
2175 }
2176 })
2177 .collect::<Vec<String>>()
2178 });
2179
2180 let base_custom_headers: Vec<(String, String)> = self
2182 .conformance_headers
2183 .iter()
2184 .filter_map(|h| {
2185 let (name, value) = h.split_once(':')?;
2186 Some((name.trim().to_string(), value.trim().to_string()))
2187 })
2188 .collect();
2189
2190 if !base_custom_headers.is_empty() {
2191 TerminalReporter::print_progress(&format!(
2192 "Using {} base custom header(s) for authentication",
2193 base_custom_headers.len()
2194 ));
2195 }
2196
2197 let annotated_ops = if !self.spec.is_empty() {
2199 TerminalReporter::print_progress("Spec-driven conformance mode: analyzing spec...");
2200 let parser = SpecParser::from_file(&self.spec[0]).await?;
2201 let operations = parser.get_operations();
2202 let annotated =
2203 crate::conformance::spec_driven::SpecDrivenConformanceGenerator::annotate_operations(
2204 &operations,
2205 parser.spec(),
2206 );
2207 TerminalReporter::print_success(&format!(
2208 "Analyzed {} operations, found {} feature annotations",
2209 operations.len(),
2210 annotated.iter().map(|a| a.features.len()).sum::<usize>()
2211 ));
2212 Some(annotated)
2213 } else {
2214 None
2215 };
2216
2217 std::fs::create_dir_all(&self.output)?;
2219
2220 struct TargetResult {
2222 url: String,
2223 passed: usize,
2224 failed: usize,
2225 elapsed: std::time::Duration,
2226 report_json: serde_json::Value,
2227 }
2228
2229 let mut target_results: Vec<TargetResult> = Vec::with_capacity(num_targets);
2230 let total_start = std::time::Instant::now();
2231
2232 for (idx, target) in targets.iter().enumerate() {
2233 tracing::info!(
2234 "Running conformance tests against target {}/{}: {}",
2235 idx + 1,
2236 num_targets,
2237 target.url
2238 );
2239 TerminalReporter::print_progress(&format!(
2240 "\n--- Target {}/{}: {} ---",
2241 idx + 1,
2242 num_targets,
2243 target.url
2244 ));
2245
2246 let mut merged_headers = base_custom_headers.clone();
2248 if let Some(ref target_headers) = target.headers {
2249 for (name, value) in target_headers {
2250 if let Some(existing) = merged_headers.iter_mut().find(|(n, _)| n == name) {
2252 existing.1 = value.clone();
2253 } else {
2254 merged_headers.push((name.clone(), value.clone()));
2255 }
2256 }
2257 }
2258 if let Some(ref auth) = target.auth {
2260 if let Some(existing) =
2261 merged_headers.iter_mut().find(|(n, _)| n.eq_ignore_ascii_case("Authorization"))
2262 {
2263 existing.1 = auth.clone();
2264 } else {
2265 merged_headers.push(("Authorization".to_string(), auth.clone()));
2266 }
2267 }
2268
2269 let config = ConformanceConfig {
2270 target_url: target.url.clone(),
2271 api_key: self.conformance_api_key.clone(),
2272 basic_auth: self.conformance_basic_auth.clone(),
2273 skip_tls_verify: self.skip_tls_verify,
2274 categories: categories.clone(),
2275 base_path: self.base_path.clone(),
2276 custom_headers: merged_headers,
2277 output_dir: Some(self.output.clone()),
2278 all_operations: self.conformance_all_operations,
2279 custom_checks_file: self.conformance_custom.clone(),
2280 request_delay_ms: self.conformance_delay_ms,
2281 };
2282
2283 let mut executor =
2284 crate::conformance::executor::NativeConformanceExecutor::new(config)?;
2285
2286 executor = if let Some(ref annotated) = annotated_ops {
2287 executor.with_spec_driven_checks(annotated)
2288 } else {
2289 executor.with_reference_checks()
2290 };
2291 executor = executor.with_custom_checks()?;
2292
2293 TerminalReporter::print_success(&format!(
2294 "Executing {} conformance checks against {}...",
2295 executor.check_count(),
2296 target.url
2297 ));
2298
2299 let target_start = std::time::Instant::now();
2300 let report = executor.execute().await?;
2301 let target_elapsed = target_start.elapsed();
2302
2303 let report_json = report.to_json();
2304
2305 let passed = report_json["summary"]["passed"].as_u64().unwrap_or(0) as usize;
2307 let failed = report_json["summary"]["failed"].as_u64().unwrap_or(0) as usize;
2308 let total_checks = passed + failed;
2309 let rate = if total_checks == 0 {
2310 0.0
2311 } else {
2312 (passed as f64 / total_checks as f64) * 100.0
2313 };
2314
2315 TerminalReporter::print_success(&format!(
2316 "Target {}: {}/{} passed ({:.1}%) in {:.1}s",
2317 target.url,
2318 passed,
2319 total_checks,
2320 rate,
2321 target_elapsed.as_secs_f64()
2322 ));
2323
2324 let target_dir = self.output.join(format!("target_{}", idx));
2326 std::fs::create_dir_all(&target_dir)?;
2327 let target_report_path = target_dir.join("conformance-report.json");
2328 let report_str = serde_json::to_string_pretty(&report_json)
2329 .map_err(|e| BenchError::Other(format!("Failed to serialize report: {}", e)))?;
2330 std::fs::write(&target_report_path, &report_str)
2331 .map_err(|e| BenchError::Other(format!("Failed to write report: {}", e)))?;
2332
2333 let failure_details = report.failure_details();
2335 if !failure_details.is_empty() {
2336 let details_path = target_dir.join("conformance-failure-details.json");
2337 if let Ok(json) = serde_json::to_string_pretty(&failure_details) {
2338 let _ = std::fs::write(&details_path, json);
2339 }
2340 }
2341
2342 target_results.push(TargetResult {
2343 url: target.url.clone(),
2344 passed,
2345 failed,
2346 elapsed: target_elapsed,
2347 report_json,
2348 });
2349 }
2350
2351 let total_elapsed = total_start.elapsed();
2352
2353 println!("\n{}", "=".repeat(80));
2355 println!(" Multi-Target Conformance Summary");
2356 println!("{}", "=".repeat(80));
2357 println!(
2358 " {:<40} {:>8} {:>8} {:>8} {:>8}",
2359 "Target URL", "Passed", "Failed", "Rate", "Time"
2360 );
2361 println!(" {}", "-".repeat(76));
2362
2363 let mut total_passed = 0usize;
2364 let mut total_failed = 0usize;
2365
2366 for result in &target_results {
2367 let total_checks = result.passed + result.failed;
2368 let rate = if total_checks == 0 {
2369 0.0
2370 } else {
2371 (result.passed as f64 / total_checks as f64) * 100.0
2372 };
2373
2374 let display_url = if result.url.len() > 38 {
2376 format!("{}...", &result.url[..35])
2377 } else {
2378 result.url.clone()
2379 };
2380
2381 println!(
2382 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2383 display_url,
2384 result.passed,
2385 result.failed,
2386 rate,
2387 result.elapsed.as_secs_f64()
2388 );
2389
2390 total_passed += result.passed;
2391 total_failed += result.failed;
2392 }
2393
2394 let grand_total = total_passed + total_failed;
2395 let overall_rate = if grand_total == 0 {
2396 0.0
2397 } else {
2398 (total_passed as f64 / grand_total as f64) * 100.0
2399 };
2400
2401 println!(" {}", "-".repeat(76));
2402 println!(
2403 " {:<40} {:>8} {:>8} {:>7.1}% {:>6.1}s",
2404 format!("TOTAL ({} targets)", num_targets),
2405 total_passed,
2406 total_failed,
2407 overall_rate,
2408 total_elapsed.as_secs_f64()
2409 );
2410 println!("{}", "=".repeat(80));
2411
2412 let per_target_summaries: Vec<serde_json::Value> = target_results
2414 .iter()
2415 .enumerate()
2416 .map(|(idx, r)| {
2417 let total_checks = r.passed + r.failed;
2418 let rate = if total_checks == 0 {
2419 0.0
2420 } else {
2421 (r.passed as f64 / total_checks as f64) * 100.0
2422 };
2423 serde_json::json!({
2424 "target_url": r.url,
2425 "target_index": idx,
2426 "checks_passed": r.passed,
2427 "checks_failed": r.failed,
2428 "total_checks": total_checks,
2429 "pass_rate": rate,
2430 "elapsed_seconds": r.elapsed.as_secs_f64(),
2431 "report": r.report_json,
2432 })
2433 })
2434 .collect();
2435
2436 let combined_summary = serde_json::json!({
2437 "total_targets": num_targets,
2438 "total_checks_passed": total_passed,
2439 "total_checks_failed": total_failed,
2440 "overall_pass_rate": overall_rate,
2441 "total_elapsed_seconds": total_elapsed.as_secs_f64(),
2442 "targets": per_target_summaries,
2443 });
2444
2445 let summary_path = self.output.join("multi-target-conformance-summary.json");
2446 let summary_str = serde_json::to_string_pretty(&combined_summary)
2447 .map_err(|e| BenchError::Other(format!("Failed to serialize summary: {}", e)))?;
2448 std::fs::write(&summary_path, &summary_str)
2449 .map_err(|e| BenchError::Other(format!("Failed to write summary: {}", e)))?;
2450 TerminalReporter::print_success(&format!(
2451 "Combined summary saved to: {}",
2452 summary_path.display()
2453 ));
2454
2455 Ok(())
2456 }
2457
2458 async fn execute_owasp_test(&self, parser: &SpecParser) -> Result<()> {
2460 TerminalReporter::print_progress("OWASP API Security Top 10 Testing Mode");
2461
2462 let custom_headers = self.parse_headers()?;
2464
2465 let mut config = OwaspApiConfig::new()
2467 .with_auth_header(&self.owasp_auth_header)
2468 .with_verbose(self.verbose)
2469 .with_insecure(self.skip_tls_verify)
2470 .with_concurrency(self.vus as usize)
2471 .with_iterations(self.owasp_iterations as usize)
2472 .with_base_path(self.base_path.clone())
2473 .with_custom_headers(custom_headers);
2474
2475 if let Some(ref token) = self.owasp_auth_token {
2477 config = config.with_valid_auth_token(token);
2478 }
2479
2480 if let Some(ref cats_str) = self.owasp_categories {
2482 let categories: Vec<OwaspCategory> = cats_str
2483 .split(',')
2484 .filter_map(|s| {
2485 let trimmed = s.trim();
2486 match trimmed.parse::<OwaspCategory>() {
2487 Ok(cat) => Some(cat),
2488 Err(e) => {
2489 TerminalReporter::print_warning(&e);
2490 None
2491 }
2492 }
2493 })
2494 .collect();
2495
2496 if !categories.is_empty() {
2497 config = config.with_categories(categories);
2498 }
2499 }
2500
2501 if let Some(ref admin_paths_file) = self.owasp_admin_paths {
2503 config.admin_paths_file = Some(admin_paths_file.clone());
2504 if let Err(e) = config.load_admin_paths() {
2505 TerminalReporter::print_warning(&format!("Failed to load admin paths file: {}", e));
2506 }
2507 }
2508
2509 if let Some(ref id_fields_str) = self.owasp_id_fields {
2511 let id_fields: Vec<String> = id_fields_str
2512 .split(',')
2513 .map(|s| s.trim().to_string())
2514 .filter(|s| !s.is_empty())
2515 .collect();
2516 if !id_fields.is_empty() {
2517 config = config.with_id_fields(id_fields);
2518 }
2519 }
2520
2521 if let Some(ref report_path) = self.owasp_report {
2523 config = config.with_report_path(report_path);
2524 }
2525 if let Ok(format) = self.owasp_report_format.parse::<ReportFormat>() {
2526 config = config.with_report_format(format);
2527 }
2528
2529 let categories = config.categories_to_test();
2531 TerminalReporter::print_success(&format!(
2532 "Testing {} OWASP categories: {}",
2533 categories.len(),
2534 categories.iter().map(|c| c.cli_name()).collect::<Vec<_>>().join(", ")
2535 ));
2536
2537 if config.valid_auth_token.is_some() {
2538 TerminalReporter::print_progress("Using provided auth token for baseline requests");
2539 }
2540
2541 TerminalReporter::print_progress("Generating OWASP security test script...");
2543 let generator = OwaspApiGenerator::new(config, self.target.clone(), parser);
2544
2545 let script = generator.generate()?;
2547 TerminalReporter::print_success("OWASP security test script generated");
2548
2549 let script_path = if let Some(output) = &self.script_output {
2551 output.clone()
2552 } else {
2553 self.output.join("k6-owasp-security-test.js")
2554 };
2555
2556 if let Some(parent) = script_path.parent() {
2557 std::fs::create_dir_all(parent)?;
2558 }
2559 std::fs::write(&script_path, &script)?;
2560 TerminalReporter::print_success(&format!("Script written to: {}", script_path.display()));
2561
2562 if self.generate_only {
2564 println!("\nOWASP security test script generated. Run it with:");
2565 println!(" k6 run {}", script_path.display());
2566 return Ok(());
2567 }
2568
2569 TerminalReporter::print_progress("Executing OWASP security tests...");
2571 let executor = K6Executor::new()?;
2572 std::fs::create_dir_all(&self.output)?;
2573
2574 let results = executor.execute(&script_path, Some(&self.output), self.verbose).await?;
2575
2576 let duration_secs = Self::parse_duration(&self.duration)?;
2577 TerminalReporter::print_summary(&results, duration_secs);
2578
2579 println!("\nOWASP security test results saved to: {}", self.output.display());
2580
2581 Ok(())
2582 }
2583}
2584
2585#[cfg(test)]
2586mod tests {
2587 use super::*;
2588 use tempfile::tempdir;
2589
2590 #[test]
2591 fn test_parse_duration() {
2592 assert_eq!(BenchCommand::parse_duration("30s").unwrap(), 30);
2593 assert_eq!(BenchCommand::parse_duration("5m").unwrap(), 300);
2594 assert_eq!(BenchCommand::parse_duration("1h").unwrap(), 3600);
2595 assert_eq!(BenchCommand::parse_duration("60").unwrap(), 60);
2596 }
2597
2598 #[test]
2599 fn test_parse_duration_invalid() {
2600 assert!(BenchCommand::parse_duration("invalid").is_err());
2601 assert!(BenchCommand::parse_duration("30x").is_err());
2602 }
2603
2604 #[test]
2605 fn test_parse_headers() {
2606 let cmd = BenchCommand {
2607 spec: vec![PathBuf::from("test.yaml")],
2608 spec_dir: None,
2609 merge_conflicts: "error".to_string(),
2610 spec_mode: "merge".to_string(),
2611 dependency_config: None,
2612 target: "http://localhost".to_string(),
2613 base_path: None,
2614 duration: "1m".to_string(),
2615 vus: 10,
2616 scenario: "ramp-up".to_string(),
2617 operations: None,
2618 exclude_operations: None,
2619 auth: None,
2620 headers: Some("X-API-Key:test123,X-Client-ID:client456".to_string()),
2621 output: PathBuf::from("output"),
2622 generate_only: false,
2623 script_output: None,
2624 threshold_percentile: "p(95)".to_string(),
2625 threshold_ms: 500,
2626 max_error_rate: 0.05,
2627 verbose: false,
2628 skip_tls_verify: false,
2629 targets_file: None,
2630 max_concurrency: None,
2631 results_format: "both".to_string(),
2632 params_file: None,
2633 crud_flow: false,
2634 flow_config: None,
2635 extract_fields: None,
2636 parallel_create: None,
2637 data_file: None,
2638 data_distribution: "unique-per-vu".to_string(),
2639 data_mappings: None,
2640 per_uri_control: false,
2641 error_rate: None,
2642 error_types: None,
2643 security_test: false,
2644 security_payloads: None,
2645 security_categories: None,
2646 security_target_fields: None,
2647 wafbench_dir: None,
2648 wafbench_cycle_all: false,
2649 owasp_api_top10: false,
2650 owasp_categories: None,
2651 owasp_auth_header: "Authorization".to_string(),
2652 owasp_auth_token: None,
2653 owasp_admin_paths: None,
2654 owasp_id_fields: None,
2655 owasp_report: None,
2656 owasp_report_format: "json".to_string(),
2657 owasp_iterations: 1,
2658 conformance: false,
2659 conformance_api_key: None,
2660 conformance_basic_auth: None,
2661 conformance_report: PathBuf::from("conformance-report.json"),
2662 conformance_categories: None,
2663 conformance_report_format: "json".to_string(),
2664 conformance_headers: vec![],
2665 conformance_all_operations: false,
2666 conformance_custom: None,
2667 conformance_delay_ms: 0,
2668 use_k6: false,
2669 };
2670
2671 let headers = cmd.parse_headers().unwrap();
2672 assert_eq!(headers.get("X-API-Key"), Some(&"test123".to_string()));
2673 assert_eq!(headers.get("X-Client-ID"), Some(&"client456".to_string()));
2674 }
2675
2676 #[test]
2677 fn test_get_spec_display_name() {
2678 let cmd = BenchCommand {
2679 spec: vec![PathBuf::from("test.yaml")],
2680 spec_dir: None,
2681 merge_conflicts: "error".to_string(),
2682 spec_mode: "merge".to_string(),
2683 dependency_config: None,
2684 target: "http://localhost".to_string(),
2685 base_path: None,
2686 duration: "1m".to_string(),
2687 vus: 10,
2688 scenario: "ramp-up".to_string(),
2689 operations: None,
2690 exclude_operations: None,
2691 auth: None,
2692 headers: None,
2693 output: PathBuf::from("output"),
2694 generate_only: false,
2695 script_output: None,
2696 threshold_percentile: "p(95)".to_string(),
2697 threshold_ms: 500,
2698 max_error_rate: 0.05,
2699 verbose: false,
2700 skip_tls_verify: false,
2701 targets_file: None,
2702 max_concurrency: None,
2703 results_format: "both".to_string(),
2704 params_file: None,
2705 crud_flow: false,
2706 flow_config: None,
2707 extract_fields: None,
2708 parallel_create: None,
2709 data_file: None,
2710 data_distribution: "unique-per-vu".to_string(),
2711 data_mappings: None,
2712 per_uri_control: false,
2713 error_rate: None,
2714 error_types: None,
2715 security_test: false,
2716 security_payloads: None,
2717 security_categories: None,
2718 security_target_fields: None,
2719 wafbench_dir: None,
2720 wafbench_cycle_all: false,
2721 owasp_api_top10: false,
2722 owasp_categories: None,
2723 owasp_auth_header: "Authorization".to_string(),
2724 owasp_auth_token: None,
2725 owasp_admin_paths: None,
2726 owasp_id_fields: None,
2727 owasp_report: None,
2728 owasp_report_format: "json".to_string(),
2729 owasp_iterations: 1,
2730 conformance: false,
2731 conformance_api_key: None,
2732 conformance_basic_auth: None,
2733 conformance_report: PathBuf::from("conformance-report.json"),
2734 conformance_categories: None,
2735 conformance_report_format: "json".to_string(),
2736 conformance_headers: vec![],
2737 conformance_all_operations: false,
2738 conformance_custom: None,
2739 conformance_delay_ms: 0,
2740 use_k6: false,
2741 };
2742
2743 assert_eq!(cmd.get_spec_display_name(), "test.yaml");
2744
2745 let cmd_multi = BenchCommand {
2747 spec: vec![PathBuf::from("a.yaml"), PathBuf::from("b.yaml")],
2748 spec_dir: None,
2749 merge_conflicts: "error".to_string(),
2750 spec_mode: "merge".to_string(),
2751 dependency_config: None,
2752 target: "http://localhost".to_string(),
2753 base_path: None,
2754 duration: "1m".to_string(),
2755 vus: 10,
2756 scenario: "ramp-up".to_string(),
2757 operations: None,
2758 exclude_operations: None,
2759 auth: None,
2760 headers: None,
2761 output: PathBuf::from("output"),
2762 generate_only: false,
2763 script_output: None,
2764 threshold_percentile: "p(95)".to_string(),
2765 threshold_ms: 500,
2766 max_error_rate: 0.05,
2767 verbose: false,
2768 skip_tls_verify: false,
2769 targets_file: None,
2770 max_concurrency: None,
2771 results_format: "both".to_string(),
2772 params_file: None,
2773 crud_flow: false,
2774 flow_config: None,
2775 extract_fields: None,
2776 parallel_create: None,
2777 data_file: None,
2778 data_distribution: "unique-per-vu".to_string(),
2779 data_mappings: None,
2780 per_uri_control: false,
2781 error_rate: None,
2782 error_types: None,
2783 security_test: false,
2784 security_payloads: None,
2785 security_categories: None,
2786 security_target_fields: None,
2787 wafbench_dir: None,
2788 wafbench_cycle_all: false,
2789 owasp_api_top10: false,
2790 owasp_categories: None,
2791 owasp_auth_header: "Authorization".to_string(),
2792 owasp_auth_token: None,
2793 owasp_admin_paths: None,
2794 owasp_id_fields: None,
2795 owasp_report: None,
2796 owasp_report_format: "json".to_string(),
2797 owasp_iterations: 1,
2798 conformance: false,
2799 conformance_api_key: None,
2800 conformance_basic_auth: None,
2801 conformance_report: PathBuf::from("conformance-report.json"),
2802 conformance_categories: None,
2803 conformance_report_format: "json".to_string(),
2804 conformance_headers: vec![],
2805 conformance_all_operations: false,
2806 conformance_custom: None,
2807 conformance_delay_ms: 0,
2808 use_k6: false,
2809 };
2810
2811 assert_eq!(cmd_multi.get_spec_display_name(), "2 spec files");
2812 }
2813
2814 #[test]
2815 fn test_parse_extracted_values_from_output_dir() {
2816 let dir = tempdir().unwrap();
2817 let path = dir.path().join("extracted_values.json");
2818 std::fs::write(
2819 &path,
2820 r#"{
2821 "pool_id": "abc123",
2822 "count": 0,
2823 "enabled": false,
2824 "metadata": { "owner": "team-a" }
2825}"#,
2826 )
2827 .unwrap();
2828
2829 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
2830 assert_eq!(extracted.get("pool_id"), Some(&serde_json::json!("abc123")));
2831 assert_eq!(extracted.get("count"), Some(&serde_json::json!(0)));
2832 assert_eq!(extracted.get("enabled"), Some(&serde_json::json!(false)));
2833 assert_eq!(extracted.get("metadata"), Some(&serde_json::json!({"owner": "team-a"})));
2834 }
2835
2836 #[test]
2837 fn test_parse_extracted_values_missing_file() {
2838 let dir = tempdir().unwrap();
2839 let extracted = BenchCommand::parse_extracted_values(dir.path()).unwrap();
2840 assert!(extracted.values.is_empty());
2841 }
2842}