twister

HTTP fault injector for testing
Log | Files | Refs | README | LICENSE

commit bcb0c70668243672a63dc6f4bba3ce417b05abb2
parent 96c743451e31e29a93006e47e2e5dca7184611e8
Author: Marcello Stanisci <stanisci.m@gmail.com>
Date:   Wed,  5 Jun 2019 16:18:00 +0200

Implementing chaos probability (5737).

Diffstat:
Msrc/twister/taler-twister-service.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
1 file changed, 61 insertions(+), 4 deletions(-)

diff --git a/src/twister/taler-twister-service.c b/src/twister/taler-twister-service.c @@ -298,6 +298,13 @@ static char *target_server_base_url; /* ******************* Transformations ***************** */ /** + * Chaos probability (in percent); this value is taken from + * the config and stays valid for all the Twister's lifetime. + */ +static long long unsigned int chaos_rate = 0; + + +/** * Set to non-zero if we should change the next response code. * In this case, this is the value to use. */ @@ -1332,6 +1339,49 @@ inflate_data (struct HttpRequest *request) } /* while (1) */ } + +/** + * Create the response object according to the "chaos rate". + * If this latter strikes, then the response will be "503 Service + * Unavailable" with a empty body (overriding every other mod that + * the user might have given.) + * + * @param request the HTTP object representing the current state. + */ +static void +create_response_with_chaos_rate (struct HttpRequest *request) +{ + uint64_t random; + + void *resp_buf; + size_t resp_len; + + random = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, + 100); + TALER_LOG_INFO ("p: %llu, random: %llu\n", + chaos_rate, + random); + + if (random <= chaos_rate) + { + /* p won */ + TALER_LOG_INFO ("Chaos probability won the case.\n"); + resp_buf = "Service unavailable"; + resp_len = strlen (resp_buf); + request->response_code = MHD_HTTP_SERVICE_UNAVAILABLE; + } + else + { + resp_len = request->io_len; + resp_buf = request->io_buf; + } + + request->response = MHD_create_response_from_buffer + (resp_len, + resp_buf, + MHD_RESPMEM_MUST_COPY); +} + /** * Main MHD callback for handling requests. * @@ -1748,10 +1798,7 @@ create_response (void *cls, malform = GNUNET_NO; } - hr->response = MHD_create_response_from_buffer - (hr->io_len, - hr->io_buf, - MHD_RESPMEM_MUST_COPY); + create_response_with_chaos_rate (hr); for (header = hr->header_head; NULL != header; @@ -1767,6 +1814,7 @@ create_response (void *cls, header->value)); } run_mhd_now (); + return MHD_queue_response (con, hr->response_code, hr->response); @@ -2095,6 +2143,15 @@ run (void *cls, "Failed to create cURL multi handle!\n"); return; } + + /* No need to check return value. If given, we take, + * otherwise it stays zero. */ + GNUNET_CONFIGURATION_get_value_number (c, + "twister", + "CHAOS_RATE", + &chaos_rate); + GNUNET_assert (100 >= chaos_rate); + if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (c,