|
24 | 24 | import static org.junit.Assert.assertNull; |
25 | 25 | import static org.junit.Assert.assertTrue; |
26 | 26 | import static org.junit.Assert.fail; |
| 27 | +import static com.google.common.truth.Truth.assertThat; |
27 | 28 |
|
28 | 29 | import com.google.api.gax.paging.Page; |
29 | 30 | import com.google.cloud.Identity; |
|
50 | 51 | import com.google.cloud.storage.StorageClass; |
51 | 52 | import com.google.cloud.storage.StorageException; |
52 | 53 | import com.google.cloud.storage.StorageRoles; |
| 54 | +import com.google.cloud.storage.StorageOptions; |
53 | 55 | import com.google.cloud.storage.testing.RemoteStorageHelper; |
54 | 56 | import com.google.common.collect.ImmutableList; |
55 | 57 | import com.google.common.collect.ImmutableMap; |
@@ -1319,6 +1321,55 @@ public void testGetBlobs() { |
1319 | 1321 | assertTrue(remoteBlobs.get(1).delete()); |
1320 | 1322 | } |
1321 | 1323 |
|
| 1324 | + @Test |
| 1325 | + public void testDownloadPublicBlobWithoutAuthentication() { |
| 1326 | + // create an unauthorized user |
| 1327 | + Storage unauthorizedStorage = StorageOptions.getUnauthenticatedInstance().getService(); |
| 1328 | + |
| 1329 | + // try to download blobs from a public bucket |
| 1330 | + String landsatBucket = "gcp-public-data-landsat"; |
| 1331 | + String landsatPrefix = "LC08/PRE/044/034/LC80440342016259LGN00/"; |
| 1332 | + String landsatBlob = landsatPrefix + "LC80440342016259LGN00_MTL.txt"; |
| 1333 | + byte[] bytes = unauthorizedStorage.readAllBytes(landsatBucket, landsatBlob); |
| 1334 | + assertThat(bytes.length).isEqualTo(7903); |
| 1335 | + int numBlobs = 0; |
| 1336 | + Iterator<Blob> blobIterator = unauthorizedStorage |
| 1337 | + .list(landsatBucket, Storage.BlobListOption.prefix(landsatPrefix)) |
| 1338 | + .iterateAll().iterator(); |
| 1339 | + while (blobIterator.hasNext()) { |
| 1340 | + numBlobs++; |
| 1341 | + blobIterator.next(); |
| 1342 | + } |
| 1343 | + assertThat(numBlobs).isEqualTo(13); |
| 1344 | + |
| 1345 | + // try to download blobs from a bucket that requires authentication |
| 1346 | + // authenticated client will succeed |
| 1347 | + // unauthenticated client will receive an exception |
| 1348 | + String sourceBlobName = "source-blob-name"; |
| 1349 | + BlobInfo sourceBlob = BlobInfo.newBuilder(BUCKET, sourceBlobName).build(); |
| 1350 | + assertThat(storage.create(sourceBlob)).isNotNull(); |
| 1351 | + assertThat(storage.readAllBytes(BUCKET, sourceBlobName)).isNotNull(); |
| 1352 | + try { |
| 1353 | + unauthorizedStorage.readAllBytes(BUCKET, sourceBlobName); |
| 1354 | + fail("Expected StorageException"); |
| 1355 | + } catch (StorageException ex) { |
| 1356 | + // expected |
| 1357 | + } |
| 1358 | + assertThat(storage.get(sourceBlob.getBlobId()).delete()).isTrue(); |
| 1359 | + |
| 1360 | + // try to upload blobs to a bucket that requires authentication |
| 1361 | + // authenticated client will succeed |
| 1362 | + // unauthenticated client will receive an exception |
| 1363 | + assertThat(storage.create(sourceBlob)).isNotNull(); |
| 1364 | + try { |
| 1365 | + unauthorizedStorage.create(sourceBlob); |
| 1366 | + fail("Expected StorageException"); |
| 1367 | + } catch (StorageException ex) { |
| 1368 | + // expected |
| 1369 | + } |
| 1370 | + assertThat(storage.get(sourceBlob.getBlobId()).delete()).isTrue(); |
| 1371 | + } |
| 1372 | + |
1322 | 1373 | @Test |
1323 | 1374 | public void testGetBlobsFail() { |
1324 | 1375 | String sourceBlobName1 = "test-get-blobs-fail-1"; |
|
0 commit comments