16
16
package software .amazon .awssdk .services .s3 .checksums ;
17
17
18
18
import static org .junit .Assert .assertArrayEquals ;
19
- import static org .junit .Assert .assertEquals ;
20
19
import static org .junit .Assert .assertFalse ;
21
20
import static org .junit .Assert .assertTrue ;
22
21
import static org .junit .Assert .fail ;
23
22
23
+ import java .io .ByteArrayOutputStream ;
24
+ import java .io .IOException ;
25
+ import java .io .UncheckedIOException ;
24
26
import java .nio .ByteBuffer ;
25
27
import java .util .ArrayList ;
26
28
import java .util .Arrays ;
31
33
import org .reactivestreams .Subscriber ;
32
34
import org .reactivestreams .Subscription ;
33
35
import software .amazon .awssdk .core .checksums .Md5Checksum ;
36
+ import software .amazon .awssdk .utils .BinaryUtils ;
34
37
35
38
/**
36
39
* Unit test for ChecksumValidatingPublisher
@@ -39,6 +42,7 @@ public class ChecksumValidatingPublisherTest {
39
42
private static int TEST_DATA_SIZE = 32 ; // size of the test data, in bytes
40
43
private static final int CHECKSUM_SIZE = 16 ;
41
44
private static byte [] testData ;
45
+ private static byte [] testDataWithoutChecksum ;
42
46
43
47
@ BeforeClass
44
48
public static void populateData () {
@@ -52,34 +56,55 @@ public static void populateData() {
52
56
for (int i = 0 ; i < CHECKSUM_SIZE ; i ++) {
53
57
testData [TEST_DATA_SIZE + i ] = checksumBytes [i ];
54
58
}
59
+
60
+ testDataWithoutChecksum = Arrays .copyOfRange (testData , 0 , TEST_DATA_SIZE );
55
61
}
56
62
57
63
@ Test
58
64
public void testSinglePacket () {
59
65
final TestPublisher driver = new TestPublisher ();
60
- final TestSubscriber s = new TestSubscriber (Arrays . copyOfRange ( testData , 0 , TEST_DATA_SIZE ) );
66
+ final TestSubscriber s = new TestSubscriber ();
61
67
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher (driver , new Md5Checksum (), TEST_DATA_SIZE + CHECKSUM_SIZE );
62
68
p .subscribe (s );
63
69
64
70
driver .doOnNext (ByteBuffer .wrap (testData ));
65
71
driver .doOnComplete ();
66
72
73
+ assertArrayEquals (testDataWithoutChecksum , s .receivedData ());
67
74
assertTrue (s .hasCompleted ());
68
75
assertFalse (s .isOnErrorCalled ());
69
76
}
70
77
78
+ @ Test
79
+ public void testLastChecksumByteCorrupted () {
80
+ TestPublisher driver = new TestPublisher ();
81
+
82
+ TestSubscriber s = new TestSubscriber ();
83
+ ChecksumValidatingPublisher p = new ChecksumValidatingPublisher (driver , new Md5Checksum (), TEST_DATA_SIZE + CHECKSUM_SIZE );
84
+ p .subscribe (s );
85
+
86
+ byte [] incorrectChecksumData = Arrays .copyOfRange (testData , 0 , TEST_DATA_SIZE );
87
+ incorrectChecksumData [TEST_DATA_SIZE - 1 ] = (byte ) ~incorrectChecksumData [TEST_DATA_SIZE - 1 ];
88
+ driver .doOnNext (ByteBuffer .wrap (incorrectChecksumData ));
89
+ driver .doOnComplete ();
90
+
91
+ assertFalse (s .hasCompleted ());
92
+ assertTrue (s .isOnErrorCalled ());
93
+ }
94
+
71
95
@ Test
72
96
public void testTwoPackets () {
73
97
for (int i = 1 ; i < TEST_DATA_SIZE + CHECKSUM_SIZE - 1 ; i ++) {
74
98
final TestPublisher driver = new TestPublisher ();
75
- final TestSubscriber s = new TestSubscriber (Arrays . copyOfRange ( testData , 0 , TEST_DATA_SIZE ) );
99
+ final TestSubscriber s = new TestSubscriber ();
76
100
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher (driver , new Md5Checksum (), TEST_DATA_SIZE + CHECKSUM_SIZE );
77
101
p .subscribe (s );
78
102
79
103
driver .doOnNext (ByteBuffer .wrap (testData , 0 , i ));
80
104
driver .doOnNext (ByteBuffer .wrap (testData , i , TEST_DATA_SIZE + CHECKSUM_SIZE - i ));
81
105
driver .doOnComplete ();
82
106
107
+ assertArrayEquals (testDataWithoutChecksum , s .receivedData ());
83
108
assertTrue (s .hasCompleted ());
84
109
assertFalse (s .isOnErrorCalled ());
85
110
}
@@ -89,7 +114,7 @@ public void testTwoPackets() {
89
114
public void testTinyPackets () {
90
115
for (int packetSize = 1 ; packetSize < CHECKSUM_SIZE ; packetSize ++) {
91
116
final TestPublisher driver = new TestPublisher ();
92
- final TestSubscriber s = new TestSubscriber (Arrays . copyOfRange ( testData , 0 , TEST_DATA_SIZE ) );
117
+ final TestSubscriber s = new TestSubscriber ();
93
118
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher (driver , new Md5Checksum (), TEST_DATA_SIZE + CHECKSUM_SIZE );
94
119
p .subscribe (s );
95
120
int currOffset = 0 ;
@@ -100,6 +125,7 @@ public void testTinyPackets() {
100
125
}
101
126
driver .doOnComplete ();
102
127
128
+ assertArrayEquals (testDataWithoutChecksum , s .receivedData ());
103
129
assertTrue (s .hasCompleted ());
104
130
assertFalse (s .isOnErrorCalled ());
105
131
}
@@ -109,7 +135,7 @@ public void testTinyPackets() {
109
135
public void testUnknownLength () {
110
136
// When the length is unknown, the last 16 bytes are treated as a checksum, but are later ignored when completing
111
137
final TestPublisher driver = new TestPublisher ();
112
- final TestSubscriber s = new TestSubscriber (Arrays . copyOfRange ( testData , 0 , TEST_DATA_SIZE ) );
138
+ final TestSubscriber s = new TestSubscriber ();
113
139
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher (driver , new Md5Checksum (), 0 );
114
140
p .subscribe (s );
115
141
@@ -122,6 +148,7 @@ public void testUnknownLength() {
122
148
driver .doOnNext (ByteBuffer .wrap (randomChecksumData ));
123
149
driver .doOnComplete ();
124
150
151
+ assertArrayEquals (testDataWithoutChecksum , s .receivedData ());
125
152
assertTrue (s .hasCompleted ());
126
153
assertFalse (s .isOnErrorCalled ());
127
154
}
@@ -130,7 +157,7 @@ public void testUnknownLength() {
130
157
public void checksumValidationFailure_throwsSdkClientException_NotNPE () {
131
158
final byte [] incorrectData = new byte [0 ];
132
159
final TestPublisher driver = new TestPublisher ();
133
- final TestSubscriber s = new TestSubscriber (Arrays . copyOfRange ( incorrectData , 0 , TEST_DATA_SIZE ) );
160
+ final TestSubscriber s = new TestSubscriber ();
134
161
final ChecksumValidatingPublisher p = new ChecksumValidatingPublisher (driver , new Md5Checksum (), TEST_DATA_SIZE + CHECKSUM_SIZE );
135
162
p .subscribe (s );
136
163
@@ -142,13 +169,11 @@ public void checksumValidationFailure_throwsSdkClientException_NotNPE() {
142
169
}
143
170
144
171
private class TestSubscriber implements Subscriber <ByteBuffer > {
145
- final byte [] expected ;
146
172
final List <ByteBuffer > received ;
147
173
boolean completed ;
148
174
boolean onErrorCalled ;
149
175
150
- TestSubscriber (byte [] expected ) {
151
- this .expected = expected ;
176
+ TestSubscriber () {
152
177
this .received = new ArrayList <>();
153
178
this .completed = false ;
154
179
}
@@ -172,17 +197,21 @@ public void onError(Throwable t) {
172
197
173
198
@ Override
174
199
public void onComplete () {
175
- int matchPos = 0 ;
176
- for (ByteBuffer buffer : received ) {
177
- byte [] bufferData = new byte [buffer .limit () - buffer .position ()];
178
- buffer .get (bufferData );
179
- assertArrayEquals (Arrays .copyOfRange (expected , matchPos , matchPos + bufferData .length ), bufferData );
180
- matchPos += bufferData .length ;
181
- }
182
- assertEquals (expected .length , matchPos );
183
200
completed = true ;
184
201
}
185
202
203
+ public byte [] receivedData () {
204
+ try {
205
+ ByteArrayOutputStream os = new ByteArrayOutputStream ();
206
+ for (ByteBuffer buffer : received ) {
207
+ os .write (BinaryUtils .copyBytesFrom (buffer ));
208
+ }
209
+ return os .toByteArray ();
210
+ } catch (IOException e ) {
211
+ throw new UncheckedIOException (e );
212
+ }
213
+ }
214
+
186
215
public boolean hasCompleted () {
187
216
return completed ;
188
217
}
0 commit comments