xref: /aosp_15_r20/external/downloader/src/test/java/com/google/android/downloader/DataUrlTest.java (revision 2b3ec505d2eb42ded0bcac2cc02f08e691c7d592)
1 // Copyright 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.google.android.downloader;
16 
17 import static com.google.common.truth.Truth.assertThat;
18 import static java.nio.charset.StandardCharsets.UTF_8;
19 import static org.junit.Assert.assertThrows;
20 
21 import com.google.android.downloader.DataUrl.DataUrlException;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /** Unit tests for DataUrl parsing. */
27 @RunWith(JUnit4.class)
28 public class DataUrlTest {
29   @Test
invalidScheme()30   public void invalidScheme() {
31     assertThrows(DataUrlException.class, () -> DataUrl.parseFromString("http://example.com"));
32   }
33 
34   @Test
invalidSyntax()35   public void invalidSyntax() {
36     assertThrows(DataUrlException.class, () -> DataUrl.parseFromString("data:foobar"));
37   }
38 
39   @Test
missingEncoding()40   public void missingEncoding() {
41     assertThrows(DataUrlException.class, () -> DataUrl.parseFromString("data:text/plain,foobar"));
42   }
43 
44   @Test
invalidEncoding()45   public void invalidEncoding() {
46     assertThrows(
47         DataUrlException.class, () -> DataUrl.parseFromString("data:text/plain;base32,foobar"));
48   }
49 
50   @Test
invalidBase64Data()51   public void invalidBase64Data() {
52     // Note that '*' is not a valid character in base64.
53     assertThrows(
54         DataUrlException.class, () -> DataUrl.parseFromString("data:text/plain;base64,foobar*"));
55   }
56 
57   @Test
validData()58   public void validData() {
59     // Note: 'Zm9vYmFy' is the base64-encoding of 'foobar'.
60     DataUrl dataUrl = DataUrl.parseFromString("data:text/plain;base64,Zm9vYmFy");
61     assertThat(dataUrl.data()).isEqualTo("foobar".getBytes(UTF_8));
62     assertThat(dataUrl.mimeType()).isEqualTo("text/plain");
63   }
64 
65   @Test
validData_notUrl()66   public void validData_notUrl() {
67     // Note: 'Zm9vLiw/YmFy' is the base64-encoding of 'foo.,?bar'. Because there's a slash in the
68     // base64 payload, this isn't a url-safe base64 encoding, so this test will verify we fall
69     // back to regular base64 encoding instead of just url-safe encoding.
70     DataUrl dataUrl = DataUrl.parseFromString("data:text/plain;base64,Zm9vLiw/YmFy");
71     assertThat(dataUrl.data()).isEqualTo("foo.,?bar".getBytes(UTF_8));
72     assertThat(dataUrl.mimeType()).isEqualTo("text/plain");
73   }
74 }
75