• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "utility_common_utils_test.h"
17 
18 #include <unistd.h>
19 
20 #include "calc_fingerprint.h"
21 #include "file_util.h"
22 #include "log_parse.h"
23 #include "tbox.h"
24 
25 using namespace std;
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 const char LOG_FILE_PATH[] = "/data/test/hiview_utility_test/";
31 constexpr int SUFFIX_0 = 0;
32 
GetLogDir(std::string & testCaseName)33 std::string GetLogDir(std::string& testCaseName)
34 {
35     std::string workPath = std::string(LOG_FILE_PATH);
36     if (workPath.back() != '/') {
37         workPath = workPath + "/";
38     }
39     workPath.append(testCaseName);
40     workPath.append("/");
41     std::string logDestDir = workPath;
42     if (!FileUtil::FileExists(logDestDir)) {
43         FileUtil::ForceCreateDirectory(logDestDir, FileUtil::FILE_PERM_770);
44     }
45     return logDestDir;
46 }
47 
GenerateLogFileName(std::string & testCaseName,int index)48 std::string GenerateLogFileName(std::string& testCaseName, int index)
49 {
50     return GetLogDir(testCaseName) + "testFile" + std::to_string(index);
51 }
52 }
53 
SetUpTestCase()54 void UtilityCommonUtilsTest::SetUpTestCase() {}
55 
TearDownTestCase()56 void UtilityCommonUtilsTest::TearDownTestCase() {}
57 
SetUp()58 void UtilityCommonUtilsTest::SetUp() {}
59 
TearDown()60 void UtilityCommonUtilsTest::TearDown()
61 {
62     (void)FileUtil::ForceRemoveDirectory(LOG_FILE_PATH);
63 }
64 
65 /**
66  * @tc.name: CalcFingerprintTest001
67  * @tc.desc: Test CalcFileSha interface method of class CalcFingerprint
68  * @tc.type: FUNC
69  * @tc.require: issueI65DUW
70  */
71 HWTEST_F(UtilityCommonUtilsTest, CalcFingerprintTest001, testing::ext::TestSize.Level3)
72 {
73     (void)FileUtil::ForceRemoveDirectory(LOG_FILE_PATH);
74     std::string caseName = "CalcFingerprintTest001";
75     CalcFingerprint calcFingerprint;
76     char hash[SHA256_DIGEST_LENGTH] = {0};
77     auto ret = calcFingerprint.CalcFileSha("", hash, SHA256_DIGEST_LENGTH);
78     ASSERT_EQ(EINVAL, ret);
79     ret = calcFingerprint.CalcFileSha(GenerateLogFileName(caseName, SUFFIX_0), hash, SHA256_DIGEST_LENGTH);
80     ASSERT_EQ(ENOENT, ret);
81     ret = calcFingerprint.CalcFileSha("//....../asdsa", hash, SHA256_DIGEST_LENGTH);
82     ASSERT_EQ(EINVAL, ret);
83     FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test");
84     ret = calcFingerprint.CalcFileSha(GenerateLogFileName(caseName, SUFFIX_0), nullptr, SHA256_DIGEST_LENGTH);
85     ASSERT_EQ(EINVAL, ret);
86     int invalidLen = 2;
87     ret = calcFingerprint.CalcFileSha(GenerateLogFileName(caseName, SUFFIX_0), hash, invalidLen);
88     ASSERT_EQ(ENOMEM, ret);
89 }
90 
91 /**
92  * @tc.name: CalcFingerprintTest002
93  * @tc.desc: Test CalcBufferSha interface method of class CalcFingerprint
94  * @tc.type: FUNC
95  * @tc.require: issueI65DUW
96  */
97 HWTEST_F(UtilityCommonUtilsTest, CalcFingerprintTest002, testing::ext::TestSize.Level3)
98 {
99     (void)FileUtil::ForceRemoveDirectory(LOG_FILE_PATH);
100     CalcFingerprint calcFingerprint;
101     char hash[SHA256_DIGEST_LENGTH] = {0};
102     std::string buffer1 = "";
103     auto ret = calcFingerprint.CalcBufferSha(buffer1, buffer1.size(), hash, SHA256_DIGEST_LENGTH);
104     ASSERT_EQ(EINVAL, ret);
105     std::string buffer2 = "123";
106     ret = calcFingerprint.CalcBufferSha(buffer2, buffer2.size(), nullptr, SHA256_DIGEST_LENGTH);
107     ASSERT_EQ(EINVAL, ret);
108 }
109 
110 /* @tc.name: LogParseTest001
111  * @tc.desc: Test IsIgnoreLibrary interface method of class LogParse
112  * @tc.type: FUNC
113  * @tc.require: issueI65DUW
114  */
115 HWTEST_F(UtilityCommonUtilsTest, LogParseTest001, testing::ext::TestSize.Level3)
116 {
117     LogParse logParse;
118     auto ret = logParse.IsIgnoreLibrary("watchdog");
119     ASSERT_TRUE(ret);
120     ret = logParse.IsIgnoreLibrary("ohos");
121     ASSERT_TRUE(!ret);
122 }
123 
124 /* @tc.name: LogParseTest002
125  * @tc.desc: Test MatchExceptionLibrary interface method of class LogParse
126  * @tc.type: FUNC
127  * @tc.require: issueI65DUW
128  */
129 HWTEST_F(UtilityCommonUtilsTest, LogParseTest002, testing::ext::TestSize.Level3)
130 {
131     LogParse logParse;
132     auto ret = logParse.MatchExceptionLibrary("IllegalAccessExceptionWrapper");
133     ASSERT_EQ("IllegalAccessException", ret);
134     ret = logParse.MatchExceptionLibrary("NoException");
135     ASSERT_EQ(LogParse::UNMATCHED_EXCEPTION, ret);
136 }
137 
138 /* @tc.name: TboxTest001
139  * @tc.desc: Test interfaces method of class Tbox
140  * @tc.type: FUNC
141  * @tc.require: issueI65DUW
142  */
143 HWTEST_F(UtilityCommonUtilsTest, TboxTest001, testing::ext::TestSize.Level3)
144 {
145     auto tboxSp = make_shared<Tbox>(); // trigger con/destrcutor
146     std::string val = "123";
147     size_t mask = 1;
148     int mode1 = FP_FILE;
149     auto ret = Tbox::CalcFingerPrint(val, mask, mode1);
150     ASSERT_TRUE(!ret.empty());
151     int mode2 = FP_BUFFER;
152     ret = Tbox::CalcFingerPrint(val, mask, mode2);
153     ASSERT_TRUE(!ret.empty());
154     int invalidMode = -1;
155     ret = Tbox::CalcFingerPrint(val, mask, invalidMode);
156     ASSERT_TRUE(ret == "0");
157     std::string stackName = "-_";
158     auto ret2 = Tbox::IsCallStack(stackName);
159     ASSERT_TRUE(ret2);
160     std::string stackName2 = "123";
161     auto ret3 = Tbox::IsCallStack(stackName2);
162     ASSERT_TRUE(ret3);
163     auto ret4 = Tbox::HasCausedBy("Caused by:Invalid description");
164     ASSERT_TRUE(ret4);
165     auto ret5 = Tbox::HasCausedBy("Suppressed:Invalid description");
166     ASSERT_TRUE(ret5);
167     auto ret6 = Tbox::HasCausedBy("Invalid description");
168     ASSERT_TRUE(!ret6);
169 }
170 
171 /* @tc.name: TboxTest002
172  * @tc.desc: Test GetStackName method of class Tbox
173  * @tc.type: FUNC
174  * @tc.require: issueI65DUW
175  */
176 HWTEST_F(UtilityCommonUtilsTest, TboxTest002, testing::ext::TestSize.Level3)
177 {
178     auto ret = Tbox::GetStackName("    at DES)");
179     ASSERT_EQ("DES", ret);
180     ret = Tbox::GetStackName("   - DES(");
181     ASSERT_EQ("unknown", ret);
182     ret = Tbox::GetStackName("   - DE+S(");
183     ASSERT_EQ("DE)", ret);
184 }
185 
186 /* @tc.name: TboxTest003
187  * @tc.desc: Test WaitForDoneFile method of class Tbox
188  * @tc.type: FUNC
189  * @tc.require: issueI65DUW
190  */
191 HWTEST_F(UtilityCommonUtilsTest, TboxTest003, testing::ext::TestSize.Level3)
192 {
193     (void)FileUtil::ForceRemoveDirectory(LOG_FILE_PATH);
194     std::string caseName = "TboxTest003";
195     auto ret = Tbox::WaitForDoneFile(GenerateLogFileName(caseName, SUFFIX_0), 1);
196     ASSERT_TRUE(!ret);
197     FileUtil::SaveStringToFile(GenerateLogFileName(caseName, SUFFIX_0), "test");
198     ret = Tbox::WaitForDoneFile(GenerateLogFileName(caseName, SUFFIX_0), 1);
199     ASSERT_TRUE(ret);
200 }
201 }
202 }