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 support for the job ID value, $next, in Jobs_Describe() #34

Merged
merged 3 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions source/include/jobs.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@
#define JOBS_API_UPDATE "update"
#define JOBS_API_UPDATE_LENGTH ( sizeof( JOBS_API_UPDATE ) - 1U )

#define JOBS_API_JOBID_NEXT "$next"
#define JOBS_API_JOBID_NEXT_LENGTH ( sizeof( JOBS_API_JOBID_NEXT ) - 1U )

#define JOBS_API_COMMON_LENGTH( thingNameLength ) \
( JOBS_API_PREFIX_LENGTH + ( thingNameLength ) + JOBS_API_BRIDGE_LENGTH )

Expand Down Expand Up @@ -302,6 +305,9 @@ JobsStatus_t Jobs_StartNext( char * buffer,
* the buffer up to one less than the buffer size. The topic is
* ended with a NUL character.
*
* @note A jobId consisting of the string, "$next", is supported to generate
* a topic string to request the next pending job.
*
* @note The thingName and jobId parameters do not need a NULL terminator.
*/
/* @[declare_jobs_describe] */
Expand Down
26 changes: 25 additions & 1 deletion source/jobs.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,29 @@ JobsStatus_t Jobs_StartNext( char * buffer,
return ret;
}

/**
* @brief Predicate returns true for a match to JOBS_API_JOBID_NEXT
*
* @param[in] jobId character sequence to check
* @param[in] jobIdLength length of the character sequence
*
* @return true if the job ID matches;
* false otherwise
*/
static bool_ isNextJobId( const char * jobId,
uint16_t jobIdLength )
{
bool_ ret = false;

if( ( jobId != NULL ) &&
( strnnEq( JOBS_API_JOBID_NEXT, JOBS_API_JOBID_NEXT_LENGTH, jobId, jobIdLength ) == JobsSuccess ) )
{
ret = true;
}

return ret;
}

/**
* See jobs.h for docs.
*
Expand All @@ -617,7 +640,8 @@ JobsStatus_t Jobs_Describe( char * buffer,
size_t start = 0U;

if( checkCommonParams() &&
( isValidJobId( jobId, jobIdLength ) == true ) )
( ( isNextJobId( jobId, jobIdLength ) == true ) ||
( isValidJobId( jobId, jobIdLength ) == true ) ) )
{
writePreamble( buffer, &start, length, thingName, thingNameLength );

Expand Down
1 change: 1 addition & 0 deletions test/cbmc/proofs/Jobs_Describe/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ EXCEED_JOBID_MAX_LENGTH=7
REMOVE_FUNCTION_BODY +=
# Use the largest of the above
UNWINDSET += isValidID.0:$(EXCEED_THINGNAME_MAX_LENGTH)
UNWINDSET += strnEq.0:$(EXCEED_JOBID_MAX_LENGTH)

# Use a stub in place of the original function.
PROOF_SOURCES += $(PROOF_STUB)/strnAppend.c
Expand Down
8 changes: 8 additions & 0 deletions test/unit-test/jobs_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ void test_Jobs_bad_parameters( void )
TEST_BAD_PARAMETER( Jobs_Describe( buf, sizeof( buf ), name_, ( THINGNAME_MAX_LENGTH + 1 ), jobId_, jobIdLength_, &outLength ) );
TEST_BAD_PARAMETER( Jobs_Update( buf, sizeof( buf ), name_, ( THINGNAME_MAX_LENGTH + 1 ), jobId_, jobIdLength_, &outLength ) );

/* job ID is NULL */
TEST_BAD_PARAMETER( Jobs_Describe( buf, sizeof( buf ), name_, nameLength_, NULL, jobIdLength_, &outLength ) );
TEST_BAD_PARAMETER( Jobs_Update( buf, sizeof( buf ), name_, nameLength_, NULL, jobIdLength_, &outLength ) );

/* bad job ID - variations tested separately */
TEST_BAD_PARAMETER( Jobs_Describe( buf, sizeof( buf ), name_, nameLength_, "!", jobIdLength_, &outLength ) );
TEST_BAD_PARAMETER( Jobs_Update( buf, sizeof( buf ), name_, nameLength_, "!", jobIdLength_, &outLength ) );
Expand Down Expand Up @@ -229,6 +233,10 @@ void test_Jobs_valid_identifiers( void )
TEST_ASSERT_EQUAL( JobsBadParameter, ret );
}
}

/* Test additional valid jobId, $next */
ret = Jobs_Describe( buf, sizeof( buf ), name_, nameLength_, JOBS_API_JOBID_NEXT, JOBS_API_JOBID_NEXT_LENGTH, &outLength );
TEST_ASSERT_EQUAL( JobsSuccess, ret );
}

/**
Expand Down