diff options
-rw-r--r-- | check/magma/servers/camel/camel_check.c | 11 | ||||
-rw-r--r-- | check/magma/servers/camel/camel_check.h | 4 | ||||
-rw-r--r-- | check/magma/servers/camel/camel_check_network.c | 109 | ||||
-rw-r--r-- | check/magma/servers/http/http_check_network.c | 6 |
4 files changed, 85 insertions, 45 deletions
diff --git a/check/magma/servers/camel/camel_check.c b/check/magma/servers/camel/camel_check.c index f7564d55..18901436 100644 --- a/check/magma/servers/camel/camel_check.c +++ b/check/magma/servers/camel/camel_check.c @@ -6,9 +6,12 @@ #include "magma_check.h" -START_TEST (check_camel_login_s) { +// LOW: Refactor for both HTTP and HTTPS. - log_disable(); +START_TEST (check_camel_auth_s) { + + //log_disable(); + log_enable(); bool_t outcome = true; client_t *client = NULL; server_t *server = NULL; @@ -23,7 +26,7 @@ START_TEST (check_camel_login_s) { st_sprint(errmsg, "Failed to connect client securely to HTTP server."); outcome = false; } - else if (!check_camel_login_sthread(client, errmsg)){ + else if (!check_camel_auth_sthread(client, errmsg)){ outcome = false; } else { @@ -68,7 +71,7 @@ Suite * suite_check_camel(void) { Suite *s = suite_create("\tCAMEL"); - suite_check_testcase(s, "CAMEL", "Camel Login/S", check_camel_login_s); + suite_check_testcase(s, "CAMEL", "Camel Auth/S", check_camel_auth_s); suite_check_testcase(s, "CAMEL", "Camel Basic/S", check_camel_basic_s); return s; diff --git a/check/magma/servers/camel/camel_check.h b/check/magma/servers/camel/camel_check.h index e0039098..a8ef139b 100644 --- a/check/magma/servers/camel/camel_check.h +++ b/check/magma/servers/camel/camel_check.h @@ -10,8 +10,8 @@ /// camel_check_network.c bool_t check_camel_status(client_t *client); stringer_t* check_camel_read_json(client_t *client, size_t length); -bool_t check_camel_login(client_t *client, uint32_t id, chr_t *user, chr_t *pass, stringer_t *cookie); -bool_t check_camel_login_sthread(client_t *client, stringer_t *errmsg); +bool_t check_camel_login(client_t *client, uint32_t id, stringer_t *user, stringer_t *pass, stringer_t *cookie); +bool_t check_camel_auth_sthread(client_t *client, stringer_t *errmsg); bool_t check_camel_basic_sthread(client_t *client, stringer_t *errmsg); /// pop_check.c diff --git a/check/magma/servers/camel/camel_check_network.c b/check/magma/servers/camel/camel_check_network.c index 2f12a830..cf48594b 100644 --- a/check/magma/servers/camel/camel_check_network.c +++ b/check/magma/servers/camel/camel_check_network.c @@ -8,6 +8,25 @@ #include "magma_check.h" /** + * @brief Reads lines from the client until the HTTP response status code is found, which it checks. + * + * @param client A client_t* to read lines from. An HTTP request should have been submitted + * from the client before this function is called. + * + * @return True if the HTTP status code of the response begins with a '2', false otherwise. + */ +bool_t check_camel_status(client_t *client) { + + while (st_cmp_cs_starts(&(client->line), NULLER("HTTP/1.1"))) { + if (client_read_line(client) <= 2) return false; + } + + return ((*(pl_char_get(client->line) + 9) == '2') ? true : false); +} + +// Combine submit and read, because we now need to handle the connection being closed between requests. + +/** * @brief Reads lines from the client until the end of the HTTP response is reached. * * @param client A client_t* to read lines from. An HTTP request should have been submitted @@ -16,35 +35,40 @@ * @return True if the end of the HTTP response was reached, false if client_read_line reads * a 0 length line before the last line is reached. */ -stringer_t* check_camel_read_json(client_t *client, size_t length) { +stringer_t * check_camel_json_read(client_t *client, size_t length) { - stringer_t *json = st_alloc(length); + stringer_t *json = NULL; + uint32_t content_read = 0; - while (st_cmp_cs_eq(&(client->line), PLACER("\r\n", 2))) client_read_line(client); - recv(client->sockd, st_char_get(json), length, 0); + while (st_cmp_cs_eq(&(client->line), PLACER("\r\n", 2))) { + if (client_read_line(client) <= 0) return NULL; + } - chr_t *foo = st_char_get(json); - (void)foo; + while (content_read < length) { + content_read += client_read(client); + json = st_append_opts(8192, json, client->buffer); + } + if (st_empty(json)) { + st_free(json); + return NULL; + } return json; } -/** - * @brief Reads lines from the client until the HTTP response status code is found, which it checks. - * - * @param client A client_t* to read lines from. An HTTP request should have been submitted - * from the client before this function is called. - * - * @return True if the HTTP status code of the response begins with a '2', false otherwise. - */ -bool_t check_camel_status(client_t *client) { +bool_t check_camel_json_submit(client_t *client, stringer_t *json, bool_t keep_alive) { - while (st_cmp_cs_starts(&(client->line), NULLER("HTTP/1.1"))) { - if (client_read_line(client) <= 0) return false; + chr_t *message = "POST /portal/camel HTTP/1.1\r\nHost: localhost:10000\r\nAccept: */*\r\n" \ + "Content-Length: %u\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: %s\r\n\r\n%s"; + + if (client_print(client, message, st_length_get(json), (keep_alive ? "keep-alive" : "close"), st_char_get(json)) != + (st_length_get(message) - 6 + st_length_get(json) + (keep_alive ? 10 : 5)) || client_status(client) != 1) { + + return false; } - return ((*(pl_char_get(client->line) + 9) == '2') ? true : false); + return true; } /** @@ -58,41 +82,52 @@ bool_t check_camel_status(client_t *client) { * * @return True if the request was successful, false otherwise. */ -bool_t check_camel_login(client_t *client, uint32_t id, chr_t *user, chr_t *pass, stringer_t *cookie) { +bool_t check_camel_login(client_t *client, uint32_t id, stringer_t *user, stringer_t *pass, stringer_t *cookie) { + json_error_t json_err; size_t content_length = 0; + json_t *parsed_json = NULL, *result = NULL, *session = NULL; uint32_t length = 62 + ns_length_get(user) + ns_length_get(pass) + uint32_digits(id); - stringer_t *json = NULL, *message = "POST /portal/camel HTTP/1.1\r\n" \ - "Host: localhost:10000\r\n" \ - "Accept: */*\r\n" \ - "Content-Length: %u\r\n" \ - "Content-Type: application/x-www-form-urlencoded\r\n" \ - "\r\n" - "{\"id\":%u,\"method\":\"auth\",\"params\":{\"username\":\"%s\",\"password\":\"%s\"}}\r\n" - "\r\n"; - - if (client_print(client, message, length, id, user, pass) != ((ns_length_get(message) - 8) + uint32_digits(length) + - uint32_digits(id) + ns_length_get(user) + ns_length_get(pass)) || client_status(client) != 1 || !check_camel_status(client) || - !(content_length = check_http_content_length_get(client)) || !(json = check_camel_read_json(client, content_length))) { + stringer_t *json = NULL, *message = NULLER("POST /portal/camel HTTP/1.1\r\nHost: localhost:10000\r\nAccept: */*\r\n" \ + "Content-Length: %u\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n{\"id\":%u,\"method\":\"auth\"," \ + "\"params\":{\"username\":\"%.*s\",\"password\":\"%.*s\"}}\r\n\r\n"); + + if (client_print(client, st_char_get(message), length, id, st_length_int(user), st_char_get(user), st_length_int(pass), + st_char_get(pass)) != ((st_length_get(message) - 12) + uint32_digits(length) + uint32_digits(id) + st_length_get(user) + + st_length_get(pass)) || client_status(client) != 1 || !check_camel_status(client) || + !(content_length = check_http_content_length_get(client)) || !(json = check_camel_json_read(client, content_length))) { + + return false; + } + else if (!(parsed_json = json_loads_d(st_char_get(json), 0, &json_err)) || !(result = json_object_get_d(parsed_json, "result")) || + !(session = json_object_get_d(result, "session"))) { return false; } + else if (cookie && st_sprint(cookie, "%s", json_string_value_d(session)) == -1) { + return false; + } - chr_t *foo = st_char_get(json); - (void)foo; - st_free(json); + st_cleanup(json); + if (result) mm_free(result); + if (session) mm_free(session); + if (parsed_json) mm_free(parsed_json); return true; } -bool_t check_camel_login_sthread(client_t *client, stringer_t *errmsg) { +// LOW: Test the four different ways of preserving a session token: Cookie, URL param, JSON param, Form post. +bool_t check_camel_auth_sthread(client_t *client, stringer_t *errmsg) { + + stringer_t *cookie = MANAGEDBUF(1024); - if (!check_camel_login(client, 1, "princess", "password", NULL)) { + if (!check_camel_login(client, 1, PLACER("princess", 8), PLACER("password", 8), cookie)) { st_sprint(errmsg, "Failed to return successful state after auth request."); return false; } + st_cleanup(cookie); client_close(client); return true; @@ -114,7 +149,7 @@ bool_t check_camel_basic_sthread(client_t *client, stringer_t *errmsg) { // }; // // for (size_t i = 0; i < sizeof(commands)/sizeof(chr_t*); i++) { -// if (client_print(commands[i], i) != ns_length_get(commands[i]) - 2 + uint32_digits(i)) { +// if (client_print(client, commands[i], i) != ns_length_get(commands[i]) - 2 + uint32_digits(i)) { // // } // } diff --git a/check/magma/servers/http/http_check_network.c b/check/magma/servers/http/http_check_network.c index 2b1e891c..7aca3d38 100644 --- a/check/magma/servers/http/http_check_network.c +++ b/check/magma/servers/http/http_check_network.c @@ -32,10 +32,12 @@ bool_t check_http_read_to_empty(client_t *client) { */ size_t check_http_content_length_get(client_t *client) { - size_t location = 0, content_length; + size_t location = 0, content_length = 0; placer_t cl_placer = pl_null(); - while (st_cmp_ci_starts(&(client->line), NULLER("Content-Length:")) != 0) client_read_line(client); + while (st_cmp_ci_starts(&(client->line), NULLER("Content-Length:")) != 0) { + if (client_read_line(client) <= 2) return content_length; + } if (!st_search_chr(&(client->line), ' ', &location)) { //st_sprint(errmsg, "The Content-Length line was improperly formed."); |