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 #define LOG_TAG "DumpHelperTest "
16 
17 #include "gtest/gtest.h"
18 #include "log_print.h"
19 #include "dump_helper.h"
20 #include "dump/dump_manager.h"
21 #include "types.h"
22 
23 using namespace OHOS;
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS::DistributedData;
27 namespace OHOS::Test {
28 namespace DistributedDataTest {
29 class DumpHelperTest : public testing::Test {
30 public:
SetUpTestCase(void)31     static void SetUpTestCase(void){};
TearDownTestCase(void)32     static void TearDownTestCase(void){};
33     void SetUp();
TearDown()34     void TearDown(){};
35 };
36 
SetUp(void)37 void DumpHelperTest::SetUp(void)
38 {
39     DumpManager &dumpManager = DumpManager::GetInstance();
40     DumpManager::Config config;
41     config.dumpName = "test_dump";
42     config.fullCmd = "test_full_cmd";
43     config.abbrCmd = "test_abbr_cmd";
44     config.countPrintf = 1;
45     config.infoName = "test_info";
46     config.minParamsNum = 2; //minParamsNum is 2
47     config.maxParamsNum = 5; //maxParamsNum is 5
48     config.parentNode = "test_parent";
49     config.childNode = "test_child";
50     config.dumpCaption = {"test_caption1", "test_caption2"};
51 
52     dumpManager.AddConfig("111", config);
53     dumpManager.GetHandler(config.infoName);
54     DumpManager::Config result = dumpManager.GetConfig("111");
55     ASSERT_EQ(result.dumpName, config.dumpName);
56 }
57 
58 /**
59 * @tc.name: GetInstanceTest
60 * @tc.desc: GetInstance test.
61 * @tc.type: FUNC
62 * @tc.require:
63 * @tc.author: SQL
64 */
65 HWTEST_F(DumpHelperTest, GetInstanceTest, TestSize.Level0)
66 {
67     DumpHelper &dumpHelper = DumpHelper::GetInstance();
68     EXPECT_NE(&dumpHelper, nullptr);
69 
70     int fd = 1;
71     std::vector<std::string> args;
72     EXPECT_TRUE(dumpHelper.Dump(fd, args));
73 
74     args = {"COMMAND_A", "ARG_1", "ARG_2"};
75     EXPECT_TRUE(dumpHelper.Dump(fd, args));
76 }
77 
78 /**
79 * @tc.name: AddErrorInfoTest
80 * @tc.desc: AddErrorInfo test.
81 * @tc.type: FUNC
82 * @tc.require:
83 * @tc.author: SQL
84 */
85 HWTEST_F(DumpHelperTest, AddErrorInfoTest, TestSize.Level0)
86 {
87     DumpHelper &dumpHelper = DumpHelper::GetInstance();
88     EXPECT_NE(&dumpHelper, nullptr);
89     int32_t errorCode = 1001;
90     std::string errorInfo = "Test error information";
91 
92     dumpHelper.AddErrorInfo(errorCode, errorInfo);
93 
94     const OHOS::DistributedData::DumpHelper::ErrorInfo& lastError = dumpHelper.errorInfo_.back();
95     EXPECT_EQ(lastError.errorCode, errorCode);
96     EXPECT_EQ(lastError.errorInfo, errorInfo);
97 }
98 } // namespace DistributedDataTest
99 } // namespace OHOS::Test