Skip to content

Commit

Permalink
Merge pull request #45 from xmidt-org/add-str-to-enum
Browse files Browse the repository at this point in the history
Add a function that allows string to alg mapping.
  • Loading branch information
schmidtw authored Aug 29, 2021
2 parents dc99df2 + f592cdf commit 201411d
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 7 deletions.
19 changes: 18 additions & 1 deletion include/cjwt/cjwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef enum {
CJWTE_HEADER_UNSUPPORTED_UNKNOWN, /* 26 */
CJWTE_KEY_TOO_LARGE, /* 27 */
CJWTE_SIGNATURE_KEY_TOO_LARGE, /* 28 */
CJWTE_UNKNOWN_ALG, /* 29 */

CJWTE_LAST /* never use! */
} cjwt_code_t;
Expand All @@ -105,7 +106,7 @@ typedef enum {
alg_rs384,
alg_rs512,

num_algorithms /* never use! */
num_algorithms
} cjwt_alg_t;

typedef struct {
Expand Down Expand Up @@ -203,4 +204,20 @@ void cjwt_destroy( cjwt_t *jwt );
*/
void cjwt_print( FILE *stream, cjwt_t *jwt );


/**
* Provides a simple way to take a string and convert it to the alg.
*
* @param s the string to inspect
* @param len the length of the string (or SIZE_MAX if unknown length)
* @param alg the resulting alg enum val if found
*
* @return CJWTE_OK if found, reason for failure otherwise
*
* Failure reasons:
* CJWTE_INVALID_PARAMETERS
* CJWTE_UNKNOWN_ALG
*/
cjwt_code_t cjwt_alg_string_to_enum(const char *s, size_t len, cjwt_alg_t *alg);

#endif
11 changes: 11 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ test_args = ['-fprofile-arcs', '-ftest-coverage', '-O0']
dependencies: common_deps,
link_args: test_args))

test('test map',
executable('test_map',
['tests/test_map.c',
'src/cjwt.c',
'src/jws_evp_openssl.c',
'src/utils.c'],
include_directories: inc,
dependencies: common_deps,
link_args: test_args))


add_test_setup('valgrind',
is_default: true,
exe_wrapper: [ 'valgrind',
Expand Down
37 changes: 31 additions & 6 deletions src/cjwt.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,9 @@ const struct alg_map the_alg_map[] = {
/*----------------------------------------------------------------------------*/
/* none */

/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
/* none */

/*----------------------------------------------------------------------------*/
/* Internal functions */
/*----------------------------------------------------------------------------*/
/* none */

static int alg_to_enum( const char *alg_str, cjwt_alg_t *alg )
{
Expand Down Expand Up @@ -360,6 +354,11 @@ static cjwt_code_t verify_time_windows( const cjwt_t *jwt, uint32_t options,
}


/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/


/**
* validates jwt token and extracts data
*/
Expand Down Expand Up @@ -490,3 +489,29 @@ void cjwt_destroy( cjwt_t *jwt )
free( jwt );
}
}


cjwt_code_t cjwt_alg_string_to_enum(const char *s, size_t len, cjwt_alg_t *alg)
{
char buf[6];
int found = 0;

if(!s || !len || !alg) {
return CJWTE_INVALID_PARAMETERS;
}

if( SIZE_MAX == len ) {
len = strlen(s);
}

if( (4 != len) && (5 != len) ) {
return CJWTE_UNKNOWN_ALG;
}

memcpy(buf, s, len);
buf[len] = '\0';

found = alg_to_enum(buf, alg);

return (0 == found) ? CJWTE_OK : CJWTE_UNKNOWN_ALG;
}
106 changes: 106 additions & 0 deletions tests/test_map.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* SPDX-FileCopyrightText: 2021 Comcast Cable Communications Management, LLC */
/* SPDX-License-Identifier: Apache-2.0 */
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <CUnit/Basic.h>

#include "cjwt.h"

void test_map( void )
{
struct vector {
const char *s;
size_t len;
cjwt_alg_t expected;
cjwt_code_t rv;
} tests[] = {
{ "none", SIZE_MAX, alg_none, CJWTE_OK },
{ "ES256", SIZE_MAX, alg_es256, CJWTE_OK },
{ "ES384", SIZE_MAX, alg_es384, CJWTE_OK },
{ "ES512", SIZE_MAX, alg_es512, CJWTE_OK },
{ "HS256", SIZE_MAX, alg_hs256, CJWTE_OK },
{ "HS384", SIZE_MAX, alg_hs384, CJWTE_OK },
{ "HS512", SIZE_MAX, alg_hs512, CJWTE_OK },
{ "PS256", SIZE_MAX, alg_ps256, CJWTE_OK },
{ "PS384", SIZE_MAX, alg_ps384, CJWTE_OK },
{ "PS512", SIZE_MAX, alg_ps512, CJWTE_OK },
{ "RS256", SIZE_MAX, alg_rs256, CJWTE_OK },
{ "RS384", SIZE_MAX, alg_rs384, CJWTE_OK },
{ "RS512", SIZE_MAX, alg_rs512, CJWTE_OK },
{ "none_", 4, alg_none , CJWTE_OK },
{ "ES256_", 5, alg_es256, CJWTE_OK },
{ "ES384_", 5, alg_es384, CJWTE_OK },
{ "ES512_", 5, alg_es512, CJWTE_OK },
{ "HS256_", 5, alg_hs256, CJWTE_OK },
{ "HS384_", 5, alg_hs384, CJWTE_OK },
{ "HS512_", 5, alg_hs512, CJWTE_OK },
{ "PS256_", 5, alg_ps256, CJWTE_OK },
{ "PS384_", 5, alg_ps384, CJWTE_OK },
{ "PS512_", 5, alg_ps512, CJWTE_OK },
{ "RS256_", 5, alg_rs256, CJWTE_OK },
{ "RS384_", 5, alg_rs384, CJWTE_OK },
{ "RS512_", 5, alg_rs512, CJWTE_OK },
{ "RS512_", 0, alg_none, CJWTE_INVALID_PARAMETERS },
{ NULL, 0, alg_none, CJWTE_INVALID_PARAMETERS },
{ NULL, 5, alg_none, CJWTE_INVALID_PARAMETERS },
{ "none", 3, alg_none, CJWTE_UNKNOWN_ALG },
{ "toolong",7, alg_none, CJWTE_UNKNOWN_ALG },
{ "tree", 4, alg_none, CJWTE_UNKNOWN_ALG },
{ "trees", 5, alg_none, CJWTE_UNKNOWN_ALG },
};
cjwt_alg_t alg;

for( size_t i = 0; i < sizeof(tests)/sizeof(struct vector); i++ ) {
if( alg_none == tests[i].expected ) {
alg = alg_es256;
} else {
alg = alg_none;
}
CU_ASSERT(tests[i].rv == cjwt_alg_string_to_enum(tests[i].s, tests[i].len, &alg));
if( CJWTE_OK == tests[i].rv ) {
CU_ASSERT(tests[i].expected == alg);
}
}

/* If a NULL alg is passed, check for that. */
CU_ASSERT(CJWTE_INVALID_PARAMETERS == cjwt_alg_string_to_enum("none", 4, NULL));
}


void add_suites( CU_pSuite *suite )
{
*suite = CU_add_suite( "Print tests", NULL, NULL );
CU_add_test( *suite, "cjwt_alg_string_to_enum() Tests", test_map );
}


/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/
int main( void )
{
unsigned rv = 1;
CU_pSuite suite = NULL;

if( CUE_SUCCESS == CU_initialize_registry() ) {
add_suites( &suite );

if( NULL != suite ) {
CU_basic_set_mode( CU_BRM_VERBOSE );
CU_basic_run_tests();
printf( "\n" );
CU_basic_show_failures( CU_get_failure_list() );
printf( "\n\n" );
rv = CU_get_number_of_tests_failed();
}

CU_cleanup_registry();
}

if( 0 != rv ) {
return 1;
}
return 0;
}

0 comments on commit 201411d

Please sign in to comment.