11401458bSopenharmony_ci/*
21401458bSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
31401458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41401458bSopenharmony_ci * you may not use this file except in compliance with the License.
51401458bSopenharmony_ci * You may obtain a copy of the License at
61401458bSopenharmony_ci *
71401458bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81401458bSopenharmony_ci *
91401458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101401458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111401458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121401458bSopenharmony_ci * See the License for the specific language governing permissions and
131401458bSopenharmony_ci * limitations under the License.
141401458bSopenharmony_ci */
151401458bSopenharmony_ci
161401458bSopenharmony_ciextern crate hisysevent;
171401458bSopenharmony_ci
181401458bSopenharmony_ciuse hisysevent::{EventType, HiSysEventRecord, RuleType, Watcher, WatchRule};
191401458bSopenharmony_ciuse hisysevent::{QueryArg, QueryRule, Querier};
201401458bSopenharmony_ci
211401458bSopenharmony_ciconst SUCCEED: i32 = 0;
221401458bSopenharmony_ciconst LISTENER_NOT_EXIST: i32 = -10;
231401458bSopenharmony_ciconst QUERY_CNT: i32 = 2;
241401458bSopenharmony_ci
251401458bSopenharmony_ci#[test]
261401458bSopenharmony_cifn test_hisysevent_write_001() {
271401458bSopenharmony_ci    let ret = hisysevent::write(
281401458bSopenharmony_ci        "HIVIEWDFX",
291401458bSopenharmony_ci        "PLUGIN_LOAD",
301401458bSopenharmony_ci        EventType::Behavior,
311401458bSopenharmony_ci        &[hisysevent::build_number_param!("INT32_SINGLE", 100i32),
321401458bSopenharmony_ci            hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_write_001"),
331401458bSopenharmony_ci            hisysevent::build_bool_param!("BOOL_SINGLE", true),
341401458bSopenharmony_ci            hisysevent::build_number_param!("FLOAT_SINGLE", 4.03f32),
351401458bSopenharmony_ci            hisysevent::build_string_array_params!("STRING_ARRAY", &["STRING1", "STRING2"]),
361401458bSopenharmony_ci            hisysevent::build_array_params!("INT32_ARRAY", &[8i32, 9i32]),
371401458bSopenharmony_ci            hisysevent::build_array_params!("BOOL_ARRAY", &[true, false, true]),
381401458bSopenharmony_ci            hisysevent::build_array_params!("FLOAT_ARRAY", &[1.55f32, 2.33f32, 4.88f32])]
391401458bSopenharmony_ci    );
401401458bSopenharmony_ci    assert!(ret == SUCCEED);
411401458bSopenharmony_ci}
421401458bSopenharmony_ci
431401458bSopenharmony_ci#[test]
441401458bSopenharmony_cifn test_hisysevent_add_remove_watcher_001() {
451401458bSopenharmony_ci    let watch_rules = [
461401458bSopenharmony_ci        WatchRule {
471401458bSopenharmony_ci            domain: "HIVIEWDFX",
481401458bSopenharmony_ci            name: "PLUGIN_LOAD",
491401458bSopenharmony_ci            tag: "",
501401458bSopenharmony_ci            rule_type: RuleType::WholeWord,
511401458bSopenharmony_ci            event_type: EventType::Behavior,
521401458bSopenharmony_ci        },
531401458bSopenharmony_ci        WatchRule {
541401458bSopenharmony_ci            domain: "HIVIEWDFX",
551401458bSopenharmony_ci            name: "PLUGIN_UNLOAD",
561401458bSopenharmony_ci            tag: "",
571401458bSopenharmony_ci            rule_type: RuleType::WholeWord,
581401458bSopenharmony_ci            event_type: EventType::Behavior,
591401458bSopenharmony_ci        }
601401458bSopenharmony_ci    ];
611401458bSopenharmony_ci    // step1: construct a mut watcher.
621401458bSopenharmony_ci    let watcher = Watcher::new(|record: HiSysEventRecord| {
631401458bSopenharmony_ci        assert!(record.get_domain() == "HIVIEWDFX");
641401458bSopenharmony_ci    }, || {
651401458bSopenharmony_ci        // do nothing.
661401458bSopenharmony_ci    }).expect("Construct a watcher by Watcher::new");
671401458bSopenharmony_ci    // step2: add this watcher.
681401458bSopenharmony_ci    let mut ret = hisysevent::add_watcher(&watcher, &watch_rules);
691401458bSopenharmony_ci    assert!(ret == SUCCEED);
701401458bSopenharmony_ci    ret = hisysevent::write(
711401458bSopenharmony_ci        "HIVIEWDFX",
721401458bSopenharmony_ci        "PLUGIN_LOAD",
731401458bSopenharmony_ci        EventType::Behavior,
741401458bSopenharmony_ci        &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_add_remove_watcher_001")]
751401458bSopenharmony_ci    );
761401458bSopenharmony_ci    assert!(ret == SUCCEED);
771401458bSopenharmony_ci    ret = hisysevent::write(
781401458bSopenharmony_ci        "HIVIEWDFX",
791401458bSopenharmony_ci        "PLUGIN_UNLOAD",
801401458bSopenharmony_ci        EventType::Behavior,
811401458bSopenharmony_ci        &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_add_remove_watcher_001")]
821401458bSopenharmony_ci    );
831401458bSopenharmony_ci    assert!(ret == SUCCEED);
841401458bSopenharmony_ci    // step3: remove this watcher.
851401458bSopenharmony_ci    ret = hisysevent::remove_watcher(&watcher);
861401458bSopenharmony_ci    assert!(ret == SUCCEED);
871401458bSopenharmony_ci    // step4: recycle allocated memories of this watcher.
881401458bSopenharmony_ci    watcher.try_to_recycle();
891401458bSopenharmony_ci    ret = hisysevent::add_watcher(&watcher, &watch_rules);
901401458bSopenharmony_ci    assert!(ret == LISTENER_NOT_EXIST);
911401458bSopenharmony_ci}
921401458bSopenharmony_ci
931401458bSopenharmony_ci#[test]
941401458bSopenharmony_cifn test_hisysevent_query_001() {
951401458bSopenharmony_ci    // write two events at first.
961401458bSopenharmony_ci    let mut ret = hisysevent::write(
971401458bSopenharmony_ci        "HIVIEWDFX",
981401458bSopenharmony_ci        "PLUGIN_LOAD",
991401458bSopenharmony_ci        EventType::Behavior,
1001401458bSopenharmony_ci        &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_query_001")]
1011401458bSopenharmony_ci    );
1021401458bSopenharmony_ci    assert!(ret == SUCCEED);
1031401458bSopenharmony_ci    ret = hisysevent::write(
1041401458bSopenharmony_ci        "HIVIEWDFX",
1051401458bSopenharmony_ci        "PLUGIN_UNLOAD",
1061401458bSopenharmony_ci        EventType::Behavior,
1071401458bSopenharmony_ci        &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_query_001")]
1081401458bSopenharmony_ci    );
1091401458bSopenharmony_ci    assert!(ret == SUCCEED);
1101401458bSopenharmony_ci    // query event.
1111401458bSopenharmony_ci    let query_arg = QueryArg {
1121401458bSopenharmony_ci        begin_time: -1,
1131401458bSopenharmony_ci        end_time: -1,
1141401458bSopenharmony_ci        max_events: 2,
1151401458bSopenharmony_ci    };
1161401458bSopenharmony_ci    let query_rules = [
1171401458bSopenharmony_ci        QueryRule {
1181401458bSopenharmony_ci            domain: "HIVIEWDFX",
1191401458bSopenharmony_ci            event_list: vec![
1201401458bSopenharmony_ci                "PLUGIN_LOAD",
1211401458bSopenharmony_ci                "PLUGIN_UNLOAD",
1221401458bSopenharmony_ci            ],
1231401458bSopenharmony_ci            condition: "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"
1241401458bSopenharmony_ci                NAME\",\"op\":\"=\",\"value\":\"SysEventService\"}]}}",
1251401458bSopenharmony_ci        },
1261401458bSopenharmony_ci        QueryRule {
1271401458bSopenharmony_ci            domain: "HIVIEWDFX",
1281401458bSopenharmony_ci            event_list: vec![
1291401458bSopenharmony_ci                "PLUGIN_LOAD",
1301401458bSopenharmony_ci            ],
1311401458bSopenharmony_ci            condition: "",
1321401458bSopenharmony_ci        }
1331401458bSopenharmony_ci    ];
1341401458bSopenharmony_ci    // step1: construct a querier.
1351401458bSopenharmony_ci    let querier = Querier::new(|records: &[HiSysEventRecord]| {
1361401458bSopenharmony_ci        for item in records {
1371401458bSopenharmony_ci            assert!(item.get_domain() == "HIVIEWDFX");
1381401458bSopenharmony_ci        }
1391401458bSopenharmony_ci    }, |reason: i32, total: i32| {
1401401458bSopenharmony_ci        assert!(reason == SUCCEED);
1411401458bSopenharmony_ci        assert!(total == QUERY_CNT);
1421401458bSopenharmony_ci    }).expect("Construct a querier by Querier::new");
1431401458bSopenharmony_ci    // step2: query.
1441401458bSopenharmony_ci    ret = hisysevent::query(&query_arg, &query_rules, &querier);
1451401458bSopenharmony_ci    assert!(ret == SUCCEED);
1461401458bSopenharmony_ci    // step3: recycle allocated memories of this Querier.
1471401458bSopenharmony_ci    querier.try_to_recycle();
1481401458bSopenharmony_ci    ret = hisysevent::query(&query_arg, &query_rules, &querier);
1491401458bSopenharmony_ci    assert!(ret == LISTENER_NOT_EXIST);
1501401458bSopenharmony_ci}
151