1 /*
2  * Copyright (C) 2020 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 // #define LOG_NDEBUG 0
18 #define LOG_TAG "PermissionManagerNativeTest"
19 
20 #include <android/permission_manager.h>
21 #include <android-base/logging.h>
22 #include <gtest/gtest.h>
23 
24 //-----------------------------------------------------------------
25 class PermissionManagerNativeTest : public ::testing::Test {
26 
27 protected:
PermissionManagerNativeTest()28     PermissionManagerNativeTest() { }
29 
~PermissionManagerNativeTest()30     virtual ~PermissionManagerNativeTest() { }
31 
32     /* Test setup*/
SetUp()33     virtual void SetUp() { }
34 
35     /* Test tear down */
TearDown()36     virtual void TearDown() { }
37 };
38 
39 //-------------------------------------------------------------------------------------------------
TEST_F(PermissionManagerNativeTest,testCheckPermission)40 TEST_F(PermissionManagerNativeTest, testCheckPermission) {
41     pid_t selfPid = ::getpid();
42     uid_t selfUid = ::getuid();
43 
44     LOG(INFO) << "testCheckPermission: uid " << selfUid << ", pid" << selfPid;
45 
46     // Test is set up to force unroot by RootTargetPreparer, so we should be running as SHELL.
47     // Check some permissions SHELL should definitely have or not have.
48     int32_t result;
49     EXPECT_EQ(APermissionManager_checkPermission("android.permission.DUMP",
50                                                  selfPid, selfUid, &result),
51               PERMISSION_MANAGER_STATUS_OK);
52     EXPECT_EQ(result, PERMISSION_MANAGER_PERMISSION_GRANTED);
53 
54     EXPECT_EQ(APermissionManager_checkPermission("android.permission.MANAGE_USERS",
55                                                  selfPid, selfUid, &result),
56               PERMISSION_MANAGER_STATUS_OK);
57     EXPECT_EQ(result, PERMISSION_MANAGER_PERMISSION_DENIED);
58 
59     EXPECT_EQ(APermissionManager_checkPermission("android.permission.NETWORK_STACK",
60                                                  selfPid, selfUid, &result),
61               PERMISSION_MANAGER_STATUS_OK);
62     EXPECT_EQ(result, PERMISSION_MANAGER_PERMISSION_DENIED);
63 }
64