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 #include <execinfo.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "functionalext.h"
19 
20 #define ZERO_LENGTH 0
21 #define INVALID_LENGTH (-1)
22 #define VALID_LENGTH 1
23 #define ZERO_RESULT 0
24 #define BT_BUF_SIZE 100
25 #define SUCCESS_RESULT 1
26 #define FAILURE_RESULT 0
27 
checkString(char **bt_strings, int bt_size)28 int checkString(char **bt_strings, int bt_size)
29 {
30     for (int i = 0; i < bt_size; ++i) {
31         char *str = bt_strings[i];
32         char *left = strstr(str, "<");
33         char *right = strstr(str, ">");
34         if (left == NULL || right == NULL || left >= right) {
35             continue;
36         }
37         int flag = 0;
38         for (; left < right; left++) {
39             if (*left == '?') {
40                 flag = 1;
41                 break;
42             }
43         }
44         if (!flag) {
45             continue;
46         }
47         right = strstr(str, "/");
48         char *lastPtr = str + strlen(str);
49         if (right == NULL) {
50             continue;
51         }
52         for (; right != lastPtr; ++right) {
53             if (*right != '?') {
54                 continue;
55             }
56         }
57         return FAILURE_RESULT;
58     }
59     return SUCCESS_RESULT;
60 }
61 
backtrace_1001null62 void backtrace_1001()
63 {
64     void *frames[10];
65     EXPECT_EQ("backtrace_1001", ZERO_RESULT, backtrace(frames, ZERO_LENGTH));
66 }
67 
backtrace_1002_supplementnull68 int backtrace_1002_supplement()
69 {
70     void *frames[10];
71     return backtrace(frames, VALID_LENGTH);
72 }
73 
backtrace_1002null74 void backtrace_1002()
75 {
76     EXPECT_LT("backtrace_1002", 0, backtrace_1002_supplement());
77 }
78 
backtrace_1003null79 void backtrace_1003()
80 {
81     void *bt_buffer[BT_BUF_SIZE];
82     int bt_size = backtrace(bt_buffer, BT_BUF_SIZE);
83     char **bt_strings = backtrace_symbols(bt_buffer, bt_size);
84     EXPECT_PTRNE("backtrace_1003", bt_strings, NULL);
85     EXPECT_TRUE("backtrace_1003_checkString", checkString(bt_strings, bt_size));
86     free(bt_strings);
87 }
88 
mainnull89 int main()
90 {
91     backtrace_1001();
92     backtrace_1002();
93     backtrace_1003();
94     return t_status;
95 }