1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 extern crate hisysevent;
15
16 use hisysevent::{
17 EventType, HiSysEventRecord, Querier, QueryArg, QueryRule, RuleType, WatchRule, Watcher,
18 };
19
20 const SUCCEED: i32 = 0;
21 const LISTENER_NOT_EXIST: i32 = -10;
22 const QUERY_CNT: i32 = 2;
23
24 #[test]
test_hisysevent_write_001()25 fn test_hisysevent_write_001() {
26 let ret = hisysevent::write(
27 "HIVIEWDFX",
28 "PLUGIN_LOAD",
29 EventType::Behavior,
30 &[
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 );
41 assert!(ret == SUCCEED);
42 }
43
44 #[test]
test_hisysevent_add_remove_watcher_001()45 fn test_hisysevent_add_remove_watcher_001() {
46 let watch_rules = [
47 WatchRule {
48 domain: "HIVIEWDFX",
49 name: "PLUGIN_LOAD",
50 tag: "",
51 rule_type: RuleType::WholeWord,
52 event_type: EventType::Behavior,
53 },
54 WatchRule {
55 domain: "HIVIEWDFX",
56 name: "PLUGIN_UNLOAD",
57 tag: "",
58 rule_type: RuleType::WholeWord,
59 event_type: EventType::Behavior,
60 },
61 ];
62 // step1: construct a mut watcher.
63 let watcher = Watcher::new(
64 |record: HiSysEventRecord| {
65 assert!(record.get_domain() == "HIVIEWDFX");
66 },
67 || {
68 // do nothing.
69 },
70 )
71 .expect("Construct a watcher by Watcher::new");
72 // step2: add this watcher.
73 let mut ret = hisysevent::add_watcher(&watcher, &watch_rules);
74 assert!(ret == SUCCEED);
75 ret = hisysevent::write(
76 "HIVIEWDFX",
77 "PLUGIN_LOAD",
78 EventType::Behavior,
79 &[hisysevent::build_str_param!(
80 "STRING_SINGLE",
81 "test_hisysevent_add_remove_watcher_001"
82 )],
83 );
84 assert!(ret == SUCCEED);
85 ret = hisysevent::write(
86 "HIVIEWDFX",
87 "PLUGIN_UNLOAD",
88 EventType::Behavior,
89 &[hisysevent::build_str_param!(
90 "STRING_SINGLE",
91 "test_hisysevent_add_remove_watcher_001"
92 )],
93 );
94 assert!(ret == SUCCEED);
95 // step3: remove this watcher.
96 ret = hisysevent::remove_watcher(&watcher);
97 assert!(ret == SUCCEED);
98 // step4: recycle allocated memories of this watcher.
99 watcher.try_to_recycle();
100 ret = hisysevent::add_watcher(&watcher, &watch_rules);
101 assert!(ret == LISTENER_NOT_EXIST);
102 }
103
104 #[test]
test_hisysevent_query_001()105 fn test_hisysevent_query_001() {
106 // write two events at first.
107 let mut ret = hisysevent::write(
108 "HIVIEWDFX",
109 "PLUGIN_LOAD",
110 EventType::Behavior,
111 &[hisysevent::build_str_param!(
112 "STRING_SINGLE",
113 "test_hisysevent_query_001"
114 )],
115 );
116 assert!(ret == SUCCEED);
117 ret = hisysevent::write(
118 "HIVIEWDFX",
119 "PLUGIN_UNLOAD",
120 EventType::Behavior,
121 &[hisysevent::build_str_param!(
122 "STRING_SINGLE",
123 "test_hisysevent_query_001"
124 )],
125 );
126 assert!(ret == SUCCEED);
127 // query event.
128 let query_arg = QueryArg {
129 begin_time: -1,
130 end_time: -1,
131 max_events: 2,
132 };
133 let query_rules = [
134 QueryRule {
135 domain: "HIVIEWDFX",
136 event_list: vec!["PLUGIN_LOAD", "PLUGIN_UNLOAD"],
137 condition: "{\"version\":\"V1\",\"condition\":{\"and\":[{\"param\":\"
138 NAME\",\"op\":\"=\",\"value\":\"SysEventService\"}]}}",
139 },
140 QueryRule {
141 domain: "HIVIEWDFX",
142 event_list: vec!["PLUGIN_LOAD"],
143 condition: "",
144 },
145 ];
146 // step1: construct a querier.
147 let querier = Querier::new(
148 |records: &[HiSysEventRecord]| {
149 for item in records {
150 assert!(item.get_domain() == "HIVIEWDFX");
151 }
152 },
153 |reason: i32, total: i32| {
154 assert!(reason == SUCCEED);
155 assert!(total == QUERY_CNT);
156 },
157 )
158 .expect("Construct a querier by Querier::new");
159 // step2: query.
160 ret = hisysevent::query(&query_arg, &query_rules, &querier);
161 assert!(ret == SUCCEED);
162 // step3: recycle allocated memories of this Querier.
163 querier.try_to_recycle();
164 ret = hisysevent::query(&query_arg, &query_rules, &querier);
165 assert!(ret == LISTENER_NOT_EXIST);
166 }
167