* Unit test ifdef for restconf http1 parser

* test: fix cli-union test
This commit is contained in:
Olof hagsand 2022-02-17 09:11:49 +01:00
parent 694f8b3f0d
commit 12695c5d87
7 changed files with 120 additions and 62 deletions

View file

@ -1073,8 +1073,21 @@ openssl_init_socket(clicon_handle h,
/* ss is a server socket that the clients connect to. The callback
therefore accepts clients on ss */
#ifdef RESTCONF_HTTP1_UNITTEST
{
restconf_conn *rc;
if ((rc = restconf_conn_new(h, 0)) == NULL)
goto done;
rc->rc_s = 0;
if (restconf_stream_data_new(rc, 0) == NULL)
goto done;
if (clixon_event_reg_fd(0, restconf_connection, rc, "restconf socket") < 0)
goto done;
}
#else
if (clixon_event_reg_fd(ss, restconf_accept_client, rsock, "restconf socket") < 0)
goto done;
#endif
retval = 0;
done:
return retval;
@ -1404,11 +1417,6 @@ usage(clicon_handle h,
exit(0);
}
/* Enable for normal use
* Disable for unit testing, fuzzing, etc
*/
#if 1
int
main(int argc,
char **argv)
@ -1611,42 +1619,3 @@ main(int argc,
restconf_terminate(h);
return retval;
}
#else /* Unit test */
int
main(int argc,
char **argv)
{
int retval = -1;
clicon_handle h = NULL;
restconf_conn *rc;
int dbg = 0;
int c;
while ((c = getopt(argc, argv, "hD:")) != -1)
switch (c) {
case 'h':
fprintf(stderr, "%s [-h] [-D <dbg>]\n", argv[0]);
break;
case 'D' : /* debug. Note this overrides any setting in the config */
if (sscanf(optarg, "%d", &dbg) != 1)
fprintf(stderr, "%s [-h] [-D <dbg>]\n", argv[0]);
break;
}
if ((h = restconf_handle_init()) == NULL)
goto done;
clicon_log_init(__FILE__, LOG_DEBUG, CLICON_LOG_STDERR);
clicon_debug_init(dbg, NULL);
if ((rc = restconf_conn_new(h, 0)) == NULL)
goto done;
if (restconf_stream_data_new(rc, 0) == NULL)
goto done;
if (clixon_http1_parse_file(h, rc, stdin, "stdin") < 0)
goto done;
retval = 0;
done:
return retval;
}
#endif

View file

@ -471,8 +471,10 @@ native_clear_input(clicon_handle h,
int retval = -1;
cbuf_reset(sd->sd_indata);
if (sd->sd_qvec)
if (sd->sd_qvec){
cvec_free(sd->sd_qvec);
sd->sd_qvec = NULL;
}
if (restconf_param_del_all(h) < 0)
goto done;
retval = 0;
@ -679,10 +681,12 @@ restconf_http1(restconf_conn *rc,
*/
if ((ret = http1_check_content_length(h, sd, &status)) < 0)
goto done;
#ifndef RESTCONF_HTTP1_UNITTEST /* Ignore content-length */
if (status == 1){
(*readmore)++;
goto ok;
}
#endif
/* nginx compatible, set HTTPS parameter if SSL */
if (rc->rc_ssl)
if (restconf_param_set(h, "HTTPS", "https") < 0)
@ -690,9 +694,15 @@ restconf_http1(restconf_conn *rc,
/* main restconf processing */
if (restconf_http1_path_root(h, rc) < 0)
goto done;
#ifdef RESTCONF_HTTP1_UNITTEST
if (native_buf_write(cbuf_get(sd->sd_outp_buf), cbuf_len(sd->sd_outp_buf),
1, rc->rc_ssl) < 0)
goto done;
#else
if (native_buf_write(cbuf_get(sd->sd_outp_buf), cbuf_len(sd->sd_outp_buf),
rc->rc_s, rc->rc_ssl) < 0)
goto done;
#endif
cvec_reset(sd->sd_outp_hdrs); /* Can be done in native_send_reply */
cbuf_reset(sd->sd_outp_buf);
cbuf_reset(sd->sd_inbuf);
@ -910,5 +920,16 @@ restconf_connection(int s,
retval = 0;
done:
clicon_debug(1, "%s retval %d", __FUNCTION__, retval);
#ifdef RESTCONF_HTTP1_UNITTEST /* unit test */
if (rc){
if (close(rc->rc_s) < 0){
clicon_err(OE_UNIX, errno, "close");
goto done;
}
clixon_event_unreg_fd(rc->rc_s, restconf_connection);
restconf_conn_free(rc);
}
exit(0);
#endif
return retval;
} /* restconf_connection */