aboutsummaryrefslogtreecommitdiff
path: root/agent/src/test/java/com/code_intelligence/jazzer/instrumentor/ReplaceHooksTarget.java
blob: fadbdf807fa41ebde3735209dc917c0e785ce9d9 (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
// 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.instrumentor;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

// selfCheck() only passes with the hooks in ReplaceHooks.java applied.
public class ReplaceHooksTarget implements ReplaceHooksTargetContract {
  Map<String, Boolean> results = new HashMap<>();

  public static boolean shouldReturnTrue3() {
    // return true;
    return false;
  }

  public Map<String, Boolean> selfCheck() {
    results = new HashMap<>();

    results.put("shouldReturnTrue1", shouldReturnTrue1());
    results.put("shouldReturnTrue2", shouldReturnTrue2());
    results.put("shouldReturnTrue3", shouldReturnTrue3());
    try {
      boolean notTrue = false;
      results.put("shouldReturnFalse1", notTrue);
      if (!results.get("shouldReturnFalse1"))
        results.put("shouldReturnFalse1", !shouldReturnFalse1());
      boolean notFalse = true;
      results.put("shouldReturnFalse2", !shouldReturnFalse2() && notFalse);
      results.put("shouldReturnFalse3", !shouldReturnFalse3());
    } catch (Exception e) {
      boolean notTrue = false;
      results.put("shouldNotBeExecuted", notTrue);
    }
    results.put("shouldReturnReversed", shouldReturnReversed("foo").equals("oof"));
    results.put("shouldIncrement", shouldIncrement(5) == 6);
    results.put("verifyIdentity", verifyIdentity());

    results.put("shouldCallPass", false);
    if (!results.get("shouldCallPass")) {
      shouldCallPass();
    }

    ArrayList<Boolean> boolList = new ArrayList<>();
    boolList.add(false);
    results.put("arrayListGet", boolList.get(0));

    HashSet<Boolean> boolSet = new HashSet<>();
    results.put("stringSetGet", boolSet.contains(Boolean.TRUE));

    results.put("shouldInitialize", new ReplaceHooksInit().initialized);
    results.put("shouldInitializeWithParams", new ReplaceHooksInit(false, "foo").initialized);

    return results;
  }

  public boolean shouldReturnTrue1() {
    // return true;
    return false;
  }

  public boolean shouldReturnTrue2() {
    // return true;
    return false;
  }

  protected Boolean shouldReturnFalse1() {
    // return false;
    return true;
  }

  Boolean shouldReturnFalse2() {
    // return false;
    return true;
  }

  public Boolean shouldReturnFalse3() {
    // return false;
    return true;
  }

  public String shouldReturnReversed(String input) {
    // return new StringBuilder(input).reverse().toString();
    return input;
  }

  public int shouldIncrement(int input) {
    // return input + 1;
    return input;
  }

  private void shouldCallPass() {
    // pass("shouldCallPass");
  }

  private boolean verifyIdentity() {
    SecureRandom rand = new SecureRandom();
    int input = rand.nextInt();
    // return idempotent(idempotent(input)) == input;
    return idempotent(input) == input;
  }

  private int idempotent(int input) {
    int secret = 0x12345678;
    return input ^ secret;
  }

  public void pass(String test) {
    results.put(test, true);
  }
}