1 /* 2 * Copyright (C) 2021 The Android Open Source Project 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 17 package android.car.cts.builtin.util; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import android.car.builtin.util.ValidationHelper; 23 import android.os.Process; 24 import android.os.UserHandle; 25 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 @RunWith(AndroidJUnit4.class) 32 public final class ValidationHelperTest { 33 // constants from android.os.UserHandle 34 private static final int USER_NULL = -10000; 35 private static final int USER_CURRENT_OR_SELF = -3; 36 private static final int USER_CURRENT = -2; 37 private static final int USER_ALL = -1; 38 private static final int USER_SYSTEM = 0; 39 private static final int PER_USER_RANGE = 100000; 40 41 @Test testUserIdValidation()42 public void testUserIdValidation() { 43 // setup 44 int maxUserId = Integer.MAX_VALUE / PER_USER_RANGE; 45 int invalidUserId = maxUserId + 1; 46 47 // assert pre-defined user ids 48 assertTrue(ValidationHelper.isUserIdValid(USER_NULL)); 49 assertTrue(ValidationHelper.isUserIdValid(USER_CURRENT_OR_SELF)); 50 assertTrue(ValidationHelper.isUserIdValid(USER_CURRENT)); 51 assertTrue(ValidationHelper.isUserIdValid(USER_ALL)); 52 assertTrue(ValidationHelper.isUserIdValid(USER_SYSTEM)); 53 assertTrue(ValidationHelper.isUserIdValid(maxUserId)); 54 55 // assert dynamical user ids 56 assertTrue(ValidationHelper.isUserIdValid(UserHandle.myUserId())); 57 58 // assert boundary conditions 59 assertFalse(ValidationHelper.isUserIdValid(invalidUserId)); 60 } 61 62 @Test testAppIdValidation()63 public void testAppIdValidation() { 64 // setup 65 int outOfRangeAppId = PER_USER_RANGE + 1; 66 67 // assert pre-defined app ids 68 assertTrue(ValidationHelper.isAppIdValid(Process.FIRST_APPLICATION_UID)); 69 assertTrue(ValidationHelper.isAppIdValid(Process.LAST_APPLICATION_UID)); 70 assertTrue(ValidationHelper.isAppIdValid(Process.ROOT_UID)); 71 assertTrue(ValidationHelper.isAppIdValid(Process.SYSTEM_UID)); 72 assertTrue(ValidationHelper.isAppIdValid(Process.SHELL_UID)); 73 74 // assert dynamical app ids 75 assertTrue(ValidationHelper.isAppIdValid(Process.myUid() % PER_USER_RANGE)); 76 assertTrue(ValidationHelper.isAppIdValid(Process.FIRST_APPLICATION_UID + 1)); 77 assertTrue(ValidationHelper.isAppIdValid(Process.LAST_APPLICATION_UID - 1)); 78 79 // assert boundary conditions 80 assertFalse(ValidationHelper.isAppIdValid(Process.INVALID_UID)); 81 assertFalse(ValidationHelper.isAppIdValid(outOfRangeAppId)); 82 } 83 } 84