• 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 #include <memory>
16 
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 
20 #include "buffer_splitter.h"
21 
22 using namespace testing::ext;
23 
24 using ConstCharPtr = std::unique_ptr<const char>::pointer;
25 
26 namespace {
27 class BufferSplitterUnittest : public ::testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase() {}
30 
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34 };
35 
36 struct TestElement {
37     int32_t index;
38     ConstCharPtr curLine;
39     size_t curLineSz;
40     ConstCharPtr curWord;
41     size_t curWordSz;
42 };
43 
44 /**
45  * @tc.name: memory plugin
46  * @tc.desc: Constructed function test.
47  * @tc.type: FUNC
48  */
49 HWTEST_F(BufferSplitterUnittest, constructorWithNullptr, TestSize.Level1)
50 {
51     char* text = nullptr;
52     int readsize = 0;
53     BufferSplitter totalbuffer(text, readsize);
54     EXPECT_EQ(nullptr, totalbuffer.CurLine());
55     EXPECT_EQ((size_t)0, totalbuffer.CurLineSize());
56     EXPECT_EQ(nullptr, totalbuffer.CurWord());
57     EXPECT_EQ((size_t)0, totalbuffer.CurWordSize());
58 }
59 
60 /**
61  * @tc.name: memory plugin
62  * @tc.desc: Constructed function test.
63  * @tc.type: FUNC
64  */
65 HWTEST_F(BufferSplitterUnittest, constructorWith1lengthBuffer01, TestSize.Level1)
66 {
67     char text[] = {'\0'};
68     int readsize = sizeof(text);
69     BufferSplitter totalbuffer(text, readsize);
70     EXPECT_EQ(nullptr, totalbuffer.CurLine());
71     EXPECT_EQ((size_t)0, totalbuffer.CurLineSize());
72     EXPECT_EQ(nullptr, totalbuffer.CurWord());
73     EXPECT_EQ((size_t)0, totalbuffer.CurWordSize());
74 }
75 
76 /**
77  * @tc.name: memory plugin
78  * @tc.desc: Constructed function test.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(BufferSplitterUnittest, constructorWith1lengthBuffer02, TestSize.Level1)
82 {
83     char text[] = {'\n'};
84     int readsize = sizeof(text);
85     BufferSplitter totalbuffer(text, readsize);
86     EXPECT_EQ(nullptr, totalbuffer.CurLine());
87     EXPECT_EQ((size_t)0, totalbuffer.CurLineSize());
88     EXPECT_EQ(nullptr, totalbuffer.CurWord());
89     EXPECT_EQ((size_t)0, totalbuffer.CurWordSize());
90 }
91 
92 /**
93  * @tc.name: memory plugin
94  * @tc.desc: Constructed function test.
95  * @tc.type: FUNC
96  */
97 HWTEST_F(BufferSplitterUnittest, constructorWith1lengthBuffer03, TestSize.Level1)
98 {
99     char text[] = {'3'};
100     int readsize = sizeof(text);
101     BufferSplitter totalbuffer(text, readsize);
102     EXPECT_EQ(nullptr, totalbuffer.CurLine());
103     EXPECT_EQ((size_t)0, totalbuffer.CurLineSize());
104     EXPECT_EQ(nullptr, totalbuffer.CurWord());
105     EXPECT_EQ((size_t)0, totalbuffer.CurWordSize());
106 }
107 
108 /**
109  * @tc.name: memory plugin
110  * @tc.desc: Constructed function test.
111  * @tc.type: FUNC
112  */
113 HWTEST_F(BufferSplitterUnittest, constructorWith1LineBuffer, TestSize.Level1)
114 {
115     char text[] = {'a', 'b', 'c', 'd', '\0'};
116     int readsize = sizeof(text);
117     BufferSplitter totalbuffer(text, readsize);
118 
119     TestElement expect = {0, text, 4, nullptr, 0};
120     EXPECT_EQ(expect.curLine, totalbuffer.CurLine());
121     EXPECT_EQ(expect.curLineSz, totalbuffer.CurLineSize());
122     EXPECT_EQ(expect.curWord, totalbuffer.CurWord());
123     EXPECT_EQ(expect.curWordSz, totalbuffer.CurWordSize());
124 }
125 
126 /**
127  * @tc.name: memory plugin
128  * @tc.desc: Constructed function test.
129  * @tc.type: FUNC
130  */
131 HWTEST_F(BufferSplitterUnittest, constructorWithMultipleLinesBuffer, TestSize.Level1)
132 {
133     char text[] = {'a', 'b', 'c', 'd', '\n', '1', '2', '3', '4', '5', '\0'};
134     TestElement expect = {0, text, 4, nullptr, 0};
135     int readsize = sizeof(text);
136     BufferSplitter totalbuffer(text, readsize);
137 
138     EXPECT_EQ(expect.curLine, totalbuffer.CurLine());
139     EXPECT_EQ(expect.curLineSz, totalbuffer.CurLineSize());
140     EXPECT_EQ(expect.curWord, totalbuffer.CurWord());
141     EXPECT_EQ(expect.curWordSz, totalbuffer.CurWordSize());
142 }
143 
144 /**
145  * @tc.name: memory plugin
146  * @tc.desc: Test NextLine().
147  * @tc.type: FUNC
148  */
149 HWTEST_F(BufferSplitterUnittest, NextLineWithOnlyOneLineBuffer, TestSize.Level1)
150 {
151     char text[] = {'a', 'b', 'c', 'd', '\0'};
152     TestElement expect = {0, text, 4, nullptr, 0};
153     int readsize = sizeof(text);
154     BufferSplitter totalbuffer(text, readsize);
155 
156     EXPECT_EQ(expect.curLine, totalbuffer.CurLine());
157     EXPECT_EQ(expect.curLineSz, totalbuffer.CurLineSize());
158     EXPECT_EQ(expect.curWord, totalbuffer.CurWord());
159     EXPECT_EQ(expect.curWordSz, totalbuffer.CurWordSize());
160 
161     EXPECT_FALSE(totalbuffer.NextLine());
162     EXPECT_EQ(nullptr, totalbuffer.CurLine());
163     EXPECT_EQ((size_t)0, totalbuffer.CurLineSize());
164 }
165 
166 /**
167  * @tc.name: memory plugin
168  * @tc.desc: Test NextLine().
169  * @tc.type: FUNC
170  */
171 HWTEST_F(BufferSplitterUnittest, NextLineWithMultipleLinesBuffer, TestSize.Level1)
172 {
173     char text[] = {'a', 'b', 'c', 'd', '\n', '1', '2', '3', '4', '5', '6', '\0'};
174     TestElement expect[] = {{0, text, 4, nullptr, 0}, {5, &text[5], 6, nullptr, 0}};
175     int readsize = sizeof(text);
176     BufferSplitter totalbuffer(text, readsize);
177 
178     size_t i = 0;
179     do {
180         EXPECT_EQ(expect[i].curLine, totalbuffer.CurLine());
181         EXPECT_EQ(expect[i].curLineSz, totalbuffer.CurLineSize());
182         EXPECT_EQ(expect[i].curWord, totalbuffer.CurWord());
183         EXPECT_EQ(expect[i].curWordSz, totalbuffer.CurWordSize());
184         i++;
185     } while (totalbuffer.NextLine());
186 }
187 
188 /**
189  * @tc.name: memory plugin
190  * @tc.desc: Test NextWord().
191  * @tc.type: FUNC
192  */
193 HWTEST_F(BufferSplitterUnittest, NextWordWithOnlyOneLineBuffer01, TestSize.Level1)
194 {
195     char text[] = {'a', 'b', ' ', 'c', 'd', '\0'};
196     TestElement expect = {0, text, 5, text, 2};
197     int readsize = sizeof(text);
198     BufferSplitter totalbuffer(text, readsize);
199 
200     EXPECT_EQ(expect.curLine, totalbuffer.CurLine());
201     EXPECT_EQ(expect.curLineSz, totalbuffer.CurLineSize());
202     EXPECT_TRUE(totalbuffer.NextWord(' '));
203     EXPECT_EQ(expect.curWord, totalbuffer.CurWord());
204     EXPECT_EQ(expect.curWordSz, totalbuffer.CurWordSize());
205 }
206 
207 /**
208  * @tc.name: memory plugin
209  * @tc.desc: Test NextWord().
210  * @tc.type: FUNC
211  */
212 HWTEST_F(BufferSplitterUnittest, NextWordWithOnlyOneLineBuffer02, TestSize.Level1)
213 {
214     char text[] = {'a', 'b', ' ', 'c', 'd', ' ', '\0'};
215     TestElement expect[] = {{0, text, 6, text, 2}, {0, text, 4, &text[3], 2}};
216     int readsize = sizeof(text);
217     BufferSplitter totalbuffer(text, readsize);
218 
219     EXPECT_EQ(expect[0].curLine, totalbuffer.CurLine());
220     EXPECT_EQ(expect[0].curLineSz, totalbuffer.CurLineSize());
221     size_t cnt = sizeof(expect) / sizeof(expect[0]);
222     for (size_t i = 0; i < cnt; i++) {
223         EXPECT_TRUE(totalbuffer.NextWord(' '));
224         EXPECT_EQ(expect[i].curWord, totalbuffer.CurWord());
225         EXPECT_EQ(expect[i].curWordSz, totalbuffer.CurWordSize());
226     }
227 }
228 
229 /**
230  * @tc.name: memory plugin
231  * @tc.desc: Test NextWord().
232  * @tc.type: FUNC
233  */
234 HWTEST_F(BufferSplitterUnittest, NextWordWithOnlyOneLineBuffer03, TestSize.Level1)
235 {
236     char text[] = {'a', 'b', ' ', 'c', 'd', ' ', '\0'};
237     TestElement expect = {0, text, 6, text, 6};
238     int readsize = sizeof(text);
239     BufferSplitter totalbuffer(text, readsize);
240 
241     EXPECT_EQ(expect.curLine, totalbuffer.CurLine());
242     EXPECT_EQ(expect.curLineSz, totalbuffer.CurLineSize());
243     EXPECT_TRUE(totalbuffer.NextWord('\0'));
244     EXPECT_EQ(expect.curWord, totalbuffer.CurWord());
245     EXPECT_EQ(expect.curWordSz, totalbuffer.CurWordSize());
246 }
247 
248 /**
249  * @tc.name: memory plugin
250  * @tc.desc: Test NextWord().
251  * @tc.type: FUNC
252  */
253 HWTEST_F(BufferSplitterUnittest, NextWordWithOnlyOneLineBuffer04, TestSize.Level1)
254 {
255     char text[] = {'a', 'b', ' ', 'c', 'd', ' ', '\0'};
256     TestElement expect = {0, text, 6, nullptr, 0};
257     int readsize = sizeof(text);
258     BufferSplitter totalbuffer(text, readsize);
259 
260     EXPECT_EQ(expect.curLine, totalbuffer.CurLine());
261     EXPECT_EQ(expect.curLineSz, totalbuffer.CurLineSize());
262     EXPECT_FALSE(totalbuffer.NextWord('e'));
263     EXPECT_EQ(expect.curWord, totalbuffer.CurWord());
264     EXPECT_EQ(expect.curWordSz, totalbuffer.CurWordSize());
265 }
266 
267 /**
268  * @tc.name: memory plugin
269  * @tc.desc: Test NextWord().
270  * @tc.type: FUNC
271  */
272 HWTEST_F(BufferSplitterUnittest, NextWordWithMultipleLinesBuffer01, TestSize.Level1)
273 {
274     char text[] = {'a', 'b', ' ', 'c', 'd', ' ', '\n', '1', '2', ' ', '3', '4', '5', ' ', '6', '\0'};
275     TestElement expect[] = {
276         {0, text, 6, text, 2}, {0, text, 6, &text[3], 2}, {0, text, 6, &text[7], 2}, {0, text, 4, &text[10], 3}};
277     int readsize = sizeof(text);
278     BufferSplitter totalbuffer(text, readsize);
279     size_t expectWordCnt = 2;
280     size_t curLineCnt = 0;
281     do {
282         for (size_t i = 0; i < expectWordCnt; i++) {
283             EXPECT_TRUE(totalbuffer.NextWord(' '));
284             EXPECT_EQ(expect[(curLineCnt * 2) + i].curWord, totalbuffer.CurWord());
285             EXPECT_EQ(expect[(curLineCnt * 2) + i].curWordSz, totalbuffer.CurWordSize());
286         }
287         curLineCnt++;
288     } while (totalbuffer.NextLine());
289 }
290 
291 /**
292  * @tc.name: memory plugin
293  * @tc.desc: Test NextWord().
294  * @tc.type: FUNC
295  */
296 HWTEST_F(BufferSplitterUnittest, NextWordWithMultipleLinesBuffer02, TestSize.Level1)
297 {
298     char text[] = {'a', 'b', ' ', 'c', 'd', ' ', '\n', '1', '2', ' ', '3', '4', '5', ' ', '6', '\0'};
299     TestElement expect[] = {
300         {0, text, 6, text, 2}, {0, text, 6, nullptr, 0}, {0, text, 6, &text[7], 2}, {0, text, 4, nullptr, 0}};
301     int readsize = sizeof(text);
302     BufferSplitter totalbuffer(text, readsize);
303     size_t expectWordCnt = 2;
304     size_t curLineCnt = 0;
305     do {
306         for (size_t i = 0; i < expectWordCnt; i++) {
307             if (i == 0) {
308                 EXPECT_TRUE(totalbuffer.NextWord(' '));
309             }
310             if (i == 1) {
311                 EXPECT_FALSE(totalbuffer.NextWord('#'));
312             }
313             EXPECT_EQ(expect[(curLineCnt * 2) + i].curWord, totalbuffer.CurWord());
314             EXPECT_EQ(expect[(curLineCnt * 2) + i].curWordSz, totalbuffer.CurWordSize());
315         }
316         curLineCnt++;
317     } while (totalbuffer.NextLine());
318 }
319 
320 /**
321  * @tc.name: memory plugin
322  * @tc.desc: Test NextWord().
323  * @tc.type: FUNC
324  */
325 HWTEST_F(BufferSplitterUnittest, NextWordWithMultipleLinesBuffer03, TestSize.Level1)
326 {
327     char text[] = {'a', 'b', ' ', 'c', 'd', ' ', '\n', '1', '2', ' ', '3', '4', '5', ' ', '6', '\0'};
328     TestElement expect[] = {
329         {0, text, 6, nullptr, 0}, {0, text, 6, text, 2}, {0, text, 6, nullptr, 0}, {0, text, 6, &text[7], 2}};
330     int readsize = sizeof(text);
331     BufferSplitter totalbuffer(text, readsize);
332     size_t expectWordCnt = 2;
333     size_t curLineCnt = 0;
334     do {
335         for (size_t i = 0; i < expectWordCnt; i++) {
336             if (i == 0) {
337                 EXPECT_FALSE(totalbuffer.NextWord('#'));
338             }
339             if (i == 1) {
340                 EXPECT_TRUE(totalbuffer.NextWord(' '));
341             }
342             EXPECT_EQ(expect[(curLineCnt * 2) + i].curWord, totalbuffer.CurWord());
343             EXPECT_EQ(expect[(curLineCnt * 2) + i].curWordSz, totalbuffer.CurWordSize());
344         }
345         curLineCnt++;
346     } while (totalbuffer.NextLine());
347 }
348 
349 /**
350  * @tc.name: memory plugin
351  * @tc.desc: Test the scene that actually used,parse /proc/meminfo.
352  * @tc.type: FUNC
353  */
354 namespace {
355 char g_kMockMeminfo[] = R"(
356 MemTotal:       16168736 kB
357 MemFree:         7154492 kB
358 MemAvailable:   15481028 kB
359 Buffers:         2397540 kB
360 Cached:          4711136 kB
361 SwapCached:        27628 kB
362 Active:          5556068 kB
363 Inactive:        1644560 kB
364 Active(anon):      62580 kB
365 Inactive(anon):    43352 kB
366 Active(file):    5493488 kB
367 Inactive(file):  1601208 kB
368 Unevictable:         388 kB
369 Mlocked:               0 kB
370 SwapTotal:      16777180 kB
371 SwapFree:       16500700 kB
372 Dirty:                 0 kB
373 Writeback:             0 kB
374 AnonPages:         87672 kB
375 Mapped:           116988 kB
376 Shmem:             13980 kB
377 KReclaimable:    1568904 kB
378 Slab:            1641176 kB
379 SReclaimable:    1568904 kB
380 SUnreclaim:        72272 kB
381 KernelStack:        7008 kB
382 PageTables:        28244 kB
383 NFS_Unstable:          0 kB
384 Bounce:                0 kB
385 WritebackTmp:          0 kB
386 CommitLimit:    24861548 kB
387 Committed_AS:    2569488 kB
388 VmallocTotal:   34359738367 kB
389 VmallocUsed:       34260 kB
390 VmallocChunk:          0 kB
391 Percpu:             2912 kB
392 HardwareCorrupted:     0 kB
393 AnonHugePages:         0 kB
394 ShmemHugePages:        0 kB
395 ShmemPmdMapped:        0 kB
396 FileHugePages:         0 kB
397 FilePmdMapped:         0 kB
398 CmaTotal:              0 kB
399 CmaFree:               0 kB
400 HugePages_Total:       0
401 HugePages_Free:        0
402 HugePages_Rsvd:        0
403 HugePages_Surp:        0
404 Hugepagesize:       2048 kB
405 Hugetlb:               0 kB
406 DirectMap4k:     1997488 kB
407 DirectMap2M:    14548992 kB
408 DirectMap1G:           0 kB)";
409 }
410 
411 HWTEST_F(BufferSplitterUnittest, MemInfoBufferTEST, TestSize.Level1)
412 {
413     int readsize = sizeof(g_kMockMeminfo);
414     BufferSplitter totalbuffer(g_kMockMeminfo, readsize);
415 
416     struct CStrCmp {
operator ()__anon0531e2cf0111::CStrCmp417         bool operator()(const char* a, const char* b) const
418         {
419             return strcmp(a, b) < 0;
420         }
421     };
422     std::map<const char*, int, CStrCmp> meminfo_counters_ = {{"MemTotal", 16168736},
423                                                              {"Active", 5556068},
424                                                              {"Inactive(anon)", 43352},
425                                                              {"HugePages_Total", 0},
426                                                              {"DirectMap1G", 0}};
427 
428     do {
429         if (!totalbuffer.NextWord(':')) {
430             continue;
431         }
432         const_cast<char *>(totalbuffer.CurWord())[totalbuffer.CurWordSize()] = '\0';
433         auto it = meminfo_counters_.find(totalbuffer.CurWord());
434         if (it == meminfo_counters_.end()) {
435             continue;
436         }
437 
438         int counterId = it->second;
439         if (!totalbuffer.NextWord(' ')) {
440             continue;
441         }
442         auto value = static_cast<uint64_t>(strtoll(totalbuffer.CurWord(), nullptr, 10));
443         EXPECT_EQ((uint64_t)counterId, value);
444     } while (totalbuffer.NextLine());
445 }
446 } // namespace
447