1/*
2 * Copyright (C) 2021-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 */
15import faultlogger from '@ohos.faultLogger'
16import hiSysEvent from '@ohos.hiSysEvent'
17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index'
18import faultloggerTestNapi from "libfaultlogger_test_napi.so"
19
20describe("FaultlogJsTest", function () {
21    const moduleName = "com.ohos.hiviewtest.faultlogjs";
22    const freezeFaultCount = 6;
23
24    beforeAll(async function() {
25        try {
26            await addAppFreezeFaultLog();
27            await addJsCrashFaultLog();
28            await addCppCrashFaultLog();
29            console.log("add FaultLog success");
30        } catch (err) {
31            console.info(err);
32        }
33        await msleep(7000);
34    })
35
36    async function addAppFreezeFaultLog() {
37        for (let i = 0; i < freezeFaultCount; i++) {
38            console.info("--------addAppFreezeFaultLog + " + i + "----------");
39            faultlogger.addFaultLog(i - 7, faultlogger.FaultType.APP_FREEZE, moduleName, `APP_FREEZE ${i}`);
40            await msleep(300);
41        }
42    }
43
44    async function addJsCrashFaultLog() {
45        return hiSysEvent.write({
46            domain: "ACE",
47            name: "JS_ERROR",
48            eventType: hiSysEvent.EventType.FAULT,
49            params: {
50                PACKAGE_NAME: moduleName,
51                PROCESS_NAME: moduleName,
52                MSG: "faultlogger testcase test.",
53                REASON: "faultlogger testcase test."
54            }
55        })
56    }
57
58    async function addCppCrashFaultLog() {
59        if (!faultloggerTestNapi.triggerCppCrash()) {
60            throw "failed to add CPP_CRASH faultLog";
61        }
62    }
63
64    function msleep(time) {
65        return new Promise((resolve, reject) => {
66            setTimeout(() => resolve("done!"), time)
67        });
68    }
69
70    function testCallbackQuery(faultType) {
71        return new Promise((resolve, reject) => {
72            faultlogger.query(faultType, (error, ret) => {
73                if (error) {
74                    reject(error);
75                } else {
76                    resolve(ret);
77                }
78            })
79        })
80    }
81    /**
82     * test
83     *
84     * @tc.number: FaultlogJsException_001
85     * @tc.name: FaultlogJsException_001
86     * @tc.desc: API8 检验函数参数输入错误时程序是否会崩溃
87     * @tc.require: AR000GICT2
88     * @tc.author:
89     * @tc.type: Function
90     * @tc.size: MediumTest
91     * @tc.level: Level 0
92     */
93    it('FaultlogJsException_001', 0, async function (done) {
94        console.info("---------------------------FaultlogJsException_001----------------------------------");
95        try {
96            let ret1 = faultlogger.querySelfFaultLog("faultloggertestsummary01");
97            console.info("FaultlogJsException_001 ret1 == " + ret1);
98            let a = expect(ret1).assertEqual(undefined);
99            console.info('ret1 assertEqual(undefined) ' + a);
100
101            let ret2 = faultlogger.querySelfFaultLog(faultlogger.FaultType.JS_CRASH, "faultloggertestsummary01");
102            console.info("FaultlogJsException_001 ret2 == " + ret2);
103            expect(ret2).assertEqual(undefined);
104
105            let ret3 = faultlogger.querySelfFaultLog();
106            console.info("FaultlogJsException_001 ret3 == " + ret3);
107            expect(ret3).assertEqual(undefined);
108            done();
109            return;
110        } catch(err) {
111            console.info(err);
112        }
113        expect(false).assertTrue();
114        done();
115    })
116
117    /**
118     * test
119     *
120     * @tc.number: FaultlogJsException_002
121     * @tc.name: FaultlogJsException_002
122     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
123     * @tc.require: issueI5VRCC
124     * @tc.author:
125     * @tc.type: Function
126     * @tc.size: MediumTest
127     * @tc.level: Level 0
128     */
129     it('FaultlogJsException_002', 0, function () {
130        console.info("---------------------------FaultlogJsException_002----------------------------------");
131        try {
132            let ret = faultlogger.query("faultloggertestsummary02");
133            console.info("FaultlogJsException_002 ret == " + ret);
134            return;
135        } catch(err) {
136            console.info(err.code);
137            console.info(err.message);
138            expect(err.code == 401).assertTrue();
139        }
140    })
141
142    /**
143     * test
144     *
145     * @tc.number: FaultlogJsException_003
146     * @tc.name: FaultlogJsException_003
147     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
148     * @tc.require: issueI5VRCC
149     * @tc.author:
150     * @tc.type: Function
151     * @tc.size: MediumTest
152     * @tc.level: Level 0
153     */
154     it('FaultlogJsException_003', 0, function () {
155        console.info("---------------------------FaultlogJsException_003----------------------------------");
156        try {
157            let ret = faultlogger.query(faultlogger.FaultType.JS_CRASH, "faultloggertestsummary03");
158            console.info("FaultlogJsException_003 ret == " + ret);
159            return;
160        } catch(err) {
161            console.info(err.code);
162            console.info(err.message);
163            expect(err.code == 401).assertTrue();
164        }
165    })
166
167    /**
168     * test
169     *
170     * @tc.number: FaultlogJsException_004
171     * @tc.name: FaultlogJsException_004
172     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
173     * @tc.require: issueI5VRCC
174     * @tc.author:
175     * @tc.type: Function
176     * @tc.size: MediumTest
177     * @tc.level: Level 0
178     */
179     it('FaultlogJsException_004', 0, function () {
180        console.info("---------------------------FaultlogJsException_004----------------------------------");
181        try {
182            let ret = faultlogger.query();
183            console.info("FaultlogJsException_004 ret == " + ret);
184            return;
185        } catch(err) {
186            console.info(err.code);
187            console.info(err.message);
188            expect(err.code == 401).assertTrue();
189        }
190    })
191
192    /**
193     * test
194     *
195     * @tc.number: FaultlogJsException_005
196     * @tc.name: FaultlogJsException_005
197     * @tc.desc: API9 检验函数参数输入错误时程序是否会崩溃并校验错误码
198     * @tc.require: issueI5VRCC
199     * @tc.author:
200     * @tc.type: Function
201     * @tc.size: MediumTest
202     * @tc.level: Level 0
203     */
204    it('FaultlogJsException_005', 0, function () {
205        console.info("---------------------------FaultlogJsException_005----------------------------------");
206        try {
207            let ret = faultlogger.query("aaa", "bbb", "ccc");
208            console.info("FaultlogJsException_005 ret == " + ret);
209            return;
210        } catch(err) {
211            console.info(err.code);
212            console.info(err.message);
213            expect(err.code == 401).assertTrue();
214        }
215    })
216
217    async function checkCppCrashFaultLogInfo(faultLogInfo) {
218        if (faultLogInfo.reason.indexOf("Signal:SIGABRT") === -1) {
219            throw "Can not find \"Signal:SIGABRT\"";
220        }
221        if (faultLogInfo.fullLog.indexOf("Fault thread info") === -1) {
222            throw "Can not find \"Fault thread info\"";
223        }
224    }
225
226    /**
227     * test
228     *
229     * @tc.number: FaultlogJsTest_005
230     * @tc.name: FaultlogJsTest_005
231     * @tc.desc: API9 检验查询CPP_CRASH类型的数据及内容
232     * @tc.require: issueI5VRCC
233     * @tc.author:
234     * @tc.type: Function
235     * @tc.size: MediumTest
236     * @tc.level: Level 0
237     */
238     it('FaultlogJsTest_005', 0, async function (done) {
239        console.info("---------------------------FaultlogJsTest_005----------------------------------");
240        try {
241            let retPromise = await faultlogger.query(faultlogger.FaultType.CPP_CRASH);
242            console.info("FaultlogJsTest_005 query retPromise length:" + retPromise.length);
243            expect(retPromise.length).assertLarger(0);
244            await checkCppCrashFaultLogInfo(retPromise[0]);
245            let retCallback = await testCallbackQuery(faultlogger.FaultType.CPP_CRASH);
246            console.info("FaultlogJsTest_005 query retCallback length:" + retPromise.length);
247            expect(retCallback.length).assertLarger(0);
248            await checkCppCrashFaultLogInfo(retCallback[0]);
249        } catch (err) {
250            console.info(`FaultlogJsTest_005 error: ${err}`);
251            expect(false).assertTrue();
252        }
253        done();
254    })
255
256    async function checkFreezeFaultLogList(faultLogInfos) {
257        for (let i = 0; i < freezeFaultCount; i++) {
258            if (faultLogInfos[i].fullLog.indexOf(`APP_FREEZE ${freezeFaultCount - 1 - i}`) === -1) {
259                throw `failed to checkFreezeFaultLog for ${i}`;
260            }
261        }
262    }
263
264    /**
265     * test
266     *
267     * @tc.number: FaultlogJsTest_006
268     * @tc.name: FaultlogJsTest_006
269     * @tc.desc: API9 校验查询APP_FREEZE类型数据的返回结果的数据内容,及顺序
270     * @tc.require: issueI5VRCC
271     * @tc.author:
272     * @tc.type: Function
273     * @tc.size: MediumTest
274     * @tc.level: Level 0
275     */
276    it('FaultlogJsTest_006', 0, async function (done) {
277        console.info("---------------------------FaultlogJsTest_006----------------------------------");
278        try {
279            let retPromise = await faultlogger.query(faultlogger.FaultType.APP_FREEZE);
280            console.info("FaultlogJsTest_006 query retPromise length:" + retPromise.length);
281            expect(retPromise.length).assertLarger(freezeFaultCount - 1);
282            await checkFreezeFaultLogList(retPromise);
283            let retCallBack = await testCallbackQuery(faultlogger.FaultType.APP_FREEZE);
284            console.info("FaultlogJsTest_006 query retCallBack length:" + retCallBack.length);
285            expect(retPromise.length).assertLarger(freezeFaultCount - 1);
286            await checkFreezeFaultLogList(retCallBack);
287        } catch (err) {
288            console.info(`FaultlogJsTest_006 error: ${err}`);
289            expect(false).assertTrue();
290        }
291        done();
292    })
293
294    function checkNoSpecificFaultLogList(faultLogInfos) {
295        expect(faultLogInfos[0].type).assertEqual(faultlogger.FaultType.CPP_CRASH);
296        expect(faultLogInfos[1].type).assertEqual(faultlogger.FaultType.JS_CRASH);
297        expect(faultLogInfos[1].timestamp).assertLess(faultLogInfos[0].timestamp);
298        expect(faultLogInfos[2].type).assertEqual(faultlogger.FaultType.APP_FREEZE);
299        expect(faultLogInfos[2].timestamp).assertLess(faultLogInfos[1].timestamp);
300    }
301
302    /**
303     * test
304     *
305     * @tc.number: FaultlogJsTest_007
306     * @tc.name: FaultlogJsTest_007
307     * @tc.desc: API9 校验查询NO_SPECIFIC类型数据,及查询数据的顺序
308     * @tc.require: issueI5VRCC
309     * @tc.author:
310     * @tc.type: Function
311     * @tc.size: MediumTest
312     * @tc.level: Level 0
313     */
314    it('FaultlogJsTest_007', 0, async function (done) {
315        console.info("---------------------------FaultlogJsTest_007----------------------------------");
316        try {
317            let retPromise = await faultlogger.query(faultlogger.FaultType.NO_SPECIFIC);
318            console.info("FaultlogJsTest_007 query retPromise length:" + retPromise.length);
319            expect(retPromise.length).assertLarger(freezeFaultCount + 2 - 1);
320            checkNoSpecificFaultLogList(retPromise);
321
322            let retCallback = await testCallbackQuery(faultlogger.FaultType.NO_SPECIFIC);
323            console.info("FaultlogJsTest_007 query retCallback length:" + retCallback.length);
324            expect(retCallback.length).assertLarger(freezeFaultCount + 2 - 1);
325            checkNoSpecificFaultLogList(retCallback);
326        } catch (err) {
327            console.info(`FaultlogJsTest_007 error: ${err}`);
328            expect(false).assertTrue();
329        }
330        done();
331    })
332})