|
| 1 | +package com.getcapacitor; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.ByteArrayInputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class BoundedInputStreamTest { |
| 11 | + |
| 12 | + private byte[] testData() { |
| 13 | + byte[] data = new byte[256]; |
| 14 | + for (int i = 0; i < data.length; i++) { |
| 15 | + data[i] = (byte) i; |
| 16 | + } |
| 17 | + return data; |
| 18 | + } |
| 19 | + |
| 20 | + @Test |
| 21 | + public void readSingleByte_stopsAtLimit() throws IOException { |
| 22 | + byte[] data = testData(); |
| 23 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream(new ByteArrayInputStream(data), 5); |
| 24 | + |
| 25 | + for (int i = 0; i < 5; i++) { |
| 26 | + assertEquals(i, bounded.read()); |
| 27 | + } |
| 28 | + assertEquals(-1, bounded.read()); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void readByteArray_stopsAtLimit() throws IOException { |
| 33 | + byte[] data = testData(); |
| 34 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream(new ByteArrayInputStream(data), 10); |
| 35 | + |
| 36 | + byte[] buf = new byte[20]; |
| 37 | + int read = bounded.read(buf, 0, 20); |
| 38 | + assertEquals(10, read); |
| 39 | + assertEquals(-1, bounded.read(buf, 0, 20)); |
| 40 | + |
| 41 | + for (int i = 0; i < 10; i++) { |
| 42 | + assertEquals((byte) i, buf[i]); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void available_reflectsLimit() throws IOException { |
| 48 | + byte[] data = testData(); |
| 49 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream(new ByteArrayInputStream(data), 10); |
| 50 | + |
| 51 | + assertEquals(10, bounded.available()); |
| 52 | + |
| 53 | + bounded.read(); |
| 54 | + assertEquals(9, bounded.available()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void limitLargerThanStream_returnsAllData() throws IOException { |
| 59 | + byte[] data = new byte[] { 1, 2, 3 }; |
| 60 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream(new ByteArrayInputStream(data), 100); |
| 61 | + |
| 62 | + assertEquals(1, bounded.read()); |
| 63 | + assertEquals(2, bounded.read()); |
| 64 | + assertEquals(3, bounded.read()); |
| 65 | + assertEquals(-1, bounded.read()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void zeroLimit_returnsNoData() throws IOException { |
| 70 | + byte[] data = testData(); |
| 71 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream(new ByteArrayInputStream(data), 0); |
| 72 | + |
| 73 | + assertEquals(-1, bounded.read()); |
| 74 | + assertEquals(0, bounded.available()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void close_closesUnderlying() throws IOException { |
| 79 | + final boolean[] closed = { false }; |
| 80 | + InputStream inner = new ByteArrayInputStream(new byte[10]) { |
| 81 | + @Override |
| 82 | + public void close() throws IOException { |
| 83 | + closed[0] = true; |
| 84 | + super.close(); |
| 85 | + } |
| 86 | + }; |
| 87 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream(inner, 5); |
| 88 | + bounded.close(); |
| 89 | + assertTrue(closed[0]); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Simulates the WebView range request behavior: |
| 94 | + * WebView seeks (consumes) fromRange bytes, then reads contentLength bytes. |
| 95 | + * The BoundedInputStream limit should be endRange + 1 to account for both. |
| 96 | + */ |
| 97 | + @Test |
| 98 | + public void simulateWebViewRangeSeek() throws IOException { |
| 99 | + // 1000-byte file, request bytes=200-499 |
| 100 | + int totalSize = 1000; |
| 101 | + int fromRange = 200; |
| 102 | + int endRange = 499; |
| 103 | + int contentLength = endRange - fromRange + 1; // 300 |
| 104 | + |
| 105 | + byte[] fileData = new byte[totalSize]; |
| 106 | + for (int i = 0; i < totalSize; i++) { |
| 107 | + fileData[i] = (byte) (i & 0xFF); |
| 108 | + } |
| 109 | + |
| 110 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream( |
| 111 | + new ByteArrayInputStream(fileData), |
| 112 | + endRange + 1 // 500 |
| 113 | + ); |
| 114 | + |
| 115 | + // WebView seeks by consuming fromRange bytes |
| 116 | + byte[] seekBuf = new byte[fromRange]; |
| 117 | + int seeked = 0; |
| 118 | + while (seeked < fromRange) { |
| 119 | + int r = bounded.read(seekBuf, seeked, fromRange - seeked); |
| 120 | + if (r == -1) break; |
| 121 | + seeked += r; |
| 122 | + } |
| 123 | + assertEquals(fromRange, seeked); |
| 124 | + |
| 125 | + // WebView then reads the actual content |
| 126 | + byte[] content = new byte[contentLength]; |
| 127 | + int totalRead = 0; |
| 128 | + while (totalRead < contentLength) { |
| 129 | + int r = bounded.read(content, totalRead, contentLength - totalRead); |
| 130 | + if (r == -1) break; |
| 131 | + totalRead += r; |
| 132 | + } |
| 133 | + assertEquals(contentLength, totalRead); |
| 134 | + |
| 135 | + // Verify data is correct (bytes 200-499) |
| 136 | + for (int i = 0; i < contentLength; i++) { |
| 137 | + assertEquals((byte) ((fromRange + i) & 0xFF), content[i]); |
| 138 | + } |
| 139 | + |
| 140 | + // No more data should be available |
| 141 | + assertEquals(-1, bounded.read()); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * When end range is omitted (request to end of file), |
| 146 | + * the limit is totalSize and the full remainder is returned. |
| 147 | + */ |
| 148 | + @Test |
| 149 | + public void simulateWebViewRangeSeek_openEnded() throws IOException { |
| 150 | + int totalSize = 500; |
| 151 | + int fromRange = 300; |
| 152 | + int endRange = totalSize - 1; // 499 |
| 153 | + |
| 154 | + byte[] fileData = new byte[totalSize]; |
| 155 | + for (int i = 0; i < totalSize; i++) { |
| 156 | + fileData[i] = (byte) (i & 0xFF); |
| 157 | + } |
| 158 | + |
| 159 | + InputStream bounded = new WebViewLocalServer.BoundedInputStream( |
| 160 | + new ByteArrayInputStream(fileData), |
| 161 | + endRange + 1 // = totalSize |
| 162 | + ); |
| 163 | + |
| 164 | + // WebView seeks |
| 165 | + for (int i = 0; i < fromRange; i++) { |
| 166 | + int b = bounded.read(); |
| 167 | + assertNotEquals(-1, b); |
| 168 | + } |
| 169 | + |
| 170 | + // Read remainder |
| 171 | + int contentLength = endRange - fromRange + 1; // 200 |
| 172 | + byte[] content = new byte[contentLength]; |
| 173 | + int totalRead = 0; |
| 174 | + while (totalRead < contentLength) { |
| 175 | + int r = bounded.read(content, totalRead, contentLength - totalRead); |
| 176 | + if (r == -1) break; |
| 177 | + totalRead += r; |
| 178 | + } |
| 179 | + assertEquals(contentLength, totalRead); |
| 180 | + |
| 181 | + // Verify last byte is correct |
| 182 | + assertEquals((byte) (endRange & 0xFF), content[contentLength - 1]); |
| 183 | + assertEquals(-1, bounded.read()); |
| 184 | + } |
| 185 | +} |
0 commit comments