1 /*
2 * Copyright (C) 2023 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 extern crate hisysevent;
17
18 use hisysevent::{EventType, HiSysEventRecord, RuleType, Watcher, WatchRule};
19 use hisysevent::{QueryArg, QueryRule, Querier};
20
21 const SUCCEED: i32 = 0;
22 const LISTENER_NOT_EXIST: i32 = -10;
23 const QUERY_CNT: i32 = 2;
24
25 #[test]
test_hisysevent_write_001()26 fn test_hisysevent_write_001() {
27 let ret = hisysevent::write(
28 "HIVIEWDFX",
29 "PLUGIN_LOAD",
30 EventType::Behavior,
31 &[hisysevent::build_number_param!("INT32_SINGLE", 100i32),
32 hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_write_001"),
33 hisysevent::build_bool_param!("BOOL_SINGLE", true),
34 hisysevent::build_number_param!("FLOAT_SINGLE", 4.03f32),
35 hisysevent::build_string_array_params!("STRING_ARRAY", &["STRING1", "STRING2"]),
36 hisysevent::build_array_params!("INT32_ARRAY", &[8i32, 9i32]),
37 hisysevent::build_array_params!("BOOL_ARRAY", &[true, false, true]),
38 hisysevent::build_array_params!("FLOAT_ARRAY", &[1.55f32, 2.33f32, 4.88f32])]
39 );
40 assert!(ret == SUCCEED);
41 }
42
43 #[test]
test_hisysevent_add_remove_watcher_001()44 fn test_hisysevent_add_remove_watcher_001() {
45 let watch_rules = [
46 WatchRule {
47 domain: "HIVIEWDFX",
48 name: "PLUGIN_LOAD",
49 tag: "",
50 rule_type: RuleType::WholeWord,
51 event_type: EventType::Behavior,
52 },
53 WatchRule {
54 domain: "HIVIEWDFX",
55 name: "PLUGIN_UNLOAD",
56 tag: "",
57 rule_type: RuleType::WholeWord,
58 event_type: EventType::Behavior,
59 }
60 ];
61 // step1: construct a mut watcher.
62 let watcher = Watcher::new(|record: HiSysEventRecord| {
63 assert!(record.get_domain() == "HIVIEWDFX");
64 }, || {
65 // do nothing.
66 }).expect("Construct a watcher by Watcher::new");
67 // step2: add this watcher.
68 let mut ret = hisysevent::add_watcher(&watcher, &watch_rules);
69 assert!(ret == SUCCEED);
70 ret = hisysevent::write(
71 "HIVIEWDFX",
72 "PLUGIN_LOAD",
73 EventType::Behavior,
74 &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_add_remove_watcher_001")]
75 );
76 assert!(ret == SUCCEED);
77 ret = hisysevent::write(
78 "HIVIEWDFX",
79 "PLUGIN_UNLOAD",
80 EventType::Behavior,
81 &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_add_remove_watcher_001")]
82 );
83 assert!(ret == SUCCEED);
84 // step3: remove this watcher.
85 ret = hisysevent::remove_watcher(&watcher);
86 assert!(ret == SUCCEED);
87 // step4: recycle allocated memories of this watcher.
88 watcher.try_to_recycle();
89 ret = hisysevent::add_watcher(&watcher, &watch_rules);
90 assert!(ret == LISTENER_NOT_EXIST);
91 }
92
93 #[test]
test_hisysevent_query_001()94 fn test_hisysevent_query_001() {
95 // write two events at first.
96 let mut ret = hisysevent::write(
97 "HIVIEWDFX",
98 "PLUGIN_LOAD",
99 EventType::Behavior,
100 &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_query_001")]
101 );
102 assert!(ret == SUCCEED);
103 ret = hisysevent::write(
104 "HIVIEWDFX",
105 "PLUGIN_UNLOAD",
106 EventType::Behavior,
107 &[hisysevent::build_str_param!("STRING_SINGLE", "test_hisysevent_query_001")]
108 );
109 assert!(ret == SUCCEED);
110 // query event.
111 let query_arg = QueryArg {
112 begin_time: -1,
113 end_time: -1,
114 max_events: 2,
115 };
116 let query_rules = [
117 QueryRule {
118 domain: "HIVIEWDFX",
119 event_list: vec![
120 "PLUGIN_LOAD",
121 "PLUGIN_UNLOAD",
122 ],
123 condition: "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"
124 NAME\",\"op\":\"=\",\"value\":\"SysEventService\"}]}}",
125 },
126 QueryRule {
127 domain: "HIVIEWDFX",
128 event_list: vec![
129 "PLUGIN_LOAD",
130 ],
131 condition: "",
132 }
133 ];
134 // step1: construct a querier.
135 let querier = Querier::new(|records: &[HiSysEventRecord]| {
136 for item in records {
137 assert!(item.get_domain() == "HIVIEWDFX");
138 }
139 }, |reason: i32, total: i32| {
140 assert!(reason == SUCCEED);
141 assert!(total == QUERY_CNT);
142 }).expect("Construct a querier by Querier::new");
143 // step2: query.
144 ret = hisysevent::query(&query_arg, &query_rules, &querier);
145 assert!(ret == SUCCEED);
146 // step3: recycle allocated memories of this Querier.
147 querier.try_to_recycle();
148 ret = hisysevent::query(&query_arg, &query_rules, &querier);
149 assert!(ret == LISTENER_NOT_EXIST);
150 }
151