1 /*
2 * Copyright (c) 2021-2022 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 using namespace OHOS::AccountSA::Constants;
29
30 class AccountCommandCreateModuleTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36
37 std::string cmd_ = "create";
38 };
39
SetUpTestCase()40 void AccountCommandCreateModuleTest::SetUpTestCase()
41 {
42 ASSERT_TRUE(MockTokenId("accountmgr"));
43 #ifdef ACCOUNT_TEST
44 AccountFileOperator osAccountFileOperator;
45 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
46 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
47 #endif // ACCOUNT_TEST
48 }
49
TearDownTestCase()50 void AccountCommandCreateModuleTest::TearDownTestCase()
51 {
52 #ifdef ACCOUNT_TEST
53 AccountFileOperator osAccountFileOperator;
54 osAccountFileOperator.DeleteDirOrFile(USER_INFO_BASE);
55 GTEST_LOG_(INFO) << "delete account test path " << USER_INFO_BASE;
56 #endif // ACCOUNT_TEST
57 }
58
SetUp(void)59 void AccountCommandCreateModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
60 {
61 testing::UnitTest *test = testing::UnitTest::GetInstance();
62 ASSERT_NE(test, nullptr);
63 const testing::TestInfo *testinfo = test->current_test_info();
64 ASSERT_NE(testinfo, nullptr);
65 string testCaseName = string(testinfo->name());
66 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
67 }
68
TearDown()69 void AccountCommandCreateModuleTest::TearDown()
70 {}
71
72 /**
73 * @tc.name: Acm_Command_Create_0100
74 * @tc.desc: Verify the "acm create -n <local-account-name> -t <type>" command.
75 * @tc.type: FUNC
76 * @tc.require: SR000GGVFO
77 */
78 HWTEST_F(AccountCommandCreateModuleTest, Acm_Command_Create_0100, TestSize.Level1)
79 {
80 std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Create_0100");
81 ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
82
83 commandResult = AccountCommandUtil::DeleteLastOsAccount();
84 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
85 }
86