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(int32_t idParam)36 bool Update(int32_t idParam) 37 { 38 this->id = idParam; 39 return true; 40 } 41 Expect(int32_t idParam)42 testing::AssertionResult Expect(int32_t 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 53 protected: 54 // static c string 55 const char *identifier; 56 57 private: 58 int32_t 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(int32_t id,int32_t stParam)65 bool Update(int32_t id, int32_t stParam) 66 { 67 if (!RecordCtx::Update(id)) { 68 return false; 69 } 70 this->st = stParam; 71 return true; 72 } 73 Expect(int32_t id,int32_t stParam)74 testing::AssertionResult Expect(int32_t id, int32_t 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 91 private: 92 int32_t st; 93 }; 94 95 class BtAddrRecordCtx : public StRecordCtx { 96 public: BtAddrRecordCtx(const char * identifier)97 explicit BtAddrRecordCtx(const char *identifier) : StRecordCtx(identifier) 98 { 99 Reset(); 100 } 101 102 bool Update(int32_t id, const SoftBusBtAddr *addr, int32_t st = 0) 103 { 104 if (!StRecordCtx::Update(id, st)) { 105 return false; 106 } 107 addrVal = *addr; 108 return true; 109 } 110 111 testing::AssertionResult Expect(int32_t id, SoftBusBtAddr *addrParam, int32_t st = 0) 112 { 113 auto result = StRecordCtx::Expect(id, st); 114 if (!result) { 115 goto ClEANUP; 116 } 117 if (memcmp(addrParam->addr, addrVal.addr, BT_ADDR_LEN) != 0) { 118 result = testing::AssertionFailure() << identifier << "is call by unexpectedly addr"; 119 goto ClEANUP; 120 } 121 result = testing::AssertionSuccess(); 122 ClEANUP: 123 Reset(); 124 return result; 125 } 126 127 private: 128 SoftBusBtAddr addrVal; Reset()129 void Reset() 130 { 131 memset_s(&addrVal, sizeof(SoftBusBtAddr), 0, sizeof(SoftBusBtAddr)); 132 } 133 }; 134 135 class IntRecordCtx : public StRecordCtx { 136 public: IntRecordCtx(const char * identifier)137 explicit IntRecordCtx(const char *identifier) : StRecordCtx(identifier), val(-1) { } 138 Update(int32_t id,int32_t st,int32_t valParam)139 bool Update(int32_t id, int32_t st, int32_t valParam) 140 { 141 if (!StRecordCtx::Update(id, st)) { 142 return false; 143 } 144 this->val = valParam; 145 return true; 146 } 147 Expect(int32_t id,int32_t st,int32_t valParam)148 testing::AssertionResult Expect(int32_t id, int32_t st, int32_t valParam) 149 { 150 auto result = StRecordCtx::Expect(id, st); 151 if (!result) { 152 goto ClEANUP; 153 } 154 if (this->val != valParam) { 155 result = testing::AssertionFailure() << identifier << " is call by unexpectedly int32_t value," 156 << "want: " << valParam << ", actual: " << this->val; 157 } else { 158 result = testing::AssertionSuccess(); 159 } 160 ClEANUP: 161 this->val = -1; 162 return result; 163 } 164 165 private: 166 int32_t val; 167 }; 168 169 #endif