Skip to content

Commit

Permalink
Fix unit tests.
Browse files Browse the repository at this point in the history
- Copy `__extract_request_authority` function to unit tests
  and use to set req->host value in appropriate tests as we
  do it in tempesta.

- Fix unit tests according to changes in `match_host`
  function.
  • Loading branch information
EvgeniiMekhanik committed May 26, 2023
1 parent eaa8415 commit 5082e1f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
55 changes: 44 additions & 11 deletions fw/t/unit/test_http_match.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "test.h"
#include "helpers.h"
#include "tfw_str_helper.h"
#include "test_http_parser_common.h"

#include "http_match.c"

Expand Down Expand Up @@ -186,6 +187,7 @@ test_chain_match(void)
static void
set_tfw_str(TfwStr *str, const char *cstr)
{
memset(str, 0, sizeof(TfwStr));
str->data = (void *)cstr;
str->len = strlen(cstr);
}
Expand Down Expand Up @@ -769,9 +771,36 @@ TEST(http_match, choose_host)
int match_id;

/* Special headers must be compound */
TFW_STR2(hdr1, "Host: ", "example.eu");
TFW_STR2(hdr2, "Host: ", "example.de");
TFW_STR2(hdr3, "Host: ", "example.net");
TfwStr hdr1 = {
.chunks = (TfwStr []) {
{ .data = "Host:" , .len = 5 },
{ .data = "example.eu", .len = 10,
.flags = TFW_STR_HDR_VALUE | TFW_STR_VALUE },
},
.len = 15,
.nchunks = 2
};

TfwStr hdr2 = {
.chunks = (TfwStr []) {
{ .data = "Host:" , .len = 5 },
{ .data = "example.de", .len = 10,
.flags = TFW_STR_HDR_VALUE | TFW_STR_VALUE },
},
.len = 15,
.nchunks = 2
};

TfwStr hdr3 = {
.chunks = (TfwStr []) {
{ .data = "Host:" , .len = 5 },
{ .data = "example.net", .len = 11,
.flags = TFW_STR_HDR_VALUE | TFW_STR_VALUE },
},
.len = 16,
.nchunks = 2
};

TfwStr hdr4 = {
.chunks = (TfwStr []) {
{ .data = "Forwarded:" , .len = 10 },
Expand Down Expand Up @@ -808,7 +837,6 @@ TEST(http_match, choose_host)
/* Host not specified. */
match_id = test_chain_match();
EXPECT_EQ(-1, match_id);

/* Host specified by URI. */
set_tfw_str(&test_req->host, "example.com");
match_id = test_chain_match();
Expand All @@ -818,13 +846,15 @@ TEST(http_match, choose_host)
* Host specified by URI and Host header
* host from uri must be matched.
*/
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr1;
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = hdr1;
extract_request_authority(test_req);
match_id = test_chain_match();
EXPECT_EQ(1, match_id);

/* Host specified by Host header */
set_tfw_str(&test_req->host, "");
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr1;
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = hdr1;
extract_request_authority(test_req);
match_id = test_chain_match();
EXPECT_EQ(2, match_id);

Expand All @@ -833,20 +863,22 @@ TEST(http_match, choose_host)
* and URI. Host from uri must be matched.
*/
set_tfw_str(&test_req->host, "example.com");
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr1;
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = hdr1;
test_req->h_tbl->tbl[TFW_HTTP_HDR_FORWARDED] = hdr4;
extract_request_authority(test_req);
match_id = test_chain_match();
EXPECT_EQ(1, match_id);

/*
* Host specified by Host and Forwarded headers.
* Host from Forwarded header must be matched.
* Host from Forwarded header must NOT be matched.
*/
set_tfw_str(&test_req->host, "");
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr2;
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = hdr2;
test_req->h_tbl->tbl[TFW_HTTP_HDR_FORWARDED] = hdr4;
extract_request_authority(test_req);
match_id = test_chain_match();
EXPECT_EQ(3, match_id);
EXPECT_EQ(-1, match_id);

/*
* HTTP2!
Expand All @@ -856,8 +888,9 @@ TEST(http_match, choose_host)
__set_bit(TFW_HTTP_B_H2, test_req->flags);
set_tfw_str(&test_req->host, "");
test_req->h_tbl->tbl[TFW_HTTP_HDR_H2_AUTHORITY] = hdr5;
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr3;
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = hdr3;
test_req->h_tbl->tbl[TFW_HTTP_HDR_FORWARDED] = hdr4;
extract_request_authority(test_req);
match_id = test_chain_match();
EXPECT_EQ(2, match_id);
}
Expand Down
34 changes: 34 additions & 0 deletions fw/t/unit/test_http_parser_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,8 @@ do_split_and_parse(int type, int chunk_mode)
r = tfw_h2_parse_req_finish(req);
}

extract_request_authority(req);

if (chunk_mode == CHUNK_OFF
|| CHUNK_SIZES[chunk_size_index] >= frames_max_sz)
/*
Expand Down Expand Up @@ -690,3 +692,35 @@ tfw_http_sess_redir_mark_disable(void)
hs_mod->cfgstart();
}

void
extract_request_authority(TfwHttpReq *req)
{
int hid = 0;
TfwStr *hdrs = req->h_tbl->tbl;

if (TFW_MSG_H2(req)) {
/* RFC 9113, sec-8.3.1:
* The recipient of an HTTP/2 request MUST NOT use
* the Host header field to determine the target
* URI if ":authority" is present.*/
if (!TFW_STR_EMPTY(&hdrs[TFW_HTTP_HDR_H2_AUTHORITY]))
hid = TFW_HTTP_HDR_H2_AUTHORITY;
else
hid = TFW_HTTP_HDR_HOST;
__h2_msg_hdr_val(&hdrs[hid], &req->host);
} else {
/* req->host can be only filled by HTTP/1.x parser from
* absoluteURI, so we act as described by RFC 9112, sec-3.2.2
* (https://www.rfc-editor.org/rfc/rfc9112.html#section-3.2.2):
* When an origin server receives a request with an
* absolute-form of request-target, the origin server
* MUST ignore the received Host header field (if any)
* and instead use the host information of the request-target.
*/
if (TFW_STR_EMPTY(&req->host)) {
tfw_http_msg_clnthdr_val(req, &hdrs[TFW_HTTP_HDR_HOST],
TFW_HTTP_HDR_HOST,
&req->host);
}
}
}
2 changes: 2 additions & 0 deletions fw/t/unit/test_http_parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,6 @@ TfwStr get_next_str_val(TfwStr *str);
void tfw_http_sess_redir_mark_enable(void);
void tfw_http_sess_redir_mark_disable(void);

void extract_request_authority(TfwHttpReq *req);

#endif /* __TFW_HTTP_PARSER_COMMON_H__ */

0 comments on commit 5082e1f

Please sign in to comment.