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