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.accounts.Account; 22 import com.google.thirdparty.robolectric.GoogleRobolectricTestRunner; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 26 @RunWith(GoogleRobolectricTestRunner.class) 27 public final class AccountSerializationTest { 28 29 @Test sharedAccount_isSerializedAsShared()30 public void sharedAccount_isSerializedAsShared() { 31 String accountStr = AccountSerialization.serialize(AccountSerialization.SHARED_ACCOUNT); 32 assertThat(accountStr).isEqualTo("shared"); 33 } 34 35 @Test shared_isDeserializedAsSharedAccount()36 public void shared_isDeserializedAsSharedAccount() { 37 Account account = AccountSerialization.deserialize("shared"); 38 assertThat(account).isEqualTo(AccountSerialization.SHARED_ACCOUNT); 39 } 40 41 @Test deserialize_withEmptyAccountName()42 public void deserialize_withEmptyAccountName() { 43 String accountStr = "google.com:"; 44 assertThrows( 45 IllegalArgumentException.class, () -> AccountSerialization.deserialize(accountStr)); 46 } 47 48 @Test deserialize_withEmptyAccountType()49 public void deserialize_withEmptyAccountType() { 50 String accountStr = ":<internal>@gmail.com"; 51 assertThrows( 52 IllegalArgumentException.class, () -> AccountSerialization.deserialize(accountStr)); 53 } 54 55 @Test deserialize_withMalformedAccount()56 public void deserialize_withMalformedAccount() { 57 String accountStr = "MALFORMED"; 58 assertThrows( 59 IllegalArgumentException.class, () -> AccountSerialization.deserialize(accountStr)); 60 } 61 62 @Test serialize_validatesAccount()63 public void serialize_validatesAccount() { 64 AccountSerialization.serialize(AccountSerialization.SHARED_ACCOUNT); 65 AccountSerialization.serialize(new Account("<internal>@gmail.com", "google.com")); 66 67 // Android account already does some validation 68 assertThrows(IllegalArgumentException.class, () -> new Account("", "")); 69 assertThrows(IllegalArgumentException.class, () -> new Account("", "google.com")); 70 assertThrows(IllegalArgumentException.class, () -> new Account("<internal>@gmail.com", "")); 71 Account typeWithColon = new Account("<internal>@gmail.com", "type:"); 72 Account typeWithSlash = new Account("<internal>@gmail.com", "type/"); 73 Account nameWithSlash = new Account("you/email.com", "google.com"); 74 assertThrows( 75 IllegalArgumentException.class, () -> AccountSerialization.serialize(typeWithColon)); 76 assertThrows( 77 IllegalArgumentException.class, () -> AccountSerialization.serialize(typeWithSlash)); 78 assertThrows( 79 IllegalArgumentException.class, () -> AccountSerialization.serialize(nameWithSlash)); 80 } 81 } 82