1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * 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 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "gtest/gtest.h" 17 18 #include <dirent.h> 19 #include <fstream> 20 #include <securec.h> 21 #include <sstream> 22 #include <sys/stat.h> 23 #include <sys/types.h> 24 25 #include "bundle_manager.h" 26 #include "dmsfwk_interface.h" 27 #include "dmslite_permission.h" 28 29 using namespace std; 30 using namespace testing::ext; 31 32 namespace OHOS { 33 namespace DistributedSchedule { 34 namespace { 35 #ifndef WEARABLE_PRODUCT 36 const int32_t NON_EXISTENT_UID = 12345; 37 const char NATIVE_APPID_DIR[] = "/system/native_appid/"; 38 const char FOUNDATION_APPID[] = "foundation_signature"; 39 const char PREFIX[] = "uid_"; 40 const char SUFFIX[] = "_appid"; 41 const char LAUNCHER_BUNDLE_NAME[] = "com.test.launcher"; 42 #endif 43 } 44 45 class PermissionTest : public testing::Test { 46 protected: SetUpTestCase()47 static void SetUpTestCase() { } TearDownTestCase()48 static void TearDownTestCase() { } SetUp()49 virtual void SetUp() { } TearDown()50 virtual void TearDown() { } 51 }; 52 53 #ifndef WEARABLE_PRODUCT 54 /** 55 * @tc.name: GetCallerBundleInfo_001 56 * @tc.desc: GetCallerBundleInfo failed with null callerInfo or bundleInfo pointer 57 * @tc.type: FUNC 58 * @tc.require: AR000FU5M6 59 */ 60 HWTEST_F(PermissionTest, GetCallerBundleInfo_001, TestSize.Level1) 61 { 62 CallerInfo callerInfo = {.uid = SHELL_UID}; 63 BundleInfo bundleInfo = {0}; 64 EXPECT_EQ(GetCallerBundleInfo(nullptr, &bundleInfo), DMS_EC_INVALID_PARAMETER); 65 EXPECT_EQ(GetCallerBundleInfo(&callerInfo, nullptr), DMS_EC_INVALID_PARAMETER); 66 } 67 68 /** 69 * @tc.name: GetCallerBundleInfo_002 70 * @tc.desc: GetCallerBundleInfo failed with SHELL_UID=0/2, which is not configured with appId 71 * @tc.type: FUNC 72 * @tc.require: AR000FU5M6 73 */ 74 HWTEST_F(PermissionTest, GetCallerBundleInfo_002, TestSize.Level1) 75 { 76 CallerInfo callerInfo = {.uid = SHELL_UID}; 77 BundleInfo bundleInfo = {0}; 78 EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &bundleInfo), DMS_EC_FAILURE); 79 } 80 81 /** 82 * @tc.name: GetCallerBundleInfo_003 83 * @tc.desc: GetCallerBundleInfo successfully with FOUNDATION_UID=7, which has been configured with appId 84 * @tc.type: FUNC 85 * @tc.require: AR000FU5M6 86 */ 87 HWTEST_F(PermissionTest, GetCallerBundleInfo_003, TestSize.Level1) 88 { 89 bool isDirExist = true; 90 DIR *dir = opendir(NATIVE_APPID_DIR); 91 if (dir == nullptr) { 92 mode_t mode = 0700; 93 EXPECT_EQ(mkdir(NATIVE_APPID_DIR, mode), 0); 94 isDirExist = false; 95 } else { 96 closedir(dir); 97 } 98 CallerInfo callerInfo = {.uid = FOUNDATION_UID}; 99 stringstream filePath; 100 filePath << NATIVE_APPID_DIR << PREFIX << callerInfo.uid << SUFFIX; 101 fstream fs(filePath.str(), ios::out); 102 EXPECT_TRUE(fs.is_open()); 103 fs << FOUNDATION_APPID; 104 fs.close(); 105 BundleInfo bundleInfo = {0}; 106 EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &bundleInfo), DMS_EC_SUCCESS); 107 EXPECT_EQ(strcmp(bundleInfo.appId, FOUNDATION_APPID), 0); 108 ClearBundleInfo(&bundleInfo); 109 remove(filePath.str().c_str()); 110 if (!isDirExist) { 111 rmdir(NATIVE_APPID_DIR); 112 } 113 } 114 115 /** 116 * @tc.name: GetCallerBundleInfo_004 117 * @tc.desc: GetCallerBundleInfo failed with a non-existent uid 118 * @tc.type: FUNC 119 * @tc.require: AR000FU5M6 120 */ 121 HWTEST_F(PermissionTest, GetCallerBundleInfo_004, TestSize.Level1) 122 { 123 CallerInfo callerInfo = {.uid = NON_EXISTENT_UID}; 124 BundleInfo bundleInfo = {0}; 125 EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &bundleInfo), DMS_EC_FAILURE); 126 } 127 128 /** 129 * @tc.name: GetCallerBundleInfo_005 130 * @tc.desc: GetCallerBundleInfo successfully with com.test.launcher uid 131 * @tc.type: FUNC 132 * @tc.require: AR000FU5M6 133 */ 134 HWTEST_F(PermissionTest, GetCallerBundleInfo_005, TestSize.Level1) 135 { 136 BundleInfo bundleInfo = {0}; 137 GetBundleInfo(LAUNCHER_BUNDLE_NAME, 0, &bundleInfo); 138 CallerInfo callerInfo = {.uid = bundleInfo.uid}; 139 BundleInfo callerBundleInfo = {0}; 140 EXPECT_EQ(GetCallerBundleInfo(&callerInfo, &callerBundleInfo), DMS_EC_FAILURE); 141 ClearBundleInfo(&callerBundleInfo); 142 } 143 #endif 144 } 145 } 146