|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertArrayEquals; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.api.client.http.HttpTransport; |
| 24 | +import com.google.api.client.json.gson.GsonFactory; |
| 25 | +import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
| 26 | +import com.google.api.core.SettableApiFuture; |
| 27 | +import com.google.api.services.storage.Storage; |
| 28 | +import com.google.api.services.storage.model.StorageObject; |
| 29 | +import com.google.cloud.storage.ApiaryUnbufferedReadableByteChannel.ApiaryReadRequest; |
| 30 | +import com.google.cloud.storage.Retrying.RetrierWithAlg; |
| 31 | +import com.google.common.collect.ImmutableMap; |
| 32 | +import java.io.ByteArrayOutputStream; |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.ByteBuffer; |
| 35 | +import java.nio.channels.Channels; |
| 36 | +import java.nio.channels.WritableByteChannel; |
| 37 | +import java.util.Map; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +public class ApiaryUnbufferedReadableByteChannelTest { |
| 41 | + |
| 42 | + private static final byte[] CONTENT_BYTES = "Hello, World!".getBytes(); |
| 43 | + private static final String CORRECT_CRC32C_BASE64 = "TVUQaA=="; |
| 44 | + private static final String WRONG_CRC32C_BASE64 = "AAAAAA=="; |
| 45 | + |
| 46 | + private Storage createMockStorageClient(String googHashHeader) { |
| 47 | + return createMockStorageClient(200, googHashHeader, null); |
| 48 | + } |
| 49 | + |
| 50 | + private Storage createMockStorageClient( |
| 51 | + int statusCode, String googHashHeader, Map<String, String> extraHeaders) { |
| 52 | + HttpTransport transport = |
| 53 | + new HttpTransport() { |
| 54 | + @Override |
| 55 | + protected com.google.api.client.http.LowLevelHttpRequest buildRequest( |
| 56 | + String method, String url) throws IOException { |
| 57 | + return new com.google.api.client.testing.http.MockLowLevelHttpRequest() { |
| 58 | + @Override |
| 59 | + public com.google.api.client.http.LowLevelHttpResponse execute() throws IOException { |
| 60 | + MockLowLevelHttpResponse lowLevelResponse = |
| 61 | + new MockLowLevelHttpResponse() |
| 62 | + .setStatusCode(statusCode) |
| 63 | + .setContent(CONTENT_BYTES) |
| 64 | + .setContentLength(CONTENT_BYTES.length) |
| 65 | + .addHeader("Content-Length", String.valueOf(CONTENT_BYTES.length)) |
| 66 | + .addHeader("x-goog-generation", "12345"); |
| 67 | + if (googHashHeader != null) { |
| 68 | + lowLevelResponse.addHeader("x-goog-hash", googHashHeader); |
| 69 | + } |
| 70 | + if (extraHeaders != null) { |
| 71 | + for (Map.Entry<String, String> entry : extraHeaders.entrySet()) { |
| 72 | + lowLevelResponse.addHeader(entry.getKey(), entry.getValue()); |
| 73 | + } |
| 74 | + } |
| 75 | + return lowLevelResponse; |
| 76 | + } |
| 77 | + }; |
| 78 | + } |
| 79 | + }; |
| 80 | + return new Storage.Builder(transport, GsonFactory.getDefaultInstance(), null) |
| 81 | + .setApplicationName("test") |
| 82 | + .build(); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testRead_successfulCrc32cValidation() throws IOException { |
| 87 | + Storage storageClient = createMockStorageClient("crc32c=" + CORRECT_CRC32C_BASE64); |
| 88 | + |
| 89 | + StorageObject from = new StorageObject().setBucket("bucket").setName("blob"); |
| 90 | + ApiaryReadRequest apiaryReadRequest = |
| 91 | + new ApiaryReadRequest(from, ImmutableMap.of(), ByteRangeSpec.nullRange()); |
| 92 | + |
| 93 | + SettableApiFuture<StorageObject> resultFuture = SettableApiFuture.create(); |
| 94 | + try (ApiaryUnbufferedReadableByteChannel channel = |
| 95 | + new ApiaryUnbufferedReadableByteChannel( |
| 96 | + apiaryReadRequest, |
| 97 | + storageClient, |
| 98 | + resultFuture, |
| 99 | + RetrierWithAlg.attemptOnce(), |
| 100 | + Retrying.neverRetry(), |
| 101 | + Hasher.defaultHasher()); ) { |
| 102 | + |
| 103 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 104 | + try (WritableByteChannel w = Channels.newChannel(out)) { |
| 105 | + ByteBuffer buf = ByteBuffer.allocate(4096); |
| 106 | + while (channel.read(new ByteBuffer[] {buf}, 0, 1) != -1) { |
| 107 | + buf.flip(); |
| 108 | + w.write(buf); |
| 109 | + buf.clear(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + assertArrayEquals(CONTENT_BYTES, out.toByteArray()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testRead_mismatchedCrc32cValidation_throwsChecksumMismatch() throws IOException { |
| 119 | + Storage storageClient = createMockStorageClient("crc32c=" + WRONG_CRC32C_BASE64); |
| 120 | + |
| 121 | + StorageObject from = new StorageObject().setBucket("bucket").setName("blob"); |
| 122 | + ApiaryReadRequest apiaryReadRequest = |
| 123 | + new ApiaryReadRequest(from, ImmutableMap.of(), ByteRangeSpec.nullRange()); |
| 124 | + |
| 125 | + SettableApiFuture<StorageObject> resultFuture = SettableApiFuture.create(); |
| 126 | + try (ApiaryUnbufferedReadableByteChannel channel = |
| 127 | + new ApiaryUnbufferedReadableByteChannel( |
| 128 | + apiaryReadRequest, |
| 129 | + storageClient, |
| 130 | + resultFuture, |
| 131 | + RetrierWithAlg.attemptOnce(), |
| 132 | + Retrying.neverRetry(), |
| 133 | + Hasher.defaultHasher()); ) { |
| 134 | + |
| 135 | + ByteBuffer buf = ByteBuffer.allocate(4096); |
| 136 | + Hasher.ChecksumMismatchException expected = |
| 137 | + assertThrows( |
| 138 | + Hasher.ChecksumMismatchException.class, |
| 139 | + () -> { |
| 140 | + while (channel.read(new ByteBuffer[] {buf}, 0, 1) != -1) { |
| 141 | + buf.clear(); |
| 142 | + } |
| 143 | + }); |
| 144 | + |
| 145 | + assertTrue(expected.getMessage().contains("Mismatch checksum value")); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + public void testRead_suffixRangeFullObjectCrc32cValidation() throws IOException { |
| 151 | + // Suffix range request resulting in content-range: bytes 0-12/13 |
| 152 | + Map<String, String> extraHeaders = ImmutableMap.of("Content-Range", "bytes 0-12/13"); |
| 153 | + Storage storageClient = |
| 154 | + createMockStorageClient(206, "crc32c=" + CORRECT_CRC32C_BASE64, extraHeaders); |
| 155 | + |
| 156 | + StorageObject from = new StorageObject().setBucket("bucket").setName("blob"); |
| 157 | + ApiaryReadRequest apiaryReadRequest = |
| 158 | + new ApiaryReadRequest(from, ImmutableMap.of(), ByteRangeSpec.nullRange()); |
| 159 | + |
| 160 | + SettableApiFuture<StorageObject> resultFuture = SettableApiFuture.create(); |
| 161 | + try (ApiaryUnbufferedReadableByteChannel channel = |
| 162 | + new ApiaryUnbufferedReadableByteChannel( |
| 163 | + apiaryReadRequest, |
| 164 | + storageClient, |
| 165 | + resultFuture, |
| 166 | + RetrierWithAlg.attemptOnce(), |
| 167 | + Retrying.neverRetry(), |
| 168 | + Hasher.defaultHasher()); ) { |
| 169 | + |
| 170 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 171 | + try (WritableByteChannel w = Channels.newChannel(out)) { |
| 172 | + ByteBuffer buf = ByteBuffer.allocate(4096); |
| 173 | + while (channel.read(new ByteBuffer[] {buf}, 0, 1) != -1) { |
| 174 | + buf.flip(); |
| 175 | + w.write(buf); |
| 176 | + buf.clear(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + assertArrayEquals(CONTENT_BYTES, out.toByteArray()); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testRead_partialRangeNoCrc32cValidation() throws IOException { |
| 186 | + // Partial range request resulting in content-range: bytes 1-12/13 |
| 187 | + Map<String, String> extraHeaders = ImmutableMap.of("Content-Range", "bytes 1-12/13"); |
| 188 | + // Even if checksum is wrong, it shouldn't throw ChecksumMismatchException because validation is |
| 189 | + // skipped for partial downloads. |
| 190 | + Storage storageClient = |
| 191 | + createMockStorageClient(206, "crc32c=" + WRONG_CRC32C_BASE64, extraHeaders); |
| 192 | + |
| 193 | + StorageObject from = new StorageObject().setBucket("bucket").setName("blob"); |
| 194 | + ApiaryReadRequest apiaryReadRequest = |
| 195 | + new ApiaryReadRequest(from, ImmutableMap.of(), ByteRangeSpec.nullRange()); |
| 196 | + |
| 197 | + SettableApiFuture<StorageObject> resultFuture = SettableApiFuture.create(); |
| 198 | + try (ApiaryUnbufferedReadableByteChannel channel = |
| 199 | + new ApiaryUnbufferedReadableByteChannel( |
| 200 | + apiaryReadRequest, |
| 201 | + storageClient, |
| 202 | + resultFuture, |
| 203 | + RetrierWithAlg.attemptOnce(), |
| 204 | + Retrying.neverRetry(), |
| 205 | + Hasher.defaultHasher()); ) { |
| 206 | + |
| 207 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 208 | + try (WritableByteChannel w = Channels.newChannel(out)) { |
| 209 | + ByteBuffer buf = ByteBuffer.allocate(4096); |
| 210 | + while (channel.read(new ByteBuffer[] {buf}, 0, 1) != -1) { |
| 211 | + buf.flip(); |
| 212 | + w.write(buf); |
| 213 | + buf.clear(); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + // We read the entire response content successfully (no exception thrown) |
| 218 | + assertArrayEquals(CONTENT_BYTES, out.toByteArray()); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments