• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::AccountSA;
26 
27 namespace {
28 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID = "local_account_id_invalid";
29 const std::string STRING_LOCAL_ACCOUNT_ID_INVALID_TWO = "1024";
30 }  // namespace
31 
32 class AccountCommandSwitchModuleTest : public testing::Test {
33 public:
34     static void SetUpTestCase();
35     static void TearDownTestCase();
36     void SetUp() override;
37     void TearDown() override;
38 
39     std::string cmd_ = "switch";
40 };
41 
SetUpTestCase()42 void AccountCommandSwitchModuleTest::SetUpTestCase()
43 {
44     ASSERT_NE(GetAllAccountPermission(), 0);
45 }
46 
TearDownTestCase()47 void AccountCommandSwitchModuleTest::TearDownTestCase()
48 {}
49 
SetUp(void)50 void AccountCommandSwitchModuleTest::SetUp(void) __attribute__((no_sanitize("cfi")))
51 {
52     testing::UnitTest *test = testing::UnitTest::GetInstance();
53     ASSERT_NE(test, nullptr);
54     const testing::TestInfo *testinfo = test->current_test_info();
55     ASSERT_NE(testinfo, nullptr);
56     string testCaseName = string(testinfo->name());
57     ACCOUNT_LOGI("[SetUp] %{public}s start", testCaseName.c_str());
58 }
59 
TearDown()60 void AccountCommandSwitchModuleTest::TearDown()
61 {}
62 
ExecuteCommand(const std::string & command)63 static std::string ExecuteCommand(const std::string& command)
64 {
65     std::string result = "";
66     FILE* file = popen(command.c_str(), "r");
67 
68     if (file != nullptr) {
69         char commandResult[1024] = { 0 };
70         while ((fgets(commandResult, sizeof(commandResult), file)) != nullptr) {
71             result.append(commandResult);
72         }
73         pclose(file);
74         file = nullptr;
75     }
76 
77     return result;
78 }
79 
80 /**
81  * @tc.name: Acm_Command_Switch_0100
82  * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
83  * @tc.type: FUNC
84  * @tc.require: SR000GGVFO
85  */
86 HWTEST_F(AccountCommandSwitchModuleTest, Acm_Command_Switch_0100, TestSize.Level1)
87 {
88     std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID;
89     GTEST_LOG_(INFO) << "command = " << command;
90 
91     std::string commandResult = ExecuteCommand(command);
92     EXPECT_EQ(commandResult, HELP_MSG_INVALID_ID_ARGUMENT + "\n" + HELP_MSG_SWITCH);
93 }
94 
95 /**
96  * @tc.name: Acm_Command_Switch_0200
97  * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
98  * @tc.type: FUNC
99  * @tc.require: SR000GGVFO
100  */
101 HWTEST_F(AccountCommandSwitchModuleTest, Acm_Command_Switch_0200, TestSize.Level1)
102 {
103     std::string command = TOOL_NAME + " " + cmd_ + " -i " + STRING_LOCAL_ACCOUNT_ID_INVALID_TWO;
104     GTEST_LOG_(INFO) << "command = " << command;
105 
106     std::string commandResult = ExecuteCommand(command);
107     EXPECT_EQ(commandResult, STRING_SWITCH_OS_ACCOUNT_NG + "\n");
108 }
109 
110 /**
111  * @tc.name: Acm_Command_Switch_0300
112  * @tc.desc: Verify the "acm delete -i <local-account-id>" command.
113  * @tc.type: FUNC
114  * @tc.require: SR000GGVFO
115  */
116 HWTEST_F(AccountCommandSwitchModuleTest, Acm_Command_Switch_0300, TestSize.Level1)
117 {
118     std::string commandResult = AccountCommandUtil::CreateOsAccount("Acm_Command_Switch_0300");
119     ASSERT_NE(commandResult.find(STRING_CREATE_OS_ACCOUNT_OK), std::string::npos);
120 
121     commandResult = AccountCommandUtil::DeleteLastOsAccount();
122     ASSERT_NE(commandResult.find(STRING_DELETE_OS_ACCOUNT_OK), std::string::npos);
123 }
124