1 /* 2 * Copyright (c) 2024 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 "edm_os_account_manager_impl.h" 19 20 using namespace testing::ext; 21 22 namespace OHOS { 23 namespace EDM { 24 namespace TEST { 25 const int32_t TEST_ACCOUNT_ID = 100; 26 const int32_t ILLEGAL_ACCOUNT_ID = -100; 27 const int32_t ILLEGAL_ACCOUNT_ID2 = 2147483647; 28 #ifdef OS_ACCOUNT_EDM_ENABLE 29 const int32_t ACCOUNT_NAME_LENGTH_MAX = 1024; 30 #endif 31 class EdmOsAccountManagerImplTest : public testing::Test {}; 32 /** 33 * @tc.name: TestQueryActiveOsAccountIds01 34 * @tc.desc: Test QueryActiveOsAccountIds function. 35 * @tc.type: FUNC 36 */ 37 HWTEST_F(EdmOsAccountManagerImplTest, TestQueryActiveOsAccountIds01, TestSize.Level1) 38 { 39 EdmOsAccountManagerImpl edmOsAccountManagerImpl; 40 std::vector<int32_t> ids; 41 int ret = edmOsAccountManagerImpl.QueryActiveOsAccountIds(ids); 42 #ifdef OS_ACCOUNT_EDM_ENABLE 43 ASSERT_TRUE(ret == ERR_OK); 44 #else 45 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY); 46 #endif 47 } 48 49 /** 50 * @tc.name: TestQueryActiveOsAccountIds02 51 * @tc.desc: Test QueryActiveOsAccountIds function. 52 * @tc.type: FUNC 53 */ 54 HWTEST_F(EdmOsAccountManagerImplTest, TestQueryActiveOsAccountIds02, TestSize.Level1) 55 { 56 EdmOsAccountManagerImpl edmOsAccountManagerImpl; 57 std::vector<int32_t> ids; 58 ids.push_back(ILLEGAL_ACCOUNT_ID); 59 int ret = edmOsAccountManagerImpl.QueryActiveOsAccountIds(ids); 60 #ifdef OS_ACCOUNT_EDM_ENABLE 61 ASSERT_TRUE(ret == ERR_OK); 62 #else 63 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY); 64 #endif 65 66 ids.clear(); 67 ids.push_back(ILLEGAL_ACCOUNT_ID2); 68 ret = edmOsAccountManagerImpl.QueryActiveOsAccountIds(ids); 69 #ifdef OS_ACCOUNT_EDM_ENABLE 70 ASSERT_TRUE(ret == ERR_OK); 71 #else 72 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY); 73 #endif 74 75 ids.clear(); 76 ids.push_back(ILLEGAL_ACCOUNT_ID); 77 ids.push_back(ILLEGAL_ACCOUNT_ID2); 78 ids.push_back(TEST_ACCOUNT_ID); 79 ret = edmOsAccountManagerImpl.QueryActiveOsAccountIds(ids); 80 #ifdef OS_ACCOUNT_EDM_ENABLE 81 ASSERT_TRUE(ret == ERR_OK); 82 #else 83 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY); 84 #endif 85 } 86 87 /** 88 * @tc.name: TestQueryActiveOsAccountIds03 89 * @tc.desc: Test QueryActiveOsAccountIds function. 90 * @tc.type: FUNC 91 */ 92 HWTEST_F(EdmOsAccountManagerImplTest, TestQueryActiveOsAccountIds03, TestSize.Level1) 93 { 94 EdmOsAccountManagerImpl edmOsAccountManagerImpl; 95 std::vector<int32_t> ids; 96 ids.push_back(TEST_ACCOUNT_ID); 97 int ret = edmOsAccountManagerImpl.QueryActiveOsAccountIds(ids); 98 #ifdef OS_ACCOUNT_EDM_ENABLE 99 ASSERT_TRUE(ret == ERR_OK); 100 #else 101 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY); 102 #endif 103 } 104 105 /** 106 * @tc.name: TestIsOsAccountExists01 107 * @tc.desc: Test IsOsAccountExists function. 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(EdmOsAccountManagerImplTest, TestIsOsAccountExists01, TestSize.Level1) 111 { 112 EdmOsAccountManagerImpl edmOsAccountManagerImpl; 113 bool isExist; 114 edmOsAccountManagerImpl.IsOsAccountExists(ILLEGAL_ACCOUNT_ID, isExist); 115 ASSERT_FALSE(isExist); 116 117 edmOsAccountManagerImpl.IsOsAccountExists(ILLEGAL_ACCOUNT_ID2, isExist); 118 ASSERT_FALSE(isExist); 119 } 120 121 /** 122 * @tc.name: TestIsOsAccountExists02 123 * @tc.desc: Test IsOsAccountExists function. 124 * @tc.type: FUNC 125 */ 126 HWTEST_F(EdmOsAccountManagerImplTest, TestIsOsAccountExists02, TestSize.Level1) 127 { 128 EdmOsAccountManagerImpl edmOsAccountManagerImpl; 129 bool isExist; 130 #ifdef OS_ACCOUNT_EDM_ENABLE 131 edmOsAccountManagerImpl.IsOsAccountExists(TEST_ACCOUNT_ID, isExist); 132 ASSERT_TRUE(isExist); 133 #else 134 int ret = edmOsAccountManagerImpl.IsOsAccountExists(TEST_ACCOUNT_ID, isExist); 135 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY); 136 #endif 137 } 138 139 #ifdef OS_ACCOUNT_EDM_ENABLE 140 /** 141 * @tc.name: TestCreateOsAccount 142 * @tc.desc: Test CreateOsAccount function. 143 * @tc.type: FUNC 144 */ 145 HWTEST_F(EdmOsAccountManagerImplTest, TestCreateOsAccount, TestSize.Level1) 146 { 147 EdmOsAccountManagerImpl edmOsAccountManagerImpl; 148 OHOS::AccountSA::OsAccountType type = OHOS::AccountSA::OsAccountType::ADMIN; 149 std::string name = "testName"; 150 for (int i = 0; i < ACCOUNT_NAME_LENGTH_MAX; i++) { 151 name.append("z"); 152 } 153 OHOS::AccountSA::OsAccountInfo accountInfo; 154 int ret = edmOsAccountManagerImpl.CreateOsAccount(name, type, accountInfo); 155 ASSERT_TRUE(ret != ERR_OK); 156 } 157 #endif 158 } // namespace TEST 159 } // namespace EDM 160 } // namespace OHOS