1 /*
2  * Copyright (c) 2024 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 #ifndef ASSERT_HELPER_H
17 #define ASSERT_HELPER_H
18 
19 #include "softbus_broadcast_adapter_type.h"
20 #include <securec.h>
21 #include <cstring>
22 #include "gmock/gmock.h"
23 
24 class RecordCtx {
25 public:
RecordCtx(const char * identifier)26     explicit RecordCtx(const char *identifier) : id(-1)
27     {
28         this->identifier = identifier;
29     }
30 
Update(int idParam)31     bool Update(int idParam)
32     {
33         this->id = idParam;
34         return true;
35     }
36 
Expect(int idParam)37     testing::AssertionResult Expect(int idParam)
38     {
39         testing::AssertionResult result = testing::AssertionSuccess();
40         if (this->id != idParam) {
41             result = testing::AssertionFailure() << identifier << " is call by unexpectedly id,"
42                                                  << "want: " << idParam << ", actual: " << this->id;
43             this->id = -1;
44         }
45         return result;
46     }
47 protected:
48     // static c string
49     const char *identifier;
50 private:
51     int id;
52 };
53 
54 class StRecordCtx : public RecordCtx {
55 public:
StRecordCtx(const char * identifier)56     explicit StRecordCtx(const char *identifier) : RecordCtx(identifier), st(-1) {}
57 
Update(int id,int stParam)58     bool Update(int id, int stParam)
59     {
60         if (!RecordCtx::Update(id)) {
61             return false;
62         }
63         this->st = stParam;
64         return true;
65     }
66 
Expect(int id,int stParam)67     testing::AssertionResult Expect(int id, int stParam)
68     {
69         auto result = RecordCtx::Expect(id);
70         if (!result) {
71             goto ClEANUP;
72         }
73         if (this->st != stParam) {
74             result = testing::AssertionFailure() << identifier << " is call by unexpectedly state,"
75                                                  << "want: " << stParam << ", actual: " << this->st;
76             goto ClEANUP;
77         }
78         result = testing::AssertionSuccess();
79     ClEANUP:
80         this->st = -1;
81         return result;
82     }
83 private:
84     int st;
85 };
86 
87 class BtAddrRecordCtx : public StRecordCtx {
88 public:
BtAddrRecordCtx(const char * identifier)89     explicit BtAddrRecordCtx(const char *identifier) : StRecordCtx(identifier)
90     {
91         Reset();
92     }
93 
94     bool Update(int id, const SoftbusMacAddr *addr, int st = 0)
95     {
96         if (!StRecordCtx::Update(id, st)) {
97             return false;
98         }
99         addrVal = *addr;
100         return true;
101     }
102 
103     testing::AssertionResult Expect(int id, SoftbusMacAddr *addrParam, int st = 0)
104     {
105         auto result = StRecordCtx::Expect(id, st);
106         if (!result) {
107             goto ClEANUP;
108         }
109         if (memcmp(addrParam->addr, addrVal.addr, SOFTBUS_ADDR_MAC_LEN) != 0) {
110             result = testing::AssertionFailure() << identifier << "is call by unexpectedly addr";
111             goto ClEANUP;
112         }
113         result = testing::AssertionSuccess();
114     ClEANUP:
115         Reset();
116         return result;
117     }
118 private:
119     SoftbusMacAddr addrVal;
Reset()120     void Reset()
121     {
122         memset_s(&addrVal, sizeof(SoftbusMacAddr), 0, sizeof(SoftbusMacAddr));
123     }
124 };
125 
126 class IntRecordCtx : public StRecordCtx {
127 public:
IntRecordCtx(const char * identifier)128     explicit IntRecordCtx(const char *identifier) : StRecordCtx(identifier), val(-1) {}
129 
Update(int id,int st,int valParam)130     bool Update(int id, int st, int valParam)
131     {
132         if (!StRecordCtx::Update(id, st)) {
133             return false;
134         }
135         this->val = valParam;
136         return true;
137     }
138 
Expect(int id,int st,int valParam)139     testing::AssertionResult Expect(int id, int st, int valParam)
140     {
141         auto result = StRecordCtx::Expect(id, st);
142         if (!result) {
143             goto ClEANUP;
144         }
145         if (this->val != valParam) {
146             result = testing::AssertionFailure() << identifier << " is call by unexpectedly int value,"
147                                                  << "want: " << valParam << ", actual: " << this->val;
148         } else {
149             result = testing::AssertionSuccess();
150         }
151     ClEANUP:
152         this->val = -1;
153         return result;
154     }
155 private:
156     int val;
157 };
158 
159 #endif