aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/code_intelligence/jazzer/mutation/combinator/MutatorCombinatorsTest.java
blob: d0d06f226056f0fdc421a04425c7923330e605d4 (plain)
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
/*
 * Copyright 2023 Code Intelligence GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.code_intelligence.jazzer.mutation.combinator;

import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.assemble;
import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.combine;
import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateProduct;
import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateProperty;
import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateSumInPlace;
import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateThenMapToImmutable;
import static com.code_intelligence.jazzer.mutation.combinator.MutatorCombinators.mutateViaView;
import static com.code_intelligence.jazzer.mutation.support.InputStreamSupport.infiniteZeros;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockCrossOver;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockCrossOverInPlace;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockInitInPlace;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockInitializer;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockMutator;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.mockPseudoRandom;
import static com.code_intelligence.jazzer.mutation.support.TestSupport.nullDataOutputStream;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.code_intelligence.jazzer.mutation.api.Debuggable;
import com.code_intelligence.jazzer.mutation.api.InPlaceMutator;
import com.code_intelligence.jazzer.mutation.api.PseudoRandom;
import com.code_intelligence.jazzer.mutation.api.Serializer;
import com.code_intelligence.jazzer.mutation.api.SerializingInPlaceMutator;
import com.code_intelligence.jazzer.mutation.api.SerializingMutator;
import com.code_intelligence.jazzer.mutation.support.TestSupport.MockPseudoRandom;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.function.ToIntFunction;
import org.junit.jupiter.api.Test;

class MutatorCombinatorsTest {
  @Test
  void testMutateProperty() {
    InPlaceMutator<Foo> mutator =
        mutateProperty(Foo::getValue, mockMutator(21, value -> 2 * value), Foo::setValue);

    assertThat(mutator.toString()).isEqualTo("Foo.Integer");

    Foo foo = new Foo(0, singletonList(13));

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.initInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(21);
    assertThat(foo.getList()).containsExactly(13);

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.mutateInPlace(foo, prng);
    }

    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(13);
  }

  @Test
  void testCrossOverProperty() {
    InPlaceMutator<Foo> mutator =
        mutateProperty(Foo::getValue, mockCrossOver((a, b) -> 42), Foo::setValue);
    Foo foo = new Foo(0);
    Foo otherFoo = new Foo(1);
    try (MockPseudoRandom prng = mockPseudoRandom(
             // use foo value
             0)) {
      mutator.crossOverInPlace(foo, otherFoo, prng);
      assertThat(foo.getValue()).isEqualTo(0);
    }
    try (MockPseudoRandom prng = mockPseudoRandom(
             // use otherFoo value
             1)) {
      mutator.crossOverInPlace(foo, otherFoo, prng);
      assertThat(foo.getValue()).isEqualTo(1);
    }
    try (MockPseudoRandom prng = mockPseudoRandom(
             // use property type cross over
             2)) {
      mutator.crossOverInPlace(foo, otherFoo, prng);
      assertThat(foo.getValue()).isEqualTo(42);
    }
  }

  @Test
  void testMutateViaView() {
    InPlaceMutator<Foo> mutator = mutateViaView(Foo::getList, new InPlaceMutator<List<Integer>>() {
      @Override
      public void initInPlace(List<Integer> reference, PseudoRandom prng) {
        reference.clear();
        reference.add(21);
      }

      @Override
      public void mutateInPlace(List<Integer> reference, PseudoRandom prng) {
        reference.add(reference.get(reference.size() - 1) + 1);
      }

      @Override
      public void crossOverInPlace(
          List<Integer> reference, List<Integer> otherReference, PseudoRandom prng) {}

      @Override
      public String toDebugString(Predicate<Debuggable> isInCycle) {
        return "List<Integer>";
      }
    });

    assertThat(mutator.toString()).isEqualTo("Foo via List<Integer>");

    Foo foo = new Foo(13, singletonList(13));

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.initInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(13);
    assertThat(foo.getList()).containsExactly(21);

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.mutateInPlace(foo, prng);
    }

    assertThat(foo.getValue()).isEqualTo(13);
    assertThat(foo.getList()).containsExactly(21, 22);
  }

  @Test
  void testCrossOverViaView() {
    InPlaceMutator<Foo> mutator = mutateViaView(Foo::getList, mockCrossOverInPlace((a, b) -> {
      a.clear();
      a.add(42);
    }));

    Foo foo = new Foo(0, singletonList(0));
    Foo otherFoo = new Foo(0, singletonList(1));
    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.crossOverInPlace(foo, otherFoo, prng);
      assertThat(foo.getList()).containsExactly(42);
    }
  }

  @Test
  void testMutateCombine() {
    InPlaceMutator<Foo> valueMutator =
        mutateProperty(Foo::getValue, mockMutator(21, value -> 2 * value), Foo::setValue);

    InPlaceMutator<Foo> listMutator =
        mutateViaView(Foo::getList, new InPlaceMutator<List<Integer>>() {
          @Override
          public void initInPlace(List<Integer> reference, PseudoRandom prng) {
            reference.clear();
            reference.add(21);
          }

          @Override
          public void mutateInPlace(List<Integer> reference, PseudoRandom prng) {
            reference.add(reference.get(reference.size() - 1) + 1);
          }

          @Override
          public void crossOverInPlace(
              List<Integer> reference, List<Integer> otherReference, PseudoRandom prng) {}

          @Override
          public String toDebugString(Predicate<Debuggable> isInCycle) {
            return "List<Integer>";
          }
        });
    InPlaceMutator<Foo> mutator = combine(valueMutator, listMutator);

    assertThat(mutator.toString()).isEqualTo("{Foo.Integer, Foo via List<Integer>}");

    Foo foo = new Foo(13, singletonList(13));

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.initInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(21);
    assertThat(foo.getList()).containsExactly(21);

    try (MockPseudoRandom prng = mockPseudoRandom(/* use valueMutator */ 0)) {
      mutator.mutateInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(21);

    try (MockPseudoRandom prng = mockPseudoRandom(/* use listMutator */ 1)) {
      mutator.mutateInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(21, 22);
  }

  @Test
  void testCrossOverCombine() {
    InPlaceMutator<Foo> valueMutator =
        mutateProperty(Foo::getValue, mockCrossOver((a, b) -> 42), Foo::setValue);
    InPlaceMutator<Foo> listMutator = mutateViaView(Foo::getList, mockCrossOverInPlace((a, b) -> {
      a.clear();
      a.add(42);
    }));
    InPlaceMutator<Foo> mutator = combine(valueMutator, listMutator);

    Foo foo = new Foo(0, singletonList(0));
    Foo fooOther = new Foo(1, singletonList(1));

    try (MockPseudoRandom prng = mockPseudoRandom(
             // call cross over in property mutator
             2)) {
      mutator.crossOverInPlace(foo, fooOther, prng);
    }
    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(42);
  }

  @Test
  void testCrossOverEmptyCombine() {
    Foo foo = new Foo(0, singletonList(0));
    Foo fooOther = new Foo(1, singletonList(1));
    InPlaceMutator<Foo> emptyCombineMutator = combine();
    try (MockPseudoRandom prng = mockPseudoRandom()) {
      emptyCombineMutator.crossOverInPlace(foo, fooOther, prng);
    }
    assertThat(foo.getValue()).isEqualTo(0);
    assertThat(foo.getList()).containsExactly(0);
  }

  @Test
  void testMutateAssemble() {
    InPlaceMutator<Foo> valueMutator =
        mutateProperty(Foo::getValue, mockMutator(21, value -> 2 * value), Foo::setValue);

    InPlaceMutator<Foo> listMutator =
        mutateViaView(Foo::getList, new InPlaceMutator<List<Integer>>() {
          @Override
          public void initInPlace(List<Integer> reference, PseudoRandom prng) {
            reference.clear();
            reference.add(21);
          }

          @Override
          public void mutateInPlace(List<Integer> reference, PseudoRandom prng) {
            reference.add(reference.get(reference.size() - 1) + 1);
          }

          @Override
          public void crossOverInPlace(
              List<Integer> reference, List<Integer> otherReference, PseudoRandom prng) {}

          @Override
          public String toDebugString(Predicate<Debuggable> isInCycle) {
            return "List<Integer>";
          }
        });

    SerializingInPlaceMutator<Foo> mutator =
        assemble((m) -> {}, () -> new Foo(0, singletonList(0)), new Serializer<Foo>() {
          @Override
          public Foo read(DataInputStream in) {
            return null;
          }

          @Override
          public void write(Foo value, DataOutputStream out) {}

          @Override
          public Foo detach(Foo value) {
            return null;
          }
        }, () -> combine(valueMutator, listMutator));

    assertThat(mutator.toString()).isEqualTo("{Foo.Integer, Foo via List<Integer>}");

    Foo foo = new Foo(13, singletonList(13));

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.initInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(21);
    assertThat(foo.getList()).containsExactly(21);

    try (MockPseudoRandom prng = mockPseudoRandom(/* use valueMutator */ 0)) {
      mutator.mutateInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(21);

    try (MockPseudoRandom prng = mockPseudoRandom(/* use listMutator */ 1)) {
      mutator.mutateInPlace(foo, prng);
    }
    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(21, 22);
  }

  @Test
  void testCrossOverAssemble() {
    InPlaceMutator<Foo> valueMutator =
        mutateProperty(Foo::getValue, mockCrossOver((a, b) -> 42), Foo::setValue);

    InPlaceMutator<Foo> listMutator = mutateViaView(Foo::getList, mockCrossOverInPlace((a, b) -> {
      a.clear();
      a.add(42);
    }));

    SerializingInPlaceMutator<Foo> mutator =
        assemble((m) -> {}, () -> new Foo(0, singletonList(0)), new Serializer<Foo>() {
          @Override
          public Foo read(DataInputStream in) {
            return null;
          }

          @Override
          public void write(Foo value, DataOutputStream out) {}

          @Override
          public Foo detach(Foo value) {
            return null;
          }
        }, () -> combine(valueMutator, listMutator));

    Foo foo = new Foo(0, singletonList(0));
    Foo fooOther = new Foo(1, singletonList(1));

    try (MockPseudoRandom prng = mockPseudoRandom(
             // cross over in property mutator
             2)) {
      mutator.crossOverInPlace(foo, fooOther, prng);
    }
    assertThat(foo.getValue()).isEqualTo(42);
    assertThat(foo.getList()).containsExactly(42);
  }

  @Test
  void testMutateThenMapToImmutable() throws IOException {
    SerializingMutator<char[]> charMutator =
        mockMutator(new char[] {'H', 'e', 'l', 'l', 'o'}, chars -> {
          for (int i = 0; i < chars.length; i++) {
            chars[i] ^= (1 << 5);
          }
          chars[chars.length - 1]++;
          return chars;
        });
    SerializingMutator<String> mutator =
        mutateThenMapToImmutable(charMutator, String::new, String::toCharArray);

    assertThat(mutator.toString()).isEqualTo("char[] -> String");

    String value = mutator.read(new DataInputStream(infiniteZeros()));
    assertThat(value).isEqualTo("Hello");

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      value = mutator.mutate(value, prng);
    }
    assertThat(value).isEqualTo("hELLP");

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      value = mutator.mutate(value, prng);
    }
    assertThat(value).isEqualTo("Hellq");

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      value = mutator.init(prng);
    }
    assertThat(value).isEqualTo("Hello");

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      value = mutator.mutate(value, prng);
    }
    assertThat(value).isEqualTo("hELLP");

    final String capturedValue = value;
    assertThrows(UnsupportedOperationException.class,
        () -> mutator.write(capturedValue, nullDataOutputStream()));
  }

  @Test
  void testCrossOverThenMapToImmutable() {
    SerializingMutator<char[]> charMutator = mockCrossOver((a, b) -> {
      assertThat(a).isEqualTo(new char[] {'H', 'e', 'l', 'l', 'o'});
      assertThat(b).isEqualTo(new char[] {'W', 'o', 'r', 'l', 'd'});
      return new char[] {'T', 'e', 's', 't', 'e', 'd'};
    });
    SerializingMutator<String> mutator =
        mutateThenMapToImmutable(charMutator, String::new, String::toCharArray);

    String crossedOver;
    try (MockPseudoRandom prng = mockPseudoRandom()) {
      crossedOver = mutator.crossOver("Hello", "World", prng);
    }
    assertThat(crossedOver).isEqualTo("Tested");
  }

  @Test
  void testCrossOverProduct() {
    SerializingMutator<Boolean> mutator1 = mockCrossOver((a, b) -> true);
    SerializingMutator<Integer> mutator2 = mockCrossOver((a, b) -> 42);
    ProductMutator mutator = mutateProduct(mutator1, mutator2);

    try (MockPseudoRandom prng = mockPseudoRandom(
             // use first value in mutator1
             0,
             // use second value in mutator2
             0)) {
      Object[] crossedOver =
          mutator.crossOver(new Object[] {false, 0}, new Object[] {true, 1}, prng);
      assertThat(crossedOver).isEqualTo(new Object[] {false, 0});
    }

    try (MockPseudoRandom prng = mockPseudoRandom(
             // use first value in mutator1
             1,
             // use second value in mutator2
             1)) {
      Object[] crossedOver =
          mutator.crossOver(new Object[] {false, 0}, new Object[] {true, 1}, prng);
      assertThat(crossedOver).isEqualTo(new Object[] {true, 1});
    }

    try (MockPseudoRandom prng = mockPseudoRandom(
             // use cross over in mutator1
             2,
             // use cross over in mutator2
             2)) {
      Object[] crossedOver =
          mutator.crossOver(new Object[] {false, 0}, new Object[] {true, 2}, prng);
      assertThat(crossedOver).isEqualTo(new Object[] {true, 42});
    }
  }

  @Test
  void testCrossOverSumInPlaceSameType() {
    ToIntFunction<List<Integer>> mutotarIndexFromValue = (r) -> 0;
    InPlaceMutator<List<Integer>> mutator1 = mockCrossOverInPlace((a, b) -> { a.add(42); });
    InPlaceMutator<List<Integer>> mutator2 = mockCrossOverInPlace((a, b) -> {});
    InPlaceMutator<List<Integer>> mutator =
        mutateSumInPlace(mutotarIndexFromValue, mutator1, mutator2);

    List<Integer> a = new ArrayList<>();
    List<Integer> b = new ArrayList<>();

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.crossOverInPlace(a, b, prng);
    }
    assertThat(a).containsExactly(42);
  }

  @Test
  void testCrossOverSumInPlaceIndeterminate() {
    InPlaceMutator<List<?>> mutator1 = mockCrossOverInPlace((a, b) -> {});
    InPlaceMutator<List<?>> mutator2 = mockCrossOverInPlace((a, b) -> {});
    ToIntFunction<List<?>> bothIndeterminate = (r) -> - 1;

    InPlaceMutator<List<?>> mutator = mutateSumInPlace(bothIndeterminate, mutator1, mutator2);

    List<Integer> a = new ArrayList<>();
    a.add(42);
    List<Integer> b = new ArrayList<>();

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.crossOverInPlace(a, b, prng);
      assertThat(a).containsExactly(42);
    }
  }

  @Test
  void testCrossOverSumInPlaceFirstIndeterminate() {
    List<Integer> reference = new ArrayList<>();
    List<Integer> otherReference = new ArrayList<>();

    InPlaceMutator<List<Integer>> mutator1 = mockCrossOverInPlace((a, b) -> {});
    InPlaceMutator<List<Integer>> mutator2 = mockInitInPlace((l) -> { l.add(42); });
    ToIntFunction<List<Integer>> firstIndeterminate = (r) -> r == reference ? -1 : 1;

    InPlaceMutator<List<Integer>> mutator =
        mutateSumInPlace(firstIndeterminate, mutator1, mutator2);

    try (MockPseudoRandom prng = mockPseudoRandom()) {
      mutator.crossOverInPlace(reference, otherReference, prng);
      assertThat(reference).containsExactly(42);
    }
  }

  static class Foo {
    private int value;
    private final List<Integer> list;

    public Foo(int value) {
      this(value, new ArrayList<>());
    }
    public Foo(int value, List<Integer> list) {
      this.value = value;
      this.list = new ArrayList<>(list);
    }

    public List<Integer> getList() {
      return list;
    }

    public int getValue() {
      return value;
    }

    public void setValue(int value) {
      this.value = value;
    }
  }
}