mutatis 0.5.2

`mutatis` is a library for writing custom, structure-aware test-case mutators for fuzzers in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
use super::*;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

/// The default mutator for `Ipv4Addr` values.
///
/// See the [`ipv4_addr()`] function to create new instances and for example
/// usage.
#[derive(Clone, Debug, Default)]
pub struct Ipv4AddrMutator {
    _private: (),
}

/// Create a new mutator for `Ipv4Addr` values.
///
/// # Example
///
/// ```
/// # fn foo() -> mutatis::Result<()> {
/// use mutatis::{mutators as m, Mutate, Session};
/// use std::net::Ipv4Addr;
///
/// let mut value = Ipv4Addr::LOCALHOST;
///
/// let mut mutator = m::ipv4_addr();
///
/// let mut session = Session::new();
/// for _ in 0..5 {
///     session.mutate_with(&mut mutator, &mut value)?;
///     println!("value = {value}");
/// }
///
/// // Example output:
/// //
/// //     value = 0.0.0.0
/// //     value = 192.168.0.1
/// //     value = 255.255.255.255
/// //     value = 10.0.0.1
/// //     value = 10.0.0.229
/// # Ok(()) }
/// # foo().unwrap();
/// ```
pub fn ipv4_addr() -> Ipv4AddrMutator {
    Ipv4AddrMutator { _private: () }
}

impl Mutate<Ipv4Addr> for Ipv4AddrMutator {
    #[inline]
    fn mutation_count(&self, _value: &Ipv4Addr, _shrink: bool) -> core::option::Option<u32> {
        // 4 octets + 5 specials.
        Some(9)
    }

    #[inline]
    fn mutate(&mut self, c: &mut Candidates, value: &mut Ipv4Addr) -> Result<()> {
        // Mutate an octet.
        c.mutation_group(4, |ctx, which| {
            let mut octets = value.octets();
            octets[which as usize] = ctx.rng().gen_u8();
            *value = Ipv4Addr::from(octets);
            Ok(())
        })?;

        // Special: loopback.
        c.mutation(|_ctx| {
            *value = Ipv4Addr::new(127, 0, 0, 1);
            Ok(())
        })?;

        // Special: unspecified.
        c.mutation(|_ctx| {
            *value = Ipv4Addr::new(0, 0, 0, 0);
            Ok(())
        })?;

        // Special: broadcast.
        c.mutation(|_ctx| {
            *value = Ipv4Addr::new(255, 255, 255, 255);
            Ok(())
        })?;

        // Special: private 192.168.0.1.
        c.mutation(|_ctx| {
            *value = Ipv4Addr::new(192, 168, 0, 1);
            Ok(())
        })?;

        // Special: private 10.0.0.1.
        c.mutation(|_ctx| {
            *value = Ipv4Addr::new(10, 0, 0, 1);
            Ok(())
        })?;

        Ok(())
    }
}

impl Generate<Ipv4Addr> for Ipv4AddrMutator {
    #[inline]
    fn generate(&mut self, ctx: &mut Context) -> Result<Ipv4Addr> {
        let a = ctx.rng().gen_u8();
        let b = ctx.rng().gen_u8();
        let c = ctx.rng().gen_u8();
        let d = ctx.rng().gen_u8();
        Ok(Ipv4Addr::new(a, b, c, d))
    }
}

impl DefaultMutate for Ipv4Addr {
    type DefaultMutate = Ipv4AddrMutator;
}

/// The default mutator for `Ipv6Addr` values.
///
/// See the [`ipv6_addr()`] function to create new instances and for example
/// usage.
#[derive(Clone, Debug, Default)]
pub struct Ipv6AddrMutator {
    _private: (),
}

/// Create a new mutator for `Ipv6Addr` values.
///
/// # Example
///
/// ```
/// # fn foo() -> mutatis::Result<()> {
/// use mutatis::{mutators as m, Mutate, Session};
/// use std::net::Ipv6Addr;
///
/// let mut value = Ipv6Addr::LOCALHOST;
///
/// let mut mutator = m::ipv6_addr();
///
/// let mut session = Session::new();
/// for _ in 0..5 {
///     session.mutate_with(&mut mutator, &mut value)?;
///     println!("value = {value}");
/// }
///
/// // Example output:
/// //
/// //     value = ::f886:0:1
/// //     value = ff02::1
/// //     value = ff02::cb11:1
/// //     value = ff02::372c:0:cb11:1
/// //     value = ff02:0:2a39:0:372c:0:cb11:1
/// # Ok(()) }
/// # foo().unwrap();
/// ```
pub fn ipv6_addr() -> Ipv6AddrMutator {
    Ipv6AddrMutator { _private: () }
}

impl Mutate<Ipv6Addr> for Ipv6AddrMutator {
    #[inline]
    fn mutation_count(&self, _value: &Ipv6Addr, _shrink: bool) -> core::option::Option<u32> {
        // 8 segments + 3 specials.
        Some(11)
    }

    #[inline]
    fn mutate(&mut self, c: &mut Candidates, value: &mut Ipv6Addr) -> Result<()> {
        // Mutate a segment.
        c.mutation_group(8, |ctx, which| {
            let mut segs = value.segments();
            segs[which as usize] = ctx.rng().gen_u16();
            *value = Ipv6Addr::from(segs);
            Ok(())
        })?;

        // Special: loopback (::1).
        c.mutation(|_ctx| {
            *value = Ipv6Addr::LOCALHOST;
            Ok(())
        })?;

        // Special: unspecified (::).
        c.mutation(|_ctx| {
            *value = Ipv6Addr::UNSPECIFIED;
            Ok(())
        })?;

        // Special: all-nodes multicast (ff02::1).
        c.mutation(|_ctx| {
            *value = Ipv6Addr::new(0xff02, 0, 0, 0, 0, 0, 0, 1);
            Ok(())
        })?;

        Ok(())
    }
}

impl Generate<Ipv6Addr> for Ipv6AddrMutator {
    #[inline]
    fn generate(&mut self, ctx: &mut Context) -> Result<Ipv6Addr> {
        let segs: [u16; 8] = [
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
            ctx.rng().gen_u16(),
        ];
        Ok(Ipv6Addr::from(segs))
    }
}

impl DefaultMutate for Ipv6Addr {
    type DefaultMutate = Ipv6AddrMutator;
}

/// The default mutator for `IpAddr` values.
///
/// See the [`ip_addr()`] function to create new instances and for example
/// usage.
#[derive(Clone, Debug, Default)]
pub struct IpAddrMutator {
    v4: Ipv4AddrMutator,
    v6: Ipv6AddrMutator,
}

/// Create a new mutator for `IpAddr` values.
///
/// # Example
///
/// ```
/// # fn foo() -> mutatis::Result<()> {
/// use mutatis::{mutators as m, Mutate, Session};
/// use std::net::IpAddr;
///
/// let mut value: IpAddr = "127.0.0.1".parse().unwrap();
///
/// let mut mutator = m::ip_addr();
///
/// let mut session = Session::new();
/// for _ in 0..5 {
///     session.mutate_with(&mut mutator, &mut value)?;
///     println!("value = {value}");
/// }
///
/// // Example output:
/// //
/// //     value = 127.0.0.41
/// //     value = 186.0.0.41
/// //     value = 186.0.0.252
/// //     value = 10.0.0.1
/// //     value = 10.0.117.1
/// # Ok(()) }
/// # foo().unwrap();
/// ```
pub fn ip_addr() -> IpAddrMutator {
    IpAddrMutator::default()
}

impl Mutate<IpAddr> for IpAddrMutator {
    #[inline]
    fn mutation_count(&self, value: &IpAddr, shrink: bool) -> core::option::Option<u32> {
        // Mutate the inner address.
        let inner = match value {
            IpAddr::V4(addr) => self.v4.mutation_count(addr, shrink)?,
            IpAddr::V6(addr) => self.v6.mutation_count(addr, shrink)?,
        };
        // Switch between V4 and V6.
        Some(inner + 1)
    }

    #[inline]
    fn mutate(&mut self, c: &mut Candidates, value: &mut IpAddr) -> Result<()> {
        // Mutate the inner address.
        match value {
            IpAddr::V4(ref mut addr) => self.v4.mutate(c, addr)?,
            IpAddr::V6(ref mut addr) => self.v6.mutate(c, addr)?,
        }

        // Switch between V4 and V6.
        c.mutation(|ctx| {
            *value = match *value {
                IpAddr::V4(_) => IpAddr::V6(self.v6.generate(ctx)?),
                IpAddr::V6(_) => IpAddr::V4(self.v4.generate(ctx)?),
            };
            Ok(())
        })?;

        Ok(())
    }
}

impl Generate<IpAddr> for IpAddrMutator {
    #[inline]
    fn generate(&mut self, ctx: &mut Context) -> Result<IpAddr> {
        if ctx.rng().gen_bool() {
            Ok(IpAddr::V4(self.v4.generate(ctx)?))
        } else {
            Ok(IpAddr::V6(self.v6.generate(ctx)?))
        }
    }
}

impl DefaultMutate for IpAddr {
    type DefaultMutate = IpAddrMutator;
}

/// The default mutator for `SocketAddrV4` values.
///
/// See the [`socket_addr_v4()`] function to create new instances and for
/// example usage.
#[derive(Clone, Debug, Default)]
pub struct SocketAddrV4Mutator {
    addr: Ipv4AddrMutator,
}

/// Create a new mutator for `SocketAddrV4` values.
///
/// # Example
///
/// ```
/// # fn foo() -> mutatis::Result<()> {
/// use mutatis::{mutators as m, Mutate, Session};
/// use std::net::SocketAddrV4;
///
/// let mut value: SocketAddrV4 = "127.0.0.1:8080".parse().unwrap();
///
/// let mut mutator = m::socket_addr_v4();
///
/// let mut session = Session::new();
/// for _ in 0..5 {
///     session.mutate_with(&mut mutator, &mut value)?;
///     println!("value = {value}");
/// }
///
/// // Example output:
/// //
/// //     value = 127.0.0.1:51985
/// //     value = 172.0.0.1:51985
/// //     value = 172.0.40.1:51985
/// //     value = 172.0.40.1:22969
/// //     value = 172.0.14.1:22969
/// # Ok(()) }
/// # foo().unwrap();
/// ```
pub fn socket_addr_v4() -> SocketAddrV4Mutator {
    SocketAddrV4Mutator::default()
}

impl Mutate<SocketAddrV4> for SocketAddrV4Mutator {
    #[inline]
    fn mutation_count(&self, _value: &SocketAddrV4, _shrink: bool) -> core::option::Option<u32> {
        // Mutate address + mutate port.
        Some(2)
    }

    #[inline]
    fn mutate(&mut self, c: &mut Candidates, value: &mut SocketAddrV4) -> Result<()> {
        // Mutate the address.
        c.mutation(|ctx| {
            let mut ip = *value.ip();
            let result = ctx.mutate_with(&mut self.addr, &mut ip);
            value.set_ip(ip);
            result
        })?;

        // Mutate the port.
        c.mutation(|ctx| {
            value.set_port(ctx.rng().gen_u16());
            Ok(())
        })?;

        Ok(())
    }
}

impl Generate<SocketAddrV4> for SocketAddrV4Mutator {
    #[inline]
    fn generate(&mut self, ctx: &mut Context) -> Result<SocketAddrV4> {
        let ip = self.addr.generate(ctx)?;
        let port = ctx.rng().gen_u16();
        Ok(SocketAddrV4::new(ip, port))
    }
}

impl DefaultMutate for SocketAddrV4 {
    type DefaultMutate = SocketAddrV4Mutator;
}

/// The default mutator for `SocketAddrV6` values.
///
/// See the [`socket_addr_v6()`] function to create new instances and for
/// example usage.
#[derive(Clone, Debug, Default)]
pub struct SocketAddrV6Mutator {
    addr: Ipv6AddrMutator,
}

/// Create a new mutator for `SocketAddrV6` values.
///
/// # Example
///
/// ```
/// # fn foo() -> mutatis::Result<()> {
/// use mutatis::{mutators as m, Mutate, Session};
/// use std::net::SocketAddrV6;
///
/// let mut value: SocketAddrV6 = "[::1]:8080".parse().unwrap();
///
/// let mut mutator = m::socket_addr_v6();
///
/// let mut session = Session::new();
/// for _ in 0..5 {
///     session.mutate_with(&mut mutator, &mut value)?;
///     println!("value = {value}");
/// }
///
/// // Example output:
/// //
/// //     value = [::1]:47657
/// //     value = [0:6b2::1]:47657
/// //     value = [0:6b2::1%3565297535]:47657
/// //     value = [0:6b2::1%3565297535]:30810
/// //     value = [0:6b2::1%75952059]:30810
/// # Ok(()) }
/// # foo().unwrap();
/// ```
pub fn socket_addr_v6() -> SocketAddrV6Mutator {
    SocketAddrV6Mutator::default()
}

impl Mutate<SocketAddrV6> for SocketAddrV6Mutator {
    #[inline]
    fn mutation_count(&self, _value: &SocketAddrV6, _shrink: bool) -> core::option::Option<u32> {
        // Mutate address + mutate port + mutate flowinfo + mutate scope_id.
        Some(4)
    }

    #[inline]
    fn mutate(&mut self, c: &mut Candidates, value: &mut SocketAddrV6) -> Result<()> {
        // Mutate the address.
        c.mutation(|ctx| {
            let mut ip = *value.ip();
            let result = ctx.mutate_with(&mut self.addr, &mut ip);
            value.set_ip(ip);
            result
        })?;

        // Mutate the port.
        c.mutation(|ctx| {
            value.set_port(ctx.rng().gen_u16());
            Ok(())
        })?;

        // Mutate the flowinfo.
        c.mutation(|ctx| {
            value.set_flowinfo(ctx.rng().gen_u32());
            Ok(())
        })?;

        // Mutate the scope_id.
        c.mutation(|ctx| {
            value.set_scope_id(ctx.rng().gen_u32());
            Ok(())
        })?;

        Ok(())
    }
}

impl Generate<SocketAddrV6> for SocketAddrV6Mutator {
    #[inline]
    fn generate(&mut self, ctx: &mut Context) -> Result<SocketAddrV6> {
        let ip = self.addr.generate(ctx)?;
        let port = ctx.rng().gen_u16();
        let flowinfo = ctx.rng().gen_u32();
        let scope_id = ctx.rng().gen_u32();
        Ok(SocketAddrV6::new(ip, port, flowinfo, scope_id))
    }
}

impl DefaultMutate for SocketAddrV6 {
    type DefaultMutate = SocketAddrV6Mutator;
}

/// The default mutator for `SocketAddr` values.
///
/// See the [`socket_addr()`] function to create new instances and for example
/// usage.
#[derive(Clone, Debug, Default)]
pub struct SocketAddrMutator {
    v4: SocketAddrV4Mutator,
    v6: SocketAddrV6Mutator,
}

/// Create a new mutator for `SocketAddr` values.
///
/// # Example
///
/// ```
/// # fn foo() -> mutatis::Result<()> {
/// use mutatis::{mutators as m, Mutate, Session};
/// use std::net::SocketAddr;
///
/// let mut value: SocketAddr = "127.0.0.1:8080".parse().unwrap();
///
/// let mut mutator = m::socket_addr();
///
/// let mut session = Session::new();
/// for _ in 0..5 {
///     session.mutate_with(&mut mutator, &mut value)?;
///     println!("value = {value}");
/// }
///
/// // Example output:
/// //
/// //     value = 10.0.0.1:8080
/// //     value = [6d8c:85ab:8ef5:7347:c496:407a:a9e8:b67f%2315231880]:20328
/// //     value = [6d8c:85ab:8ef5:7347:c496:407a:a9e8:b67f%2315231880]:20328
/// //     value = [ff02::1%2315231880]:20328
/// //     value = 64.139.39.75:13206
/// # Ok(()) }
/// # foo().unwrap();
/// ```
pub fn socket_addr() -> SocketAddrMutator {
    SocketAddrMutator::default()
}

impl Mutate<SocketAddr> for SocketAddrMutator {
    #[inline]
    fn mutation_count(&self, value: &SocketAddr, shrink: bool) -> core::option::Option<u32> {
        // Mutate the inner address.
        let inner = match value {
            SocketAddr::V4(addr) => self.v4.mutation_count(addr, shrink)?,
            SocketAddr::V6(addr) => self.v6.mutation_count(addr, shrink)?,
        };
        // Switch between V4 and V6.
        Some(inner + 1)
    }

    #[inline]
    fn mutate(&mut self, c: &mut Candidates, value: &mut SocketAddr) -> Result<()> {
        // Mutate the inner address.
        match value {
            SocketAddr::V4(ref mut addr) => self.v4.mutate(c, addr)?,
            SocketAddr::V6(ref mut addr) => self.v6.mutate(c, addr)?,
        }

        // Switch between V4 and V6.
        c.mutation(|ctx| {
            *value = match *value {
                SocketAddr::V4(_) => SocketAddr::V6(self.v6.generate(ctx)?),
                SocketAddr::V6(_) => SocketAddr::V4(self.v4.generate(ctx)?),
            };
            Ok(())
        })?;

        Ok(())
    }
}

impl Generate<SocketAddr> for SocketAddrMutator {
    #[inline]
    fn generate(&mut self, ctx: &mut Context) -> Result<SocketAddr> {
        if ctx.rng().gen_bool() {
            Ok(SocketAddr::V4(self.v4.generate(ctx)?))
        } else {
            Ok(SocketAddr::V6(self.v6.generate(ctx)?))
        }
    }
}

impl DefaultMutate for SocketAddr {
    type DefaultMutate = SocketAddrMutator;
}