3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * Implementation of the fault-injection utilities for CHIP.
23 #include "CHIPFaultInjection.h"
29 #if CHIP_CONFIG_TEST && CHIP_WITH_NLFAULTINJECTION
32 namespace FaultInjection {
34 static nl::FaultInjection::Record sFaultRecordArray[kFault_NumItems];
35 static int32_t sFault_CHIPNotificationSize_Arguments[1];
36 static int32_t sFault_FuzzExchangeHeader_Arguments[1];
37 static class nl::FaultInjection::Manager sChipFaultInMgr;
38 static const nl::FaultInjection::Name sManagerName = "chip";
39 static const nl::FaultInjection::Name sFaultNames[] = {
40 "AllocExchangeContext", "DropIncomingUDPMsg", "DropOutgoingUDPMsg", "AllocBinding", "SendAlarm",
41 "HandleAlarm", "FuzzExchangeHeaderTx", "RMPDoubleTx", "RMPSendError", "BDXBadBlockCounter",
42 "BDXAllocTransfer", "CASEKeyConfirm", "SecMgrBusy",
43 #if CONFIG_NETWORK_LAYER_BLE
45 #endif // CONFIG_NETWORK_LAYER_BLE
49 * Get the singleton FaultInjection::Manager for Inet faults
51 nl::FaultInjection::Manager & GetManager()
53 if (0 == sChipFaultInMgr.GetNumFaults())
55 sChipFaultInMgr.Init(kFault_NumItems, sFaultRecordArray, sManagerName, sFaultNames);
56 memset(&sFault_CHIPNotificationSize_Arguments, 0, sizeof(sFault_CHIPNotificationSize_Arguments));
57 memset(&sFault_FuzzExchangeHeader_Arguments, 0, sizeof(sFault_FuzzExchangeHeader_Arguments));
58 sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mArguments = sFault_FuzzExchangeHeader_Arguments;
59 sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mLengthOfArguments =
60 static_cast<uint8_t>(sizeof(sFault_FuzzExchangeHeader_Arguments) / sizeof(sFault_FuzzExchangeHeader_Arguments[0]));
62 return sChipFaultInMgr;
66 * Fuzz a byte of a CHIP Exchange Header
68 * @param[in] p Pointer to the encoded Exchange Header
69 * @param[in] arg An index from 0 to (CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES * 5 -1)
70 * that specifies the byte to be corrupted and the value to use.
72 DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg)
74 // CHIP is little endian; this function alters the
75 // least significant byte of the header fields.
76 const uint8_t offsets[] = {
77 0, // flags and version
83 const uint8_t values[CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES] = { 0x1, 0x2, 0xFF };
84 size_t offsetIndex = 0;
85 size_t valueIndex = 0;
86 size_t numOffsets = sizeof(offsets) / sizeof(offsets[0]);
87 offsetIndex = static_cast<uint32_t>(arg) % (numOffsets);
88 valueIndex = (static_cast<uint32_t>(arg) / numOffsets) % CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES;
89 p[offsetIndex] ^= values[valueIndex];
92 } // namespace FaultInjection
95 #endif // CHIP_CONFIG_TEST