|
37 | 37 | import org.junit.rules.ExpectedException; |
38 | 38 | import org.junit.runner.RunWith; |
39 | 39 | import org.junit.runners.JUnit4; |
| 40 | +import org.mockito.ArgumentCaptor; |
40 | 41 |
|
41 | 42 | import javax.net.ssl.SSLHandshakeException; |
42 | 43 | import java.io.IOException; |
@@ -191,4 +192,25 @@ public void testSetPosition() throws IOException { |
191 | 192 | verify(gcsChannel).seek(1); |
192 | 193 | verify(gcsChannel, times(5)).isOpen(); |
193 | 194 | } |
| 195 | + |
| 196 | + /* |
| 197 | + * This test case was crafted in response to a bug in CloudStorageReadChannel in which the |
| 198 | + * channel position (a long) was getting truncated to an int when seeking on the encapsulated |
| 199 | + * ReadChannel in innerOpen(). This test case fails when the bad long -> int cast is present, |
| 200 | + * and passes when it's removed. |
| 201 | + */ |
| 202 | + @Test |
| 203 | + public void testChannelPositionDoesNotGetTruncatedToInt() throws IOException { |
| 204 | + // This position value will overflow to a negative value if a long -> int cast is attempted |
| 205 | + long startPosition = 11918483280L; |
| 206 | + ArgumentCaptor<Long> captor = ArgumentCaptor.forClass(Long.class); |
| 207 | + |
| 208 | + // Invoke CloudStorageReadChannel.create() to trigger a call to the private |
| 209 | + // CloudStorageReadChannel.innerOpen() method, which does a seek on our gcsChannel. |
| 210 | + CloudStorageReadChannel.create(gcsStorage, file, startPosition, 1); |
| 211 | + |
| 212 | + // Confirm that our position did not overflow during the seek in CloudStorageReadChannel.innerOpen() |
| 213 | + verify(gcsChannel).seek(captor.capture()); |
| 214 | + assertThat(captor.getValue()).isEqualTo(startPosition); |
| 215 | + } |
194 | 216 | } |
0 commit comments