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