aboutsummaryrefslogtreecommitdiff
path: root/agent/src/test/java/com/code_intelligence/jazzer/autofuzz/MetaTest.java
blob: 0615e9ae0ddcd6531bf31f6364217371738438be (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
// Copyright 2021 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.autofuzz;

import static com.code_intelligence.jazzer.autofuzz.TestHelpers.autofuzzTestCase;
import static com.code_intelligence.jazzer.autofuzz.TestHelpers.consumeTestCase;
import static org.junit.Assert.assertEquals;

import com.code_intelligence.jazzer.api.CannedFuzzedDataProvider;
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.google.json.JsonSanitizer;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;

public class MetaTest {
  public static boolean isFive(int arg) {
    return arg == 5;
  }

  public static boolean intEquals(int arg1, int arg2) {
    return arg1 == arg2;
  }

  public enum TestEnum {
    FOO,
    BAR,
    BAZ,
  }

  @Test
  public void testConsume() {
    consumeTestCase(5, "5", Collections.singletonList(5));
    consumeTestCase((short) 5, "(short) 5", Collections.singletonList((short) 5));
    consumeTestCase(5L, "5L", Collections.singletonList(5L));
    consumeTestCase(5.0F, "5.0F", Collections.singletonList(5.0F));
    consumeTestCase('\n', "'\\\\n'", Collections.singletonList('\n'));
    consumeTestCase('\'', "'\\\\''", Collections.singletonList('\''));
    consumeTestCase('\\', "'\\\\'", Collections.singletonList('\\'));

    String testString = "foo\n\t\\\"bar";
    // The expected string is obtained from testString by escaping, wrapping into quotes and
    // escaping again.
    consumeTestCase(testString, "\"foo\\\\n\\\\t\\\\\\\\\"bar\"",
        Arrays.asList((byte) 1, // do not return null
            testString.length(), testString));

    consumeTestCase(null, "null", Collections.singletonList((byte) 0));

    boolean[] testBooleans = new boolean[] {true, false, true};
    consumeTestCase(testBooleans, "new boolean[]{true, false, true}",
        Arrays.asList((byte) 1, // do not return null for the array
            2 * 3, testBooleans));

    char[] testChars = new char[] {'a', '\n', '\''};
    consumeTestCase(testChars, "new char[]{'a', '\\\\n', '\\\\''}",
        Arrays.asList((byte) 1, // do not return null for the array
            2 * 3 * Character.BYTES + Character.BYTES, testChars[0], 2 * 3 * Character.BYTES,
            2 * 3 * Character.BYTES, // remaining bytes, 2 times what is needed for 3 chars
            testChars[1], testChars[2]));

    char[] testNoChars = new char[] {};
    consumeTestCase(testNoChars, "new char[]{}",
        Arrays.asList((byte) 1, // do not return null for the array
            0, 'a', 0, 0));

    short[] testShorts = new short[] {(short) 1, (short) 2, (short) 3};
    consumeTestCase(testShorts, "new short[]{(short) 1, (short) 2, (short) 3}",
        Arrays.asList((byte) 1, // do not return null for the array
            2 * 3 * Short.BYTES, // remaining bytes
            testShorts));

    long[] testLongs = new long[] {1L, 2L, 3L};
    consumeTestCase(testLongs, "new long[]{1L, 2L, 3L}",
        Arrays.asList((byte) 1, // do not return null for the array
            2 * 3 * Long.BYTES, // remaining bytes
            testLongs));

    consumeTestCase(new String[] {"foo", "bar", "foo\nbar"},
        "new java.lang.String[]{\"foo\", \"bar\", \"foo\\\\nbar\"}",
        Arrays.asList((byte) 1, // do not return null for the array
            32, // remaining bytes
            (byte) 1, // do not return null for the string
            31, // remaining bytes
            "foo",
            28, // remaining bytes
            28, // array length
            (byte) 1, // do not return null for the string
            27, // remaining bytes
            "bar",
            (byte) 1, // do not return null for the string
            23, // remaining bytes
            "foo\nbar"));

    byte[] testInputStreamBytes = new byte[] {(byte) 1, (byte) 2, (byte) 3};
    consumeTestCase(new ByteArrayInputStream(testInputStreamBytes),
        "new java.io.ByteArrayInputStream(new byte[]{(byte) 1, (byte) 2, (byte) 3})",
        Arrays.asList((byte) 1, // do not return null for the InputStream
            2 * 3, // remaining bytes (twice the desired length)
            testInputStreamBytes));

    consumeTestCase(TestEnum.BAR,
        String.format("%s.%s", TestEnum.class.getName(), TestEnum.BAR.name()),
        Arrays.asList((byte) 1, // do not return null for the enum value
            1 /* second value */
            ));

    consumeTestCase(YourAverageJavaClass.class,
        "com.code_intelligence.jazzer.autofuzz.YourAverageJavaClass.class",
        Collections.singletonList((byte) 1));
  }

  @Test
  public void testAutofuzz() throws NoSuchMethodException {
    autofuzzTestCase(true, "com.code_intelligence.jazzer.autofuzz.MetaTest.isFive(5)",
        MetaTest.class.getMethod("isFive", int.class), Collections.singletonList(5));
    autofuzzTestCase(false, "com.code_intelligence.jazzer.autofuzz.MetaTest.intEquals(5, 4)",
        MetaTest.class.getMethod("intEquals", int.class, int.class), Arrays.asList(5, 4));
    autofuzzTestCase("foobar", "\"foo\".concat(\"bar\")",
        String.class.getMethod("concat", String.class),
        Arrays.asList((byte) 1, 6, "foo", (byte) 1, 6, "bar"));
    autofuzzTestCase("jazzer", "new java.lang.String(\"jazzer\")",
        String.class.getConstructor(String.class), Arrays.asList((byte) 1, 12, "jazzer"));
    autofuzzTestCase("\"jazzer\"", "com.google.json.JsonSanitizer.sanitize(\"jazzer\")",
        JsonSanitizer.class.getMethod("sanitize", String.class),
        Arrays.asList((byte) 1, 12, "jazzer"));

    FuzzedDataProvider data =
        CannedFuzzedDataProvider.create(Arrays.asList((byte) 1, // do not return null
            8, // remainingBytes
            "buzz"));
    assertEquals("fizzbuzz", Meta.autofuzz(data, "fizz" ::concat));
  }
}