Skip to content

Commit a2e0961

Browse files
committed
Add tests for testing the "suffix" match operator. (#495, #471)
1 parent b2b8ebc commit a2e0961

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

tempesta_fw/t/unit/test_http_match.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,37 @@ TEST(http_match, uri_prefix)
135135
EXPECT_EQ(-1, match_id);
136136
}
137137

138+
TEST(http_match, uri_suffix)
139+
{
140+
int match_id;
141+
142+
test_mlst_add(1, TFW_HTTP_MATCH_F_URI, TFW_HTTP_MATCH_O_SUFFIX,
143+
".jpg");
144+
test_mlst_add(2, TFW_HTTP_MATCH_F_URI, TFW_HTTP_MATCH_O_SUFFIX,
145+
"/people.html");
146+
test_mlst_add(3, TFW_HTTP_MATCH_F_URI, TFW_HTTP_MATCH_O_SUFFIX,
147+
"/bar/folks.html");
148+
149+
set_tfw_str(&test_req->uri_path, "/foo/bar/picture.jpg");
150+
match_id = test_mlst_match();
151+
EXPECT_EQ(1, match_id);
152+
153+
set_tfw_str(&test_req->uri_path, "/foo/bar/people.html");
154+
match_id = test_mlst_match();
155+
EXPECT_EQ(2, match_id);
156+
157+
set_tfw_str(&test_req->uri_path, "/foo/bar/folks.html");
158+
match_id = test_mlst_match();
159+
EXPECT_EQ(3, match_id);
160+
161+
set_tfw_str(&test_req->uri_path, "../foo");
162+
match_id = test_mlst_match();
163+
EXPECT_EQ(-1, match_id);
164+
165+
set_tfw_str(&test_req->uri_path, "/foo/bar/picture.png");
166+
match_id = test_mlst_match();
167+
EXPECT_EQ(-1, match_id);
168+
}
138169
TEST(http_match, host_eq)
139170
{
140171
int match_id;
@@ -217,6 +248,64 @@ TEST(http_match, hdr_host_prefix)
217248
free_all_str();
218249
}
219250

251+
TEST(http_match, hdr_host_suffix)
252+
{
253+
create_str_pool();
254+
255+
{
256+
int match_id;
257+
258+
/* Special headers must be compound */
259+
TFW_STR2(hdr1, "Host: ", "example.biz");
260+
TFW_STR2(hdr2, "Host: ", "example.com");
261+
TFW_STR2(hdr3, "Host: ", "example.ru");
262+
TFW_STR2(hdr4, "Host: ", "eXample.COM");
263+
TFW_STR2(hdr5, "Host: ", "www");
264+
TFW_STR2(hdr6, "Host: ", "TEST.FOLKS.COM");
265+
266+
test_mlst_add(1, TFW_HTTP_MATCH_F_HDR_CONN,
267+
TFW_HTTP_MATCH_O_EQ, "Connection: Keep-Alive");
268+
test_mlst_add(2, TFW_HTTP_MATCH_F_HDR_HOST,
269+
TFW_HTTP_MATCH_O_SUFFIX, ".ru");
270+
test_mlst_add(3, TFW_HTTP_MATCH_F_HDR_HOST,
271+
TFW_HTTP_MATCH_O_SUFFIX, ".biz");
272+
test_mlst_add(4, TFW_HTTP_MATCH_F_HDR_HOST,
273+
TFW_HTTP_MATCH_O_SUFFIX, ".folks.com");
274+
test_mlst_add(5, TFW_HTTP_MATCH_F_HDR_HOST,
275+
TFW_HTTP_MATCH_O_SUFFIX, ".com");
276+
277+
set_tfw_str(&test_req->host, "example.com");
278+
match_id = test_mlst_match();
279+
EXPECT_EQ(-1, match_id);
280+
281+
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr1;
282+
match_id = test_mlst_match();
283+
EXPECT_EQ(3, match_id);
284+
285+
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr2;
286+
match_id = test_mlst_match();
287+
EXPECT_EQ(5, match_id);
288+
289+
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr3;
290+
match_id = test_mlst_match();
291+
EXPECT_EQ(2, match_id);
292+
293+
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr4;
294+
match_id = test_mlst_match();
295+
EXPECT_EQ(5, match_id);
296+
297+
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr5;
298+
match_id = test_mlst_match();
299+
EXPECT_EQ(-1, match_id);
300+
301+
test_req->h_tbl->tbl[TFW_HTTP_HDR_HOST] = *hdr6;
302+
match_id = test_mlst_match();
303+
EXPECT_EQ(4, match_id);
304+
}
305+
306+
free_all_str();
307+
}
308+
220309
TEST(http_match, method_eq)
221310
{
222311
int match_id;
@@ -259,8 +348,10 @@ TEST_SUITE(http_match)
259348

260349
TEST_RUN(tfw_http_match_req, returns_first_matching_rule);
261350
TEST_RUN(http_match, uri_prefix);
351+
TEST_RUN(http_match, uri_suffix);
262352
TEST_RUN(http_match, host_eq);
263353
TEST_RUN(http_match, headers_eq);
264354
TEST_RUN(http_match, hdr_host_prefix);
355+
TEST_RUN(http_match, hdr_host_suffix);
265356
TEST_RUN(http_match, method_eq);
266357
}

tempesta_fw/t/unit/test_tfw_str.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,53 @@ TEST(tfw_str_eq_cstr, supports_prefix)
364364
TFW_STR_EQ_PREFIX_CASEI));
365365
}
366366

367+
TEST(tfw_str_eq_cstr_off, supports_suffix)
368+
{
369+
TFW_STR(s, "/foo/bar/baz.test");
370+
const char *p1 = "/foo/bar/baz.test";
371+
const char *p2 = "foo/bar/baz.test";
372+
const char *p3 = "bar/baz.test";
373+
const char *p4 = "/baz.test";
374+
const char *p5 = ".test";
375+
const char *p6 = "";
376+
const char *f1 = "/bar/foo/baz.test";
377+
const char *f2 = "/foo/bar/";
378+
const char *extra = "/bar/foo/baz.test100";
379+
const char *i1 = "/foo/bar/baz.tesT";
380+
const char *i2 = ".TeSt";
381+
382+
#define X_EXPECT_TRUE(s, p, flags) \
383+
do { \
384+
int plen = strlen(p); \
385+
EXPECT_TRUE(tfw_str_eq_cstr_off(s, s->len - plen, p, plen, flags)); \
386+
} while(0)
387+
#define X_EXPECT_FALSE(s, p, flags) \
388+
do { \
389+
int plen = strlen(p); \
390+
EXPECT_FALSE(tfw_str_eq_cstr_off(s, s->len - plen, p, plen, flags)); \
391+
} while(0)
392+
393+
X_EXPECT_TRUE(s, p1, TFW_STR_EQ_DEFAULT);
394+
X_EXPECT_TRUE(s, p2, TFW_STR_EQ_DEFAULT);
395+
X_EXPECT_TRUE(s, p3, TFW_STR_EQ_DEFAULT);
396+
X_EXPECT_TRUE(s, p4, TFW_STR_EQ_DEFAULT);
397+
X_EXPECT_TRUE(s, p5, TFW_STR_EQ_DEFAULT);
398+
X_EXPECT_TRUE(s, p6, TFW_STR_EQ_DEFAULT);
399+
400+
X_EXPECT_FALSE(s, f1, TFW_STR_EQ_DEFAULT);
401+
X_EXPECT_FALSE(s, f2, TFW_STR_EQ_DEFAULT);
402+
403+
X_EXPECT_FALSE(s, extra, TFW_STR_EQ_DEFAULT);
404+
X_EXPECT_FALSE(s, i1, TFW_STR_EQ_DEFAULT);
405+
X_EXPECT_FALSE(s, i2, TFW_STR_EQ_DEFAULT);
406+
407+
X_EXPECT_FALSE(s, i1, TFW_STR_EQ_DEFAULT | TFW_STR_EQ_CASEI);
408+
X_EXPECT_FALSE(s, i2, TFW_STR_EQ_DEFAULT | TFW_STR_EQ_CASEI);
409+
410+
#undef X_EXPECT_TRUE
411+
#undef X_EXPECT_FALSE
412+
}
413+
367414
static const char *foxstr = "The quick brown fox jumps over the lazy dog";
368415

369416
TEST(tfw_str_eq_cstr_pos, plain)
@@ -395,6 +442,32 @@ TEST(tfw_str_eq_cstr_pos, plain)
395442

396443
}
397444

445+
TEST(tfw_str_eq_cstr_off, plain)
446+
{
447+
TfwStr *fox = make_plain_str(foxstr);
448+
long i, offset = 0, foxlen = fox->len;
449+
450+
for (i = 0; i < fox->len; i++) {
451+
EXPECT_TRUE(tfw_str_eq_cstr_off(fox, fox->len + i,
452+
foxstr + offset,
453+
foxlen - offset,
454+
TFW_STR_EQ_CASEI));
455+
EXPECT_FALSE(tfw_str_eq_cstr_off(fox, fox->len + i,
456+
"1234567890", 10,
457+
TFW_STR_EQ_CASEI));
458+
++offset;
459+
}
460+
461+
EXPECT_TRUE(tfw_str_eq_cstr_off(fox, foxlen,
462+
foxstr, foxlen, TFW_STR_EQ_CASEI));
463+
464+
EXPECT_FALSE(tfw_str_eq_cstr_off(fox, foxlen + 1,
465+
foxstr, foxlen, TFW_STR_EQ_CASEI));
466+
EXPECT_FALSE(tfw_str_eq_cstr_off(fox, -1,
467+
foxstr, foxlen, TFW_STR_EQ_CASEI));
468+
469+
}
470+
398471
TEST(tfw_str_eq_cstr_pos, compound)
399472
{
400473
TfwStr *fox = make_compound_str(foxstr), *c, *end;
@@ -423,6 +496,32 @@ TEST(tfw_str_eq_cstr_pos, compound)
423496
TFW_STR_EQ_CASEI));
424497
}
425498

499+
TEST(tfw_str_eq_cstr_off, compound)
500+
{
501+
TfwStr *fox = make_compound_str(foxstr);
502+
long i, offset = 0, foxlen = fox->len;
503+
504+
for (i = 0; i < fox->len; i++) {
505+
EXPECT_TRUE(tfw_str_eq_cstr_off(fox, fox->len + i,
506+
foxstr + offset,
507+
foxlen - offset,
508+
TFW_STR_EQ_CASEI));
509+
EXPECT_FALSE(tfw_str_eq_cstr_off(fox, fox->len + i,
510+
"1234567890", 10,
511+
TFW_STR_EQ_CASEI));
512+
++offset;
513+
}
514+
515+
EXPECT_TRUE(tfw_str_eq_cstr_off(fox, foxlen,
516+
foxstr, foxlen, TFW_STR_EQ_CASEI));
517+
518+
EXPECT_FALSE(tfw_str_eq_cstr_off(fox, foxlen + 1,
519+
foxstr, foxlen, TFW_STR_EQ_CASEI));
520+
EXPECT_FALSE(tfw_str_eq_cstr_off(fox, -1,
521+
foxstr, foxlen, TFW_STR_EQ_CASEI));
522+
523+
}
524+
426525
TEST_SUITE(tfw_str)
427526
{
428527
TEST_SETUP(create_str_pool);
@@ -449,7 +548,10 @@ TEST_SUITE(tfw_str)
449548
TEST_RUN(tfw_str_eq_cstr, handles_empty_strs);
450549
TEST_RUN(tfw_str_eq_cstr, supports_casei);
451550
TEST_RUN(tfw_str_eq_cstr, supports_prefix);
551+
TEST_RUN(tfw_str_eq_cstr_off, supports_suffix);
452552

453553
TEST_RUN(tfw_str_eq_cstr_pos, plain);
554+
TEST_RUN(tfw_str_eq_cstr_off, plain);
454555
TEST_RUN(tfw_str_eq_cstr_pos, compound);
556+
TEST_RUN(tfw_str_eq_cstr_off, compound);
455557
}

0 commit comments

Comments
 (0)