static char *test_jwt_get_string(apr_pool_t *pool) { //apr_jwt_get_string const char *s = "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9" ".eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ" ".dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"; apr_jwt_t *jwt = NULL; apr_jwt_error_t err; TST_ASSERT_ERR("apr_jwt_parse", apr_jwt_parse(pool, s, &jwt, NULL, &err), pool, err); char *dst; dst = NULL; TST_ASSERT("apr_jwt_get_string (1a)", apr_jwt_get_string(pool, jwt->header.value.json, "typ", TRUE, &dst, &err)); TST_ASSERT_STR("apr_jwt_get_string (1b)", dst, "JWT"); dst = NULL; TST_ASSERT("apr_jwt_get_string (2a)", apr_jwt_get_string(pool, jwt->header.value.json, "alg", TRUE, &dst, &err)); TST_ASSERT_STR("apr_jwt_get_string (2b)", dst, "HS256"); dst = NULL; TST_ASSERT("apr_jwt_get_string (3a)", apr_jwt_get_string(pool, jwt->header.value.json, "dummy", FALSE, &dst, &err)); TST_ASSERT_STR("apr_jwt_get_string (3b)", dst, NULL); apr_jwt_destroy(jwt); return 0; }
int LER_LerParmChar( char * Parm ) { tpElemSimbolo * pElem = NULL ; #ifdef _DEBUG TST_ASSERT( Inicializado ) ; TST_ASSERT( Parm != NULL ) ; #endif PularBrancos( ) ; /* Tratar parâmetro char simbólico */ if ( isalpha( LER_Buffer[ LER_InxBuffer ] )) { pElem = ObterDado( ID_TIPO_CHAR ) ; if ( pElem == NULL ) { *Parm = 0 ; return FALSE ; } /* if */ * Parm = pElem->ValorTipado.ValorChar ; return TRUE ; } /* fim ativa: Tratar parâmetro char simbólico */ /* Tratar parâmetro char literal */ if ( LER_Buffer[ LER_InxBuffer ] != '\'' ) { * Parm = 0 ; ExibirErro( "Faltou aspas antes de literal char." ) ; return FALSE ; } /* if */ LER_InxBuffer ++ ; if ( LER_Buffer[ LER_InxBuffer ] == '\\' ) { LER_InxBuffer ++ ; *Parm = LerCharCntrl( ) ; } else { *Parm = LER_Buffer[ LER_InxBuffer ] ; LER_InxBuffer ++ ; } /* if */ if ( LER_Buffer[ LER_InxBuffer ] != '\'' ) { ExibirErro( "Faltou aspas após literal char." ) ; return FALSE ; } /* if */ LER_InxBuffer ++ ; return TRUE ; } /* Fim função: LER &Ler parâmetro caractere */
TBS_tpCondRet TBS_ExcluirSimbolo( TBS_tppTabela pTabela , char * pSimbolo ) { unsigned inxHash ; tpLista * pElem ; #ifdef _DEBUG TST_ASSERT( pTabela != NULL ) ; TST_ASSERT( pSimbolo != NULL ) ; #endif pElem = ProcurarSimbolo( pTabela , pSimbolo , &inxHash ) ; if ( pElem == NULL ) { return TBS_CondRetSimboloNaoExiste ; } /* if */ if ( pElem->pProx != NULL ) { pElem->pProx->pAnt = pElem->pAnt ; } /* if */ if ( pElem->pAnt != NULL ) { pElem->pAnt->pProx = pElem->pProx ; } else { pTabela->pVtHash[ inxHash ] = pElem->pProx ; } /* if */ LiberarElemento( pTabela , pElem ) ; return TBS_CondRetOK ; } /* Fim função: TBS &Excluir símbolo */
static char * test_jwt_parse(apr_pool_t *pool) { // from http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-20 // 3.1. Example JWT char *s = apr_pstrdup(pool, "eyJ0eXAiOiJKV1QiLA0KICJhbGciOiJIUzI1NiJ9" \ ".eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ" \ ".dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"); apr_jwt_t *jwt = NULL; TST_ASSERT("apr_jwt_parse", apr_jwt_parse(pool, s, &jwt, NULL, NULL)); TST_ASSERT_STR("header.alg", jwt->header.alg, "HS256"); TST_ASSERT_STR("header.enc", jwt->header.enc, NULL); TST_ASSERT_STR("header.kid", jwt->header.kid, NULL); TST_ASSERT_STR("payload.iss", jwt->payload.iss, "joe"); TST_ASSERT_LONG("payload.exp", (long)apr_time_sec(jwt->payload.exp), 1300819380L); char *str_key = "AyM1SysPpbyDfgZld3umj1qzKObwVMkoqQ-EstJQLr_T-1qS0gZH75aKtMN3Yj0iPS4hcgUuTwjAzZr1Z9CAow"; char *raw_key = NULL; int raw_key_len = apr_jwt_base64url_decode(pool, &raw_key, str_key, 1); TST_ASSERT("apr_jws_verify_hmac", apr_jws_verify_hmac(pool, jwt, raw_key, raw_key_len)); s[5] = '.'; TST_ASSERT("corrupted header (1) apr_jwt_parse", apr_jwt_parse(pool, s, &jwt, NULL, NULL) == FALSE); s[0] = '\0'; TST_ASSERT("corrupted header (2) apr_jwt_parse", apr_jwt_parse(pool, s, &jwt, NULL, NULL) == FALSE); return 0; }
int LER_LerParmDouble( double * Parm ) { tpElemSimbolo * pElem = NULL ; int inxOrg ; double ValDouble ; #ifdef _DEBUG TST_ASSERT( Inicializado ) ; TST_ASSERT( Parm != NULL ) ; #endif PularBrancos( ) ; /* Tratar parâmetro double simbólico */ if ( isalpha( LER_Buffer[ LER_InxBuffer ] )) { pElem = ObterDado( ID_TIPO_DOUBLE ) ; if ( pElem == NULL ) { *Parm = 0. ; return FALSE ; } /* if */ * Parm = pElem->ValorTipado.ValorDouble ; return TRUE ; } /* fim ativa: Tratar parâmetro double simbólico */ /* Tratar parâmetro double literal */ inxOrg = LER_InxBuffer ; while ( LER_InxBuffer < LER_TamBuffer ) { if ( EhBranco( LER_Buffer[ LER_InxBuffer ] )) { break ; } /* if */ LER_InxBuffer ++ ; } /* while */ if ( sscanf( LER_Buffer + inxOrg , "%lf" , &ValDouble ) != 1 ) { ExibirErro( "Literal double errado." ) ; *Parm = 0. ; return FALSE ; } /* if */ *Parm = ValDouble ; return TRUE ; } /* Fim função: LER &Ler parâmetro flutuante */
static char *test_jwt_array_has_string(apr_pool_t *pool) { apr_array_header_t *haystack = apr_array_make(pool, 3, sizeof(const char*)); *(const char**) apr_array_push(haystack) = "a"; *(const char**) apr_array_push(haystack) = "b"; *(const char**) apr_array_push(haystack) = "c"; TST_ASSERT("jwt_array_has_string (1)", apr_jwt_array_has_string(haystack, "a")); TST_ASSERT("jwt_array_has_string (2)", apr_jwt_array_has_string(haystack, "d") == FALSE); return 0; }
void LER_ExibirParametro( char * Simbolo ) { tpElemSimbolo * pElem ; #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif pElem = ( tpElemSimbolo * ) TBS_ProcurarSimbolo( pTabela , Simbolo ) ; if ( pElem == NULL ) { TST_ExibirPrefixo( INFO_LER , "Parâmetro não definido: " ) ; fprintf( TST_ObterArqLog( ) , "%s" , Simbolo ) ; return ; } /* if */ TST_ExibirPrefixo( INFO_LER , "Parâmetro: " ) ; fprintf( TST_ObterArqLog( ) , "%s" , Simbolo ) ; TST_ExibirEspacoHexa( sizeof( tpElemSimbolo ) , pElem ) ; if ( ( pElem->idTipo == ID_TIPO_STRING ) || ( pElem->idTipo == ID_TIPO_NOME )) { TST_ExibirEspacoHexa( strlen( pElem->ValorTipado.ValorString ) + 1 , pElem->ValorTipado.ValorString ) ; } /* if */ } /* Fim função: LER &Exibir declaração de símbolo */
static char *test_jwt_url_encode_decode(apr_pool_t *pool) { char *dst = NULL; char *src = "abcd"; TST_ASSERT("apr_jwt_base64url_encode (1)", apr_jwt_base64url_encode(pool, &dst, src, strlen(src), 0)); TST_ASSERT_STR("apr_jwt_base64url_encode (2)", dst, "YWJjZA"); src = dst; TST_ASSERT("apr_jwt_base64url_decode (1)", apr_jwt_base64url_decode(pool, &dst, src, 1)); TST_ASSERT_STR("apr_jwt_base64url_decode (2)", dst, "abcd"); return 0; }
LER_tpCondRet LER_AbrirArquivoScript( char * NomeArqParm ) { #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif strcpy( NomeArqScript , NomeArqParm ) ; if ( TST_ObterInicioExtensao( NomeArqScript ) == NULL ) { strcat( NomeArqScript , DEFAULT_EXT_SCRIPT ) ; } /* if */ pArqScript = fopen( NomeArqScript , "rb" ) ; if ( pArqScript != NULL ) { printf( "\n Arquivo de teste: %s\n" , NomeArqParm ) ; AcabouScript = 0 ; return LER_CondRetOK ; } /* if */ NomeArqScript[ 0 ] = 0 ; return LER_CondRetNaoAbriu ; } /* Fim função: LER &Abrir arquivo script de teste */
static char *test_proto_validate_code(request_rec *r) { // from http://openid.net/specs/openid-connect-core-1_0.html#code-id_tokenExample // A.4 Example using response_type=code id_token const char *s = "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogIml" "zcyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ" "4Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiA" "ibi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDE" "zMTEyODA5NzAsCiAiY19oYXNoIjogIkxEa3RLZG9RYWszUGswY25YeENsdEE" "iCn0.XW6uhdrkBgcGx6zVIrCiROpWURs-4goO1sKA4m9jhJIImiGg5muPUcN" "egx6sSv43c5DSn37sxCRrDZZm4ZPBKKgtYASMcE20SDgvYJdJS0cyuFw7Ijp" "_7WnIjcrl6B5cmoM6ylCvsLMwkoQAxVublMwH10oAxjzD6NEFsu9nipkszWh" "sPePf_rM4eMpkmCbTzume-fzZIi5VjdWGGEmzTg32h3jiex-r5WTHbj-u5HL" "7u_KP3rmbdYNzlzd1xWRYTUs4E8nOTgzAUwvwXkIQhOh5TPcSMBYy6X3E7-_" "gr9Ue6n4ND7hTFhtjYs3cjNKIA08qm5cpVYFMFMG6PkhzLQ"; apr_jwt_error_t err; apr_jwt_t *jwt = NULL; TST_ASSERT_ERR("apr_jwt_parse", apr_jwt_parse(r->pool, s, &jwt, NULL, &err), r->pool, err); const char *code = "Qcb0Orv1zh30vL1MPRsbm-diHiMwcLyZvn1arpZv-Jxf_11jnpEX3Tgfvk"; TST_ASSERT("oidc_proto_validate_code", oidc_proto_validate_code(r, NULL, jwt, "code id_token", code)); apr_jwt_destroy(jwt); return 0; }
static char * test_proto_authorization_request(request_rec *r) { oidc_provider_t provider; provider.issuer = "https://idp.example.com"; provider.authorization_endpoint_url = "https://idp.example.com/authorize"; provider.scope = "openid"; provider.client_id = "client_id"; provider.response_type = "code"; provider.auth_request_params = NULL; const char *redirect_uri = "https://www.example.com/protected/"; const char *state = "12345"; json_t * proto_state = json_object(); json_object_set_new(proto_state, "nonce", json_string("anonce")); json_object_set_new(proto_state, "original_url", json_string("https://localhost/protected/index.php")); json_object_set_new(proto_state, "original_method", json_string("get")); json_object_set_new(proto_state, "issuer", json_string(provider.issuer)); json_object_set_new(proto_state, "response_type", json_string(provider.response_type)); json_object_set_new(proto_state, "timestamp", json_integer(apr_time_sec(apr_time_now()))); TST_ASSERT("oidc_proto_authorization_request (1)", oidc_proto_authorization_request(r, &provider, NULL, redirect_uri, state, proto_state, NULL, NULL) == HTTP_MOVED_TEMPORARILY); TST_ASSERT_STR("oidc_proto_authorization_request (2)", apr_table_get(r->headers_out, "Location"), "https://idp.example.com/authorize?response_type=code&scope=openid&client_id=client_id&state=12345&redirect_uri=https%3A%2F%2Fwww.example.com%2Fprotected%2F&nonce=anonce"); return 0; }
void LER_PularComando( void ) { int TemComando = 0 ; #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif while ( LER_InxBuffer < LER_TamBuffer ) { if ( LER_Buffer[ LER_InxBuffer ] == PREFIXO_COMANDO ) { TemComando = 1 ; } else if ( EhBranco( LER_Buffer[ LER_InxBuffer ] )) { if ( TemComando ) { break ; } /* if */ } /* if */ LER_InxBuffer ++ ; } /* while */ } /* Fim função: LER &Pular comando de teste */
static char *test_proto_validate_access_token(request_rec *r) { // from http://openid.net/specs/openid-connect-core-1_0.html#id_token-tokenExample // A.3 Example using response_type=id_token token const char *s = "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogIml" "zcyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ" "4Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiA" "ibi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDE" "zMTEyODA5NzAsCiAiYXRfaGFzaCI6ICI3N1FtVVB0alBmeld0RjJBbnBLOVJ" "RIgp9.F9gRev0Dt2tKcrBkHy72cmRqnLdzw9FLCCSebV7mWs7o_sv2O5s6zM" "ky2kmhHTVx9HmdvNnx9GaZ8XMYRFeYk8L5NZ7aYlA5W56nsG1iWOou_-gji0" "ibWIuuf4Owaho3YSoi7EvsTuLFz6tq-dLyz0dKABMDsiCmJ5wqkPUDTE3QTX" "jzbUmOzUDli-gCh5QPuZAq0cNW3pf_2n4zpvTYtbmj12cVcxGIMZby7TMWES" "RjQ9_o3jvhVNcCGcE0KAQXejhA1ocJhNEvQNqMFGlBb6_0RxxKjDZ-Oa329e" "GDidOvvp0h5hoES4a8IuGKS7NOcpp-aFwp0qVMDLI-Xnm-Pg"; apr_jwt_error_t err; apr_jwt_t *jwt = NULL; TST_ASSERT_ERR("apr_jwt_parse", apr_jwt_parse(r->pool, s, &jwt, NULL, &err), r->pool, err); const char *access_token = "jHkWEdUXMU1BwAsC4vtUsZwnNvTIxEl0z9K3vx5KF0Y"; TST_ASSERT("oidc_proto_validate_access_token", oidc_proto_validate_access_token(r, NULL, jwt, "id_token token", access_token)); apr_jwt_destroy(jwt); return 0; }
void TBS_DestruirTabela( TBS_tppTabela pTabela ) { unsigned inxElem ; tpLista * pElem ; tpLista * pProx ; #ifdef _DEBUG TST_ASSERT( pTabela != NULL ) ; #endif for ( inxElem = 0 ; inxElem < pTabela->tamVtHash ; inxElem++ ) { /* Destruir todos elementos de lista de colisão */ pElem = pTabela->pVtHash[ inxElem ] ; while ( pElem != NULL ) { pProx = pElem->pProx ; LiberarElemento( pTabela , pElem ) ; pElem = pProx ; } /* while */ } /* fim repete: Raiz de TBS &Destruir tabela de símbolos */ free( pTabela->pVtHash ) ; free( pTabela ) ; } /* Fim função: TBS &Destruir tabela de símbolos */
int LER_InicializarLeitura( void ) { #ifdef _DEBUG TST_ASSERT( ! Inicializado ) ; #endif NomeArqScript[ 0 ] = 0 ; pArqScript = NULL ; AcabouScript = 1 ; ContaLinhas = 0 ; LER_Buffer[ 0 ] = 0 ; LER_TamBuffer = 0 ; pTabela = TBS_CriarTabela( DIM_TABELA , ObterSimbolo , NULL ) ; if ( pTabela == NULL ) { return FALSE ; } /* if */ Inicializado = 1 ; return TRUE ; } /* Fim função: LER &Inicializar módulo LerParm */
static char * test_proto_validate_nonce(request_rec *r) { oidc_cfg *c = ap_get_module_config(r->server->module_config, &auth_openidc_module); const char *nonce = "avSk7S69G4kEE8Km4bPiOjrfChHt6nO4Z397Lp_bQnc,"; /* * { * "typ": "JWT", * "alg": "RS256", * "x5t": "Z1NCjojeiHAib-Gm8vFE6ya6lPM" * } * { * "nonce": "avSk7S69G4kEE8Km4bPiOjrfChHt6nO4Z397Lp_bQnc,", * "iat": 1411580876, * "at_hash": "yTqsoONZbuWbN6TbgevuDQ", * "sub": "6343a29c-5399-44a7-9b35-4990f4377c96", * "amr": "password", * "auth_time": 1411577267, * "idp": "idsrv", * "name": "ksonaty", * "iss": "https://agsync.com", * "aud": "agsync_implicit", * "exp": 1411584475, * "nbf": 1411580875 * } */ char *s_jwt = apr_pstrdup(r->pool, "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IloxTkNqb2plaUhBaWItR204dkZFNnlhNmxQTSJ9.eyJub25jZSI6ImF2U2s3UzY5RzRrRUU4S200YlBpT2pyZkNoSHQ2bk80WjM5N0xwX2JRbmMsIiwiaWF0IjoxNDExNTgwODc2LCJhdF9oYXNoIjoieVRxc29PTlpidVdiTjZUYmdldnVEUSIsInN1YiI6IjYzNDNhMjljLTUzOTktNDRhNy05YjM1LTQ5OTBmNDM3N2M5NiIsImFtciI6InBhc3N3b3JkIiwiYXV0aF90aW1lIjoxNDExNTc3MjY3LCJpZHAiOiJpZHNydiIsIm5hbWUiOiJrc29uYXR5IiwiaXNzIjoiaHR0cHM6Ly9hZ3N5bmMuY29tIiwiYXVkIjoiYWdzeW5jX2ltcGxpY2l0IiwiZXhwIjoxNDExNTg0NDc1LCJuYmYiOjE0MTE1ODA4NzV9.lEG-DgHHa0JuOEuOTBvCqyexjRVcKXBnJJm289o2HyTgclpH80DsOMED9RlXCFfuDY7nw9i2cxUmIMAV42AdTxkMPomK3chytcajvpAZJirlk653bo9GTDXJSKZr5fwyEu--qahsoT5t9qvoWyFdYkvmMHFw1-mAHDGgVe23voc9jPuFFIhRRqIn4e8ikzN4VQeEV1UXJD02kYYFn2TRWURgiFyVeTr2r0MTn-auCEsFS_AfR1Bl_kmpMfqwrsicf5MTBvfPJeuSMt3t3d3LOGBkg36_z21X-ZRN7wy1KTjagr7iQ_y5csIpmtqs_QM55TTB9dW1HIosJPhiuMEJEA"); apr_jwt_t *jwt = NULL; apr_jwt_error_t err; TST_ASSERT_ERR("apr_jwt_parse", apr_jwt_parse(r->pool, s_jwt, &jwt, NULL, &err), r->pool, err); TST_ASSERT("oidc_proto_validate_nonce (1)", oidc_proto_validate_nonce(r, c, &c->provider, nonce, jwt)); TST_ASSERT("oidc_proto_validate_nonce (2)", oidc_proto_validate_nonce( r, c, &c->provider, nonce, jwt) == FALSE); apr_jwt_destroy(jwt); return 0; }
int LER_LerParmNome( char * Parm , int * tamNome , int dimNome ) { int inxParm = 0 ; #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif PularBrancos( ) ; *tamNome = 0 ; if ( ! isalpha( LER_Buffer[ LER_InxBuffer ] )) { ExibirErro( "Nome deve iniciar com letra." ) ; Parm[ inxParm ] = 0 ; return 0 ; } /* if */ Parm[ inxParm ] = LER_Buffer[ LER_InxBuffer ] ; LER_InxBuffer ++ ; inxParm ++ ; while ( LER_InxBuffer < LER_TamBuffer ) { if ( __iscsym( LER_Buffer[ LER_InxBuffer ] )) { Parm[ inxParm ] = LER_Buffer[ LER_InxBuffer ] ; } else { Parm[ inxParm ] = 0 ; *tamNome = inxParm ; return TRUE ; } /* if */ LER_InxBuffer ++ ; inxParm ++ ; if ( inxParm > dimNome ) { ExibirErro( "Nome longo demais." ) ; Parm[ dimNome - 1 ] = 0 ; *tamNome = dimNome - 1 ; return FALSE ; } /* if */ } /* while */ Parm[ inxParm ] = 0 ; *tamNome = inxParm ; return TRUE ; } /* Fim função: LER &Ler parâmetro nome */
void InstrumentarString( char * pString , int dimString ) { TST_ASSERT( dimString >= 2 ) ; pString[ dimString - 1 ] = 0 ; pString[ dimString - 2 ] = '\xFC' ; memset( pString , '?' , dimString - 2 ) ; } /* Fim função: TCED -Instrumentar string local */
void LiberarDadoTipoTres( void * pDado ) { #ifdef _DEBUG TST_ASSERT( pDado != NULL ) ; #endif free( (( tpTipo3 * ) pDado )->pString ) ; } /* Fim função: TTBS -Liberar dado tipo 3 */
char * ObterSimboloTipoTres( void * pDado ) { #ifdef _DEBUG TST_ASSERT( pDado != NULL ) ; #endif return (( tpTipo3 * ) pDado )->SimboloTres ; } /* Fim função: TTBS -Obter símbolo tipo 3 */
void * TBS_ProcurarSimbolo( TBS_tppTabela pTabela , char * pSimbolo ) { tpLista * pElem ; unsigned inxHash ; #ifdef _DEBUG TST_ASSERT( pTabela != NULL ) ; TST_ASSERT( pSimbolo != NULL ) ; #endif pElem = ProcurarSimbolo( pTabela , pSimbolo , &inxHash ) ; if ( pElem == NULL ) { return NULL ; } /* if */ return pElem->pDado ; } /* Fim função: TBS &Procurar símbolo */
void InicializarTeste( void ) { NomeArqLog[ 0 ] = 0 ; pArqLog = stdout ; ContaComandosExecutados = 0 ; ContaFalhas = 0 ; ContaCasosTeste = 0 ; TST_ASSERT( LER_InicializarLeitura( ) ) ; } /* Fim função: TSTG -Inicializar o módulo de teste */
TBS_tppTabela TBS_CriarTabela( int tamVetor , char * ( * ObterSimbolo ) ( void * pDado ) , void ( * LiberarDado ) ( void * pDado ) ) { TBS_tpTabela * pTabela = NULL ; int i ; #ifdef _DEBUG TST_ASSERT( tamVetor > 1 ) ; TST_ASSERT( ObterSimbolo != NULL ) ; #endif pTabela = ( TBS_tpTabela * ) malloc( sizeof( TBS_tpTabela )) ; if ( pTabela == NULL ) { return NULL ; } /* if */ pTabela->pVtHash = ( tpLista ** ) malloc( tamVetor * sizeof( tpLista * )) ; if ( pTabela->pVtHash == NULL ) { free( pTabela ) ; return NULL ; } /* if */ for( i = 0 ; i < tamVetor ; i++ ) { pTabela->pVtHash[ i ] = NULL ; } /* for */ pTabela->tamVtHash = tamVetor ; pTabela->ObterSimbolo = ObterSimbolo ; pTabela->LiberarDado = LiberarDado ; return pTabela ; } /* Fim função: TBS &Criar tabela de símbolos */
static char *_jwk_parse(apr_pool_t *pool, const char *s, apr_jwk_t **jwk, apr_jwt_error_t *err) { json_t *j_jwk = json_loads(s, 0, NULL); TST_ASSERT("json_loads", ((j_jwk != NULL) && (json_is_object(j_jwk)))); TST_ASSERT_ERR("apr_jwk_parse_json", apr_jwk_parse_json(pool, j_jwk, jwk, err), pool, (*err)); json_decref(j_jwk); return 0; }
TBS_tpCondRet TBS_InserirSimbolo( TBS_tppTabela pTabela , void * pDado ) { unsigned inxHash ; tpLista * pElem ; #ifdef _DEBUG TST_ASSERT( pTabela != NULL ) ; TST_ASSERT( pDado != NULL ) ; #endif /* Procurar símbolo */ if ( ProcurarSimbolo( pTabela , pTabela->ObterSimbolo( pDado ) , &inxHash ) != NULL ) { return TBS_CondRetSimboloExiste ; } /* if */ /* Inserir o símbolo */ pElem = ( tpLista * ) malloc( sizeof( tpLista )) ; pElem->pDado = pDado ; pElem->pAnt = NULL ; pElem->pProx = pTabela->pVtHash[ inxHash ] ; if ( pElem->pProx != NULL ) { pElem->pProx->pAnt = pElem ; } /* if */ pTabela->pVtHash[ inxHash ] = pElem ; return TBS_CondRetOK ; } /* Fim função: TBS &Inserir símbolo */
void LER_TerminarLeitura( void ) { #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif FecharScript( ) ; TBS_DestruirTabela( pTabela ) ; pTabela = NULL ; Inicializado = 0 ; } /* Fim função: LER &Terminar módulo LerParm */
void FecharScript( void ) { #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif if ( pArqScript != NULL ) { fclose( pArqScript ) ; pArqScript = NULL ; NomeArqScript[ 0 ] = 0 ; } /* if */ } /* Fim função: LER -Fechar arquivo script */
TST_tpCondRet LER_InterpretarComandos( char * ComandoTeste ) { char Parm[ LER_DIM_NOME ] ; int tamParm ; #ifdef _DEBUG TST_ASSERT( Inicializado ) ; #endif /* Tratar declaração de nome */ if ( strcmp( ComandoTeste , DECLARAR_SIMBOLO_CMD ) == 0 ) { if ( ! LER_DeclararParametro( )) { return TST_NotificarFalha( "Não declarou o parâmetro." ) ; } /* if */ return TST_CondRetOK ; } /* fim ativa: Tratar declaração de nome */ /* Tratar exibição de uma declaração */ else if ( strcmp( ComandoTeste , EXIBIR_SIMBOLO_CMD ) == 0 ) { LER_PularComando( ) ; if ( ! LER_LerParmNome( Parm , &tamParm , LER_DIM_NOME )) { return TST_CondRetParm ; } /* if */ LER_ExibirParametro( Parm ) ; return TST_CondRetOK ; } /* fim ativa: Tratar exibição de uma declaração */ /* Tratar comando não é para LER */ return TST_CondRetNaoExecutou ; } /* Fim função: LER &Interpretar comandos de teste */
unsigned Hash( char * pSimbolo , unsigned tamVtHash ) { unsigned inxHash = 0 ; unsigned i = 0 ; #ifdef _DEBUG TST_ASSERT( pSimbolo != NULL ) ; #endif for( i = 0 ; i < strlen( pSimbolo ) ; i ++ ) { inxHash = ( inxHash << 2 ) + pSimbolo[ i ] ; } /* for */ return inxHash % tamVtHash ; } /* Fim função: TBS -Computar hash */
TST_tpCondRet TBS_ValidarTabela( TBS_tppTabela pTabela ) { unsigned inxHash ; tpLista * pElem ; #ifdef _DEBUG TST_ASSERT( pTabela != NULL ) ; #endif /* Validar existência de dados da cabeça */ if ( pTabela->tamVtHash <= 1 ) { return TST_NotificarFalha( "Tamanho incorreto do vetor de randomização." ) ; } /* if */ if ( pTabela->pVtHash == NULL ) { return TST_NotificarFalha( "Falta vetor de randomização." ) ; } /* if */ if ( pTabela->ObterSimbolo == NULL ) { return TST_NotificarFalha( "Falta função obter simbolo." ) ; } /* if */ /* Validar listas de colisão */ for ( inxHash = 0 ; inxHash < pTabela->tamVtHash ; inxHash ++ ) { /* Validar toda a lista de colisão */ pElem = pTabela->pVtHash[ inxHash ] ; while ( pElem != NULL ) { /* Validar elemento da lista de colisão */ if ( pElem->pDado == NULL ) { return TST_NotificarFalha( "Faltou dado em elemento de lista." ) ; } /* if */ if ( Hash( pTabela->ObterSimbolo( pElem->pDado ) , pTabela->tamVtHash ) != inxHash ) { return TST_NotificarFalha( "Índice has de elemento está incorreto." ) ; } /* if */ if ( pElem->pAnt != NULL ) { if ( pElem->pAnt->pProx != pElem ) { return TST_NotificarFalha( "Erro de encadeamento à esquerda em elemento de lista." ) ; } /* if */ } else { if ( pElem != pTabela->pVtHash[ inxHash ] ) { return TST_NotificarFalha( "Erro de encadeamento origem em elemento de lista." ) ; } /* if */ } /* if */ if ( pElem->pProx != NULL ) { if ( pElem->pProx->pAnt != pElem ) { return TST_NotificarFalha( "Erro de encadeamento à direita em elemento de lista." ) ; } /* if */ } /* if */ pElem = pElem->pProx ; } /* fim repete: Validar toda a lista de colisão */ } /* fim repete: Validar listas de colisão */ return TST_CondRetOK ; } /* Fim função: TBS &Validar tabela de símbolos */