Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / lib / support / CHIPFaultInjection.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 /**
20  *    @file
21  *      Implementation of the fault-injection utilities for CHIP.
22  */
23 #include "CHIPFaultInjection.h"
24
25 #include <nlassert.h>
26
27 #include <string.h>
28
29 #if CHIP_CONFIG_TEST && CHIP_WITH_NLFAULTINJECTION
30
31 namespace chip {
32 namespace FaultInjection {
33
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
44     "CHIPOBLESend",
45 #endif // CONFIG_NETWORK_LAYER_BLE
46 };
47
48 /**
49  * Get the singleton FaultInjection::Manager for Inet faults
50  */
51 nl::FaultInjection::Manager & GetManager()
52 {
53     if (0 == sChipFaultInMgr.GetNumFaults())
54     {
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]));
61     }
62     return sChipFaultInMgr;
63 }
64
65 /**
66  * Fuzz a byte of a CHIP Exchange Header
67  *
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.
71  */
72 DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg)
73 {
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
78         1, // MessageType
79         2, // ExchangeId
80         4, // ProfileId
81         8  // AckMsgId
82     };
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];
90 }
91
92 } // namespace FaultInjection
93 } // namespace chip
94
95 #endif // CHIP_CONFIG_TEST