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.openers; 17 18 import static com.google.android.libraries.mobiledatadownload.file.common.testing.StreamUtils.createFile; 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.net.Uri; 22 import com.google.android.libraries.mobiledatadownload.file.SynchronousFileStorage; 23 import com.google.android.libraries.mobiledatadownload.file.backends.JavaFileBackend; 24 import com.google.android.libraries.mobiledatadownload.file.common.testing.TemporaryUri; 25 import com.google.android.libraries.mobiledatadownload.file.transforms.IntegrityTransform; 26 import com.google.android.libraries.mobiledatadownload.file.transforms.TransformProtoFragments; 27 import com.google.common.collect.ImmutableList; 28 import com.google.mobiledatadownload.TransformProto; 29 import org.junit.Before; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.RobolectricTestRunner; 34 35 @RunWith(RobolectricTestRunner.class) 36 public final class IntegrityUriComputingOpenerTest { 37 38 private static final String ORIGTEXT = "This is some regular old text ABC 123 !@#"; 39 private static final String ORIGTEXT_SHA256_B64 = "FoR1HrxdAhY05DE/gAUj0yjpzYpfWb0fJE+XBp8lY0o="; 40 private static final String ORIGTEXT_SHA256_B64_INCORRECT = "INCORRECT"; 41 42 @Rule public TemporaryUri tmpUri = new TemporaryUri(); 43 44 private SynchronousFileStorage storage; 45 46 @Before setUpStorage()47 public void setUpStorage() throws Exception { 48 storage = 49 new SynchronousFileStorage( 50 ImmutableList.of(new JavaFileBackend()), ImmutableList.of(new IntegrityTransform())); 51 } 52 53 @Test integrityUriComputingOpener_shouldProduceCorrectChecksum()54 public void integrityUriComputingOpener_shouldProduceCorrectChecksum() throws Exception { 55 56 Uri initialUri = createTestFile(ORIGTEXT); 57 Uri uriWithChecksum = storage.open(initialUri, IntegrityUriComputingOpener.create()); 58 String digest = IntegrityTransform.getDigestIfPresent(uriWithChecksum); 59 60 assertThat(digest).isEqualTo(ORIGTEXT_SHA256_B64); 61 } 62 63 @Test integrityUriComputingOpener_shouldIgnoreIncorrectInitialSpec()64 public void integrityUriComputingOpener_shouldIgnoreIncorrectInitialSpec() throws Exception { 65 TransformProto.Transform spec = 66 TransformProto.Transform.newBuilder() 67 .setIntegrity( 68 TransformProto.IntegrityTransform.newBuilder() 69 .setSha256(ORIGTEXT_SHA256_B64_INCORRECT)) 70 .build(); 71 Uri initialUri = TransformProtoFragments.addOrReplaceTransform(createTestFile(ORIGTEXT), spec); 72 Uri uriWithChecksum = storage.open(initialUri, IntegrityUriComputingOpener.create()); 73 String digest = IntegrityTransform.getDigestIfPresent(uriWithChecksum); 74 75 assertThat(digest).isEqualTo(ORIGTEXT_SHA256_B64); 76 } 77 78 @Test integrityUriComputingOpener_shouldIgnoreIntegrityParamWithNoSubparam()79 public void integrityUriComputingOpener_shouldIgnoreIntegrityParamWithNoSubparam() 80 throws Exception { 81 TransformProto.Transform spec = 82 TransformProto.Transform.newBuilder() 83 .setIntegrity(TransformProto.IntegrityTransform.getDefaultInstance()) 84 .build(); 85 86 Uri initialUri = TransformProtoFragments.addOrReplaceTransform(createTestFile(ORIGTEXT), spec); 87 Uri uriWithChecksum = storage.open(initialUri, IntegrityUriComputingOpener.create()); 88 String digest = IntegrityTransform.getDigestIfPresent(uriWithChecksum); 89 90 assertThat(digest).isEqualTo(ORIGTEXT_SHA256_B64); 91 } 92 createTestFile(String contents)93 private Uri createTestFile(String contents) throws Exception { 94 Uri uri = tmpUri.newUri(); 95 createFile(storage, uri, contents); 96 return uri; 97 } 98 } 99