Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-aarch32-jdk8u
Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/rmi/testlibrary/TestSocketFactory.java
83402 views
1
/*
2
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.io.ByteArrayOutputStream;
25
import java.io.FilterInputStream;
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.io.OutputStream;
29
import java.io.Serializable;
30
import java.net.InetAddress;
31
import java.net.ServerSocket;
32
import java.net.Socket;
33
import java.net.SocketAddress;
34
import java.net.SocketException;
35
import java.net.SocketOption;
36
import java.nio.channels.ServerSocketChannel;
37
import java.nio.channels.SocketChannel;
38
import java.rmi.server.RMIClientSocketFactory;
39
import java.rmi.server.RMIServerSocketFactory;
40
import java.rmi.server.RMISocketFactory;
41
import java.util.ArrayList;
42
import java.util.Arrays;
43
import java.util.List;
44
import java.util.Objects;
45
import java.util.Set;
46
47
import org.testng.Assert;
48
import org.testng.annotations.Test;
49
import org.testng.annotations.DataProvider;
50
51
/*
52
* @test
53
* @summary TestSocket Factory and tests of the basic trigger, match, and replace functions
54
* @run testng TestSocketFactory
55
* @bug 8186539
56
*/
57
58
/**
59
* A RMISocketFactory utility factory to log RMI stream contents and to
60
* trigger, and then match and replace output stream contents to simulate failures.
61
* <p>
62
* The trigger is a sequence of bytes that must be found before looking
63
* for the bytes to match and replace. If the trigger sequence is empty
64
* matching is immediately enabled. While waiting for the trigger to be found
65
* bytes written to the streams are written through to the output stream.
66
* The when triggered and when a trigger is non-empty, matching looks for
67
* the sequence of bytes supplied. If the sequence is empty, no matching or
68
* replacement is performed.
69
* While waiting for a complete match, the partial matched bytes are not
70
* written to the output stream. When the match is incomplete, the partial
71
* matched bytes are written to the output. When a match is complete the
72
* full replacement byte array is written to the output.
73
* <p>
74
* The trigger, match, and replacement bytes arrays can be changed at any
75
* time and immediately reset and restart matching. Changes are propagated
76
* to all of the sockets created from the factories immediately.
77
*/
78
public class TestSocketFactory extends RMISocketFactory
79
implements RMIClientSocketFactory, RMIServerSocketFactory, Serializable {
80
81
private static final long serialVersionUID = 1L;
82
83
private volatile transient byte[] triggerBytes;
84
85
private volatile transient byte[] matchBytes;
86
87
private volatile transient byte[] replaceBytes;
88
89
private transient final List<InterposeSocket> sockets = new ArrayList<>();
90
91
private transient final List<InterposeServerSocket> serverSockets = new ArrayList<>();
92
93
static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
94
95
// True to enable logging of matches and replacements.
96
private static volatile boolean debugLogging = false;
97
98
/**
99
* Debugging output can be synchronized with logging of RMI actions.
100
*
101
* @param format a printf format
102
* @param args any args
103
*/
104
public static void DEBUG(String format, Object... args) {
105
if (debugLogging) {
106
System.err.printf(format, args);
107
}
108
}
109
110
/**
111
* Create a socket factory that creates InputStreams
112
* and OutputStreams that log.
113
*/
114
public TestSocketFactory() {
115
this.triggerBytes = EMPTY_BYTE_ARRAY;
116
this.matchBytes = EMPTY_BYTE_ARRAY;
117
this.replaceBytes = EMPTY_BYTE_ARRAY;
118
}
119
120
/**
121
* Set debug to true to generate logging output of matches and substitutions.
122
* @param debug {@code true} to generate logging output
123
* @return the previous value
124
*/
125
public static boolean setDebug(boolean debug) {
126
boolean oldDebug = debugLogging;
127
debugLogging = debug;
128
return oldDebug;
129
}
130
131
/**
132
* Set the match and replacement bytes, with an empty trigger.
133
* The match and replacements are propagated to all existing sockets.
134
*
135
* @param matchBytes bytes to match
136
* @param replaceBytes bytes to replace the matched bytes
137
*/
138
public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
139
setMatchReplaceBytes(EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
140
}
141
142
/**
143
* Set the trigger, match, and replacement bytes.
144
* The trigger, match, and replacements are propagated to all existing sockets.
145
*
146
* @param triggerBytes array of bytes to use as a trigger, may be zero length
147
* @param matchBytes bytes to match after the trigger has been seen
148
* @param replaceBytes bytes to replace the matched bytes
149
*/
150
public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes,
151
byte[] replaceBytes) {
152
this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
153
this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
154
this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
155
sockets.forEach( s -> s.setMatchReplaceBytes(triggerBytes, matchBytes,
156
replaceBytes));
157
serverSockets.forEach( s -> s.setMatchReplaceBytes(triggerBytes, matchBytes,
158
replaceBytes));
159
}
160
161
@Override
162
public Socket createSocket(String host, int port) throws IOException {
163
Socket socket = RMISocketFactory.getDefaultSocketFactory()
164
.createSocket(host, port);
165
InterposeSocket s = new InterposeSocket(socket,
166
triggerBytes, matchBytes, replaceBytes);
167
sockets.add(s);
168
return s;
169
}
170
171
/**
172
* Return the current list of sockets.
173
* @return Return a snapshot of the current list of sockets
174
*/
175
public List<InterposeSocket> getSockets() {
176
List<InterposeSocket> snap = new ArrayList<>(sockets);
177
return snap;
178
}
179
180
@Override
181
public ServerSocket createServerSocket(int port) throws IOException {
182
183
ServerSocket serverSocket = RMISocketFactory.getDefaultSocketFactory()
184
.createServerSocket(port);
185
InterposeServerSocket ss = new InterposeServerSocket(serverSocket,
186
triggerBytes, matchBytes, replaceBytes);
187
serverSockets.add(ss);
188
return ss;
189
}
190
191
/**
192
* Return the current list of server sockets.
193
* @return Return a snapshot of the current list of server sockets
194
*/
195
public List<InterposeServerSocket> getServerSockets() {
196
List<InterposeServerSocket> snap = new ArrayList<>(serverSockets);
197
return snap;
198
}
199
200
/**
201
* An InterposeSocket wraps a socket that produces InputStreams
202
* and OutputStreams that log the traffic.
203
* The OutputStreams it produces watch for a trigger and then
204
* match an array of bytes and replace them.
205
* Useful for injecting protocol and content errors.
206
*/
207
public static class InterposeSocket extends Socket {
208
private final Socket socket;
209
private InputStream in;
210
private MatchReplaceOutputStream out;
211
private volatile byte[] triggerBytes;
212
private volatile byte[] matchBytes;
213
private volatile byte[] replaceBytes;
214
private final ByteArrayOutputStream inLogStream;
215
private final ByteArrayOutputStream outLogStream;
216
private final String name;
217
private static volatile int num = 0; // index for created InterposeSockets
218
219
/**
220
* Construct a socket that interposes on a socket to match and replace.
221
* The trigger is empty.
222
* @param socket the underlying socket
223
* @param matchBytes the bytes that must match
224
* @param replaceBytes the replacement bytes
225
*/
226
public InterposeSocket(Socket socket, byte[] matchBytes, byte[] replaceBytes) {
227
this(socket, EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
228
}
229
230
/**
231
* Construct a socket that interposes on a socket to match and replace.
232
* @param socket the underlying socket
233
* @param triggerBytes array of bytes to enable matching
234
* @param matchBytes the bytes that must match
235
* @param replaceBytes the replacement bytes
236
*/
237
public InterposeSocket(Socket socket, byte[]
238
triggerBytes, byte[] matchBytes, byte[] replaceBytes) {
239
this.socket = socket;
240
this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
241
this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
242
this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
243
this.inLogStream = new ByteArrayOutputStream();
244
this.outLogStream = new ByteArrayOutputStream();
245
this.name = "IS" + ++num + "::"
246
+ Thread.currentThread().getName() + ": "
247
+ socket.getLocalPort() + " < " + socket.getPort();
248
}
249
250
/**
251
* Set the match and replacement bytes, with an empty trigger.
252
* The match and replacements are propagated to all existing sockets.
253
*
254
* @param matchBytes bytes to match
255
* @param replaceBytes bytes to replace the matched bytes
256
*/
257
public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
258
this.setMatchReplaceBytes(EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
259
}
260
261
/**
262
* Set the trigger, match, and replacement bytes.
263
* The trigger, match, and replacements are propagated to the
264
* MatchReplaceOutputStream.
265
*
266
* @param triggerBytes array of bytes to use as a trigger, may be zero length
267
* @param matchBytes bytes to match after the trigger has been seen
268
* @param replaceBytes bytes to replace the matched bytes
269
*/
270
public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes,
271
byte[] replaceBytes) {
272
this.triggerBytes = triggerBytes;
273
this.matchBytes = matchBytes;
274
this.replaceBytes = replaceBytes;
275
out.setMatchReplaceBytes(triggerBytes, matchBytes, replaceBytes);
276
}
277
278
@Override
279
public void connect(SocketAddress endpoint) throws IOException {
280
socket.connect(endpoint);
281
}
282
283
@Override
284
public void connect(SocketAddress endpoint, int timeout) throws IOException {
285
socket.connect(endpoint, timeout);
286
}
287
288
@Override
289
public void bind(SocketAddress bindpoint) throws IOException {
290
socket.bind(bindpoint);
291
}
292
293
@Override
294
public InetAddress getInetAddress() {
295
return socket.getInetAddress();
296
}
297
298
@Override
299
public InetAddress getLocalAddress() {
300
return socket.getLocalAddress();
301
}
302
303
@Override
304
public int getPort() {
305
return socket.getPort();
306
}
307
308
@Override
309
public int getLocalPort() {
310
return socket.getLocalPort();
311
}
312
313
@Override
314
public SocketAddress getRemoteSocketAddress() {
315
return socket.getRemoteSocketAddress();
316
}
317
318
@Override
319
public SocketAddress getLocalSocketAddress() {
320
return socket.getLocalSocketAddress();
321
}
322
323
@Override
324
public SocketChannel getChannel() {
325
return socket.getChannel();
326
}
327
328
@Override
329
public synchronized void close() throws IOException {
330
socket.close();
331
}
332
333
@Override
334
public String toString() {
335
return "InterposeSocket " + name + ": " + socket.toString();
336
}
337
338
@Override
339
public boolean isConnected() {
340
return socket.isConnected();
341
}
342
343
@Override
344
public boolean isBound() {
345
return socket.isBound();
346
}
347
348
@Override
349
public boolean isClosed() {
350
return socket.isClosed();
351
}
352
353
@Override
354
public synchronized InputStream getInputStream() throws IOException {
355
if (in == null) {
356
in = socket.getInputStream();
357
String name = Thread.currentThread().getName() + ": "
358
+ socket.getLocalPort() + " < " + socket.getPort();
359
in = new LoggingInputStream(in, name, inLogStream);
360
DEBUG("Created new InterposeInputStream: %s%n", name);
361
}
362
return in;
363
}
364
365
@Override
366
public synchronized OutputStream getOutputStream() throws IOException {
367
if (out == null) {
368
OutputStream o = socket.getOutputStream();
369
String name = Thread.currentThread().getName() + ": "
370
+ socket.getLocalPort() + " > " + socket.getPort();
371
out = new MatchReplaceOutputStream(o, name, outLogStream,
372
triggerBytes, matchBytes, replaceBytes);
373
DEBUG("Created new MatchReplaceOutputStream: %s%n", name);
374
}
375
return out;
376
}
377
378
/**
379
* Return the bytes logged from the input stream.
380
* @return Return the bytes logged from the input stream.
381
*/
382
public byte[] getInLogBytes() {
383
return inLogStream.toByteArray();
384
}
385
386
/**
387
* Return the bytes logged from the output stream.
388
* @return Return the bytes logged from the output stream.
389
*/
390
public byte[] getOutLogBytes() {
391
return outLogStream.toByteArray();
392
}
393
394
}
395
396
/**
397
* InterposeServerSocket is a ServerSocket that wraps each Socket it accepts
398
* with an InterposeSocket so that its input and output streams can be monitored.
399
*/
400
public static class InterposeServerSocket extends ServerSocket {
401
private final ServerSocket socket;
402
private volatile byte[] triggerBytes;
403
private volatile byte[] matchBytes;
404
private volatile byte[] replaceBytes;
405
private final List<InterposeSocket> sockets = new ArrayList<>();
406
407
/**
408
* Construct a server socket that interposes on a socket to match and replace.
409
* The trigger is empty.
410
* @param socket the underlying socket
411
* @param matchBytes the bytes that must match
412
* @param replaceBytes the replacement bytes
413
*/
414
public InterposeServerSocket(ServerSocket socket, byte[] matchBytes,
415
byte[] replaceBytes) throws IOException {
416
this(socket, EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
417
}
418
419
/**
420
* Construct a server socket that interposes on a socket to match and replace.
421
* @param socket the underlying socket
422
* @param triggerBytes array of bytes to enable matching
423
* @param matchBytes the bytes that must match
424
* @param replaceBytes the replacement bytes
425
*/
426
public InterposeServerSocket(ServerSocket socket, byte[] triggerBytes,
427
byte[] matchBytes, byte[] replaceBytes) throws IOException {
428
this.socket = socket;
429
this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
430
this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
431
this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
432
}
433
434
/**
435
* Set the match and replacement bytes, with an empty trigger.
436
* The match and replacements are propagated to all existing sockets.
437
*
438
* @param matchBytes bytes to match
439
* @param replaceBytes bytes to replace the matched bytes
440
*/
441
public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
442
setMatchReplaceBytes(EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
443
}
444
445
/**
446
* Set the trigger, match, and replacement bytes.
447
* The trigger, match, and replacements are propagated to all existing sockets.
448
*
449
* @param triggerBytes array of bytes to use as a trigger, may be zero length
450
* @param matchBytes bytes to match after the trigger has been seen
451
* @param replaceBytes bytes to replace the matched bytes
452
*/
453
public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes,
454
byte[] replaceBytes) {
455
this.triggerBytes = triggerBytes;
456
this.matchBytes = matchBytes;
457
this.replaceBytes = replaceBytes;
458
sockets.forEach(s -> s.setMatchReplaceBytes(triggerBytes, matchBytes, replaceBytes));
459
}
460
/**
461
* Return a snapshot of the current list of sockets created from this server socket.
462
* @return Return a snapshot of the current list of sockets
463
*/
464
public List<InterposeSocket> getSockets() {
465
List<InterposeSocket> snap = new ArrayList<>(sockets);
466
return snap;
467
}
468
469
@Override
470
public void bind(SocketAddress endpoint) throws IOException {
471
socket.bind(endpoint);
472
}
473
474
@Override
475
public void bind(SocketAddress endpoint, int backlog) throws IOException {
476
socket.bind(endpoint, backlog);
477
}
478
479
@Override
480
public InetAddress getInetAddress() {
481
return socket.getInetAddress();
482
}
483
484
@Override
485
public int getLocalPort() {
486
return socket.getLocalPort();
487
}
488
489
@Override
490
public SocketAddress getLocalSocketAddress() {
491
return socket.getLocalSocketAddress();
492
}
493
494
@Override
495
public Socket accept() throws IOException {
496
Socket s = socket.accept();
497
InterposeSocket socket = new InterposeSocket(s, matchBytes, replaceBytes);
498
sockets.add(socket);
499
return socket;
500
}
501
502
@Override
503
public void close() throws IOException {
504
socket.close();
505
}
506
507
@Override
508
public ServerSocketChannel getChannel() {
509
return socket.getChannel();
510
}
511
512
@Override
513
public boolean isClosed() {
514
return socket.isClosed();
515
}
516
517
@Override
518
public String toString() {
519
return socket.toString();
520
}
521
522
@Override
523
public synchronized void setSoTimeout(int timeout) throws SocketException {
524
socket.setSoTimeout(timeout);
525
}
526
527
@Override
528
public synchronized int getSoTimeout() throws IOException {
529
return socket.getSoTimeout();
530
}
531
}
532
533
/**
534
* LoggingInputStream is a stream and logs all bytes read to it.
535
* For identification it is given a name.
536
*/
537
public static class LoggingInputStream extends FilterInputStream {
538
private int bytesIn = 0;
539
private final String name;
540
private final OutputStream log;
541
542
public LoggingInputStream(InputStream in, String name, OutputStream log) {
543
super(in);
544
this.name = name;
545
this.log = log;
546
}
547
548
@Override
549
public int read() throws IOException {
550
int b = super.read();
551
if (b >= 0) {
552
log.write(b);
553
bytesIn++;
554
}
555
return b;
556
}
557
558
@Override
559
public int read(byte[] b, int off, int len) throws IOException {
560
int bytes = super.read(b, off, len);
561
if (bytes > 0) {
562
log.write(b, off, bytes);
563
bytesIn += bytes;
564
}
565
return bytes;
566
}
567
568
@Override
569
public int read(byte[] b) throws IOException {
570
return read(b, 0, b.length);
571
}
572
573
@Override
574
public void close() throws IOException {
575
super.close();
576
}
577
578
@Override
579
public String toString() {
580
return String.format("%s: In: (%d)", name, bytesIn);
581
}
582
}
583
584
/**
585
* An OutputStream that looks for a trigger to enable matching and
586
* replaces one string of bytes with another.
587
* If any range matches, the match starts after the partial match.
588
*/
589
static class MatchReplaceOutputStream extends OutputStream {
590
private final OutputStream out;
591
private final String name;
592
private volatile byte[] triggerBytes;
593
private volatile byte[] matchBytes;
594
private volatile byte[] replaceBytes;
595
int triggerIndex;
596
int matchIndex;
597
private int bytesOut = 0;
598
private final OutputStream log;
599
600
MatchReplaceOutputStream(OutputStream out, String name, OutputStream log,
601
byte[] matchBytes, byte[] replaceBytes) {
602
this(out, name, log, EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
603
}
604
605
MatchReplaceOutputStream(OutputStream out, String name, OutputStream log,
606
byte[] triggerBytes, byte[] matchBytes,
607
byte[] replaceBytes) {
608
this.out = out;
609
this.name = name;
610
this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
611
triggerIndex = 0;
612
this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
613
this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
614
matchIndex = 0;
615
this.log = log;
616
}
617
618
public void setMatchReplaceBytes(byte[] matchBytes, byte[] replaceBytes) {
619
setMatchReplaceBytes(EMPTY_BYTE_ARRAY, matchBytes, replaceBytes);
620
}
621
622
public void setMatchReplaceBytes(byte[] triggerBytes, byte[] matchBytes,
623
byte[] replaceBytes) {
624
this.triggerBytes = Objects.requireNonNull(triggerBytes, "triggerBytes");
625
triggerIndex = 0;
626
this.matchBytes = Objects.requireNonNull(matchBytes, "matchBytes");
627
this.replaceBytes = Objects.requireNonNull(replaceBytes, "replaceBytes");
628
matchIndex = 0;
629
}
630
631
632
public void write(int b) throws IOException {
633
b = b & 0xff;
634
if (matchBytes.length == 0) {
635
// fast path, no match
636
out.write(b);
637
log.write(b);
638
bytesOut++;
639
return;
640
}
641
// if trigger not satisfied, keep looking
642
if (triggerBytes.length != 0 && triggerIndex < triggerBytes.length) {
643
out.write(b);
644
log.write(b);
645
bytesOut++;
646
647
triggerIndex = (b == (triggerBytes[triggerIndex] & 0xff))
648
? ++triggerIndex // matching advance
649
: 0;
650
} else {
651
// trigger not used or has been satisfied
652
if (b == (matchBytes[matchIndex] & 0xff)) {
653
if (++matchIndex >= matchBytes.length) {
654
matchIndex = 0;
655
triggerIndex = 0; // match/replace ok, reset trigger
656
DEBUG("TestSocketFactory MatchReplace %s replaced %d bytes " +
657
"at offset: %d (x%04x)%n",
658
name, replaceBytes.length, bytesOut, bytesOut);
659
out.write(replaceBytes);
660
log.write(replaceBytes);
661
bytesOut += replaceBytes.length;
662
}
663
} else {
664
if (matchIndex > 0) {
665
// mismatch, write out any that matched already
666
DEBUG("Partial match %s matched %d bytes at offset: %d (0x%04x), expected: x%02x, actual: x%02x%n",
667
name, matchIndex, bytesOut, bytesOut, matchBytes[matchIndex], b);
668
out.write(matchBytes, 0, matchIndex);
669
log.write(matchBytes, 0, matchIndex);
670
bytesOut += matchIndex;
671
matchIndex = 0;
672
}
673
if (b == (matchBytes[matchIndex] & 0xff)) {
674
matchIndex++;
675
} else {
676
out.write(b);
677
log.write(b);
678
bytesOut++;
679
}
680
}
681
}
682
}
683
684
public void flush() throws IOException {
685
if (matchIndex > 0) {
686
// write out any that matched already to avoid consumer hang.
687
// Match/replace across a flush is not supported.
688
DEBUG( "Flush partial match %s matched %d bytes at offset: %d (0x%04x)%n",
689
name, matchIndex, bytesOut, bytesOut);
690
out.write(matchBytes, 0, matchIndex);
691
log.write(matchBytes, 0, matchIndex);
692
bytesOut += matchIndex;
693
matchIndex = 0;
694
}
695
}
696
697
@Override
698
public String toString() {
699
return String.format("%s: Out: (%d)", name, bytesOut);
700
}
701
}
702
703
private static byte[] obj1Data = new byte[] {
704
0x7e, 0x7e, 0x7e,
705
(byte) 0x80, 0x05,
706
0x7f, 0x7f, 0x7f,
707
0x73, 0x72, 0x00, 0x10, // TC_OBJECT, TC_CLASSDESC, length = 16
708
(byte)'j', (byte)'a', (byte)'v', (byte)'a', (byte)'.',
709
(byte)'l', (byte)'a', (byte)'n', (byte)'g', (byte)'.',
710
(byte)'n', (byte)'u', (byte)'m', (byte)'b', (byte)'e', (byte)'r'
711
};
712
private static byte[] obj1Result = new byte[] {
713
0x7e, 0x7e, 0x7e,
714
(byte) 0x80, 0x05,
715
0x7f, 0x7f, 0x7f,
716
0x73, 0x72, 0x00, 0x11, // TC_OBJECT, TC_CLASSDESC, length = 17
717
(byte)'j', (byte)'a', (byte)'v', (byte)'a', (byte)'.',
718
(byte)'l', (byte)'a', (byte)'n', (byte)'g', (byte)'.',
719
(byte)'I', (byte)'n', (byte)'t', (byte)'e', (byte)'g', (byte)'e', (byte)'r'
720
};
721
private static byte[] obj1Trigger = new byte[] {
722
(byte) 0x80, 0x05
723
};
724
private static byte[] obj1Trigger2 = new byte[] {
725
0x7D, 0x7D, 0x7D, 0x7D,
726
};
727
private static byte[] obj1Trigger3 = new byte[] {
728
0x7F,
729
};
730
private static byte[] obj1Match = new byte[] {
731
0x73, 0x72, 0x00, 0x10, // TC_OBJECT, TC_CLASSDESC, length = 16
732
(byte)'j', (byte)'a', (byte)'v', (byte)'a', (byte)'.',
733
(byte)'l', (byte)'a', (byte)'n', (byte)'g', (byte)'.',
734
(byte)'n', (byte)'u', (byte)'m', (byte)'b', (byte)'e', (byte)'r'
735
};
736
private static byte[] obj1Repl = new byte[] {
737
0x73, 0x72, 0x00, 0x11, // TC_OBJECT, TC_CLASSDESC, length = 17
738
(byte)'j', (byte)'a', (byte)'v', (byte)'a', (byte)'.',
739
(byte)'l', (byte)'a', (byte)'n', (byte)'g', (byte)'.',
740
(byte)'I', (byte)'n', (byte)'t', (byte)'e', (byte)'g', (byte)'e', (byte)'r'
741
};
742
743
@DataProvider(name = "MatchReplaceData")
744
static Object[][] matchReplaceData() {
745
byte[] empty = new byte[0];
746
byte[] byte1 = new byte[]{1, 2, 3, 4, 5, 6};
747
byte[] bytes2 = new byte[]{1, 2, 4, 3, 5, 6};
748
byte[] bytes3 = new byte[]{6, 5, 4, 3, 2, 1};
749
byte[] bytes4 = new byte[]{1, 2, 0x10, 0x20, 0x30, 0x40, 5, 6};
750
byte[] bytes4a = new byte[]{1, 2, 0x10, 0x20, 0x30, 0x40, 5, 7}; // mostly matches bytes4
751
byte[] bytes5 = new byte[]{0x30, 0x40, 5, 6};
752
byte[] bytes6 = new byte[]{1, 2, 0x10, 0x20, 0x30};
753
754
return new Object[][]{
755
{EMPTY_BYTE_ARRAY, new byte[]{}, new byte[]{},
756
empty, empty},
757
{EMPTY_BYTE_ARRAY, new byte[]{}, new byte[]{},
758
byte1, byte1},
759
{EMPTY_BYTE_ARRAY, new byte[]{3, 4}, new byte[]{4, 3},
760
byte1, bytes2}, //swap bytes
761
{EMPTY_BYTE_ARRAY, new byte[]{3, 4}, new byte[]{0x10, 0x20, 0x30, 0x40},
762
byte1, bytes4}, // insert
763
{EMPTY_BYTE_ARRAY, new byte[]{1, 2, 0x10, 0x20}, new byte[]{},
764
bytes4, bytes5}, // delete head
765
{EMPTY_BYTE_ARRAY, new byte[]{0x40, 5, 6}, new byte[]{},
766
bytes4, bytes6}, // delete tail
767
{EMPTY_BYTE_ARRAY, new byte[]{0x40, 0x50}, new byte[]{0x60, 0x50},
768
bytes4, bytes4}, // partial match, replace nothing
769
{EMPTY_BYTE_ARRAY, bytes4a, bytes3,
770
bytes4, bytes4}, // long partial match, not replaced
771
{EMPTY_BYTE_ARRAY, obj1Match, obj1Repl,
772
obj1Match, obj1Repl},
773
{obj1Trigger, obj1Match, obj1Repl,
774
obj1Data, obj1Result},
775
{obj1Trigger3, obj1Match, obj1Repl,
776
obj1Data, obj1Result}, // different trigger, replace
777
{obj1Trigger2, obj1Match, obj1Repl,
778
obj1Data, obj1Data}, // no trigger, no replace
779
};
780
}
781
782
@Test(dataProvider = "MatchReplaceData")
783
public static void test1(byte[] trigger, byte[] match, byte[] replace,
784
byte[] input, byte[] expected) {
785
System.out.printf("trigger: %s, match: %s, replace: %s%n", Arrays.toString(trigger),
786
Arrays.toString(match), Arrays.toString(replace));
787
try (ByteArrayOutputStream output = new ByteArrayOutputStream();
788
ByteArrayOutputStream log = new ByteArrayOutputStream();
789
OutputStream out = new MatchReplaceOutputStream(output, "test3",
790
log, trigger, match, replace)) {
791
out.write(input);
792
byte[] actual = output.toByteArray();
793
794
if (!Arrays.equals(actual, expected)) {
795
System.out.printf("actual: %s%n", Arrays.toString(actual));
796
System.out.printf("expected: %s%n", Arrays.toString(expected));
797
}
798
Assert.assertEquals(actual, expected, "match/replace fail");
799
} catch (IOException ioe) {
800
Assert.fail("unexpected exception", ioe);
801
}
802
}
803
}
804
805