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