• 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  */
15 
16 #include <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 
19 #include "args_filter.h"
20 #include "binder_filter.h"
21 #include "process_filter.h"
22 #include "slice_filter.h"
23 #include "stat_filter.h"
24 #include "trace_plugin_result.pb.h"
25 #include "ts_common.h"
26 
27 using namespace testing::ext;
28 using namespace SysTuning::TraceStreamer;
29 namespace SysTuning {
30 namespace TraceStreamer {
31 class BinderFilterTest : public ::testing::Test {
32 public:
SetUp()33     void SetUp()
34     {
35         streamFilters_.processFilter_ = std::make_unique<ProcessFilter>(&traceDataCache_, &streamFilters_);
36         streamFilters_.argsFilter_ = std::make_unique<ArgsFilter>(&traceDataCache_, &streamFilters_);
37         streamFilters_.binderFilter_ = std::make_unique<BinderFilter>(&traceDataCache_, &streamFilters_);
38         streamFilters_.statFilter_ = std::make_unique<StatFilter>(&traceDataCache_, &streamFilters_);
39         streamFilters_.sliceFilter_ = std::make_unique<SliceFilter>(&traceDataCache_, &streamFilters_);
40     }
41 
TearDown()42     void TearDown() {}
43 
44 public:
45     TraceStreamerFilters streamFilters_;
46     TraceDataCache traceDataCache_;
47 };
48 
49 /**
50  * @tc.name: BinderSenderfilterNeedReply
51  * @tc.desc: Binder event needs reply to
52  * @tc.type: FUNC
53  */
54 HWTEST_F(BinderFilterTest, BinderSenderfilterNeedReply, TestSize.Level1)
55 {
56     TS_LOGI("test1-1");
57     uint64_t ts1 = 100;
58     uint32_t tid1 = 1;
59     uint64_t transactionId1 = 1;
60     int32_t destNode1 = 1;
61     int32_t destTgid1 = 2;
62     int32_t destTid1 = 3;
63     bool isReply = false;
64     int32_t flags = 0x02; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_); 0x01
65     int32_t code = 0;     // not important
66     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1, destTgid1,
67                                                destTid1, isReply, flags, code); // start binder
68     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
69     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[0] == 0);
70     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 7);
71     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().TimeStamData()[0] == ts1);
72 }
73 
74 /**
75  * @tc.name: BinderSenderfilterNeedReplyAndReceive
76  * @tc.desc: Complete event, Binder event needs reply to
77  * @tc.type: FUNC
78  */
79 HWTEST_F(BinderFilterTest, BinderSenderfilterNeedReplyAndReceive, TestSize.Level1)
80 {
81     TS_LOGI("test1-2");
82     int64_t ts1 = 100;
83     uint32_t tid1 = 1;
84     uint64_t transactionId1 = 1;
85     int32_t destNode1 = 1;
86     int32_t destTgid1 = 2;
87     int32_t destTid1 = 3;
88     bool isReply = false;
89     int32_t flags = 0x02; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
90     int32_t code = 0;     // not importent
91     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1,
92                                                destTgid1, destTid1, isReply, flags, code); // start binder
93     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
94     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 7);
95     ts1 = 200;
96     uint32_t pid1 = 1;
97     streamFilters_.binderFilter_->ReceiveTraction(ts1, pid1, transactionId1); // receive binder
98     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 2);
99     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[0] == 0);
100     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[1] == 1);
101     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 11);
102     auto len = traceDataCache_.GetConstArgSetData().Size();
103     for (uint64_t i = 0; i < len; i++) {
104         if (traceDataCache_.GetConstArgSetData().names_[i] == streamFilters_.binderFilter_->isReplayId_) {
105             EXPECT_TRUE(traceDataCache_.GetConstArgSetData().values_[i] == static_cast<int64_t>(isReply));
106         } else if (traceDataCache_.GetConstArgSetData().names_[i] == streamFilters_.binderFilter_->destNodeId_) {
107             EXPECT_TRUE(traceDataCache_.GetConstArgSetData().values_[i] == static_cast<int64_t>(destNode1));
108         } else if (traceDataCache_.GetConstArgSetData().names_[i] == streamFilters_.binderFilter_->destThreadId_) {
109             EXPECT_TRUE(traceDataCache_.GetConstArgSetData().values_[i] == static_cast<int64_t>(pid1));
110         } else if (traceDataCache_.GetConstArgSetData().names_[i] == streamFilters_.binderFilter_->callingTid_) {
111             EXPECT_TRUE(traceDataCache_.GetConstArgSetData().values_[i] == static_cast<int64_t>(tid1));
112         } else if (traceDataCache_.GetConstArgSetData().names_[i] == streamFilters_.binderFilter_->transId_) {
113             EXPECT_TRUE(traceDataCache_.GetConstArgSetData().values_[i] == static_cast<int64_t>(transactionId1));
114         }
115     }
116 }
117 
118 /**
119  * @tc.name: BinderSenderfilterNeedReplyAndReceiveWithAlloc
120  * @tc.desc: The binder test that needs to be replied and there is an allock event
121  * @tc.type: FUNC
122  */
123 HWTEST_F(BinderFilterTest, BinderSenderfilterNeedReplyAndReceiveWithAlloc, TestSize.Level1)
124 {
125     TS_LOGI("test1-3");
126     int64_t ts1 = 100;
127     uint32_t tid1 = 1;
128     uint64_t transactionId1 = 1;
129     int32_t destNode1 = 1;
130     int32_t destTgid1 = 2;
131     int32_t destTid1 = 3;
132     bool isReply = false;
133     int32_t flags = 0x02; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
134     int32_t code = 0;     // not importent
135     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1, destTgid1,
136                                                destTid1, isReply, flags,
137                                                code); // start binder
138     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
139     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 7);
140 
141     ts1 = 150;
142     uint64_t dataSize = 100;
143     uint64_t offsetSize = 200;
144     streamFilters_.binderFilter_->TransactionAllocBuf(ts1, tid1, dataSize, offsetSize);
145     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 9);
146 
147     ts1 = 200;
148     uint32_t pid1 = 1;
149     streamFilters_.binderFilter_->ReceiveTraction(ts1, pid1, transactionId1); // receive binder
150     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 2);
151     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[0] == 0);
152     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[1] == 1);
153     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 13);
154 }
155 
156 /**
157  * @tc.name: BinderSenderfilterNeedReplyAndReceiveNotmatch
158  * @tc.desc: The binder test that needs to be replied but not match
159  * @tc.type: FUNC
160  */
161 HWTEST_F(BinderFilterTest, BinderSenderfilterNeedReplyAndReceiveNotmatch, TestSize.Level1)
162 {
163     TS_LOGI("test1-4");
164     int64_t ts1 = 100;
165     uint32_t tid1 = 1;
166     uint64_t transactionId1 = 1;
167     int32_t destNode1 = 1;
168     int32_t destTgid1 = 2;
169     int32_t destTid1 = 3;
170     bool isReply = false;
171     int32_t flags = 0x02; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
172     int32_t code = 0;     // not importent
173     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1,
174                                                destTgid1, destTid1, isReply, flags,
175                                                code); // start binder
176     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
177     ts1 = 200;
178     uint32_t pid1 = 1;
179     uint64_t transactionId2 = 2;
180     streamFilters_.binderFilter_->ReceiveTraction(ts1, pid1, transactionId2); // receive binder
181     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
182     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[0] == 0);
183 }
184 
185 /**
186  * @tc.name: BinderSenderfilterNoNeedReply
187  * @tc.desc: The binder test that donot needs to be replied
188  * @tc.type: FUNC
189  */
190 HWTEST_F(BinderFilterTest, BinderSenderfilterNoNeedReply, TestSize.Level1)
191 {
192     TS_LOGI("test1-5");
193     int64_t ts1 = 100;
194     uint32_t tid1 = 1;
195     uint64_t transactionId1 = 1;
196     int32_t destNode1 = 1;
197     int32_t destTgid1 = 2;
198     int32_t destTid1 = 3;
199     bool isReply = false;
200     int32_t flags = 0x01; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
201     int32_t code = 0;     // not importent
202     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1, destTgid1,
203                                                destTid1, isReply, flags, code); // start binder
204     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
205 }
206 
207 /**
208  * @tc.name: BinderSenderNoneedReplyAndReceivefilter
209  * @tc.desc: Complete event, The binder test that donot needs to be replied
210  * @tc.type: FUNC
211  */
212 HWTEST_F(BinderFilterTest, BinderSenderNoneedReplyAndReceivefilter, TestSize.Level1)
213 {
214     TS_LOGI("test1-6");
215     int64_t ts1 = 100;
216     uint32_t tid1 = 1;
217     uint64_t transactionId1 = 1;
218     int32_t destNode1 = 1;
219     int32_t destTgid1 = 2;
220     int32_t destTid1 = 3;
221     bool isReply = false;
222     int32_t flags = 0x01; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
223     int32_t code = 0;     // not importent
224     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1, destTgid1,
225                                                destTid1, isReply, flags,
226                                                code); // start binder
227     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
228 
229     ts1 = 200;
230     uint32_t pid1 = 1;
231     streamFilters_.binderFilter_->ReceiveTraction(ts1, pid1, transactionId1); // receive binder
232     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 2);
233     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[0] == 0);
234     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[1] == 0);
235 }
236 
237 /**
238  * @tc.name: BinderSenderNoneedReplyAndReceivefilterNotmatch
239  * @tc.desc: Not Match, The binder test that donot needs to be replied
240  * @tc.type: FUNC
241  */
242 HWTEST_F(BinderFilterTest, BinderSenderNoneedReplyAndReceivefilterNotmatch, TestSize.Level1)
243 {
244     TS_LOGI("test1-7");
245     int64_t ts1 = 100;
246     uint32_t tid1 = 1;
247     uint64_t transactionId1 = 1;
248     int32_t destNode1 = 1;
249     int32_t destTgid1 = 2;
250     int32_t destTid1 = 3;
251     bool isReply = false;
252     int32_t flags = 0x01; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
253     int32_t code = 0;     // not importent
254     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1, destTgid1,
255                                                destTid1, isReply, flags,
256                                                code); // start binder
257     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
258 
259     ts1 = 200;
260     uint32_t pid1 = 1;
261     uint64_t transactionId2 = 2;
262     streamFilters_.binderFilter_->ReceiveTraction(ts1, pid1, transactionId2); // receive binder
263     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 1);
264     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().ArgSetIdsData()[0] == 0);
265 }
266 
267 /**
268  * @tc.name: BinderSenderfilterWrongReply
269  * @tc.desc: The binder test with wrong replie
270  * @tc.type: FUNC
271  */
272 HWTEST_F(BinderFilterTest, BinderSenderfilterWrongReply, TestSize.Level1)
273 {
274     TS_LOGI("test1-8");
275     int64_t ts1 = 100;
276     uint32_t tid1 = 1;
277     uint64_t transactionId1 = 1;
278     int32_t destNode1 = 1;
279     int32_t destTgid1 = 2;
280     int32_t destTid1 = 3;
281     bool isReply = true;
282     int32_t flags = 0x01; // if need reply  bool needReply = !isReply && !(flags & noReturnMsgFlag_)
283     int32_t code = 0;     // not important
284     streamFilters_.binderFilter_->SendTraction(ts1, tid1, transactionId1, destNode1,
285                                                destTgid1, destTid1, isReply, flags,
286                                                code); // start binder
287     EXPECT_TRUE(traceDataCache_.GetConstInternalSlicesData().Size() == 0);
288     EXPECT_TRUE(traceDataCache_.GetConstArgSetData().Size() == 0);
289 }
290 } // namespace TraceStreamer
291 } // namespace SysTuning
292