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