Skip to content

Commit

Permalink
[#289] Add unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSomeMan committed Apr 17, 2024
1 parent 4cb63ce commit 8d620ec
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/http_server_handle_req_get_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ http_server_handle_req_get_auth_ruuvi(
{
if (HTTP_SERVER_AUTH_TYPE_ALLOW == auth_type)
{
return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan);
if (flag_check)
{
return http_server_handle_req_get_auth_allow(p_param->p_hostinfo, p_param->flag_access_from_lan);
}
else
{
return http_server_resp_200_auth_allow_with_new_session_id(
p_param->p_remote_ip,
p_param->p_hostinfo,
p_extra_header_fields);
}
}
if (flag_check)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class TestHttpServerHandleReqGetAuth : public ::testing::Test
SetUp() override
{
this->m_idx_random_value = 0;
std::fill(arr_of_random_values.begin(), arr_of_random_values.end(), 0);
set_random_values(this->arr_of_random_values.data(), this->arr_of_random_values.size());
http_server_auth_clear_authorized_sessions();
}

Expand All @@ -39,9 +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;
const uint32_t* m_p_random_values;
size_t m_num_random_values;
size_t m_idx_random_value;
std::array<uint32_t, 50> arr_of_random_values;

TestHttpServerHandleReqGetAuth();

Expand Down Expand Up @@ -90,7 +93,7 @@ esp_random(void)

/*** Unit-Tests *******************************************************************************************************/

TEST_F(TestHttpServerHandleReqGetAuth, test_auth_allow) // NOLINT
TEST_F(TestHttpServerHandleReqGetAuth, test_req_get_auth_allow) // NOLINT
{
const http_server_auth_info_t auth_info = {
HTTP_SERVER_AUTH_TYPE_ALLOW,
Expand Down Expand Up @@ -128,6 +131,57 @@ TEST_F(TestHttpServerHandleReqGetAuth, test_auth_allow) // NOLINT
ASSERT_EQ(HTTP_CONTENT_ENCODING_NONE, resp.content_encoding);
ASSERT_EQ(exp_json_resp, string(reinterpret_cast<const char*>(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
{
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_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(
&param,
&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_FALSE(flag_access_by_bearer_token);
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<const char*>(resp.select_location.memory.p_buf)));
ASSERT_EQ(exp_json_resp.length(), resp.content_len);
ASSERT_EQ(string(""), string(extra_header_fields.buf));
}

Expand Down
12 changes: 12 additions & 0 deletions tests/test_http_server_resp/test_http_server_resp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "gtest/gtest.h"
#include "http_server_resp.h"
#include "http_server_auth.h"
#include <string>

using namespace std;
Expand Down Expand Up @@ -376,4 +377,15 @@ TEST_F(TestHttpServerResp, test_http_server_resp_200_auth_allow_with_new_session
"{\"gateway_name\": \"hostname\", \"fw_ver\": \"v1.15.0\", \"nrf52_fw_ver\": \"v1.0.0\", \"lan_auth_type\": "
"\"lan_auth_allow\", \"lan\": true}",
string(reinterpret_cast<const char*>(resp.select_location.memory.p_buf)));
ASSERT_EQ(
"WWW-Authenticate: x-ruuvi-interactive realm=\"hostname\" "
"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_ruuvi_t* const p_auth = http_server_auth_ruuvi_get_info();
const http_server_auth_ruuvi_authorized_session_t* const p_session = &p_auth->authorized_sessions[0];
ASSERT_EQ("AAAAAAAAAAAAAAAA", string(p_session->session_id.buf));
ASSERT_EQ(string(remote_ip.buf), string(p_session->remote_ip.buf));
}

0 comments on commit 8d620ec

Please sign in to comment.