1 /*
2  * coreMQTT Agent v1.1.0
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /**
24  * @file agent_message_stubs.h
25  * @brief Stub functions to interact with queues.
26  */
27 
28 #include "core_mqtt_agent.h"
29 #include "agent_message_stubs.h"
30 
commandCompleteCallbackStub(void * pCmdCallbackContext,MQTTAgentReturnInfo_t * pReturnInfo)31 static void commandCompleteCallbackStub( void * pCmdCallbackContext,
32                                          MQTTAgentReturnInfo_t * pReturnInfo )
33 {
34     __CPROVER_assert( pReturnInfo != NULL,
35                       "Command complete return info is not NULL." );
36 }
37 
allocateCommand()38 static MQTTAgentCommand_t * allocateCommand()
39 {
40     MQTTAgentSubscribeArgs_t * pSubscribeArgs;
41     MQTTPublishInfo_t * pPublishInfo;
42     static bool terminate = false;
43 
44     MQTTAgentCommand_t * command = malloc( sizeof( MQTTAgentCommand_t ) );
45 
46     /* Second command always is TERMINATE to keep the MQTTAgent_CommandLoop unwind bound. */
47     if( terminate == true )
48     {
49         __CPROVER_assume( command != NULL );
50         __CPROVER_assume( command->commandType == TERMINATE );
51     }
52 
53     if( command != NULL )
54     {
55         __CPROVER_assume( command->commandType >= NONE && command->commandType < NUM_COMMANDS );
56 
57         if( ( command->commandType == SUBSCRIBE ) || ( command->commandType == UNSUBSCRIBE ) )
58         {
59             pSubscribeArgs = malloc( sizeof( MQTTAgentSubscribeArgs_t ) );
60             command->pArgs = ( void * ) pSubscribeArgs;
61         }
62         else if( command->commandType == PUBLISH )
63         {
64             pPublishInfo = malloc( sizeof( MQTTPublishInfo_t ) );
65             command->pArgs = ( void * ) pPublishInfo;
66         }
67         else
68         {
69             /* Empty else. */
70         }
71 
72         __CPROVER_assume( command->pCommandCompleteCallback == commandCompleteCallbackStub );
73     }
74 
75     terminate = true;
76     return command;
77 }
78 
AgentMessageSendStub(MQTTAgentMessageContext_t * pMsgCtx,const void * pData,uint32_t blockTimeMs)79 bool AgentMessageSendStub( MQTTAgentMessageContext_t * pMsgCtx,
80                            const void * pData,
81                            uint32_t blockTimeMs )
82 {
83     /* For the proofs, returning a non deterministic boolean value
84      * will be good enough. */
85     return nondet_bool();
86 }
87 
AgentMessageRecvStub(MQTTAgentMessageContext_t * pMsgCtx,void * pBuffer,uint32_t blockTimeMs)88 bool AgentMessageRecvStub( MQTTAgentMessageContext_t * pMsgCtx,
89                            void * pBuffer,
90                            uint32_t blockTimeMs )
91 {
92     MQTTAgentCommand_t * command;
93     bool returnStatus;
94 
95     __CPROVER_assert( pBuffer != NULL,
96                       "Command buffer is not NULL." );
97 
98     command = allocateCommand();
99 
100     if( ( command != NULL ) && ( command->commandType == TERMINATE ) )
101     {
102         returnStatus = false;
103     }
104 
105     *( ( MQTTAgentCommand_t ** ) pBuffer ) = command;
106 
107     return returnStatus;
108 }
109