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 
16 #include "adapter_loglibrary_ability_test.h"
17 
18 #include <fcntl.h>
19 #include <string>
20 #include <vector>
21 
22 #include "adapter_loglibrary_test_tools.h"
23 #include "file_util.h"
24 #include "hiview_service_ability.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 namespace {
29 const std::string DEST_PATH = "/data/log/logpack/betaclub/";
30 const std::string SOURCE_PATH = "/data/log/logpack/remotelog/";
31 const std::string NON_LOG_TYPE = "NONTYPE";
32 const std::string LOG_TYPE = "REMOTELOG";
33 }
SetUpTestCase()34 void AdapterLoglibraryAbilityTest::SetUpTestCase() {}
35 
TearDownTestCase()36 void AdapterLoglibraryAbilityTest::TearDownTestCase() {}
37 
SetUp()38 void AdapterLoglibraryAbilityTest::SetUp()
39 {
40     platform.GetPluginMap();
41     AdapterLoglibraryTestTools::ApplyPermissionAccess();
42     AdapterLoglibraryTestTools::CreateDir(SOURCE_PATH);
43     AdapterLoglibraryTestTools::CreateDir(DEST_PATH);
44     if (service == nullptr) {
45         service = new HiviewService();
46         HiviewServiceAbility::GetOrSetHiviewService(service);
47     }
48 }
49 
TearDown()50 void AdapterLoglibraryAbilityTest::TearDown()
51 {
52     AdapterLoglibraryTestTools::RemoveDir(DEST_PATH);
53     AdapterLoglibraryTestTools::RemoveDir(SOURCE_PATH);
54     if (service != nullptr) {
55         delete service;
56         service = nullptr;
57     }
58 }
59 
60 /**
61  * @tc.name: LoglibraryAbilityListTest001
62  * @tc.desc: Check Func List
63  * @tc.type: FUNC
64  */
65 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityListTest001, testing::ext::TestSize.Level1)
66 {
67     std::string fileName = "AbilityList.txt";
68     FileUtil::SaveStringToFile(SOURCE_PATH + fileName, "AbilityListtestcontent", true);
69     std::vector<HiviewFileInfo> fileInfos;
70     HiviewServiceAbility ability;
71     int32_t result = ability.List(LOG_TYPE, fileInfos);
72     ASSERT_EQ(result, 0);
73 }
74 
75 /**
76  * @tc.name: LoglibraryAbilityListTest002
77  * @tc.desc: Check Func List
78  * @tc.type: FUNC
79  */
80 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityListTest002, testing::ext::TestSize.Level1)
81 {
82     std::vector<HiviewFileInfo> fileInfos;
83     HiviewServiceAbility ability;
84     int32_t result = ability.List(NON_LOG_TYPE, fileInfos);
85     ASSERT_EQ(result, HiviewNapiErrCode::ERR_INNER_INVALID_LOGTYPE);
86 }
87 
88 /**
89  * @tc.name: LoglibraryAbilityCopyTest001
90  * @tc.desc: Check Func Copy
91  * @tc.type: FUNC
92  */
93 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityCopyTest001, testing::ext::TestSize.Level1)
94 {
95     std::string fileName = "AbilityCopy.txt";
96     FileUtil::SaveStringToFile(SOURCE_PATH + fileName, "AbilityCopytestcontent", true);
97     HiviewServiceAbility ability;
98     int32_t result = ability.Copy(LOG_TYPE, fileName, DEST_PATH);
99     ASSERT_EQ(result, -1);
100 }
101 
102 /**
103  * @tc.name: LoglibraryAbilityCopyTest002
104  * @tc.desc: Check Func Copy
105  * @tc.type: FUNC
106  */
107 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityCopyTest002, testing::ext::TestSize.Level1)
108 {
109     std::string fileName = "AbilityCopy.txt";
110     HiviewServiceAbility ability;
111     int32_t result = ability.Copy(NON_LOG_TYPE, fileName, DEST_PATH);
112     ASSERT_EQ(result, HiviewNapiErrCode::ERR_INNER_INVALID_LOGTYPE);
113 }
114 
115 /**
116  * @tc.name: LoglibraryAbilityMoveTest001
117  * @tc.desc: Check Func Move
118  * @tc.type: FUNC
119  */
120 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityMoveTest001, testing::ext::TestSize.Level1)
121 {
122     std::string fileName = "AbilityMove.txt";
123     FileUtil::SaveStringToFile(SOURCE_PATH + fileName, "AbilityMovetestcontent", true);
124     HiviewServiceAbility ability;
125     int32_t result = ability.Move(LOG_TYPE, fileName, DEST_PATH);
126     ASSERT_EQ(result, -1);
127 }
128 
129 /**
130  * @tc.name: LoglibraryAbilityMoveTest002
131  * @tc.desc: Check Func Move
132  * @tc.type: FUNC
133  */
134 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityMoveTest002, testing::ext::TestSize.Level1)
135 {
136     std::string fileName = "AbilityMove.txt";
137     HiviewServiceAbility ability;
138     int32_t result = ability.Move(NON_LOG_TYPE, fileName, DEST_PATH);
139     ASSERT_EQ(result, HiviewNapiErrCode::ERR_INNER_INVALID_LOGTYPE);
140 }
141 
142 /**
143  * @tc.name: LoglibraryAbilityRemoveTest001
144  * @tc.desc: Check Func Remove
145  * @tc.type: FUNC
146  */
147 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityRemoveTest001, testing::ext::TestSize.Level1)
148 {
149     std::string fileName = "AbilityRemove.txt";
150     FileUtil::SaveStringToFile(SOURCE_PATH + fileName, "AbilityRemovetestcontent", true);
151     HiviewServiceAbility ability;
152     int32_t result = ability.Remove(LOG_TYPE, fileName);
153     ASSERT_EQ(result, 0);
154 }
155 
156 /**
157  * @tc.name: LoglibraryAbilityRemoveTest002
158  * @tc.desc: Check Func Remove
159  * @tc.type: FUNC
160  */
161 HWTEST_F(AdapterLoglibraryAbilityTest, LoglibraryAbilityRemoveTest002, testing::ext::TestSize.Level1)
162 {
163     std::string fileName = "AbilityRemove.txt";
164     HiviewServiceAbility ability;
165     int32_t result = ability.Remove(NON_LOG_TYPE, fileName);
166     ASSERT_EQ(result, HiviewNapiErrCode::ERR_INNER_INVALID_LOGTYPE);
167 }
168 
169 /**
170  * @tc.name: DumpTest001
171  * @tc.desc: Check Func Remove
172  * @tc.type: FUNC
173  */
174 HWTEST_F(AdapterLoglibraryAbilityTest, DumpTest001, testing::ext::TestSize.Level1)
175 {
176     int fd = open("/dev/null", O_RDWR | O_CREAT | O_TRUNC, 0600); // 0600 for file mode
177     ASSERT_TRUE(fd > 0);
178     HiviewServiceAbility ability;
179     ASSERT_EQ(ability.Dump(fd, {}), 0);
180     ASSERT_EQ(ability.Dump(fd, {u"-d"}), 0);
181     ASSERT_EQ(ability.Dump(fd, {u"-p"}), 0);
182     (void)close(fd);
183 }
184 }
185 }