Matter SDK Coverage Report
Current view: top level - lib/support - CHIPFaultInjection.cpp (source / functions) Coverage Total Hit
Test: SHA:09f6fdf93a7e847a42518c076e487f336877a722 Lines: 33.3 % 18 6
Test Date: 2025-06-07 07:10:33 Functions: 33.3 % 3 1

            Line data    Source code
       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 <string.h>
      26              : 
      27              : namespace chip {
      28              : namespace FaultInjection {
      29              : 
      30              : static nl::FaultInjection::Record sFaultRecordArray[kFault_NumItems];
      31              : static int32_t sFault_CHIPNotificationSize_Arguments[1];
      32              : static int32_t sFault_FuzzExchangeHeader_Arguments[1];
      33              : static class nl::FaultInjection::Manager sChipFaultInMgr;
      34              : static const nl::FaultInjection::Name sManagerName = "chip";
      35              : 
      36              : /*
      37              :  * Array of strings containing the names for each Fault as defined in the CHIP_FAULTS_ENUMERATE(X) Macro in the Header file
      38              :  */
      39              : static const nl::FaultInjection::Name sFaultNames[kFault_NumItems] = {
      40              : #define _CHIP_FAULT_NAMES_STRING(FAULT, ...) #FAULT,
      41              :     CHIP_FAULTS_ENUMERATE(_CHIP_FAULT_NAMES_STRING) //
      42              : };
      43              : 
      44              : static_assert(kFault_NumItems == sizeof(sFaultNames) / sizeof(sFaultNames[0]),
      45              :               "The last member of the Id enum (kFault_NumItems) should equal the length of sFaultNames[] ");
      46              : 
      47              : /**
      48              :  * Get the singleton FaultInjection::Manager for CHIP faults
      49              :  */
      50          235 : nl::FaultInjection::Manager & GetManager()
      51              : {
      52          235 :     if (0 == sChipFaultInMgr.GetNumFaults())
      53              :     {
      54            7 :         sChipFaultInMgr.Init(kFault_NumItems, sFaultRecordArray, sManagerName, sFaultNames);
      55              :         memset(&sFault_CHIPNotificationSize_Arguments, 0, sizeof(sFault_CHIPNotificationSize_Arguments));
      56              :         memset(&sFault_FuzzExchangeHeader_Arguments, 0, sizeof(sFault_FuzzExchangeHeader_Arguments));
      57            7 :         sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mArguments = sFault_FuzzExchangeHeader_Arguments;
      58            7 :         sFaultRecordArray[kFault_FuzzExchangeHeaderTx].mLengthOfArguments =
      59              :             static_cast<uint8_t>(sizeof(sFault_FuzzExchangeHeader_Arguments) / sizeof(sFault_FuzzExchangeHeader_Arguments[0]));
      60              :     }
      61          235 :     return sChipFaultInMgr;
      62              : }
      63              : 
      64              : /**
      65              :  * Get the number of times the fault injection point location got checked. This is useful for verifying that the code path
      66              :  * containing the fault injection was actually executed.
      67              :  * Note: The count includes all checks, even if the fault was not triggered.
      68              :  */
      69            0 : uint32_t GetFaultCounter(uint32_t faultID)
      70              : {
      71            0 :     return GetManager().GetFaultRecords()[faultID].mNumTimesChecked;
      72              : }
      73              : 
      74              : /**
      75              :  * Fuzz a byte of a CHIP Exchange Header
      76              :  *
      77              :  * @param[in] p     Pointer to the encoded Exchange Header
      78              :  * @param[in] arg   An index from 0 to (CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES * 5 -1)
      79              :  *                  that specifies the byte to be corrupted and the value to use.
      80              :  */
      81            0 : DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg)
      82              : {
      83              :     // CHIP is little endian; this function alters the
      84              :     // least significant byte of the header fields.
      85            0 :     const uint8_t offsets[] = {
      86              :         0, // flags and version
      87              :         1, // MessageType
      88              :         2, // ExchangeId
      89              :         4, // ProfileId
      90              :         8  // AckMessageCounter
      91              :     };
      92            0 :     const uint8_t values[CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES] = { 0x1, 0x2, 0xFF };
      93            0 :     size_t offsetIndex                                         = 0;
      94            0 :     size_t valueIndex                                          = 0;
      95            0 :     size_t numOffsets                                          = sizeof(offsets) / sizeof(offsets[0]);
      96            0 :     offsetIndex                                                = static_cast<uint32_t>(arg) % (numOffsets);
      97            0 :     valueIndex = (static_cast<uint32_t>(arg) / numOffsets) % CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES;
      98            0 :     p[offsetIndex] ^= values[valueIndex];
      99            0 : }
     100              : 
     101              : } // namespace FaultInjection
     102              : } // namespace chip
        

Generated by: LCOV version 2.0-1