14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include <cstdint>
174514f5e3Sopenharmony_ci#include <fstream>
184514f5e3Sopenharmony_ci#include <memory>
194514f5e3Sopenharmony_ci#include <string>
204514f5e3Sopenharmony_ci#include <utility>
214514f5e3Sopenharmony_ci
224514f5e3Sopenharmony_ci#include <gtest/gtest.h>
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_ci#include "ecmascript/napi/include/jsnapi.h"
254514f5e3Sopenharmony_ci#include "ecmascript/ohos/aot_runtime_info.h"
264514f5e3Sopenharmony_ci#include "ecmascript/ohos/tests/mock/mock_aot_runtime_info.h"
274514f5e3Sopenharmony_ci#include "ecmascript/platform/aot_crash_info.h"
284514f5e3Sopenharmony_ci#include "ecmascript/platform/file.h"
294514f5e3Sopenharmony_ci#include "ecmascript/tests/test_helper.h"
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ciusing namespace panda;
324514f5e3Sopenharmony_ciusing namespace panda::ecmascript;
334514f5e3Sopenharmony_ciusing namespace panda::ecmascript::kungfu;
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_cinamespace panda::test {
364514f5e3Sopenharmony_ciclass CrashTest : public testing::Test {
374514f5e3Sopenharmony_cipublic:
384514f5e3Sopenharmony_ci    static void SetUpTestCase()
394514f5e3Sopenharmony_ci    {
404514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "SetUpTestCase";
414514f5e3Sopenharmony_ci    }
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci    static void TearDownTestCase()
444514f5e3Sopenharmony_ci    {
454514f5e3Sopenharmony_ci        GTEST_LOG_(INFO) << "TearDownCase";
464514f5e3Sopenharmony_ci    }
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci    void SetUp() override {}
494514f5e3Sopenharmony_ci    void TearDown() override {}
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    MockAotRuntimeInfo mockAotRuntimeInfo;
524514f5e3Sopenharmony_ciprotected:
534514f5e3Sopenharmony_ci    EcmaVM *vm_ {nullptr};
544514f5e3Sopenharmony_ci};
554514f5e3Sopenharmony_ci
564514f5e3Sopenharmony_ciHWTEST_F_L0(CrashTest, CrashGetBuildId)
574514f5e3Sopenharmony_ci{
584514f5e3Sopenharmony_ci    char soBuildId[NAME_MAX];
594514f5e3Sopenharmony_ci    ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
604514f5e3Sopenharmony_ci    runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
614514f5e3Sopenharmony_ci    ASSERT_TRUE(std::string(soBuildId).size() > 0);
624514f5e3Sopenharmony_ci    ASSERT_EQ(std::string(soBuildId), "abcd1234567890");
634514f5e3Sopenharmony_ci}
644514f5e3Sopenharmony_ci
654514f5e3Sopenharmony_ciHWTEST_F_L0(CrashTest, GetMicrosecondsTimeStamp)
664514f5e3Sopenharmony_ci{
674514f5e3Sopenharmony_ci    char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
684514f5e3Sopenharmony_ci    ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
694514f5e3Sopenharmony_ci    runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
704514f5e3Sopenharmony_ci    ASSERT_TRUE(std::string(timestamp).size() > 0);
714514f5e3Sopenharmony_ci    ASSERT_EQ(std::string(timestamp), "1970-01-01 00:00:00");
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ciHWTEST_F_L0(CrashTest, BuildCrashRuntimeInfo)
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
774514f5e3Sopenharmony_ci    char soBuildId[NAME_MAX];
784514f5e3Sopenharmony_ci    ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
794514f5e3Sopenharmony_ci    runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
804514f5e3Sopenharmony_ci    runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
814514f5e3Sopenharmony_ci    char sanboxRealPath[PATH_MAX];
824514f5e3Sopenharmony_ci    mkdir(MockAotRuntimeInfo::SANBOX_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
834514f5e3Sopenharmony_ci    std::ofstream file(sanboxRealPath);
844514f5e3Sopenharmony_ci    file.close();
854514f5e3Sopenharmony_ci    runtimeInfo->GetCrashSandBoxRealPath(sanboxRealPath, NAME_MAX);
864514f5e3Sopenharmony_ci
874514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
884514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
894514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
904514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JS);
914514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JIT);
924514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::OTHERS);
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    std::map<ecmascript::ohos::RuntimeInfoType, int> escapeMap = runtimeInfo->CollectCrashSum();
954514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::AOT_CRASH], 3);
964514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::JS], 1);
974514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::JIT], 1);
984514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMap[ecmascript::ohos::RuntimeInfoType::OTHERS], 1);
994514f5e3Sopenharmony_ci    ASSERT_EQ(runtimeInfo->GetCompileCountByType(ecmascript::ohos::RuntimeInfoType::AOT_CRASH), 3);
1004514f5e3Sopenharmony_ci
1014514f5e3Sopenharmony_ci    unlink(sanboxRealPath);
1024514f5e3Sopenharmony_ci    rmdir(MockAotRuntimeInfo::SANBOX_DIR);
1034514f5e3Sopenharmony_ci}
1044514f5e3Sopenharmony_ci
1054514f5e3Sopenharmony_ciHWTEST_F_L0(CrashTest, BuildCompileRuntimeInfo)
1064514f5e3Sopenharmony_ci{
1074514f5e3Sopenharmony_ci    char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
1084514f5e3Sopenharmony_ci    char soBuildId[NAME_MAX];
1094514f5e3Sopenharmony_ci    ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo();
1104514f5e3Sopenharmony_ci    runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
1114514f5e3Sopenharmony_ci    runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
1124514f5e3Sopenharmony_ci    char sanboxRealPath[PATH_MAX];
1134514f5e3Sopenharmony_ci    mkdir(MockAotRuntimeInfo::SANBOX_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
1144514f5e3Sopenharmony_ci    std::ofstream file(sanboxRealPath);
1154514f5e3Sopenharmony_ci    file.close();
1164514f5e3Sopenharmony_ci    runtimeInfo->GetCrashSandBoxRealPath(sanboxRealPath, NAME_MAX);
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath);
1194514f5e3Sopenharmony_ci    runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath);
1204514f5e3Sopenharmony_ci    runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath);
1214514f5e3Sopenharmony_ci    runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JS, sanboxRealPath);
1224514f5e3Sopenharmony_ci    runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::JIT, sanboxRealPath);
1234514f5e3Sopenharmony_ci    runtimeInfo->BuildCompileRuntimeInfo(ecmascript::ohos::RuntimeInfoType::OTHERS, sanboxRealPath);
1244514f5e3Sopenharmony_ci    std::map<ecmascript::ohos::RuntimeInfoType, int> escapeMapRealPath = runtimeInfo->CollectCrashSum(sanboxRealPath);
1254514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::AOT_CRASH], 3);
1264514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::JS], 1);
1274514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::JIT], 1);
1284514f5e3Sopenharmony_ci    ASSERT_EQ(escapeMapRealPath[ecmascript::ohos::RuntimeInfoType::OTHERS], 1);
1294514f5e3Sopenharmony_ci    ASSERT_EQ(runtimeInfo->GetCompileCountByType(ecmascript::ohos::RuntimeInfoType::AOT_CRASH, sanboxRealPath), 3);
1304514f5e3Sopenharmony_ci
1314514f5e3Sopenharmony_ci    unlink(sanboxRealPath);
1324514f5e3Sopenharmony_ci    rmdir(MockAotRuntimeInfo::SANBOX_DIR);
1334514f5e3Sopenharmony_ci}
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_ciHWTEST_F_L0(CrashTest, CrashGetRuntimeInfoByPath)
1364514f5e3Sopenharmony_ci{
1374514f5e3Sopenharmony_ci    char timestamp[ohos::AotRuntimeInfo::TIME_STAMP_SIZE];
1384514f5e3Sopenharmony_ci    char soBuildId[NAME_MAX];
1394514f5e3Sopenharmony_ci    ohos::AotRuntimeInfo *runtimeInfo = new MockAotRuntimeInfo(true);
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_ci    runtimeInfo->GetMicrosecondsTimeStamp(timestamp, ohos::AotRuntimeInfo::TIME_STAMP_SIZE);
1424514f5e3Sopenharmony_ci    runtimeInfo->GetRuntimeBuildId(soBuildId, NAME_MAX);
1434514f5e3Sopenharmony_ci    char sanboxRealPath[PATH_MAX];
1444514f5e3Sopenharmony_ci    mkdir(MockAotRuntimeInfo::SANBOX_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
1454514f5e3Sopenharmony_ci    std::ofstream file(sanboxRealPath);
1464514f5e3Sopenharmony_ci    file.close();
1474514f5e3Sopenharmony_ci
1484514f5e3Sopenharmony_ci    runtimeInfo->GetCrashSandBoxRealPath(sanboxRealPath, NAME_MAX);
1494514f5e3Sopenharmony_ci    runtimeInfo->BuildCrashRuntimeInfo(ecmascript::ohos::RuntimeInfoType::AOT_CRASH);
1504514f5e3Sopenharmony_ci    runtimeInfo->CollectCrashSum();
1514514f5e3Sopenharmony_ci
1524514f5e3Sopenharmony_ci    unlink(sanboxRealPath);
1534514f5e3Sopenharmony_ci    rmdir(MockAotRuntimeInfo::SANBOX_DIR);
1544514f5e3Sopenharmony_ci}
1554514f5e3Sopenharmony_ci} // namespace panda::test