Home | History | Annotate | Download | only in slice
      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.junit.Assert.assertFalse;
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.os.UserHandle;
     22 import android.support.test.filters.SmallTest;
     23 import android.util.Xml.Encoding;
     24 
     25 import com.android.server.UiServiceTestCase;
     26 
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.xmlpull.v1.XmlPullParser;
     30 import org.xmlpull.v1.XmlPullParserException;
     31 import org.xmlpull.v1.XmlPullParserFactory;
     32 import org.xmlpull.v1.XmlSerializer;
     33 
     34 import java.io.ByteArrayInputStream;
     35 import java.io.ByteArrayOutputStream;
     36 import java.io.IOException;
     37 
     38 @SmallTest
     39 public class SliceFullAccessListTest extends UiServiceTestCase {
     40 
     41     private static final String TEST_XML = "<slice-access-list version=\"1\"><user "
     42             + "user=\"0\"><pkg>pkg</pkg><pkg>pkg1</pkg></user><user "
     43             + "user=\"1\"><pkg>pkg</pkg></user><user "
     44             + "user=\"3\"><pkg>pkg2</pkg></user></slice-access-list>";
     45 
     46     private SliceFullAccessList mAccessList;
     47 
     48     @Before
     49     public void setup() {
     50         mAccessList = new SliceFullAccessList(mContext);
     51     }
     52 
     53     @Test
     54     public void testNoDefaultAccess() {
     55         assertFalse(mAccessList.hasFullAccess("pkg", 0));
     56     }
     57 
     58     @Test
     59     public void testGrantAccess() {
     60         mAccessList.grantFullAccess("pkg", 0);
     61         assertTrue(mAccessList.hasFullAccess("pkg", 0));
     62     }
     63 
     64     @Test
     65     public void testUserSeparation() {
     66         mAccessList.grantFullAccess("pkg", 1);
     67         assertFalse(mAccessList.hasFullAccess("pkg", 0));
     68     }
     69 
     70     @Test
     71     public void testRemoveAccess() {
     72         mAccessList.grantFullAccess("pkg", 0);
     73         assertTrue(mAccessList.hasFullAccess("pkg", 0));
     74 
     75         mAccessList.removeGrant("pkg", 0);
     76         assertFalse(mAccessList.hasFullAccess("pkg", 0));
     77     }
     78 
     79     @Test
     80     public void testSerialization() throws XmlPullParserException, IOException {
     81         mAccessList.grantFullAccess("pkg", 0);
     82         mAccessList.grantFullAccess("pkg1", 0);
     83         mAccessList.grantFullAccess("pkg", 1);
     84         mAccessList.grantFullAccess("pkg2", 3);
     85 
     86         ByteArrayOutputStream output = new ByteArrayOutputStream();
     87         XmlSerializer out = XmlPullParserFactory.newInstance().newSerializer();
     88         out.setOutput(output, Encoding.UTF_8.name());
     89         mAccessList.writeXml(out, UserHandle.USER_ALL);
     90         out.flush();
     91 
     92         assertEquals(TEST_XML, output.toString(Encoding.UTF_8.name()));
     93     }
     94 
     95     @Test
     96     public void testDeSerialization() throws XmlPullParserException, IOException {
     97         ByteArrayInputStream input = new ByteArrayInputStream(TEST_XML.getBytes());
     98         XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
     99         parser.setInput(input, Encoding.UTF_8.name());
    100 
    101         mAccessList.readXml(parser);
    102 
    103         assertTrue(mAccessList.hasFullAccess("pkg", 0));
    104         assertTrue(mAccessList.hasFullAccess("pkg1", 0));
    105         assertTrue(mAccessList.hasFullAccess("pkg", 1));
    106         assertTrue(mAccessList.hasFullAccess("pkg2", 3));
    107 
    108         assertFalse(mAccessList.hasFullAccess("pkg3", 0));
    109         assertFalse(mAccessList.hasFullAccess("pkg1", 1));
    110         assertFalse(mAccessList.hasFullAccess("pkg", 3));
    111         assertFalse(mAccessList.hasFullAccess("pkg", 2));
    112     }
    113 }