• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <string>
16 #include <vector>
17 
18 #include <fcntl.h>
19 #include <gtest/gtest.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 #include "event.h"
25 #include "faultlog_util.h"
26 #include "faultlogger.h"
27 #include "file_util.h"
28 #include "hiview_platform.h"
29 
30 using namespace testing::ext;
31 using namespace OHOS::HiviewDFX;
32 namespace OHOS {
33 namespace HiviewDFX {
34 class FaultloggerUnittest : public testing::Test {
35 public:
SetUp()36     void SetUp()
37     {
38         sleep(1);
39     };
TearDown()40     void TearDown(){};
41 
CreateFaultloggerInstance() const42     std::shared_ptr<Faultlogger> CreateFaultloggerInstance() const
43     {
44         static std::unique_ptr<HiviewPlatform> platform = std::make_unique<HiviewPlatform>();
45         auto plugin = std::make_shared<Faultlogger>();
46         plugin->SetName("Faultlogger");
47         plugin->SetHandle(nullptr);
48         plugin->SetHiviewContext(platform.get());
49         plugin->OnLoad();
50         return plugin;
51     }
52 };
53 
54 /**
55  * @tc.name: dumpFileListTest001
56  * @tc.desc: dump with cmds, check the result
57  * @tc.type: FUNC
58  * @tc.require: SR000F7UQ6 AR000F83AF
59  */
60 HWTEST_F(FaultloggerUnittest, dumpFileListTest001, testing::ext::TestSize.Level3)
61 {
62     /**
63      * @tc.steps: step1. add multiple cmds to faultlogger
64      * @tc.expected: check the content size of the dump function
65      */
66     auto plugin = CreateFaultloggerInstance();
67     auto fd = open("/data/test/testFile", O_CREAT | O_WRONLY | O_TRUNC, 770);
68     if (fd < 0) {
69         printf("Fail to create test result file.\n");
70         return;
71     }
72 
73     std::vector<std::string> cmds;
74     plugin->Dump(fd, cmds);
75     cmds.push_back("Faultlogger");
76     plugin->Dump(fd, cmds);
77     cmds.push_back("-l");
78     plugin->Dump(fd, cmds);
79     cmds.push_back("-f");
80     plugin->Dump(fd, cmds);
81     cmds.push_back("cppcrash-ModuleName-10-20201209103823");
82     plugin->Dump(fd, cmds);
83     cmds.push_back("-d");
84     plugin->Dump(fd, cmds);
85     cmds.push_back("-t");
86     plugin->Dump(fd, cmds);
87     cmds.push_back("20201209103823");
88     plugin->Dump(fd, cmds);
89     cmds.push_back("-m");
90     plugin->Dump(fd, cmds);
91     cmds.push_back("FAULTLOGGER");
92     plugin->Dump(fd, cmds);
93     close(fd);
94     fd = -1;
95 
96     std::string result;
97     if (FileUtil::LoadStringFromFile("/data/test/testFile", result)) {
98         ASSERT_GT(result.length(), 0ul);
99     } else {
100         FAIL();
101     }
102 }
103 
104 /**
105  * @tc.name: genCppCrashLogTest001
106  * @tc.desc: create cpp crash event and send it to faultlogger
107  * @tc.type: FUNC
108  * @tc.require: SR000F7UQ6 AR000F4380
109  */
110 HWTEST_F(FaultloggerUnittest, genCppCrashLogTest001, testing::ext::TestSize.Level3)
111 {
112     /**
113      * @tc.steps: step1. create a cpp crash event and pass it to faultlogger
114      * @tc.expected: the calling is success and the file has been created
115      */
116     auto plugin = CreateFaultloggerInstance();
117     FaultLogInfo info;
118     info.time = 1607161163;
119     info.id = 0;
120     info.pid = 7497;
121     info.faultLogType = 2;
122     info.module = "com.example.myapplication";
123     info.sectionMap["APPVERSION"] = "1.0";
124     info.sectionMap["FAULT_MESSAGE"] = "Nullpointer";
125     info.sectionMap["TRACEID"] = "0x1646145645646";
126     info.sectionMap["KEY_THREAD_INFO"] = "Test Thread Info";
127     info.sectionMap["REASON"] = "TestReason";
128     info.sectionMap["STACKTRACE"] = "#01 xxxxxx\n#02 xxxxxx\n";
129     plugin->AddFaultLog(info);
130     std::string timeStr = GetFormatedTime(info.time);
131     std::string fileName = "/data/log/faultlog/faultlogger/cppcrash-com.example.myapplication-0-" + timeStr;
132     bool exist = FileUtil::FileExists(fileName);
133     ASSERT_EQ(exist, true);
134     auto size = FileUtil::GetFileSize(fileName);
135     ASSERT_GT(size, 0ul);
136     auto parsedInfo = plugin->GetFaultLogInfo(fileName);
137     ASSERT_EQ(parsedInfo->module, "com.example.myapplication");
138 }
139 } // namespace HiviewDFX
140 } // namespace OHOS
141