1 /* 2 * Copyright 2022 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 package com.google.android.libraries.mobiledatadownload.file.backends; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.junit.Assert.assertThrows; 20 21 import android.content.Context; 22 import android.net.Uri; 23 import androidx.test.core.app.ApplicationProvider; 24 import com.google.android.libraries.mobiledatadownload.file.common.MalformedUriException; 25 import com.google.common.io.BaseEncoding; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.robolectric.RobolectricTestRunner; 29 30 @RunWith(RobolectricTestRunner.class) 31 public final class BlobUriTest { 32 33 private final Context context = ApplicationProvider.getApplicationContext(); 34 35 @Test builder_setCorrectsParameters()36 public void builder_setCorrectsParameters() throws Exception { 37 Uri blobUri = BlobUri.builder(context).setBlobParameters("1234").build(); 38 39 assertThat(blobUri.getScheme()).isEqualTo("blobstore"); 40 assertThat(blobUri.getAuthority()).isEqualTo(context.getPackageName()); 41 assertThat(blobUri.getPath()).isEqualTo("/1234"); 42 assertThat(blobUri.toString()) 43 .isEqualTo( 44 "blobstore://com.google.android.libraries.mobiledatadownload.file.backends/1234"); 45 46 Uri leaseUri = BlobUri.builder(context).setAllLeasesParameters().build(); 47 48 assertThat(leaseUri.getScheme()).isEqualTo("blobstore"); 49 assertThat(leaseUri.getAuthority()).isEqualTo(context.getPackageName()); 50 assertThat(leaseUri.getPath()).isEqualTo("/*.lease"); 51 assertThat(leaseUri.toString()) 52 .isEqualTo( 53 "blobstore://com.google.android.libraries.mobiledatadownload.file.backends/*.lease"); 54 } 55 56 @Test builder_emptyChecksum_shouldThrow()57 public void builder_emptyChecksum_shouldThrow() throws Exception { 58 assertThrows( 59 MalformedUriException.class, () -> BlobUri.builder(context).setBlobParameters("").build()); 60 assertThrows( 61 MalformedUriException.class, 62 () -> BlobUri.builder(context).setLeaseParameters("", 1).build()); 63 } 64 65 @Test validateUri_wrongNumberOfSegments_shouldThrow()66 public void validateUri_wrongNumberOfSegments_shouldThrow() throws Exception { 67 Uri invalidUri = 68 BlobUri.builder(context) 69 .setBlobParameters("1234") 70 .build() 71 .buildUpon() 72 .appendPath("newSegment") 73 .build(); 74 assertThrows(MalformedUriException.class, () -> BlobUri.validateUri(invalidUri)); 75 } 76 77 @Test validateUri_allowOnlyPermittedChecksumExtensions()78 public void validateUri_allowOnlyPermittedChecksumExtensions() throws Exception { 79 Uri blobUri = BlobUri.builder(context).setBlobParameters("1234").build(); 80 Uri leaseUri = BlobUri.builder(context).setLeaseParameters("1234", 1).build(); 81 BlobUri.validateUri(blobUri); 82 BlobUri.validateUri(leaseUri); 83 84 Uri wrongExtensionUri = blobUri.buildUpon().path("1234.exts").build(); 85 assertThrows(MalformedUriException.class, () -> BlobUri.validateUri(wrongExtensionUri)); 86 Uri emptyChecksum = blobUri.buildUpon().path(".lease").build(); 87 assertThrows(MalformedUriException.class, () -> BlobUri.validateUri(emptyChecksum)); 88 } 89 90 @Test validateUri_allowOnlyPermittedQueryParameters()91 public void validateUri_allowOnlyPermittedQueryParameters() throws Exception { 92 Uri emptyQueryUri = new Uri.Builder().path("1234").build(); 93 BlobUri.validateUri(emptyQueryUri); 94 Uri queryWithExpiryDateUri = 95 new Uri.Builder().path("1234").appendQueryParameter("expiryDateSecs", "1").build(); 96 BlobUri.validateUri(queryWithExpiryDateUri); 97 98 Uri queryTooLongUri = 99 new Uri.Builder() 100 .path("1234") 101 .appendQueryParameter("fileSize", "1") 102 .appendQueryParameter("expiryDate", "1") 103 .build(); 104 assertThrows(MalformedUriException.class, () -> BlobUri.validateUri(queryTooLongUri)); 105 106 Uri queryWithUnexpectedParameterUri = 107 new Uri.Builder().path("1234").appendQueryParameter("wrongParameter", "1").build(); 108 assertThrows( 109 MalformedUriException.class, () -> BlobUri.validateUri(queryWithUnexpectedParameterUri)); 110 } 111 112 @Test isLeaseUri()113 public void isLeaseUri() throws Exception { 114 Uri leaseUri = BlobUri.builder(context).setLeaseParameters("1234", 1).build(); 115 assertThat(BlobUri.isLeaseUri(leaseUri.getPath())).isTrue(); 116 117 Uri nonLeaseUri = BlobUri.builder(context).setBlobParameters("1234").build(); 118 assertThat(BlobUri.isLeaseUri(nonLeaseUri.getPath())).isFalse(); 119 nonLeaseUri = new Uri.Builder().path("1234.exts").build(); 120 assertThat(BlobUri.isLeaseUri(nonLeaseUri.getPath())).isFalse(); 121 } 122 123 @Test getExpiryDateSecs_shouldSucceed()124 public void getExpiryDateSecs_shouldSucceed() throws Exception { 125 Uri leaseUri = BlobUri.builder(context).setLeaseParameters("1234", 1).build(); 126 assertThat(BlobUri.getExpiryDateSecs(leaseUri)).isEqualTo(1); 127 } 128 129 @Test getExpiryDateSecs_emptyQuery_shouldThrow()130 public void getExpiryDateSecs_emptyQuery_shouldThrow() throws Exception { 131 Uri leaseUri = BlobUri.builder(context).setBlobParameters("1234").build(); 132 assertThrows(MalformedUriException.class, () -> BlobUri.getExpiryDateSecs(leaseUri)); 133 } 134 135 @Test getChecksum()136 public void getChecksum() throws Exception { 137 Uri blobUri = BlobUri.builder(context).setBlobParameters("1234").build(); 138 byte[] expectedBytes = BaseEncoding.base16().lowerCase().decode("1234"); 139 assertThat(BlobUri.getChecksum(blobUri.getPath())).isEqualTo(expectedBytes); 140 } 141 } 142