Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. 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 distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.server.slice; |
| 16 | |
| 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.mockito.ArgumentMatchers.eq; |
| 19 | import static org.mockito.Mockito.clearInvocations; |
| 20 | import static org.mockito.Mockito.mock; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 23 | import android.testing.AndroidTestingRunner; |
| 24 | import android.testing.TestableLooper.RunWithLooper; |
| 25 | import android.util.Xml.Encoding; |
| 26 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 27 | import androidx.test.filters.SmallTest; |
| 28 | |
Jason Monk | bf3eedc | 2018-04-05 20:56:42 -0400 | [diff] [blame] | 29 | import com.android.server.UiServiceTestCase; |
| 30 | import com.android.server.slice.SlicePermissionManager.PkgUser; |
| 31 | import com.android.server.slice.SliceProviderPermissions.SliceAuthority; |
| 32 | |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.xmlpull.v1.XmlPullParser; |
| 36 | import org.xmlpull.v1.XmlPullParserException; |
| 37 | import org.xmlpull.v1.XmlPullParserFactory; |
| 38 | import org.xmlpull.v1.XmlSerializer; |
| 39 | |
| 40 | import java.io.ByteArrayInputStream; |
| 41 | import java.io.ByteArrayOutputStream; |
| 42 | import java.io.IOException; |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.Comparator; |
| 45 | |
| 46 | @SmallTest |
| 47 | @RunWith(AndroidTestingRunner.class) |
| 48 | @RunWithLooper |
| 49 | public class SliceProviderPermissionsTest extends UiServiceTestCase { |
| 50 | |
| 51 | @Test |
| 52 | public void testDirty_addAuthority() { |
| 53 | PkgUser pkg = new PkgUser("com.android.pkg", 0); |
| 54 | DirtyTracker tracker = mock(DirtyTracker.class); |
| 55 | SliceProviderPermissions provider = new SliceProviderPermissions(pkg, tracker); |
| 56 | |
| 57 | provider.getOrCreateAuthority("some_auth"); |
| 58 | |
| 59 | verify(tracker).onPersistableDirty(eq(provider)); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testDirty_addPkg() { |
| 64 | PkgUser pkg = new PkgUser("com.android.pkg", 0); |
| 65 | DirtyTracker tracker = mock(DirtyTracker.class); |
| 66 | SliceProviderPermissions provider = new SliceProviderPermissions(pkg, tracker); |
| 67 | |
| 68 | SliceAuthority auth = provider.getOrCreateAuthority("some_auth"); |
| 69 | clearInvocations(tracker); |
| 70 | |
| 71 | auth.addPkg(new PkgUser("pkg", 0)); |
| 72 | |
| 73 | verify(tracker).onPersistableDirty(eq(provider)); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testCreation() { |
| 78 | SliceProviderPermissions provider = createProvider(); |
| 79 | ArrayList<SliceAuthority> authorities = new ArrayList<>(provider.getAuthorities()); |
| 80 | authorities.sort(Comparator.comparing(SliceAuthority::getAuthority)); |
| 81 | |
| 82 | assertEquals(2, authorities.size()); |
| 83 | assertEquals("com.android.pkg", authorities.get(0).getAuthority()); |
| 84 | assertEquals("com.android.pkg.slices", authorities.get(1).getAuthority()); |
| 85 | |
| 86 | assertEquals(1, authorities.get(0).getPkgs().size()); |
| 87 | assertEquals(2, authorities.get(1).getPkgs().size()); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testSerialization() throws XmlPullParserException, IOException { |
| 92 | SliceProviderPermissions provider = createProvider(); |
| 93 | |
| 94 | ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 95 | XmlSerializer serializer = XmlPullParserFactory.newInstance().newSerializer(); |
| 96 | serializer.setOutput(output, Encoding.UTF_8.name()); |
| 97 | |
| 98 | provider.writeTo(serializer); |
| 99 | serializer.flush(); |
| 100 | |
| 101 | ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray()); |
| 102 | XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); |
| 103 | parser.setInput(input, Encoding.UTF_8.name()); |
| 104 | |
| 105 | SliceProviderPermissions deser = SliceProviderPermissions.createFrom(parser, |
| 106 | mock(DirtyTracker.class)); |
| 107 | |
| 108 | assertEquivalent(provider, deser); |
| 109 | } |
| 110 | |
| 111 | private void assertEquivalent(SliceProviderPermissions o1, SliceProviderPermissions o2) { |
| 112 | assertEquals(o1.getPkg(), o2.getPkg()); |
| 113 | assertEquals(o1.getAuthorities(), o2.getAuthorities()); |
| 114 | } |
| 115 | |
| 116 | private static SliceProviderPermissions createProvider() { |
| 117 | PkgUser pkg = new PkgUser("com.android.pkg", 2); |
| 118 | DirtyTracker tracker = mock(DirtyTracker.class); |
| 119 | SliceProviderPermissions provider = new SliceProviderPermissions(pkg, tracker); |
| 120 | |
| 121 | SliceAuthority auth = provider.getOrCreateAuthority("com.android.pkg.slices"); |
| 122 | auth.addPkg(new PkgUser("com.example.pkg", 0)); |
| 123 | auth.addPkg(new PkgUser("example.pkg.com", 10)); |
| 124 | |
| 125 | auth = provider.getOrCreateAuthority("com.android.pkg"); |
| 126 | auth.addPkg(new PkgUser("com.example.pkg", 2)); |
| 127 | return provider; |
| 128 | } |
| 129 | |
| 130 | } |