From 5248283ee82b7dcc57c07d53f2037cbf7ee1617d Mon Sep 17 00:00:00 2001 From: TheSomeMan Date: Thu, 18 Apr 2024 00:19:41 +0700 Subject: [PATCH] [#289] Add unit-tests --- .../test_http_server_handle_req_get_auth.cpp | 257 +++++++++++++++--- 1 file changed, 214 insertions(+), 43 deletions(-) diff --git a/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp b/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp index 409cae6..1002683 100644 --- a/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp +++ b/tests/test_http_server_handle_req_get_auth/test_http_server_handle_req_get_auth.cpp @@ -41,10 +41,10 @@ class TestHttpServerHandleReqGetAuth : public ::testing::Test } public: - const uint32_t* m_p_random_values; - size_t m_num_random_values; - size_t m_idx_random_value; - std::array arr_of_random_values; + const uint32_t* m_p_random_values; + size_t m_num_random_values; + size_t m_idx_random_value; + std::array arr_of_random_values; TestHttpServerHandleReqGetAuth(); @@ -95,48 +95,219 @@ esp_random(void) TEST_F(TestHttpServerHandleReqGetAuth, test_req_get_auth_allow) // NOLINT { - const http_server_auth_info_t auth_info = { - HTTP_SERVER_AUTH_TYPE_ALLOW, - "", - "", - }; - const wifiman_hostinfo_t hostinfo = { - .hostname = { "RuuviGatewayEEFF" }, - .fw_ver = { "1.13.0" }, - .nrf52_fw_ver = { "1.0.0" }, - }; + { + const http_server_auth_info_t auth_info = { + HTTP_SERVER_AUTH_TYPE_ALLOW, + "", + "", + }; + const wifiman_hostinfo_t hostinfo = { + .hostname = { "RuuviGatewayEEFF" }, + .fw_ver = { "1.13.0" }, + .nrf52_fw_ver = { "1.0.0" }, + }; - const http_req_header_t http_header = { "" }; - const sta_ip_string_t remote_ip = { "192.168.1.10" }; - http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; + const http_req_header_t http_header = { "" }; + const sta_ip_string_t remote_ip = { "192.168.1.10" }; + http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; - const http_server_handle_req_auth_param_t param = { - .flag_access_from_lan = true, - .flag_check_rw_access_with_bearer_token = false, - .http_header = http_header, - .p_remote_ip = &remote_ip, - .p_auth_info = &auth_info, - .p_hostinfo = &hostinfo, - }; + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; - const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); - const string exp_json_resp - = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; - ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); - ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); - ASSERT_TRUE(resp.flag_no_cache); - ASSERT_TRUE(resp.flag_add_header_date); - ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); - ASSERT_EQ(nullptr, resp.p_content_type_param); - ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); - ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); - ASSERT_EQ(exp_json_resp.length(), resp.content_len); - ASSERT_EQ( - string("WWW-Authenticate: x-ruuvi-interactive realm=\"RuuviGatewayEEFF\" " - "challenge=\"66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925\" " - "session_cookie=\"RUUVISESSION\" session_id=\"AAAAAAAAAAAAAAAA\"\r\n" - "Set-Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n"), - string(extra_header_fields.buf)); + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp + = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; + ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); + ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); + ASSERT_TRUE(resp.flag_no_cache); + ASSERT_TRUE(resp.flag_add_header_date); + ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); + ASSERT_EQ(nullptr, resp.p_content_type_param); + ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); + ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ(exp_json_resp.length(), resp.content_len); + ASSERT_EQ( + string("WWW-Authenticate: x-ruuvi-interactive realm=\"RuuviGatewayEEFF\" " + "challenge=\"66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925\" " + "session_cookie=\"RUUVISESSION\" session_id=\"AAAAAAAAAAAAAAAA\"\r\n" + "Set-Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n"), + string(extra_header_fields.buf)); + } + { + const http_server_auth_info_t auth_info = { + HTTP_SERVER_AUTH_TYPE_ALLOW, + "", + "", + }; + const wifiman_hostinfo_t hostinfo = { + .hostname = { "RuuviGatewayEEFF" }, + .fw_ver = { "1.13.0" }, + .nrf52_fw_ver = { "1.0.0" }, + }; + + const http_req_header_t http_header = { "Cookie: RUUVISESSION=QQQQQQQQQQQQQQQQ\r\n" }; + const sta_ip_string_t remote_ip = { "192.168.1.10" }; + http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + bool flag_access_by_bearer_token = false; + const http_server_resp_t resp = http_server_handle_req_check_auth( + ¶m, + &extra_header_fields, + &flag_access_by_bearer_token); + const string exp_json_resp + = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; + ASSERT_EQ(HTTP_RESP_CODE_401, resp.http_resp_code); + ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); + ASSERT_TRUE(resp.flag_no_cache); + ASSERT_TRUE(resp.flag_add_header_date); + ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); + ASSERT_EQ(nullptr, resp.p_content_type_param); + ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); + ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ(exp_json_resp.length(), resp.content_len); + ASSERT_EQ(string(""), string(extra_header_fields.buf)); + } + { + const http_server_auth_info_t auth_info = { + HTTP_SERVER_AUTH_TYPE_ALLOW, + "", + "", + }; + const wifiman_hostinfo_t hostinfo = { + .hostname = { "RuuviGatewayEEFF" }, + .fw_ver = { "1.13.0" }, + .nrf52_fw_ver = { "1.0.0" }, + }; + + const http_req_header_t http_header = { "Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n" }; + const sta_ip_string_t remote_ip = { "192.168.1.10" }; + http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + bool flag_access_by_bearer_token = false; + const http_server_resp_t resp = http_server_handle_req_check_auth( + ¶m, + &extra_header_fields, + &flag_access_by_bearer_token); + const string exp_json_resp + = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; + ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); + ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); + ASSERT_TRUE(resp.flag_no_cache); + ASSERT_TRUE(resp.flag_add_header_date); + ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); + ASSERT_EQ(nullptr, resp.p_content_type_param); + ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); + ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ(exp_json_resp.length(), resp.content_len); + ASSERT_EQ(string(""), string(extra_header_fields.buf)); + } + { + const http_server_auth_info_t auth_info = { + HTTP_SERVER_AUTH_TYPE_ALLOW, + "", + "", + }; + const wifiman_hostinfo_t hostinfo = { + .hostname = { "RuuviGatewayEEFF" }, + .fw_ver = { "1.13.0" }, + .nrf52_fw_ver = { "1.0.0" }, + }; + + const http_req_header_t http_header = { "Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n" }; + const sta_ip_string_t remote_ip = { "192.168.1.10" }; + http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp + = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; + ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); + ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); + ASSERT_TRUE(resp.flag_no_cache); + ASSERT_TRUE(resp.flag_add_header_date); + ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); + ASSERT_EQ(nullptr, resp.p_content_type_param); + ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); + ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ(exp_json_resp.length(), resp.content_len); + ASSERT_EQ(string(""), string(extra_header_fields.buf)); + } + { + const http_server_auth_info_t auth_info = { + HTTP_SERVER_AUTH_TYPE_ALLOW, + "", + "", + }; + const wifiman_hostinfo_t hostinfo = { + .hostname = { "RuuviGatewayEEFF" }, + .fw_ver = { "1.13.0" }, + .nrf52_fw_ver = { "1.0.0" }, + }; + + const http_req_header_t http_header = { "Cookie: RUUVISESSION=QQQQQQQQQQQQQQQQ\r\n" }; + const sta_ip_string_t remote_ip = { "192.168.1.10" }; + http_header_extra_fields_t extra_header_fields = { .buf = { '\0' } }; + + const http_server_handle_req_auth_param_t param = { + .flag_access_from_lan = true, + .flag_check_rw_access_with_bearer_token = false, + .http_header = http_header, + .p_remote_ip = &remote_ip, + .p_auth_info = &auth_info, + .p_hostinfo = &hostinfo, + }; + + const http_server_resp_t resp = http_server_handle_req_get_auth(¶m, &extra_header_fields); + const string exp_json_resp + = R"({"gateway_name": "RuuviGatewayEEFF", "fw_ver": "1.13.0", "nrf52_fw_ver": "1.0.0", "lan_auth_type": "lan_auth_allow", "lan": true})"; + ASSERT_EQ(HTTP_RESP_CODE_200, resp.http_resp_code); + ASSERT_EQ(HTTP_CONTENT_LOCATION_STATIC_MEM, resp.content_location); + ASSERT_TRUE(resp.flag_no_cache); + ASSERT_TRUE(resp.flag_add_header_date); + ASSERT_EQ(HTTP_CONTENT_TYPE_APPLICATION_JSON, resp.content_type); + ASSERT_EQ(nullptr, resp.p_content_type_param); + ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding); + ASSERT_EQ(exp_json_resp, string(reinterpret_cast(resp.select_location.memory.p_buf))); + ASSERT_EQ(exp_json_resp.length(), resp.content_len); + ASSERT_EQ( + string("WWW-Authenticate: x-ruuvi-interactive realm=\"RuuviGatewayEEFF\" " + "challenge=\"66687aadf862bd776c8fc18b8e9f8e20089714856ee233b3902a591d0d5f2925\" " + "session_cookie=\"RUUVISESSION\" session_id=\"AAAAAAAAAAAAAAAA\"\r\n" + "Set-Cookie: RUUVISESSION=AAAAAAAAAAAAAAAA\r\n"), + string(extra_header_fields.buf)); + } } TEST_F(TestHttpServerHandleReqGetAuth, test_req_check_auth_allow) // NOLINT