Skip to content

Commit

Permalink
Add tests for the ring buffer mapped to userspace
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Ivanov <[email protected]>
  • Loading branch information
ai-tmpst committed Oct 10, 2024
1 parent 064efbf commit c4cbdfb
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions fw/t/unit/ringbuffer.c
4 changes: 4 additions & 0 deletions fw/t/unit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ TEST_SUITE(tls);
TEST_SUITE(hpack);
TEST_SUITE(pool);
TEST_SUITE(ebtree);
TEST_SUITE(ringbuffer);

extern int tfw_pool_init(void);
extern void tfw_pool_exit(void);
Expand Down Expand Up @@ -160,6 +161,9 @@ test_run_all(void)
TEST_SUITE_RUN(ebtree);
__fpu_schedule();

TEST_SUITE_RUN(ringbuffer);
__fpu_schedule();

kernel_fpu_end();

tfw_pool_exit();
Expand Down
129 changes: 129 additions & 0 deletions fw/t/unit/test_ringbuffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Tempesta FW
*
* Copyright (C) 2024 Tempesta Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "ringbuffer.h"
#include "tfw_str_helper.h"
#include "test.h"

#undef EXPORT_SYMBOL
#define EXPORT_SYMBOL(...)

static void
fill_buf(unsigned char *buf, int size)
{
int i;
for (i = 0; i < size; ++i)
buf[i] = i % 256;
}

static void
test_write_read(TfwStr *s1, char *buf, int expect_wr, int expect_rd)
{
int r;
TfwStr s2;

TFW_STR_INIT(&s2);
s2.data = buf;
s2.len = s1->len;

r = tfw_ringbuffer_write(&s1, 1);
EXPECT_EQ(r, expect_wr);
if (r != 0)
return;
EXPECT_EQ(tfw_ringbuffer_test_read(buf, s2.len), expect_rd);
EXPECT_EQ(tfw_strcmp(s1, &s2), 0);
}

TEST(tfw_ringbuffer, init)
{
EXPECT_EQ(tfw_ringbuffer_init(0), -EINVAL);
EXPECT_EQ(tfw_ringbuffer_init(TFW_RINGBUFFER_MIN_SIZE - 1), -EINVAL);
EXPECT_EQ(tfw_ringbuffer_init(TFW_RINGBUFFER_MIN_SIZE + 1), -EINVAL);
EXPECT_EQ(tfw_ringbuffer_init(TFW_RINGBUFFER_MAX_SIZE * 2), -EINVAL);
EXPECT_EQ(tfw_ringbuffer_init(TFW_RINGBUFFER_MIN_SIZE), 0);
tfw_ringbuffer_cleanup();
}

TEST(tfw_ringbuffer, write_read)
{
unsigned char *buf_wr, *buf_rd;
TfwStr *s, sb;
TfwStr *sps[16];
int i;

create_str_pool();

kernel_fpu_end();
buf_wr = vmalloc(TFW_RINGBUFFER_MIN_SIZE);
buf_rd = vmalloc(TFW_RINGBUFFER_MIN_SIZE);
kernel_fpu_begin();

fill_buf(buf_wr, TFW_RINGBUFFER_MIN_SIZE);
TFW_STR_INIT(&sb);
sb.data = buf_wr;

EXPECT_EQ(tfw_ringbuffer_init(TFW_RINGBUFFER_MIN_SIZE), 0);

s = make_plain_str("0123456789");
test_write_read(s, buf_rd, -EAGAIN, 0);
tfw_ringbuffer_test_set_unmapped(0);
test_write_read(s, buf_rd, 0, 0);

s = make_compound_str("0123456789");
test_write_read(s, buf_rd, 0, 0);

sb.len = TFW_RINGBUFFER_MIN_SIZE;
test_write_read(&sb, buf_rd, -ENOMEM, 0);

sb.len = TFW_RINGBUFFER_MIN_SIZE - 1;
test_write_read(&sb, buf_rd, 0, 0);

sb.len = 0;
test_write_read(&sb, buf_rd, 0, 0);

sb.len = 1000;
test_write_read(&sb, buf_rd, 0, 0);

for (i = 0; i < 16; ++i) {
sps[i] = make_compound_str("0123456789");
sps[i]->data[0] += i;
}
EXPECT_EQ(tfw_ringbuffer_write(sps, 16), 0);
EXPECT_EQ(tfw_ringbuffer_test_read(buf_rd, 160), 0);
for (i = 0; i < 16; ++i) {
int j;
EXPECT_EQ(buf_rd[i*10], '0' + i);
for (j = 1; j < 10; ++j)
EXPECT_EQ(buf_rd[i*10+j], '0' + j);
break;
}

kernel_fpu_end();
vfree(buf_rd);
vfree(buf_wr);
kernel_fpu_begin();

tfw_ringbuffer_cleanup();
}

TEST_SUITE(ringbuffer)
{
TEST_RUN(tfw_ringbuffer, init);
TEST_RUN(tfw_ringbuffer, write_read);
}

0 comments on commit c4cbdfb

Please sign in to comment.