1 /*
2 * Copyright (c) 2021-2025 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 <filesystem>
17 #include <gtest/gtest.h>
18
19 #include "account_command.h"
20 #include "account_command_util.h"
21 #include "account_file_operator.h"
22 #include "account_log_wrapper.h"
23
24 using namespace testing::ext;
25 using namespace OHOS;
26 using namespace OHOS::AAFwk;
27 using namespace OHOS::AccountSA;
28 namespace {
29 #ifndef ACCOUNT_TEST
30 const std::string USER_INFO_BASE = "/data/service/el1/public/account";
31 #else
32 const std::string USER_INFO_BASE = "/data/service/el1/public/account/test";
33 #endif // ACCOUNT_TEST
34 } // namespace
35 class AccountCommandCreateModuleTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp() override;
40 void TearDown() override;
41
42 std::string cmd_ = "create";
43 };
44
SetUpTestCase()45 void AccountCommandCreateModuleTest::SetUpTestCase()
46 {
47 ASSERT_TRUE(MockTokenId("accountmgr"));
48 #ifdef ACCOUNT_TEST
49 AccountFileOperator osAccountFileOperator;
50 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
51 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
52 #endif // ACCOUNT_TEST
53 }
54
TearDownTestCase()55 void AccountCommandCreateModuleTest::TearDownTestCase()
56 {
57 #ifdef ACCOUNT_TEST
58 AccountFileOperator osAccountFileOperator;
59 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
60 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
61 #endif // ACCOUNT_TEST
62 }
63
SetUp(void)64 void AccountCommandCreateModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
65 {
66 testing::UnitTest *test = testing::UnitTest::GetInstance();
67 ASSERT_NE(test, nullptr);
68 const testing::TestInfo *testinfo = test->current_test_info();
69 ASSERT_NE(testinfo, nullptr);
70 string testCaseName = string(testinfo->name());
71 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
72 }
73
TearDown()74 void AccountCommandCreateModuleTest::TearDown()
75 {}
76
77 /**
78 * @tc.name: Acm_Command_Create_0100
79 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command.
80 * @tc.type: FUNC
81 * @tc.require: SR000GGVFO
82 */
83 HWTEST_F(AccountCommandCreateModuleTest, Acm_Command_Create_0100, TestSize.Level1)
84 {
85 std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Create_0100");
86 ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
87
88 commandResult = AccountCommandUtil::DeleteLastOsAccount();
89 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
90 }
91
92 /**
93 * @tc.name: Acm_Command_Create_0200
94 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command.
95 * @tc.type: FUNC
96 * @tc.require:
97 */
98 HWTEST_F(AccountCommandCreateModuleTest, Acm_Command_Create_0200, TestSize.Level1)
99 {
100 std::string commandResult = AccountCommandUtil::CreateOsAccount(
101 "Acm_Command_Create_0200", "maintenance");
102 ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
103
104 commandResult = AccountCommandUtil::DeleteLastOsAccount();
105 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
106 }
107