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 <gtest/gtest.h>
17 #include <thread>
18 #include "account_command.h"
19 #include "account_command_util.h"
20 #include "account_log_wrapper.h"
21 #include "tool_system_test.h"
22
23 using namespace testing::ext;
24 using namespace OHOS;
25 using namespace OHOS::AAFwk;
26 using namespace OHOS::AccountSA;
27
28 namespace {
29 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID = "local_account_id_invalid";
30 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID_TWO = "1024";
31 } // namespace
32
33 class AccountCommandSwitchModuleTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp() override;
38 void TearDown() override;
39
40 std::string cmd_ = "switch";
41 };
42
SetUpTestCase()43 void AccountCommandSwitchModuleTest::SetUpTestCase()
44 {
45 ASSERT_NE(GetAllAccountPermission(), 0);
46 }
47
TearDownTestCase()48 void AccountCommandSwitchModuleTest::TearDownTestCase()
49 {}
50
SetUp(void)51 void AccountCommandSwitchModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
52 {
53 testing::UnitTest *test = testing::UnitTest::GetInstance();
54 ASSERT_NE(test, nullptr);
55 const testing::TestInfo *testinfo = test->current_test_info();
56 ASSERT_NE(testinfo, nullptr);
57 string testCaseName = string(testinfo->name());
58 ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
59 }
60
TearDown()61 void AccountCommandSwitchModuleTest::TearDown()
62 {}
63
64 /**
65 * @tc.name: Acm_Command_Switch_0100
66 * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
67 * @tc.type: FUNC
68 * @tc.require: SR000GGVFO
69 */
70 HWTEST_F(AccountCommandSwitchModuleTest, Acm_Command_Switch_0100, TestSize.Level1)
71 {
72 std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID;
73 GTEST_LOG_(INFO) << "command = " << command;
74
75 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
76 EXPECT_EQ(commandResult, HELP_MSG_INVALID_ID_ARGUMENT + "\n" + HELP_MSG_SWITCH);
77 }
78
79 /**
80 * @tc.name: Acm_Command_Switch_0200
81 * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
82 * @tc.type: FUNC
83 * @tc.require: SR000GGVFO
84 */
85 HWTEST_F(AccountCommandSwitchModuleTest, Acm_Command_Switch_0200, TestSize.Level1)
86 {
87 std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID_TWO;
88 GTEST_LOG_(INFO) << "command = " << command;
89
90 std::string commandResult = ToolSystemTest::ExecuteCommand(command);
91 EXPECT_EQ(commandResult, STRING_SWITCH_OS_ACCOUNT_NG + "\n");
92 }
93
94 /**
95 * @tc.name: Acm_Command_Switch_0300
96 * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
97 * @tc.type: FUNC
98 * @tc.require: SR000GGVFO
99 */
100 HWTEST_F(AccountCommandSwitchModuleTest, Acm_Command_Switch_0300, TestSize.Level1)
101 {
102 std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Switch_0300");
103 ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
104
105 commandResult = AccountCommandUtil::DeleteLastOsAccount();
106 ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
107 }
108