1 /*
2 * Copyright (c) 2023 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 #include "c_mock_common.h"
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 static std::map<std::string, DlpCMockCondition> g_conditionList;
21
IsFuncNeedMock(const std::string & funcName)22 bool IsFuncNeedMock(const std::string& funcName)
23 {
24 if (g_conditionList.count(funcName) == 0) {
25 return false;
26 }
27 DlpCMockCondition& condition = g_conditionList[funcName];
28
29 if (condition.mockSequence.size() == 0 ||
30 condition.currentTimes > condition.mockSequence.size() - 1) {
31 condition.currentTimes++;
32 return false;
33 }
34
35 bool currentFail = condition.mockSequence[condition.currentTimes];
36 condition.currentTimes++;
37 return currentFail;
38 }
39
GetMockFunc(const std::string & funcName)40 CommonMockFuncT GetMockFunc(const std::string& funcName)
41 {
42 if (g_conditionList.count(funcName) == 0) {
43 return nullptr;
44 }
45 return g_conditionList[funcName].mockCallback;
46 }
47
SetMockConditions(const std::string & funcName,DlpCMockCondition & condition)48 void SetMockConditions(const std::string& funcName, DlpCMockCondition& condition)
49 {
50 condition.currentTimes = 0;
51 g_conditionList[funcName] = condition;
52 }
53
SetMockCallback(const std::string & funcName,CommonMockFuncT func)54 void SetMockCallback(const std::string& funcName, CommonMockFuncT func)
55 {
56 if (g_conditionList.count(funcName) == 0) {
57 return;
58 }
59
60 g_conditionList[funcName].mockCallback = func;
61 }
62
CleanMockConditions(void)63 void CleanMockConditions(void)
64 {
65 g_conditionList.clear();
66 }
67
GetMockConditionCounts(const std::string & funcName)68 uint32_t GetMockConditionCounts(const std::string& funcName)
69 {
70 if (g_conditionList.count(funcName) == 0) {
71 return 0;
72 }
73 return g_conditionList[funcName].currentTimes;
74 }
75 #ifdef __cplusplus
76 }
77 #endif
78