1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.mina.filter.errorgenerating;
22
23 import java.util.Random;
24
25 import org.apache.mina.core.buffer.IoBuffer;
26 import org.apache.mina.core.filterchain.IoFilter;
27 import org.apache.mina.core.filterchain.IoFilterAdapter;
28 import org.apache.mina.core.session.IoSession;
29 import org.apache.mina.core.write.DefaultWriteRequest;
30 import org.apache.mina.core.write.WriteRequest;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class ErrorGeneratingFilter extends IoFilterAdapter {
56 private int removeByteProbability = 0;
57
58 private int insertByteProbability = 0;
59
60 private int changeByteProbability = 0;
61
62 private int removePduProbability = 0;
63
64 private int duplicatePduProbability = 0;
65
66 private int resendPduLasterProbability = 0;
67
68 private int maxInsertByte = 10;
69
70 private boolean manipulateWrites = false;
71
72 private boolean manipulateReads = false;
73
74 private Random rng = new Random();
75
76 final private Logger logger = LoggerFactory.getLogger(ErrorGeneratingFilter.class);
77
78 @Override
79 public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
80 if (manipulateWrites) {
81
82 if (writeRequest.getMessage() instanceof IoBuffer) {
83 manipulateIoBuffer(session, (IoBuffer) writeRequest.getMessage());
84 IoBuffer buffer = insertBytesToNewIoBuffer(session, (IoBuffer) writeRequest.getMessage());
85 if (buffer != null) {
86 writeRequest = new DefaultWriteRequest(buffer, writeRequest.getFuture(),
87 writeRequest.getDestination());
88 }
89
90 } else {
91 if (duplicatePduProbability > rng.nextInt()) {
92 nextFilter.filterWrite(session, writeRequest);
93 }
94
95 if (resendPduLasterProbability > rng.nextInt()) {
96
97
98
99 }
100 if (removePduProbability > rng.nextInt()) {
101 return;
102 }
103 }
104 }
105 nextFilter.filterWrite(session, writeRequest);
106 }
107
108 @Override
109 public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
110 if (manipulateReads) {
111 if (message instanceof IoBuffer) {
112
113 manipulateIoBuffer(session, (IoBuffer) message);
114 IoBuffer buffer = insertBytesToNewIoBuffer(session, (IoBuffer) message);
115 if (buffer != null) {
116 message = buffer;
117 }
118 } else {
119
120
121 }
122 }
123 nextFilter.messageReceived(session, message);
124 }
125
126 private IoBuffer insertBytesToNewIoBuffer(IoSession session, IoBuffer buffer) {
127 if (insertByteProbability > rng.nextInt(1000)) {
128 logger.info(buffer.getHexDump());
129
130 int pos = rng.nextInt(buffer.remaining()) - 1;
131
132
133 int count = rng.nextInt(maxInsertByte - 1) + 1;
134
135 IoBuffer newBuff = IoBuffer.allocate(buffer.remaining() + count);
136 for (int i = 0; i < pos; i++)
137 newBuff.put(buffer.get());
138 for (int i = 0; i < count; i++) {
139 newBuff.put((byte) (rng.nextInt(256)));
140 }
141 while (buffer.remaining() > 0) {
142 newBuff.put(buffer.get());
143 }
144 newBuff.flip();
145
146 logger.info("Inserted " + count + " bytes.");
147 logger.info(newBuff.getHexDump());
148 return newBuff;
149 }
150 return null;
151 }
152
153 private void manipulateIoBuffer(IoSession session, IoBuffer buffer) {
154 if ((buffer.remaining() > 0) && (removeByteProbability > rng.nextInt(1000))) {
155 logger.info(buffer.getHexDump());
156
157 int pos = rng.nextInt(buffer.remaining());
158
159 int count = rng.nextInt(buffer.remaining() - pos) + 1;
160 if (count == buffer.remaining())
161 count = buffer.remaining() - 1;
162
163 IoBuffer newBuff = IoBuffer.allocate(buffer.remaining() - count);
164 for (int i = 0; i < pos; i++)
165 newBuff.put(buffer.get());
166
167 buffer.skip(count);
168 while (newBuff.remaining() > 0)
169 newBuff.put(buffer.get());
170 newBuff.flip();
171
172 buffer.rewind();
173 buffer.put(newBuff);
174 buffer.flip();
175 logger.info("Removed " + count + " bytes at position " + pos + ".");
176 logger.info(buffer.getHexDump());
177 }
178 if ((buffer.remaining() > 0) && (changeByteProbability > rng.nextInt(1000))) {
179 logger.info(buffer.getHexDump());
180
181 int count = rng.nextInt(buffer.remaining() - 1) + 1;
182
183 byte[] values = new byte[count];
184 rng.nextBytes(values);
185 for (int i = 0; i < values.length; i++) {
186 int pos = rng.nextInt(buffer.remaining());
187 buffer.put(pos, values[i]);
188 }
189 logger.info("Modified " + count + " bytes.");
190 logger.info(buffer.getHexDump());
191 }
192 }
193
194 public int getChangeByteProbability() {
195 return changeByteProbability;
196 }
197
198
199
200
201
202
203
204 public void setChangeByteProbability(int changeByteProbability) {
205 this.changeByteProbability = changeByteProbability;
206 }
207
208 public int getDuplicatePduProbability() {
209 return duplicatePduProbability;
210 }
211
212
213
214
215
216 public void setDuplicatePduProbability(int duplicatePduProbability) {
217 this.duplicatePduProbability = duplicatePduProbability;
218 }
219
220 public int getInsertByteProbability() {
221 return insertByteProbability;
222 }
223
224
225
226
227
228
229
230 public void setInsertByteProbability(int insertByteProbability) {
231 this.insertByteProbability = insertByteProbability;
232 }
233
234 public boolean isManipulateReads() {
235 return manipulateReads;
236 }
237
238
239
240
241
242
243 public void setManipulateReads(boolean manipulateReads) {
244 this.manipulateReads = manipulateReads;
245 }
246
247 public boolean isManipulateWrites() {
248 return manipulateWrites;
249 }
250
251
252
253
254
255
256 public void setManipulateWrites(boolean manipulateWrites) {
257 this.manipulateWrites = manipulateWrites;
258 }
259
260 public int getRemoveByteProbability() {
261 return removeByteProbability;
262 }
263
264
265
266
267
268
269
270
271 public void setRemoveByteProbability(int removeByteProbability) {
272 this.removeByteProbability = removeByteProbability;
273 }
274
275 public int getRemovePduProbability() {
276 return removePduProbability;
277 }
278
279
280
281
282
283 public void setRemovePduProbability(int removePduProbability) {
284 this.removePduProbability = removePduProbability;
285 }
286
287 public int getResendPduLasterProbability() {
288 return resendPduLasterProbability;
289 }
290
291
292
293
294
295 public void setResendPduLasterProbability(int resendPduLasterProbability) {
296 this.resendPduLasterProbability = resendPduLasterProbability;
297 }
298
299 public int getMaxInsertByte() {
300 return maxInsertByte;
301 }
302
303
304
305
306
307
308 public void setMaxInsertByte(int maxInsertByte) {
309 this.maxInsertByte = maxInsertByte;
310 }
311 }