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 <cstdint>
17 #include <fstream>
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include <gtest/gtest.h>
23 
24 #include "ecmascript/napi/include/jsnapi.h"
25 #include "ecmascript/ohos/aot_runtime_info.h"
26 #include "ecmascript/ohos/tests/mock/mock_aot_runtime_info.h"
27 #include "ecmascript/platform/aot_crash_info.h"
28 #include "ecmascript/platform/file.h"
29 #include "ecmascript/tests/test_helper.h"
30 
31 using namespace panda;
32 using namespace panda::ecmascript;
33 using namespace panda::ecmascript::kungfu;
34 
35 namespace panda::test {
36 class CrashTest : public testing::Test {
37 public:
SetUpTestCase()38     static void SetUpTestCase()
39     {
40         GTEST_LOG_(INFO) << "SetUpTestCase";
41     }
42 
TearDownTestCase()43     static void TearDownTestCase()
44     {
45         GTEST_LOG_(INFO) << "TearDownCase";
46     }
47 
48     void SetUp() override {}
49     void TearDown() override {}
50 
51     MockAotRuntimeInfo mockAotRuntimeInfo;
52 protected:
53     EcmaVM *vm_ {nullptr};
54 };
55 
HWTEST_F_L0(CrashTest, CrashGetBuildId)56 HWTEST_F_L0(CrashTest, CrashGetBuildId)
57 {
58     char soBuildId[NAME_MAX];
59     ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
60     runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
61     ASSERT_TRUE(std::string(soBuildId).size() > 0);
62     ASSERT_EQ(std::string(soBuildId), "abcd1234567890");
63 }
64 
HWTEST_F_L0(CrashTest, GetMicrosecondsTimeStamp)65 HWTEST_F_L0(CrashTest, GetMicrosecondsTimeStamp)
66 {
67     char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
68     ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
69     runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
70     ASSERT_TRUE(std::string(timestamp).size() > 0);
71     ASSERT_EQ(std::string(timestamp), "1970-01-01 00:00:00");
72 }
73 
HWTEST_F_L0(CrashTest, BuildCrashRuntimeInfo)74 HWTEST_F_L0(CrashTest, BuildCrashRuntimeInfo)
75 {
76     char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
77     char soBuildId[NAME_MAX];
78     ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
79     runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
80     runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
81     char sanboxRealPath[PATH_MAX];
82     mkdir(MockAotRuntimeInfo::SANBOX_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
83     std::ofstream file(sanboxRealPath);
84     file.close();
85     runtimeInfo->GetCrashSandBoxRealPath(sanboxRealPath, NAME_MAX);
86 
87     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
88     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
89     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
90     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JS);
91     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JIT);
92     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::OTHERS);
93 
94     std::map<ecmascript::ohos::RuntimeInfoType, int> escapeMap = runtimeInfo->CollectCrashSum();
95     ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::AOT_CRASH], 3);
96     ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::JS], 1);
97     ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::JIT], 1);
98     ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::OTHERS], 1);
99     ASSERT_EQ(runtimeInfo->GetCompileCountByType(ecmascript::ohos::RuntimeInfoType::AOT_CRASH), 3);
100 
101     unlink(sanboxRealPath);
102     rmdir(MockAotRuntimeInfo::SANBOX_DIR);
103 }
104 
HWTEST_F_L0(CrashTest, BuildCompileRuntimeInfo)105 HWTEST_F_L0(CrashTest, BuildCompileRuntimeInfo)
106 {
107     char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
108     char soBuildId[NAME_MAX];
109     ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
110     runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
111     runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
112     char sanboxRealPath[PATH_MAX];
113     mkdir(MockAotRuntimeInfo::SANBOX_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
114     std::ofstream file(sanboxRealPath);
115     file.close();
116     runtimeInfo->GetCrashSandBoxRealPath(sanboxRealPath, NAME_MAX);
117 
118     runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath);
119     runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath);
120     runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath);
121     runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JS, sanboxRealPath);
122     runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JIT, sanboxRealPath);
123     runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::OTHERS, sanboxRealPath);
124     std::map<ecmascript::ohos::RuntimeInfoType, int> escapeMapRealPath = runtimeInfo->CollectCrashSum(sanboxRealPath);
125     ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::AOT_CRASH], 3);
126     ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::JS], 1);
127     ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::JIT], 1);
128     ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::OTHERS], 1);
129     ASSERT_EQ(runtimeInfo->GetCompileCountByType(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath), 3);
130 
131     unlink(sanboxRealPath);
132     rmdir(MockAotRuntimeInfo::SANBOX_DIR);
133 }
134 
HWTEST_F_L0(CrashTest, CrashGetRuntimeInfoByPath)135 HWTEST_F_L0(CrashTest, CrashGetRuntimeInfoByPath)
136 {
137     char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
138     char soBuildId[NAME_MAX];
139     ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo(true);
140 
141     runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
142     runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
143     char sanboxRealPath[PATH_MAX];
144     mkdir(MockAotRuntimeInfo::SANBOX_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
145     std::ofstream file(sanboxRealPath);
146     file.close();
147 
148     runtimeInfo->GetCrashSandBoxRealPath(sanboxRealPath, NAME_MAX);
149     runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
150     runtimeInfo->CollectCrashSum();
151 
152     unlink(sanboxRealPath);
153     rmdir(MockAotRuntimeInfo::SANBOX_DIR);
154 }
155 } // namespace panda::test