Skip to main content

mockforge_bench/
invalid_data.rs

1//! Invalid data generation for error testing
2//!
3//! This module provides functionality to generate invalid request data
4//! for testing error handling. Supports mixing valid and invalid requests
5//! based on a configurable error rate.
6
7use serde::{Deserialize, Serialize};
8use std::collections::HashSet;
9
10/// Types of invalid data that can be generated
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
12#[serde(rename_all = "kebab-case")]
13pub enum InvalidDataType {
14    /// Omit a required field
15    MissingField,
16    /// Provide wrong data type (string where number expected, etc.)
17    WrongType,
18    /// Provide empty string where value required
19    Empty,
20    /// Provide null where not nullable
21    Null,
22    /// Provide value outside min/max constraints
23    OutOfRange,
24    /// Provide malformed data (invalid email, URL, etc.)
25    Malformed,
26}
27
28impl std::fmt::Display for InvalidDataType {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        match self {
31            Self::MissingField => write!(f, "missing-field"),
32            Self::WrongType => write!(f, "wrong-type"),
33            Self::Empty => write!(f, "empty"),
34            Self::Null => write!(f, "null"),
35            Self::OutOfRange => write!(f, "out-of-range"),
36            Self::Malformed => write!(f, "malformed"),
37        }
38    }
39}
40
41impl std::str::FromStr for InvalidDataType {
42    type Err = String;
43
44    fn from_str(s: &str) -> Result<Self, Self::Err> {
45        match s.to_lowercase().replace('_', "-").as_str() {
46            "missing-field" | "missingfield" => Ok(Self::MissingField),
47            "wrong-type" | "wrongtype" => Ok(Self::WrongType),
48            "empty" => Ok(Self::Empty),
49            "null" => Ok(Self::Null),
50            "out-of-range" | "outofrange" => Ok(Self::OutOfRange),
51            "malformed" => Ok(Self::Malformed),
52            _ => Err(format!("Invalid error type: '{}'", s)),
53        }
54    }
55}
56
57/// Configuration for invalid data generation
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct InvalidDataConfig {
60    /// Percentage of requests that should use invalid data (0.0 to 1.0)
61    pub error_rate: f64,
62    /// Types of invalid data to generate
63    pub error_types: HashSet<InvalidDataType>,
64    /// Specific fields to target for invalidation (if empty, any field)
65    pub target_fields: Vec<String>,
66}
67
68impl Default for InvalidDataConfig {
69    fn default() -> Self {
70        let mut error_types = HashSet::new();
71        error_types.insert(InvalidDataType::MissingField);
72        error_types.insert(InvalidDataType::WrongType);
73        error_types.insert(InvalidDataType::Empty);
74
75        Self {
76            error_rate: 0.2, // 20% invalid by default
77            error_types,
78            target_fields: Vec::new(),
79        }
80    }
81}
82
83impl InvalidDataConfig {
84    /// Create a new config with a specific error rate
85    pub fn new(error_rate: f64) -> Self {
86        Self {
87            error_rate: error_rate.clamp(0.0, 1.0),
88            ..Default::default()
89        }
90    }
91
92    /// Set the error types to generate
93    pub fn with_error_types(mut self, types: HashSet<InvalidDataType>) -> Self {
94        self.error_types = types;
95        self
96    }
97
98    /// Add specific target fields
99    pub fn with_target_fields(mut self, fields: Vec<String>) -> Self {
100        self.target_fields = fields;
101        self
102    }
103
104    /// Parse error types from a comma-separated string
105    pub fn parse_error_types(s: &str) -> Result<HashSet<InvalidDataType>, String> {
106        if s.is_empty() {
107            return Ok(HashSet::new());
108        }
109
110        s.split(',').map(|t| t.trim().parse::<InvalidDataType>()).collect()
111    }
112}
113
114/// Generates k6 JavaScript code for invalid data testing
115pub struct InvalidDataGenerator;
116
117impl InvalidDataGenerator {
118    /// Generate k6 code for determining if current request should be invalid
119    pub fn generate_should_invalidate(error_rate: f64) -> String {
120        format!(
121            "// Determine if this request should use invalid data\n\
122             const shouldInvalidate = Math.random() < {};\n",
123            error_rate
124        )
125    }
126
127    /// Generate k6 code for selecting a random invalid data type
128    pub fn generate_type_selection(types: &HashSet<InvalidDataType>) -> String {
129        let type_array: Vec<String> = types.iter().map(|t| format!("'{}'", t)).collect();
130
131        format!(
132            "// Select random invalid data type\n\
133             const invalidTypes = [{}];\n\
134             const invalidType = invalidTypes[Math.floor(Math.random() * invalidTypes.length)];\n",
135            type_array.join(", ")
136        )
137    }
138
139    /// Generate k6 code for creating invalid data based on type
140    pub fn generate_invalidation_logic() -> String {
141        r#"// Apply invalidation based on selected type
142function invalidateField(value, fieldName, invalidType) {
143  switch (invalidType) {
144    case 'missing-field':
145      return undefined; // Will be filtered out
146    case 'wrong-type':
147      if (typeof value === 'number') return 'not_a_number';
148      if (typeof value === 'string') return 12345;
149      if (typeof value === 'boolean') return 'not_a_boolean';
150      if (Array.isArray(value)) return 'not_an_array';
151      return null;
152    case 'empty':
153      if (typeof value === 'string') return '';
154      if (Array.isArray(value)) return [];
155      if (typeof value === 'object') return {};
156      return null;
157    case 'null':
158      return null;
159    case 'out-of-range':
160      if (typeof value === 'number') return value > 0 ? -9999999 : 9999999;
161      if (typeof value === 'string') return 'x'.repeat(10000);
162      return value;
163    case 'malformed':
164      if (typeof value === 'string') {
165        // Check common formats and malform them
166        if (value.includes('@')) return 'not-an-email';
167        if (value.startsWith('http')) return 'not://a.valid.url';
168        return value + '%%%invalid%%%';
169      }
170      return value;
171    default:
172      return value;
173  }
174}
175
176function invalidatePayload(payload, targetFields, invalidType) {
177  const result = { ...payload };
178
179  // Determine which fields to invalidate
180  let fieldsToInvalidate;
181  if (targetFields && targetFields.length > 0) {
182    fieldsToInvalidate = targetFields;
183  } else {
184    // Pick a random field
185    const allFields = Object.keys(result);
186    fieldsToInvalidate = [allFields[Math.floor(Math.random() * allFields.length)]];
187  }
188
189  for (const field of fieldsToInvalidate) {
190    if (result.hasOwnProperty(field)) {
191      const newValue = invalidateField(result[field], field, invalidType);
192      if (newValue === undefined) {
193        delete result[field];
194      } else {
195        result[field] = newValue;
196      }
197    }
198  }
199
200  return result;
201}
202"#
203        .to_string()
204    }
205
206    /// Generate k6 code for a complete invalid data test scenario
207    pub fn generate_complete_invalidation(
208        config: &InvalidDataConfig,
209        target_fields_js: &str,
210    ) -> String {
211        let mut code = String::new();
212
213        code.push_str(&Self::generate_should_invalidate(config.error_rate));
214        code.push('\n');
215        code.push_str(&Self::generate_type_selection(&config.error_types));
216        code.push('\n');
217        code.push_str(&format!("const targetFields = {};\n\n", target_fields_js));
218        code.push_str("// Apply invalidation if needed\n");
219        code.push_str("const finalPayload = shouldInvalidate\n");
220        code.push_str("  ? invalidatePayload(payload, targetFields, invalidType)\n");
221        code.push_str("  : payload;\n");
222
223        code
224    }
225
226    /// Generate k6 code for checking expected error responses
227    pub fn generate_error_checks() -> String {
228        r#"// Check response based on whether we sent invalid data
229if (shouldInvalidate) {
230  check(res, {
231    'invalid request: expects error response': (r) => r.status >= 400,
232    'invalid request: has error message': (r) => {
233      try {
234        const body = r.json();
235        return body.error || body.message || body.errors;
236      } catch (e) {
237        return r.body && r.body.length > 0;
238      }
239    },
240  });
241} else {
242  check(res, {
243    'valid request: status is OK': (r) => r.status >= 200 && r.status < 300,
244  });
245}
246"#
247        .to_string()
248    }
249
250    /// Generate complete test helper functions
251    pub fn generate_helper_functions() -> String {
252        Self::generate_invalidation_logic()
253    }
254}
255
256#[cfg(test)]
257mod tests {
258    use super::*;
259    use std::str::FromStr;
260
261    #[test]
262    fn test_invalid_data_type_display() {
263        assert_eq!(InvalidDataType::MissingField.to_string(), "missing-field");
264        assert_eq!(InvalidDataType::WrongType.to_string(), "wrong-type");
265        assert_eq!(InvalidDataType::Empty.to_string(), "empty");
266        assert_eq!(InvalidDataType::Null.to_string(), "null");
267        assert_eq!(InvalidDataType::OutOfRange.to_string(), "out-of-range");
268        assert_eq!(InvalidDataType::Malformed.to_string(), "malformed");
269    }
270
271    #[test]
272    fn test_invalid_data_type_from_str() {
273        assert_eq!(
274            InvalidDataType::from_str("missing-field").unwrap(),
275            InvalidDataType::MissingField
276        );
277        assert_eq!(InvalidDataType::from_str("wrong-type").unwrap(), InvalidDataType::WrongType);
278        assert_eq!(InvalidDataType::from_str("empty").unwrap(), InvalidDataType::Empty);
279        assert_eq!(InvalidDataType::from_str("null").unwrap(), InvalidDataType::Null);
280        assert_eq!(InvalidDataType::from_str("out-of-range").unwrap(), InvalidDataType::OutOfRange);
281    }
282
283    #[test]
284    fn test_invalid_data_type_from_str_variants() {
285        // With underscores
286        assert_eq!(
287            InvalidDataType::from_str("missing_field").unwrap(),
288            InvalidDataType::MissingField
289        );
290
291        // Without separator
292        assert_eq!(InvalidDataType::from_str("wrongtype").unwrap(), InvalidDataType::WrongType);
293    }
294
295    #[test]
296    fn test_invalid_data_type_from_str_invalid() {
297        assert!(InvalidDataType::from_str("invalid").is_err());
298    }
299
300    #[test]
301    fn test_invalid_data_config_default() {
302        let config = InvalidDataConfig::default();
303        assert!((config.error_rate - 0.2).abs() < f64::EPSILON);
304        assert!(config.error_types.contains(&InvalidDataType::MissingField));
305        assert!(config.error_types.contains(&InvalidDataType::WrongType));
306        assert!(config.error_types.contains(&InvalidDataType::Empty));
307        assert!(config.target_fields.is_empty());
308    }
309
310    #[test]
311    fn test_invalid_data_config_new() {
312        let config = InvalidDataConfig::new(0.5);
313        assert!((config.error_rate - 0.5).abs() < f64::EPSILON);
314    }
315
316    #[test]
317    fn test_invalid_data_config_clamp() {
318        let config1 = InvalidDataConfig::new(1.5);
319        assert!((config1.error_rate - 1.0).abs() < f64::EPSILON);
320
321        let config2 = InvalidDataConfig::new(-0.5);
322        assert!((config2.error_rate - 0.0).abs() < f64::EPSILON);
323    }
324
325    #[test]
326    fn test_invalid_data_config_builders() {
327        let mut types = HashSet::new();
328        types.insert(InvalidDataType::Null);
329
330        let config = InvalidDataConfig::new(0.3)
331            .with_error_types(types)
332            .with_target_fields(vec!["email".to_string()]);
333
334        assert!((config.error_rate - 0.3).abs() < f64::EPSILON);
335        assert!(config.error_types.contains(&InvalidDataType::Null));
336        assert_eq!(config.error_types.len(), 1);
337        assert_eq!(config.target_fields, vec!["email"]);
338    }
339
340    #[test]
341    fn test_parse_error_types() {
342        let types = InvalidDataConfig::parse_error_types("missing-field,wrong-type,null").unwrap();
343        assert_eq!(types.len(), 3);
344        assert!(types.contains(&InvalidDataType::MissingField));
345        assert!(types.contains(&InvalidDataType::WrongType));
346        assert!(types.contains(&InvalidDataType::Null));
347    }
348
349    #[test]
350    fn test_parse_error_types_empty() {
351        let types = InvalidDataConfig::parse_error_types("").unwrap();
352        assert!(types.is_empty());
353    }
354
355    #[test]
356    fn test_generate_should_invalidate() {
357        let code = InvalidDataGenerator::generate_should_invalidate(0.2);
358        assert!(code.contains("Math.random() < 0.2"));
359        assert!(code.contains("shouldInvalidate"));
360    }
361
362    #[test]
363    fn test_generate_type_selection() {
364        let mut types = HashSet::new();
365        types.insert(InvalidDataType::MissingField);
366        types.insert(InvalidDataType::Null);
367
368        let code = InvalidDataGenerator::generate_type_selection(&types);
369        assert!(code.contains("invalidTypes"));
370        assert!(code.contains("Math.random()"));
371    }
372
373    #[test]
374    fn test_generate_invalidation_logic() {
375        let code = InvalidDataGenerator::generate_invalidation_logic();
376        assert!(code.contains("function invalidateField"));
377        assert!(code.contains("function invalidatePayload"));
378        assert!(code.contains("missing-field"));
379        assert!(code.contains("wrong-type"));
380        assert!(code.contains("out-of-range"));
381    }
382
383    #[test]
384    fn test_generate_complete_invalidation() {
385        let config = InvalidDataConfig::default();
386        let code = InvalidDataGenerator::generate_complete_invalidation(&config, "[]");
387
388        assert!(code.contains("shouldInvalidate"));
389        assert!(code.contains("invalidType"));
390        assert!(code.contains("targetFields"));
391        assert!(code.contains("finalPayload"));
392    }
393
394    #[test]
395    fn test_generate_error_checks() {
396        let code = InvalidDataGenerator::generate_error_checks();
397        assert!(code.contains("shouldInvalidate"));
398        assert!(code.contains("expects error response"));
399        assert!(code.contains("status is OK"));
400    }
401}