Skip to content

Commit

Permalink
testing/nettest: Add test for net bufpool
Browse files Browse the repository at this point in the history
Signed-off-by: Zhe Weng <[email protected]>
  • Loading branch information
wengzhe authored and zs39 committed Jan 14, 2025
1 parent a6b9e71 commit ff88e3c
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 0 deletions.
23 changes: 23 additions & 0 deletions testing/nettest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,27 @@ if(CONFIG_TESTING_NET_TEST)
${SRCS})
endif()

if(CONFIG_TESTING_NET_OTHERS)
set(SRCS
${CMAKE_CURRENT_LIST_DIR}/others/test_others.c
${CMAKE_CURRENT_LIST_DIR}/others/test_others_common.c
${CMAKE_CURRENT_LIST_DIR}/others/test_others_bufpool.c)

nuttx_add_application(
NAME
cmocka_net_others
PRIORITY
${CONFIG_TESTING_NET_TEST_PRIORITY}
STACKSIZE
${CONFIG_TESTING_NET_TEST_STACKSIZE}
MODULE
${CONFIG_TESTING_NET_TEST}
DEPENDS
cmocka
INCLUDE_DIRECTORIES
${CMAKE_CURRENT_LIST_DIR}/utils
SRCS
${SRCS})
endif()

endif()
5 changes: 5 additions & 0 deletions testing/nettest/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ config TESTING_NET_TEST
tristate "vela cmocka net test"
default n
depends on TESTING_CMOCKA
depends on NET
---help---
Enable the cmocka net test

Expand Down Expand Up @@ -34,4 +35,8 @@ config TESTING_NET_TCP_PORT

endif

config TESTING_NET_OTHERS
bool "Enable cmocka net other test"
default y

endif
6 changes: 6 additions & 0 deletions testing/nettest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,11 @@ endif

endif

ifeq ($(CONFIG_TESTING_NET_OTHERS),y)
MAINSRC += others/test_others.c
PROGNAME += cmocka_net_others
CSRCS += others/test_others_common.c others/test_others_bufpool.c
endif

endif
include $(APPDIR)/Application.mk
46 changes: 46 additions & 0 deletions testing/nettest/others/test_others.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/****************************************************************************
* apps/testing/nettest/others/test_others.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>

#include "test_others.h"

/****************************************************************************
* Public Functions
****************************************************************************/

int main(int argc, FAR char *argv[])
{
const struct CMUnitTest others_tests[] =
{
cmocka_unit_test(test_others_bufpool),
};

return cmocka_run_group_tests(others_tests, test_others_group_setup,
test_others_group_teardown);
}
52 changes: 52 additions & 0 deletions testing/nettest/others/test_others.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/****************************************************************************
* apps/testing/nettest/others/test_others.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

#ifndef __APPS_TESTING_NETTEST_OTHERS_TEST_OTHERS_H
#define __APPS_TESTING_NETTEST_OTHERS_TEST_OTHERS_H

/****************************************************************************
* Included Files
****************************************************************************/

#include <nuttx/compiler.h>

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

/****************************************************************************
* Name: test_others_group_setup
****************************************************************************/

int test_others_group_setup(FAR void **state);

/****************************************************************************
* Name: test_others_group_teardown
****************************************************************************/

int test_others_group_teardown(FAR void **state);

/****************************************************************************
* Name: test_others_bufpool
****************************************************************************/

void test_others_bufpool(FAR void **state);

#endif /* __APPS_TESTING_NETTEST_OTHERS_TEST_OTHERS_H */
134 changes: 134 additions & 0 deletions testing/nettest/others/test_others_bufpool.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/****************************************************************************
* apps/testing/nettest/others/test_others_bufpool.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include <errno.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>

#include "../../nuttx/net/utils/utils.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define TEST_PREALLOC 10
#define TEST_DYNALLOC 1
#define TEST_MAXALLOC 20

/****************************************************************************
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: test_net_bufpool_alloc_to
****************************************************************************/

static void test_net_bufpool_alloc_to(FAR struct net_bufpool_s *pool,
FAR sq_queue_t *allocated)
{
FAR sq_entry_t *node;
assert_true(NET_BUFPOOL_TEST(*pool) == OK);
node = NET_BUFPOOL_ALLOC(*pool);
assert_non_null(node);
sq_addlast(node, allocated);
}

/****************************************************************************
* Name: test_net_bufpool_free_from
****************************************************************************/

static void test_net_bufpool_free_from(FAR struct net_bufpool_s *pool,
FAR sq_queue_t *allocated)
{
while (!sq_empty(allocated))
{
FAR sq_entry_t *node = sq_remfirst(allocated);
NET_BUFPOOL_FREE(*pool, node);
assert_true(NET_BUFPOOL_TEST(*pool) == OK);
}
}

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: test_others_bufpool
****************************************************************************/

void test_others_bufpool(FAR void **state)
{
NET_BUFPOOL_DECLARE(fixed, sizeof(sq_entry_t), TEST_PREALLOC, 0, 0);
NET_BUFPOOL_DECLARE(limited, sizeof(sq_entry_t), TEST_PREALLOC,
TEST_DYNALLOC, TEST_MAXALLOC);
NET_BUFPOOL_DECLARE(unlimited, sizeof(sq_entry_t), TEST_PREALLOC,
TEST_DYNALLOC, 0);
sq_queue_t allocated_fixed;
sq_queue_t allocated_limited;
sq_queue_t allocated_unlimited;
FAR sq_entry_t *node;
int i;

#ifdef NET_BUFPOOL_INIT
NET_BUFPOOL_INIT(fixed);
NET_BUFPOOL_INIT(limited);
NET_BUFPOOL_INIT(unlimited);
#endif
sq_init(&allocated_fixed);
sq_init(&allocated_limited);
sq_init(&allocated_unlimited);

for (i = 0; i < TEST_PREALLOC; i++)
{
test_net_bufpool_alloc_to(&fixed, &allocated_fixed);
test_net_bufpool_alloc_to(&limited, &allocated_limited);
test_net_bufpool_alloc_to(&unlimited, &allocated_unlimited);
}

for (i = TEST_PREALLOC; i < TEST_MAXALLOC; i++)
{
test_net_bufpool_alloc_to(&limited, &allocated_limited);
test_net_bufpool_alloc_to(&unlimited, &allocated_unlimited);
}

node = NET_BUFPOOL_TRYALLOC(fixed);
assert_null(node);
assert_true(NET_BUFPOOL_TEST(fixed) == -ENOSPC);

node = NET_BUFPOOL_TRYALLOC(limited);
assert_null(node);
assert_true(NET_BUFPOOL_TEST(limited) == -ENOSPC);

node = NET_BUFPOOL_TRYALLOC(unlimited);
assert_non_null(node);
assert_true(NET_BUFPOOL_TEST(unlimited) == OK);
NET_BUFPOOL_FREE(unlimited, node);

test_net_bufpool_free_from(&fixed, &allocated_fixed);
test_net_bufpool_free_from(&limited, &allocated_limited);
test_net_bufpool_free_from(&unlimited, &allocated_unlimited);
}
48 changes: 48 additions & 0 deletions testing/nettest/others/test_others_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/****************************************************************************
* apps/testing/nettest/others/test_others_common.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/

/****************************************************************************
* Included Files
****************************************************************************/

#include "test_others.h"

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: test_others_group_setup
****************************************************************************/

int test_others_group_setup(FAR void **state)
{
return 0;
}

/****************************************************************************
* Name: test_others_group_teardown
****************************************************************************/

int test_others_group_teardown(FAR void **state)
{
return 0;
}

0 comments on commit ff88e3c

Please sign in to comment.