Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add System Object, Timer, and Packet Buffer Unit Tests #199

Merged
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,7 @@ src/setup_payload/tests/Makefile
src/inet/Makefile
src/inet/tests/Makefile
src/lib/Makefile
src/lib/support/Makefile
src/platform/Makefile
tests/Makefile
])
Expand Down
12 changes: 8 additions & 4 deletions src/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

include $(abs_top_nlbuild_autotools_dir)/automake/pre.am

SUBDIRS = \
support \
$(NULL)

# Pull in the sources that comprise the CHIP library.

include ../system/SystemLayer.am
Expand All @@ -31,6 +35,10 @@ include ../ble/BleLayer.am
include core/CoreLayer.am
include support/SupportLayer.am

EXTRA_DIST = \
$(CHIP_BUILD_CORE_LAYER_HEADER_FILES) \
$(CHIP_BUILD_SUPPORT_LAYER_HEADER_FILES)

lib_LIBRARIES = libCHIP.a

libCHIP_a_CPPFLAGS = \
Expand All @@ -54,8 +62,4 @@ if CONFIG_NETWORK_LAYER_BLE
libCHIP_a_SOURCES += $(CHIP_BUILD_BLE_LAYER_SOURCE_FILES)
endif # CONFIG_NETWORK_LAYER_BLE

EXTRA_DIST = \
$(CHIP_BUILD_CORE_LAYER_HEADER_FILES) \
$(CHIP_BUILD_SUPPORT_LAYER_HEADER_FILES)

include $(abs_top_nlbuild_autotools_dir)/automake/post.am
2 changes: 1 addition & 1 deletion src/lib/core/CHIPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#ifndef CHIP_CONFIG_H_
#define CHIP_CONFIG_H_

#include <SystemConfig.h>
#include <system/SystemConfig.h>

/* COMING SOON: making the INET Layer optional entails making this inclusion optional. */
//#include "InetConfig.h"
Expand Down
23 changes: 11 additions & 12 deletions src/lib/support/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ typedef uint8_t (*Base64CharToValFunct)(uint8_t c);
// Output buffer must be at least (inLen + 2) / 3 * 4 bytes long.
// Input and output buffers CANNOT overlap.
//
extern uint16_t Base64Encode(const uint8_t *in, uint16_t inLen, char *out);
extern uint16_t Base64URLEncode(const uint8_t *in, uint16_t inLen, char *out);
extern uint16_t Base64Encode(const uint8_t *in, uint16_t inLen, char *out, Base64ValToCharFunct valToCharFunct);
extern uint16_t Base64Encode(const uint8_t * in, uint16_t inLen, char * out);
extern uint16_t Base64URLEncode(const uint8_t * in, uint16_t inLen, char * out);
extern uint16_t Base64Encode(const uint8_t * in, uint16_t inLen, char * out, Base64ValToCharFunct valToCharFunct);

// Decode a base64 string to bytes.
//
Expand All @@ -50,18 +50,18 @@ extern uint16_t Base64Encode(const uint8_t *in, uint16_t inLen, char *out, Base6
// may be shorter than this due to padding.
// Supports decode in place by setting out pointer equal to in.
//
extern uint16_t Base64Decode(const char *in, uint16_t inLen, uint8_t *out);
extern uint16_t Base64URLDecode(const char *in, uint16_t inLen, uint8_t *out);
extern uint16_t Base64Decode(const char *in, uint16_t inLen, uint8_t *out, Base64CharToValFunct charToValFunct);
extern uint16_t Base64Decode(const char * in, uint16_t inLen, uint8_t * out);
extern uint16_t Base64URLDecode(const char * in, uint16_t inLen, uint8_t * out);
extern uint16_t Base64Decode(const char * in, uint16_t inLen, uint8_t * out, Base64CharToValFunct charToValFunct);

// Encode/decode functions that take/return 32-bit lengths.
//
// Similar to the above functions, except Base64Decode32() returns UINT32_MAX if the input cannot be decoded.
//
extern uint32_t Base64Encode32(const uint8_t *in, uint32_t inLen, char *out);
extern uint32_t Base64Encode32(const uint8_t *in, uint32_t inLen, char *out, Base64ValToCharFunct valToCharFunct);
extern uint32_t Base64Decode32(const char *in, uint32_t inLen, uint8_t *out);
extern uint32_t Base64Decode32(const char *in, uint32_t inLen, uint8_t *out, Base64CharToValFunct charToValFunct);
extern uint32_t Base64Encode32(const uint8_t * in, uint32_t inLen, char * out);
extern uint32_t Base64Encode32(const uint8_t * in, uint32_t inLen, char * out, Base64ValToCharFunct valToCharFunct);
extern uint32_t Base64Decode32(const char * in, uint32_t inLen, uint8_t * out);
extern uint32_t Base64Decode32(const char * in, uint32_t inLen, uint8_t * out, Base64CharToValFunct charToValFunct);

/** Computes the base-64 encoded length for a given input length.
*
Expand All @@ -75,8 +75,7 @@ extern uint32_t Base64Decode32(const char *in, uint32_t inLen, uint8_t *out, Bas
*
* NOTE: The actual decoded length may be smaller than this due to padding.
*/
#define BASE64_MAX_DECODED_LEN(LEN) ((LEN) * 3 / 4)

#define BASE64_MAX_DECODED_LEN(LEN) ((LEN) *3 / 4)

} // namespace chip

Expand Down
132 changes: 65 additions & 67 deletions src/lib/support/CHIPFaultInjection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,68 +47,68 @@ namespace FaultInjection {
*/
typedef enum
{
kFault_AllocExchangeContext, /**< Fail the allocation of an ExchangeContext */
kFault_DropIncomingUDPMsg, /**< Drop an incoming UDP message without any processing */
kFault_DropOutgoingUDPMsg, /**< Drop an outgoing UDP message at the chip Message layer */
kFault_AllocBinding, /**< Fail the allocation of a Binding */
kFault_SendAlarm, /**< Fail to send an alarm message */
kFault_HandleAlarm, /**< Fail to handle an alarm message */
kFault_FuzzExchangeHeaderTx, /**< Fuzz a chip Exchange Header after it has been encoded into the packet buffer;
when the fault is enabled, it expects an integer argument, which is an index into
a table of modifications that can be applied to the header. @see FuzzExchangeHeader */
kFault_AllocExchangeContext, /**< Fail the allocation of an ExchangeContext */
kFault_DropIncomingUDPMsg, /**< Drop an incoming UDP message without any processing */
kFault_DropOutgoingUDPMsg, /**< Drop an outgoing UDP message at the chip Message layer */
kFault_AllocBinding, /**< Fail the allocation of a Binding */
kFault_SendAlarm, /**< Fail to send an alarm message */
kFault_HandleAlarm, /**< Fail to handle an alarm message */
kFault_FuzzExchangeHeaderTx, /**< Fuzz a chip Exchange Header after it has been encoded into the packet buffer;
when the fault is enabled, it expects an integer argument, which is an index into
a table of modifications that can be applied to the header. @see FuzzExchangeHeader */
#if CHIP_CONFIG_ENABLE_RELIABLE_MESSAGING
kFault_WRMDoubleTx, /**< Force WRMP to transmit the outgoing message twice */
kFault_WRMSendError, /**< Fail a transmission in WRMP as if the max number of retransmission has been exceeded */
#endif // CHIP_CONFIG_ENABLE_RELIABLE_MESSAGING
kFault_BDXBadBlockCounter, /**< Corrupt the BDX Block Counter in the BDX BlockSend or BlockEOF message about to be sent */
kFault_BDXAllocTransfer, /**< Fail the allocation of a BDXTransfer object */
kFault_WRMDoubleTx, /**< Force WRMP to transmit the outgoing message twice */
kFault_WRMSendError, /**< Fail a transmission in WRMP as if the max number of retransmission has been exceeded */
#endif // CHIP_CONFIG_ENABLE_RELIABLE_MESSAGING
kFault_BDXBadBlockCounter, /**< Corrupt the BDX Block Counter in the BDX BlockSend or BlockEOF message about to be sent */
kFault_BDXAllocTransfer, /**< Fail the allocation of a BDXTransfer object */
#if CHIP_CONFIG_ENABLE_SERVICE_DIRECTORY
kFault_ServiceManager_ConnectRequestNew, /**< Fail the allocation of a chipServiceManager::ConnectRequest */
kFault_ServiceManager_Lookup, /**< Fail the lookup of an endpoint id */
kFault_ServiceDirectoryReplaceError, /**< Fail the replacement of a ServiceDirectory entry */
#endif // CHIP_CONFIG_ENABLE_SERVICE_DIRECTORY
kFault_WDM_TraitInstanceNew, /**< Fail the allocation of a WDM TraitInstanceInfo object */
kFault_WDM_SubscriptionHandlerNew, /**< Fail the allocation of a WDM SubscriptionHandler object */
kFault_WDM_SubscriptionClientNew, /**< Fail the allocation of a WDM SubscriptionClient object */
kFault_WDM_BadSubscriptionId, /**< Corrupt the SubscriptionId of an incoming notification */
kFault_WDM_SendUnsupportedReqMsgType, /**< Corrupt the message type of an outgoing SubscriptionRequest, so it is received as an unsupported
message by the responder */
kFault_WDM_NotificationSize, /**< Override the max payload size in a SubscriptionHandler; the size to be used can passed as
an argument to the fault */
kFault_WDM_SendCommandExpired, /**< Force the ExpiryTime of a WDM command to be in the past */
kFault_WDM_SendCommandBadVersion, /**< Alter the version of a WDM command being transmitted */
kFault_WDM_SendUpdateBadVersion, /**< Alter the version of a WDM update data element being transmitted */
kFault_WDM_DelayUpdateResponse, /**< Drop the message received after sending an UpdateRequest, which usually is the StatusReport;
this causes the NotificationRequest to be processed first */
kFault_WDM_UpdateRequestTimeout, /**< Inject an exchange timeout for the UpdateRequest */
kFault_WDM_UpdateRequestSendErrorInline, /**< Inject an inline Inet Send error for the UpdateRequest */
kFault_WDM_UpdateRequestSendErrorAsync, /**< Inject a WRM SendError for the UpdateRequest */
kFault_WDM_UpdateRequestBadProfile, /**< Inject an invalid Profile ID in the UpdateRequest */
kFault_WDM_UpdateRequestDropMessage, /**< Drop an outgoing WDM UpdateRequest message using the DropOutgoingUDPMsg fault */
kFault_WDM_UpdateResponseBusy, /**< Inject a status code busy in the StatusList */
kFault_WDM_PathStoreFull, /**< Inject a WDM_PATH_STORE_FULL error */
kFault_WDM_TreatNotifyAsCancel, /**< Process a Notify request as a CancelSubscription request */
kFault_CASEKeyConfirm, /**< Trigger a CHIP_ERROR_KEY_CONFIRMATION_FAILED error in chipCASEEngine */
kFault_SecMgrBusy, /**< Trigger a CHIP_ERROR_SECURITY_MANAGER_BUSY when starting an authentication session */
kFault_ServiceManager_ConnectRequestNew, /**< Fail the allocation of a chipServiceManager::ConnectRequest */
kFault_ServiceManager_Lookup, /**< Fail the lookup of an endpoint id */
kFault_ServiceDirectoryReplaceError, /**< Fail the replacement of a ServiceDirectory entry */
#endif // CHIP_CONFIG_ENABLE_SERVICE_DIRECTORY
kFault_WDM_TraitInstanceNew, /**< Fail the allocation of a WDM TraitInstanceInfo object */
kFault_WDM_SubscriptionHandlerNew, /**< Fail the allocation of a WDM SubscriptionHandler object */
kFault_WDM_SubscriptionClientNew, /**< Fail the allocation of a WDM SubscriptionClient object */
kFault_WDM_BadSubscriptionId, /**< Corrupt the SubscriptionId of an incoming notification */
kFault_WDM_SendUnsupportedReqMsgType, /**< Corrupt the message type of an outgoing SubscriptionRequest, so it is received as an
unsupported message by the responder */
kFault_WDM_NotificationSize, /**< Override the max payload size in a SubscriptionHandler; the size to be used can passed as
an argument to the fault */
kFault_WDM_SendCommandExpired, /**< Force the ExpiryTime of a WDM command to be in the past */
kFault_WDM_SendCommandBadVersion, /**< Alter the version of a WDM command being transmitted */
kFault_WDM_SendUpdateBadVersion, /**< Alter the version of a WDM update data element being transmitted */
kFault_WDM_DelayUpdateResponse, /**< Drop the message received after sending an UpdateRequest, which usually is the
StatusReport; this causes the NotificationRequest to be processed first */
kFault_WDM_UpdateRequestTimeout, /**< Inject an exchange timeout for the UpdateRequest */
kFault_WDM_UpdateRequestSendErrorInline, /**< Inject an inline Inet Send error for the UpdateRequest */
kFault_WDM_UpdateRequestSendErrorAsync, /**< Inject a WRM SendError for the UpdateRequest */
kFault_WDM_UpdateRequestBadProfile, /**< Inject an invalid Profile ID in the UpdateRequest */
kFault_WDM_UpdateRequestDropMessage, /**< Drop an outgoing WDM UpdateRequest message using the DropOutgoingUDPMsg fault */
kFault_WDM_UpdateResponseBusy, /**< Inject a status code busy in the StatusList */
kFault_WDM_PathStoreFull, /**< Inject a WDM_PATH_STORE_FULL error */
kFault_WDM_TreatNotifyAsCancel, /**< Process a Notify request as a CancelSubscription request */
kFault_CASEKeyConfirm, /**< Trigger a CHIP_ERROR_KEY_CONFIRMATION_FAILED error in chipCASEEngine */
kFault_SecMgrBusy, /**< Trigger a CHIP_ERROR_SECURITY_MANAGER_BUSY when starting an authentication session */
#if CHIP_CONFIG_ENABLE_TUNNELING
kFault_TunnelQueueFull, /**< Trigger a CHIP_ERROR_TUNNEL_SERVICE_QUEUE_FULL when enqueueing a packet in the Tunnel queue */
kFault_TunnelPacketDropByPolicy, /**< Trigger an explicit drop of the packet as if done by an application policy */
#endif // CHIP_CONFIG_ENABLE_TUNNELING
kFault_TunnelQueueFull, /**< Trigger a CHIP_ERROR_TUNNEL_SERVICE_QUEUE_FULL when enqueueing a packet in the Tunnel queue */
kFault_TunnelPacketDropByPolicy, /**< Trigger an explicit drop of the packet as if done by an application policy */
#endif // CHIP_CONFIG_ENABLE_TUNNELING
#if CONFIG_NETWORK_LAYER_BLE
kFault_CHIPOBLESend, /**< Inject a GATT error when sending the first fragment of a chip message over BLE */
#endif // CONFIG_NETWORK_LAYER_BLE
kFault_CHIPOBLESend, /**< Inject a GATT error when sending the first fragment of a chip message over BLE */
#endif // CONFIG_NETWORK_LAYER_BLE
kFault_NumItems,
} Id;

DLL_EXPORT nl::FaultInjection::Manager &GetManager(void);
DLL_EXPORT nl::FaultInjection::Manager & GetManager(void);

/**
* The number of ways in which chip Fault Injection fuzzers can
* alter a byte in a payload.
*/
#define CHIP_FAULT_INJECTION_NUM_FUZZ_VALUES 3

DLL_EXPORT void FuzzExchangeHeader(uint8_t *p, int32_t arg);
DLL_EXPORT void FuzzExchangeHeader(uint8_t * p, int32_t arg);

} // namespace FaultInjection
} // namespace chip
Expand All @@ -120,8 +120,7 @@ DLL_EXPORT void FuzzExchangeHeader(uint8_t *p, int32_t arg);
* @param[in] aFaultID A chip fault-injection id
* @param[in] aStatements Statements to be executed if the fault is enabled.
*/
#define CHIP_FAULT_INJECT( aFaultID, aStatements ) \
nlFAULT_INJECT(chip::FaultInjection::GetManager(), aFaultID, aStatements)
#define CHIP_FAULT_INJECT(aFaultID, aStatements) nlFAULT_INJECT(chip::FaultInjection::GetManager(), aFaultID, aStatements)

/**
* Execute the statements included if the chip fault is
Expand All @@ -136,16 +135,17 @@ DLL_EXPORT void FuzzExchangeHeader(uint8_t *p, int32_t arg);
* @param[in] aUnprotectedStatements Statements to be executed if the fault is enabled without holding the
* Manager's lock
*/
#define CHIP_FAULT_INJECT_MAX_ARG( aFaultID, aMaxArg, aProtectedStatements, aUnprotectedStatements ) \
do { \
FaultInjection::Manager &mgr = chip::FaultInjection::GetManager(); \
const FaultInjection::Record *records = mgr.GetFaultRecords(); \
if (records[aFaultID].mNumArguments == 0) \
{ \
int32_t arg = aMaxArg; \
mgr.StoreArgsAtFault(aFaultID, 1, &arg); \
} \
nlFAULT_INJECT_WITH_ARGS(mgr, aFaultID, aProtectedStatements, aUnprotectedStatements ); \
#define CHIP_FAULT_INJECT_MAX_ARG(aFaultID, aMaxArg, aProtectedStatements, aUnprotectedStatements) \
do \
{ \
FaultInjection::Manager & mgr = chip::FaultInjection::GetManager(); \
const FaultInjection::Record * records = mgr.GetFaultRecords(); \
if (records[aFaultID].mNumArguments == 0) \
{ \
int32_t arg = aMaxArg; \
mgr.StoreArgsAtFault(aFaultID, 1, &arg); \
} \
nlFAULT_INJECT_WITH_ARGS(mgr, aFaultID, aProtectedStatements, aUnprotectedStatements); \
} while (0)

/**
Expand All @@ -158,20 +158,18 @@ DLL_EXPORT void FuzzExchangeHeader(uint8_t *p, int32_t arg);
* @param[in] aUnprotectedStatements Statements to be executed if the fault is enabled without holding the
* Manager's lock
*/
#define CHIP_FAULT_INJECT_WITH_ARGS( aFaultID, aProtectedStatements, aUnprotectedStatements ) \
nlFAULT_INJECT_WITH_ARGS(chip::FaultInjection::GetManager(), aFaultID, \
aProtectedStatements, aUnprotectedStatements );
#define CHIP_FAULT_INJECT_WITH_ARGS(aFaultID, aProtectedStatements, aUnprotectedStatements) \
nlFAULT_INJECT_WITH_ARGS(chip::FaultInjection::GetManager(), aFaultID, aProtectedStatements, aUnprotectedStatements);

#define CHIP_FAULT_INJECTION_EXCH_HEADER_NUM_FIELDS 4
#define CHIP_FAULT_INJECTION_EXCH_HEADER_NUM_FIELDS_WRMP 5

#else // CHIP_CONFIG_TEST

#define CHIP_FAULT_INJECT( aFaultID, aStatements )
#define CHIP_FAULT_INJECT_WITH_ARGS( aFaultID, aProtectedStatements, aUnprotectedStatements )
#define CHIP_FAULT_INJECT_MAX_ARG( aFaultID, aMaxArg, aProtectedStatements, aUnprotectedStatements )
#define CHIP_FAULT_INJECT(aFaultID, aStatements)
#define CHIP_FAULT_INJECT_WITH_ARGS(aFaultID, aProtectedStatements, aUnprotectedStatements)
#define CHIP_FAULT_INJECT_MAX_ARG(aFaultID, aMaxArg, aProtectedStatements, aUnprotectedStatements)

#endif // CHIP_CONFIG_TEST


#endif // CHIP_FAULT_INJECTION_H_
Loading