mirror of
https://git.sr.ht/~thestr4ng3r/chiaki
synced 2025-08-14 18:57:07 -07:00
Add Test for Takion Send Buffer
This commit is contained in:
parent
8914ac5f0b
commit
03b81e9509
6 changed files with 192 additions and 4 deletions
|
@ -10,7 +10,9 @@ add_executable(chiaki-unit
|
|||
takion.c
|
||||
seqnum.c
|
||||
reorderqueue.c
|
||||
fec.c)
|
||||
fec.c
|
||||
test_log.c
|
||||
test_log.h)
|
||||
|
||||
target_link_libraries(chiaki-unit chiaki-lib munit)
|
||||
|
||||
|
|
112
test/takion.c
112
test/takion.c
|
@ -18,8 +18,14 @@
|
|||
#include <munit.h>
|
||||
|
||||
#include <chiaki/takion.h>
|
||||
#include <chiaki/seqnum.h>
|
||||
#include <chiaki/base64.h>
|
||||
|
||||
#define CHIAKI_UNIT_TEST
|
||||
#include "../lib/src/takionsendbuffer.c"
|
||||
|
||||
#include "test_log.h"
|
||||
|
||||
|
||||
static MunitResult test_av_packet_parse(const MunitParameter params[], void *user)
|
||||
{
|
||||
|
@ -67,6 +73,104 @@ static MunitResult test_av_packet_parse_real_video(const MunitParameter params[]
|
|||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static void random_seqnums(ChiakiSeqNum32 *nums, size_t count)
|
||||
{
|
||||
for(size_t i=0; i<count; i++)
|
||||
{
|
||||
ChiakiSeqNum32 seqnum;
|
||||
retry:
|
||||
seqnum = munit_rand_uint32();
|
||||
for(size_t j=0; j<i; j++)
|
||||
{
|
||||
if(nums[j] == seqnum)
|
||||
goto retry;
|
||||
}
|
||||
nums[i] = seqnum;
|
||||
}
|
||||
}
|
||||
|
||||
static bool check_send_buffer_contents(ChiakiTakionSendBuffer *send_buffer, const ChiakiSeqNum32 *nums_expected, size_t nums_expected_count)
|
||||
{
|
||||
// nums_expected must be unique
|
||||
|
||||
if(chiaki_mutex_lock(&send_buffer->mutex) != CHIAKI_ERR_SUCCESS)
|
||||
return false;
|
||||
|
||||
if(send_buffer->packets_count != nums_expected_count)
|
||||
goto fail;
|
||||
|
||||
for(size_t i=0; i<nums_expected_count; i++)
|
||||
{
|
||||
bool found = false;
|
||||
for(size_t j=0; j<send_buffer->packets_count; j++)
|
||||
{
|
||||
if(send_buffer->packets[j].seq_num == nums_expected[i])
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!found)
|
||||
goto fail;
|
||||
}
|
||||
|
||||
chiaki_mutex_unlock(&send_buffer->mutex);
|
||||
return true;
|
||||
fail:
|
||||
chiaki_mutex_unlock(&send_buffer->mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void seqnums_ack(ChiakiSeqNum32 *nums, size_t *nums_count, ChiakiSeqNum32 ack_num)
|
||||
{
|
||||
// simulate ack of ack_num
|
||||
for(size_t i=0; i<*nums_count; i++)
|
||||
{
|
||||
if(nums[i] == ack_num || chiaki_seq_num_32_lt(nums[i], ack_num))
|
||||
{
|
||||
for(size_t j=i+1; j<*nums_count; j++)
|
||||
nums[j-1] = nums[j];
|
||||
(*nums_count)--;
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static MunitResult test_takion_send_buffer(const MunitParameter params[], void *user)
|
||||
{
|
||||
static const size_t nums_count = 0x30;
|
||||
ChiakiTakionSendBuffer send_buffer;
|
||||
ChiakiErrorCode err = chiaki_takion_send_buffer_init(&send_buffer, NULL, nums_count);
|
||||
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
|
||||
send_buffer.log = get_test_log();
|
||||
|
||||
ChiakiSeqNum32 nums_expected[nums_count + 1];
|
||||
random_seqnums(nums_expected, nums_count + 1);
|
||||
|
||||
for(size_t i=0; i<nums_count; i++)
|
||||
{
|
||||
err = chiaki_takion_send_buffer_push(&send_buffer, nums_expected[i], malloc(8), 8);
|
||||
munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
|
||||
}
|
||||
|
||||
err = chiaki_takion_send_buffer_push(&send_buffer, nums_expected[nums_count], malloc(8), 8);
|
||||
munit_assert_int(err, ==, CHIAKI_ERR_OVERFLOW);
|
||||
|
||||
size_t nums_count_cur = nums_count;
|
||||
while(nums_count_cur > 0)
|
||||
{
|
||||
ChiakiSeqNum32 ack_num = nums_expected[nums_count_cur - 1]
|
||||
+ munit_rand_int_range(-1, 1) * munit_rand_int_range(1, 32);
|
||||
chiaki_takion_send_buffer_ack(&send_buffer, ack_num);
|
||||
seqnums_ack(nums_expected, &nums_count_cur, ack_num);
|
||||
bool correct = check_send_buffer_contents(&send_buffer, nums_expected, nums_count_cur);
|
||||
munit_assert(correct);
|
||||
}
|
||||
|
||||
chiaki_takion_send_buffer_fini(&send_buffer);
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
MunitTest tests_takion[] = {
|
||||
|
@ -86,5 +190,13 @@ MunitTest tests_takion[] = {
|
|||
MUNIT_TEST_OPTION_NONE,
|
||||
NULL
|
||||
},
|
||||
{
|
||||
"/send_buffer",
|
||||
test_takion_send_buffer,
|
||||
NULL,
|
||||
NULL,
|
||||
MUNIT_TEST_OPTION_NONE,
|
||||
NULL
|
||||
},
|
||||
{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
|
||||
};
|
30
test/test_log.c
Normal file
30
test/test_log.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* This file is part of Chiaki.
|
||||
*
|
||||
* Chiaki 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Chiaki 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 Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "test_log.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
static bool initialized = false;
|
||||
static ChiakiLog log_quiet;
|
||||
|
||||
ChiakiLog *get_test_log()
|
||||
{
|
||||
if(!initialized)
|
||||
chiaki_log_init(&log_quiet, 0, NULL, NULL);
|
||||
return &log_quiet;
|
||||
}
|
25
test/test_log.h
Normal file
25
test/test_log.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* This file is part of Chiaki.
|
||||
*
|
||||
* Chiaki 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 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Chiaki 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 Chiaki. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef CHIAKI_TEST_LOG_H
|
||||
#define CHIAKI_TEST_LOG_H
|
||||
|
||||
#include <chiaki/log.h>
|
||||
|
||||
ChiakiLog *get_test_log();
|
||||
|
||||
#endif // CHIAKI_LOG_QUIET_H
|
Loading…
Add table
Add a link
Reference in a new issue