1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dexfuzz.listeners;
18 
19 import dexfuzz.ExecutionResult;
20 import dexfuzz.executors.Executor;
21 import dexfuzz.program.Mutation;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 
27 /**
28  * Handles situation where multiple Listeners are wanted, passes notifications
29  * onto each Listener it is responsible for.
30  */
31 public class MultiplexerListener extends BaseListener {
32 
33   private List<BaseListener> listeners;
34 
35   @Override
setup()36   public void setup() {
37     listeners = new ArrayList<BaseListener>();
38   }
39 
addListener(BaseListener listener)40   public void addListener(BaseListener listener) {
41     listeners.add(listener);
42     listener.setup();
43   }
44 
45   @Override
shutdown()46   public void shutdown() {
47     for (BaseListener listener : listeners) {
48       listener.shutdown();
49     }
50   }
51 
52   @Override
handleSuccessfulHostVerification()53   public void handleSuccessfulHostVerification() {
54     for (BaseListener listener : listeners) {
55       listener.handleSuccessfulHostVerification();
56     }
57   }
58 
59   @Override
handleFailedHostVerification(ExecutionResult verificationResult)60   public void handleFailedHostVerification(ExecutionResult verificationResult) {
61     for (BaseListener listener : listeners) {
62       listener.handleFailedHostVerification(verificationResult);
63     }
64   }
65 
66   @Override
handleFailedTargetVerification()67   public void handleFailedTargetVerification() {
68     for (BaseListener listener : listeners) {
69       listener.handleFailedTargetVerification();
70     }
71   }
72 
73   @Override
handleIterationStarted(int iteration)74   public void handleIterationStarted(int iteration) {
75     for (BaseListener listener : listeners) {
76       listener.handleIterationStarted(iteration);
77     }
78   }
79 
80   @Override
handleIterationFinished(int iteration)81   public void handleIterationFinished(int iteration) {
82     for (BaseListener listener : listeners) {
83       listener.handleIterationFinished(iteration);
84     }
85   }
86 
87   @Override
handleTimeouts(List<Executor> timedOut, List<Executor> didNotTimeOut)88   public void handleTimeouts(List<Executor> timedOut, List<Executor> didNotTimeOut) {
89     for (BaseListener listener : listeners) {
90       listener.handleTimeouts(timedOut, didNotTimeOut);
91     }
92   }
93 
94   @Override
handleDivergences(Map<String, List<Executor>> outputMap)95   public void handleDivergences(Map<String, List<Executor>> outputMap) {
96     for (BaseListener listener : listeners) {
97       listener.handleDivergences(outputMap);
98     }
99   }
100 
101   @Override
handleFuzzingFile(String inputFile)102   public void handleFuzzingFile(String inputFile) {
103     for (BaseListener listener : listeners) {
104       listener.handleFuzzingFile(inputFile);
105     }
106   }
107 
108   @Override
handleSeed(long seed)109   public void handleSeed(long seed) {
110     for (BaseListener listener : listeners) {
111       listener.handleSeed(seed);
112     }
113   }
114 
115   @Override
handleHostVerificationSigabort(ExecutionResult verificationResult)116   public void handleHostVerificationSigabort(ExecutionResult verificationResult) {
117     for (BaseListener listener : listeners) {
118       listener.handleHostVerificationSigabort(verificationResult);
119     }
120   }
121 
122   @Override
handleSuccess(Map<String, List<Executor>> outputMap)123   public void handleSuccess(Map<String, List<Executor>> outputMap) {
124     for (BaseListener listener : listeners) {
125       listener.handleSuccess(outputMap);
126     }
127   }
128 
129   @Override
handleDumpOutput(String outputLine, Executor executor)130   public void handleDumpOutput(String outputLine, Executor executor) {
131     for (BaseListener listener : listeners) {
132       listener.handleDumpOutput(outputLine, executor);
133     }
134   }
135 
136   @Override
handleDumpVerify(String verifyLine)137   public void handleDumpVerify(String verifyLine) {
138     for (BaseListener listener : listeners) {
139       listener.handleDumpVerify(verifyLine);
140     }
141   }
142 
143   @Override
handleMutationStats(String statsString)144   public void handleMutationStats(String statsString) {
145     for (BaseListener listener : listeners) {
146       listener.handleMutationStats(statsString);
147     }
148   }
149 
150   @Override
handleTiming(String name, float elapsedTime)151   public void handleTiming(String name, float elapsedTime) {
152     for (BaseListener listener : listeners) {
153       listener.handleTiming(name, elapsedTime);
154     }
155   }
156 
157   @Override
handleMutationFail()158   public void handleMutationFail() {
159     for (BaseListener listener : listeners) {
160       listener.handleMutationFail();
161     }
162   }
163 
164   @Override
handleSummary()165   public void handleSummary() {
166     for (BaseListener listener : listeners) {
167       listener.handleSummary();
168     }
169   }
170 
171   @Override
handleSuccessfullyFuzzedFile(String programName)172   public void handleSuccessfullyFuzzedFile(String programName) {
173     for (BaseListener listener : listeners) {
174       listener.handleSuccessfullyFuzzedFile(programName);
175     }
176   }
177 
178   @Override
handleSelfDivergence()179   public void handleSelfDivergence() {
180     for (BaseListener listener : listeners) {
181       listener.handleSelfDivergence();
182     }
183   }
184 
185   @Override
handleMessage(String msg)186   public void handleMessage(String msg) {
187     for (BaseListener listener : listeners) {
188       listener.handleMessage(msg);
189     }
190   }
191 
192   @Override
handleMutations(List<Mutation> mutations)193   public void handleMutations(List<Mutation> mutations) {
194     for (BaseListener listener : listeners) {
195       listener.handleMutations(mutations);
196     }
197   }
198 
199   @Override
handleArchitectureSplit()200   public void handleArchitectureSplit() {
201     for (BaseListener listener : listeners) {
202       listener.handleArchitectureSplit();
203     }
204   }
205 }
206