• 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 */
15import faultlogger from '@ohos.faultLogger'
16import hiSysEvent from '@ohos.hiSysEvent'
17import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
18export default function FaultlogJsTest() {
19
20describe("FaultlogJsTest", function () {
21    beforeAll(function() {
22        console.info('FaultlogJsTest beforeAll called')
23    })
24
25    afterAll(function() {
26        console.info('FaultlogJsTest afterAll called')
27    })
28
29    beforeEach(function() {
30        console.info('FaultlogJsTest beforeEach called')
31    })
32
33    afterEach(function() {
34        console.info('FaultlogJsTest afterEach called')
35    })
36
37    async function msleep(time) {
38        let timeoutID = null;
39        let promise = new Promise((resolve, reject) => {
40            timeoutID = setTimeout(() => resolve("done!"), time)
41        });
42        let result = await promise;
43        clearTimeout(timeoutID);
44    }
45
46    /**
47     * test
48     *
49     * @tc.name: DFX_DFR_Faultlogger_Interface_0200
50     * @tc.desc: 检验函数参数输入错误时程序是否会崩溃
51     * @tc.require: AR000GICT2
52     */
53    it('DFX_DFR_Faultlogger_Interface_0200', 0, async function (done) {
54        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0200----------------------------------");
55        try {
56            let ret1 = faultlogger.querySelfFaultLog("faultloggertestsummary01");
57            console.info("DFX_DFR_Faultlogger_Interface_0200 ret1 == " + ret1);
58            expect(ret1).assertEqual(undefined);
59
60            let ret2 = faultlogger.querySelfFaultLog(faultlogger.FaultType.JS_CRASH, "faultloggertestsummary01");
61            console.info("DFX_DFR_Faultlogger_Interface_0200 ret2 == " + ret2);
62            expect(ret2).assertEqual(undefined);
63
64            let ret3 = faultlogger.querySelfFaultLog();
65            console.info("DFX_DFR_Faultlogger_Interface_0200 ret3 == " + ret3);
66            expect(ret3).assertEqual(undefined);
67            done();
68            return;
69        } catch(err) {
70            console.info(err);
71        }
72        expect(false).assertTrue();
73        done();
74    })
75
76
77    /**
78     * test
79     *
80     * @tc.name: DFX_DFR_Faultlogger_Interface_0400
81     * @tc.desc: 检验promise同步方式获取faultlog CPP_CRASH日志
82     * @tc.require: AR000GICT2
83     */
84    it('DFX_DFR_Faultlogger_Interface_0400', 0, async function (done) {
85        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0400----------------------------------");
86        try {
87            let now = Date.now();
88            console.info("DFX_DFR_Faultlogger_Interface_0400 2 + " + now);
89            let module = "ohos.faultloggerjs.test";
90            const loopTimes = 10;
91            for (let i = 0; i < loopTimes; i++) {
92                console.info("--------DFX_DFR_Faultlogger_Interface_0400 3 + " + i + "----------");
93                faultlogger.addFaultLog(i - 400,
94                    faultlogger.FaultType.CPP_CRASH, module, "faultloggertestsummary02 " + i);
95                await msleep(300);
96            }
97            await msleep(1000);
98
99            console.info("--------DFX_DFR_Faultlogger_Interface_0400 4" + "----------");
100            let ret = await faultlogger.querySelfFaultLog(faultlogger.FaultType.CPP_CRASH);
101            console.info("DFX_DFR_Faultlogger_Interface_0400 ret == " + ret.length);
102            expect(ret.length).assertEqual(loopTimes);
103            for (let i = 0; i < loopTimes; i++) {
104                console.info("faultloggertestsummary02 " + i + " fullLog.length " + ret[i].fullLog.length);
105                if (ret[i].fullLog.indexOf("faultloggertestsummary02 " + (loopTimes - 1 - i)) != -1) {
106                    console.info("DFX_DFR_Faultlogger_Interface_0400 " + ret[i].fullLog.length);
107                    expect(typeof(ret[i].pid) == "number").assertTrue();
108                    expect(typeof(ret[i].uid) == "number").assertTrue();
109                    expect(typeof(ret[i].type) == "number").assertTrue();
110                    expect(typeof(ret[i].timestamp) == "number").assertTrue();
111                    expect(typeof(ret[i].reason) == "string").assertTrue();
112                    expect(typeof(ret[i].module) == "string").assertTrue();
113                    expect(typeof(ret[i].summary) == "string").assertTrue();
114                    expect(typeof(ret[i].fullLog) == "string").assertTrue();
115                    expect(true).assertTrue();
116                } else {
117                    expect(false).assertTrue();
118                }
119            }
120
121            console.info("--------DFX_DFR_Faultlogger_Interface_0400 5" + "----------");
122            ret = await faultlogger.querySelfFaultLog(faultlogger.FaultType.NO_SPECIFIC);
123            console.info("DFX_DFR_Faultlogger_Interface_0400 ret == " + ret.length);
124            expect(ret.length).assertEqual(loopTimes);
125            done();
126            return;
127        } catch (err) {
128            console.info("catch (err) == " + err);
129        }
130        console.info("DFX_DFR_Faultlogger_Interface_0400 error");
131        expect(false).assertTrue();
132        done();
133    })
134
135    /**
136     * test
137     *
138     * @tc.name: DFX_DFR_Faultlogger_Interface_0500
139     * @tc.desc: 检验promise同步方式获取faultlog JS_CRASH日志
140     * @tc.require: AR000GICT2
141     */
142     it('DFX_DFR_Faultlogger_Interface_0500', 0, async function (done) {
143        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0500----------------------------------");
144        try {
145            let now = Date.now();
146            console.info("DFX_DFR_Faultlogger_Interface_0500 2 + " + now);
147            hiSysEvent.write({
148                domain: "ACE",
149                name: "JS_ERROR",
150                eventType: hiSysEvent.EventType.FAULT,
151                params: {
152                    PID: 487,
153                    UID:103,
154                    PACKAGE_NAME: "com.ohos.faultlogger.test",
155                    PROCESS_NAME: "com.ohos.faultlogger.test",
156                    MSG: "faultlogger testcase test.",
157                    REASON: "faultlogger testcase test."
158                }
159            }).then(
160                (value) => {
161                    console.log(`HiSysEvent json-callback-success value=${value}`);
162                })
163            await msleep(1000);
164
165            console.info("--------DFX_DFR_Faultlogger_Interface_0500 4" + "----------");
166            let ret = await faultlogger.querySelfFaultLog(faultlogger.FaultType.JS_CRASH);
167            console.info("DFX_DFR_Faultlogger_Interface_0500 ret == " + ret.length);
168            if (ret.length > 0) {
169                expect(true).assertTrue();
170            } else {
171                expect(false).assertTrue();
172            }
173            for (let i = 0; i < ret.length; i++) {
174                console.info("faultloggertestsummary03 " + i + " fullLog.length " + ret[i].fullLog.length);
175            }
176            done();
177            return;
178        } catch (err) {
179            console.info("catch (err) == " + err);
180        }
181        console.info("DFX_DFR_Faultlogger_Interface_0500 error");
182        expect(false).assertTrue();
183        done();
184    })
185
186    /**
187     * test
188     *
189     * @tc.name: DFX_DFR_Faultlogger_Interface_0300
190     * @tc.desc: 检验promise同步方式获取faultlog APP_FREEZE日志
191     * @tc.require: AR000GICT2
192     */
193     it('DFX_DFR_Faultlogger_Interface_0300', 0, async function (done) {
194        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0300----------------------------------");
195        try {
196            let now = Date.now();
197            console.info("DFX_DFR_Faultlogger_Interface_0300 2 + " + now);
198            let module = "ohos.faultloggerjs.test";
199            const loopTimes = 10;
200            for (let i = 0; i < loopTimes; i++) {
201                console.info("--------DFX_DFR_Faultlogger_Interface_0300 3 + " + i + "----------");
202                faultlogger.addFaultLog(i - 400,
203                    faultlogger.FaultType.APP_FREEZE, module, "faultloggertestsummary04 " + i);
204                await msleep(300);
205            }
206            await msleep(1000);
207
208            console.info("--------DFX_DFR_Faultlogger_Interface_0300 4" + "----------");
209            let ret = await faultlogger.querySelfFaultLog(faultlogger.FaultType.APP_FREEZE);
210            console.info("DFX_DFR_Faultlogger_Interface_0300 ret == " + ret.length);
211            expect(ret.length).assertEqual(loopTimes);
212            for (let i = 0; i < loopTimes; i++) {
213                console.info("faultloggertestsummary04 " + i + " fullLog.length " + ret[i].fullLog.length);
214                if (ret[i].fullLog.indexOf("faultloggertestsummary04 " + (loopTimes - 1 - i)) != -1) {
215                    console.info("DFX_DFR_Faultlogger_Interface_0300 " + ret[i].fullLog.length);
216                    expect(true).assertTrue();
217                } else {
218                    expect(false).assertTrue();
219                }
220            }
221            done();
222            return;
223        } catch (err) {
224            console.info("catch (err) == " + err);
225        }
226        console.info("DFX_DFR_Faultlogger_Interface_0300 error");
227        expect(false).assertTrue();
228        done();
229    })
230
231    /**
232     * test
233     *
234     * @tc.name: DFX_DFR_Faultlogger_Interface_0100
235     * @tc.desc: 检验通过回调方式获取faultlog日志
236     * @tc.require: AR000GICT2
237     */
238    it('DFX_DFR_Faultlogger_Interface_0100', 0, async function (done) {
239        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0100----------------------------------");
240        try {
241            let now = Date.now();
242            console.info("DFX_DFR_Faultlogger_Interface_0100 start + " + now);
243            let module = "ohos.faultloggerjs.test";
244            const loopTimes = 10;
245            for (let i = 0; i < loopTimes; i++) {
246                console.info("--------DFX_DFR_Faultlogger_Interface_0100 + " + i + "----------");
247                faultlogger.addFaultLog(i - 300,
248                    faultlogger.FaultType.CPP_CRASH, module, "faultloggertestsummary05 " + i);
249                await msleep(300);
250            }
251            await msleep(1000);
252
253            console.info("--------DFX_DFR_Faultlogger_Interface_0100 4----------");
254            function queryFaultLogCallback(error, ret) {
255                if (error) {
256                    console.info('DFX_DFR_Faultlogger_Interface_0100  once error is ' + error);
257                } else {
258                    console.info("DFX_DFR_Faultlogger_Interface_0100 ret == " + ret.length);
259                    expect(ret.length).assertEqual(loopTimes);
260                    for (let i = 0; i < loopTimes; i++) {
261                        console.info("faultloggertestsummary05 " + i + " fullLog.length " + ret[i].fullLog.length);
262                        if (ret[i].fullLog.indexOf("faultloggertestsummary05 " + (loopTimes - 1 - i)) != -1) {
263                            console.info("DFX_DFR_Faultlogger_Interface_0100 " + ret[i].fullLog.length);
264                            expect(true).assertTrue();
265                        } else {
266                            expect(false).assertTrue();
267                        }
268                    }
269                }
270                done();
271            }
272            faultlogger.querySelfFaultLog(faultlogger.FaultType.CPP_CRASH, queryFaultLogCallback);
273            return;
274        } catch (err) {
275            console.info(err);
276        }
277        console.info("DFX_DFR_Faultlogger_Interface_0100 error");
278        expect(false).assertTrue();
279        done();
280    })
281
282    /**
283     *
284     * @tc.name: DFX_DFR_Faultlogger_Interface_0600
285     * @tc.desc: 检验query函数正常入参
286     * @tc.require: AR000GICT2
287     */
288    it('DFX_DFR_Faultlogger_Interface_0600', 0, function () {
289        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0600----------------------------------");
290        try {
291            let ret = faultlogger.query("faultloggertestsummary06");
292            console.info("DFX_DFR_Faultlogger_Interface_0600 ret == " + ret);
293            expect(ret).assertTrue();
294            return;
295        } catch(err) {
296            console.info(err.code);
297            console.info(err.message);
298            expect(err.code == 401).assertTrue();
299        }
300    })
301
302    /**
303     *
304     * @tc.name: DFX_DFR_Faultlogger_Interface_0700
305     * @tc.desc: 检验query函数入参数量为2时程序是否会崩溃
306     * @tc.require: AR000GICT2
307     */
308    it('DFX_DFR_Faultlogger_Interface_0700', 0, function () {
309        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0700----------------------------------");
310        try {
311            let ret = faultlogger.query(faultlogger.FaultType.JS_CRASH, "faultloggertestsummary07");
312            console.info("DFX_DFR_Faultlogger_Interface_0700 ret == " + ret);
313            return;
314        } catch(err) {
315            console.info(err.code);
316            console.info(err.message);
317            expect(err.code == 401).assertTrue();
318        }
319    })
320
321    /**
322     *
323     * @tc.name: DFX_DFR_Faultlogger_Interface_0800
324     * @tc.desc: 检验query函数入参为空时程序是否会崩溃
325     * @tc.require: AR000GICT2
326     */
327    it('DFX_DFR_Faultlogger_Interface_0800', 0, function () {
328        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0800----------------------------------");
329        try {
330            let ret = faultlogger.query();
331            console.info("DFX_DFR_Faultlogger_Interface_0800 ret == " + ret);
332            return;
333        } catch(err) {
334            console.info(err.code);
335            console.info(err.message);
336            expect(err.code == 401).assertTrue();
337        }
338    })
339
340    /**
341     *
342     * @tc.name: DFX_DFR_Faultlogger_Interface_0900
343     * @tc.desc: 检验promise同步方式获取faultlog JS_CRASH日志
344     * @tc.require: AR000GICT2
345     */
346     it('DFX_DFR_Faultlogger_Interface_0900', 0, async function (done) {
347        console.info("---------------------------DFX_DFR_Faultlogger_Interface_0900----------------------------------");
348        try {
349            let now = Date.now();
350            console.info("DFX_DFR_Faultlogger_Interface_0900 2 + " + now);
351            hiSysEvent.write({
352                domain: "ACE",
353                name: "JS_ERROR",
354                eventType: hiSysEvent.EventType.FAULT,
355                params: {
356                    PID: 487,
357                    UID:103,
358                    PACKAGE_NAME: "com.ohos.faultlogger.test",
359                    PROCESS_NAME: "com.ohos.faultlogger.test",
360                    MSG: "faultlogger testcase test.",
361                    REASON: "faultlogger testcase test."
362                }
363            }).then(
364                (value) => {
365                    console.log(`HiSysEvent json-callback-success value=${value}`);
366                })
367            await msleep(1000);
368
369            console.info("--------DFX_DFR_Faultlogger_Interface_0900 4" + "----------");
370            let ret = await faultlogger.query(faultlogger.FaultType.JS_CRASH);
371            console.info("DFX_DFR_Faultlogger_Interface_0900 ret == " + ret.length);
372            if (ret.length > 0) {
373                expect(true).assertTrue();
374            } else {
375                expect(false).assertTrue();
376            }
377            for (let i = 0; i < ret.length; i++) {
378                console.info("faultloggertestsummary08 " + i + " fullLog.length " + ret[i].fullLog.length);
379            }
380            done();
381            return;
382        } catch (err) {
383            console.info("catch (err) == " + err);
384        }
385        console.info("DFX_DFR_Faultlogger_Interface_0900 error");
386        expect(false).assertTrue();
387        done();
388    })
389
390    /**
391     *
392     * @tc.name: DFX_DFR_Faultlogger_Interface_1000
393     * @tc.desc: 检验promise同步方式获取faultlog APP_FREEZE日志
394     * @tc.require: AR000GICT2
395     */
396     it('DFX_DFR_Faultlogger_Interface_1000', 0, async function (done) {
397        console.info("---------------------------DFX_DFR_Faultlogger_Interface_1000----------------------------------");
398        try {
399            let now = Date.now();
400            console.info("DFX_DFR_Faultlogger_Interface_1000 2 + " + now);
401            let module = "ohos.faultloggerjs.test";
402            const loopTimes = 10;
403            for (let i = 0; i < loopTimes; i++) {
404                console.info("--------DFX_DFR_Faultlogger_Interface_1000 3 + " + i + "----------");
405                faultlogger.addFaultLog(i - 200,
406                    faultlogger.FaultType.APP_FREEZE, module, "faultloggertestsummary09 " + i);
407                await msleep(300);
408            }
409            await msleep(1000);
410
411            console.info("--------DFX_DFR_Faultlogger_Interface_1000 4" + "----------");
412            let ret = await faultlogger.query(faultlogger.FaultType.APP_FREEZE);
413            console.info("DFX_DFR_Faultlogger_Interface_1000 ret == " + ret.length);
414            expect(ret.length).assertEqual(loopTimes);
415            for (let i = 0; i < loopTimes; i++) {
416                console.info("faultloggertestsummary09 " + i + " fullLog.length " + ret[i].fullLog.length);
417                if (ret[i].fullLog.indexOf("faultloggertestsummary09 " + (loopTimes - 1 - i)) != -1) {
418                    console.info("DFX_DFR_Faultlogger_Interface_1000 " + ret[i].fullLog.length);
419                    expect(true).assertTrue();
420                } else {
421                    expect(false).assertTrue();
422                }
423            }
424            done();
425            return;
426        } catch (err) {
427            console.info("catch (err) == " + err);
428        }
429        console.info("DFX_DFR_Faultlogger_Interface_1000 error");
430        expect(false).assertTrue();
431        done();
432    })
433
434    /**
435     * test
436     *
437     * @tc.name: DFX_DFR_Faultlogger_Interface_1100
438     * @tc.desc: 检验通过回调方式获取faultlog日志
439     * @tc.require: AR000GICT2
440     */
441    it('DFX_DFR_Faultlogger_Interface_1100', 0, async function (done) {
442        console.info("---------------------------DFX_DFR_Faultlogger_Interface_1100----------------------------------");
443        try {
444            let now = Date.now();
445            console.info("DFX_DFR_Faultlogger_Interface_1100 start + " + now);
446            let module = "ohos.faultloggerjs.test";
447            const loopTimes = 10;
448            for (let i = 0; i < loopTimes; i++) {
449                console.info("--------DFX_DFR_Faultlogger_Interface_1100 + " + i + "----------");
450                faultlogger.addFaultLog(i - 100,
451                    faultlogger.FaultType.CPP_CRASH, module, "faultloggertestsummary10 " + i);
452                await msleep(300);
453            }
454            await msleep(1000);
455
456            console.info("--------DFX_DFR_Faultlogger_Interface_1100 4----------");
457            function queryFaultLogCallback(error, ret) {
458                if (error) {
459                    console.info('DFX_DFR_Faultlogger_Interface_1100  once error is ' + error);
460                } else {
461                    console.info("DFX_DFR_Faultlogger_Interface_1100 ret == " + ret.length);
462                    expect(ret.length).assertEqual(loopTimes);
463                    for (let i = 0; i < loopTimes; i++) {
464                        console.info("faultloggertestsummary10 " + i + " fullLog.length " + ret[i].fullLog.length);
465                        if (ret[i].fullLog.indexOf("faultloggertestsummary10 " + (loopTimes - 1 - i)) != -1) {
466                            console.info("DFX_DFR_Faultlogger_Interface_1100 " + ret[i].fullLog.length);
467                            expect(true).assertTrue();
468                        } else {
469                            expect(false).assertTrue();
470                        }
471                    }
472                }
473                done();
474            }
475            faultlogger.query(faultlogger.FaultType.CPP_CRASH, queryFaultLogCallback);
476            return;
477        } catch (err) {
478            console.info(err);
479        }
480        console.info("DFX_DFR_Faultlogger_Interface_1100 error");
481        expect(false).assertTrue();
482        done();
483    })
484})
485}
486