• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Code Intelligence GmbH
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.code_intelligence.jazzer.autofuzz;
16 
17 import static com.code_intelligence.jazzer.autofuzz.TestHelpers.consumeTestCase;
18 
19 import java.util.Arrays;
20 import java.util.Objects;
21 import org.junit.Test;
22 
23 public class InterfaceCreationTest {
24   public interface InterfaceA {
foo()25     void foo();
26 
bar()27     void bar();
28   }
29 
30   public static abstract class ClassA1 implements InterfaceA {
31     @Override
foo()32     public void foo() {}
33   }
34 
35   public static class ClassB1 extends ClassA1 {
36     int n;
37 
ClassB1(int _n)38     public ClassB1(int _n) {
39       n = _n;
40     }
41 
42     @Override
bar()43     public void bar() {}
44 
45     @Override
equals(Object o)46     public boolean equals(Object o) {
47       if (this == o)
48         return true;
49       if (o == null || getClass() != o.getClass())
50         return false;
51       ClassB1 classB1 = (ClassB1) o;
52       return n == classB1.n;
53     }
54 
55     @Override
hashCode()56     public int hashCode() {
57       return Objects.hash(n);
58     }
59   }
60 
61   public static class ClassB2 implements InterfaceA {
62     String s;
63 
ClassB2(String _s)64     public ClassB2(String _s) {
65       s = _s;
66     }
67 
68     @Override
foo()69     public void foo() {}
70 
71     @Override
bar()72     public void bar() {}
73 
74     @Override
equals(Object o)75     public boolean equals(Object o) {
76       if (this == o)
77         return true;
78       if (o == null || getClass() != o.getClass())
79         return false;
80       ClassB2 classB2 = (ClassB2) o;
81       return Objects.equals(s, classB2.s);
82     }
83 
84     @Override
hashCode()85     public int hashCode() {
86       return Objects.hash(s);
87     }
88   }
89   @Test
testConsumeInterface()90   public void testConsumeInterface() {
91     consumeTestCase(InterfaceA.class, new ClassB1(5),
92         "(com.code_intelligence.jazzer.autofuzz.InterfaceCreationTest.InterfaceA) new com.code_intelligence.jazzer.autofuzz.InterfaceCreationTest.ClassB1(5)",
93         Arrays.asList((byte) 1, // do not return null
94             0, // pick ClassB1
95             (byte) 1, // do not return null
96             0, // pick first constructor
97             5 // arg for ClassB1 constructor
98             ));
99     consumeTestCase(InterfaceA.class, new ClassB2("test"),
100         "(com.code_intelligence.jazzer.autofuzz.InterfaceCreationTest.InterfaceA) new com.code_intelligence.jazzer.autofuzz.InterfaceCreationTest.ClassB2(\"test\")",
101         Arrays.asList((byte) 1, // do not return null
102             1, // pick ClassB2
103             (byte) 1, // do not return null
104             0, // pick first constructor
105             (byte) 1, // do not return null
106             8, // remaining bytes
107             "test" // arg for ClassB2 constructor
108             ));
109   }
110 }
111