From patchwork Wed Nov 19 13:39:03 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Taras Kondratiuk X-Patchwork-Id: 41160 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 601DB20CBB for ; Wed, 19 Nov 2014 13:39:27 +0000 (UTC) Received: by mail-wi0-f197.google.com with SMTP id l15sf718496wiw.0 for ; Wed, 19 Nov 2014 05:39:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:subject :precedence:list-id:list-unsubscribe:list-archive:list-post :list-help:list-subscribe:mime-version:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=OiKm4T25F3MGw29A1sFTsNEQkGUNnS71p03fr3IRkuw=; b=ipBSeOtprr71et3/CPKHAf70/50BBk7QPIHnPHZKxkUEByt+tI96jdzmZ/TxAmOTa+ OB8LIPZog64/dvY4hc60FW8K/xpAuqwOksc65R3vKgAC3ZZ2XkKexHP4pO4xCkodaQaX 1UBNFUIih7+F8aWd1LBWdqyfOiBIdWaehhxvFtlIcElqrahMJh7dZO2+TvVDsdFS0ofG +YKYa0FT1gGvhlq9Vf26AkPgkXjJw3jenv62pyq/apDKuIou13+VhCopGI42Bv74nNXA 7qYYe6rR1s+4m++24UcaWrZch7ubk114iq3oAVIxWR1rL3O8cbtHme9e8TYv89ahaUuH I//Q== X-Gm-Message-State: ALoCoQn1kMs5lEzidR368t/B0xSN/JgkdJfSdQsj4gQvEyFyLJbKgFPZftqmjHrzZSs1R1YWIVI4 X-Received: by 10.152.29.226 with SMTP id n2mr60638lah.9.1416404366635; Wed, 19 Nov 2014 05:39:26 -0800 (PST) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.120.4 with SMTP id ky4ls1476436lab.23.gmail; Wed, 19 Nov 2014 05:39:26 -0800 (PST) X-Received: by 10.152.204.9 with SMTP id ku9mr5429436lac.55.1416404366472; Wed, 19 Nov 2014 05:39:26 -0800 (PST) Received: from mail-la0-f48.google.com (mail-la0-f48.google.com. [209.85.215.48]) by mx.google.com with ESMTPS id pt7si1790964lbb.120.2014.11.19.05.39.26 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Nov 2014 05:39:26 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.48 as permitted sender) client-ip=209.85.215.48; Received: by mail-la0-f48.google.com with SMTP id s18so509656lam.35 for ; Wed, 19 Nov 2014 05:39:26 -0800 (PST) X-Received: by 10.152.37.69 with SMTP id w5mr5319865laj.67.1416404366244; Wed, 19 Nov 2014 05:39:26 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.184.201 with SMTP id ew9csp82782lbc; Wed, 19 Nov 2014 05:39:24 -0800 (PST) X-Received: by 10.52.234.230 with SMTP id uh6mr31545377vdc.10.1416404364407; Wed, 19 Nov 2014 05:39:24 -0800 (PST) Received: from ip-10-35-177-41.ec2.internal (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTPS id ch7si660487vcb.90.2014.11.19.05.39.23 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 19 Nov 2014 05:39:24 -0800 (PST) Received-SPF: none (google.com: lng-odp-bounces@lists.linaro.org does not designate permitted sender hosts) client-ip=54.225.227.206; Received: from localhost ([127.0.0.1] helo=ip-10-35-177-41.ec2.internal) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1Xr5Te-0004jO-2k; Wed, 19 Nov 2014 13:39:22 +0000 Received: from mail-la0-f45.google.com ([209.85.215.45]) by ip-10-35-177-41.ec2.internal with esmtp (Exim 4.76) (envelope-from ) id 1Xr5TX-0004jG-Oz for lng-odp@lists.linaro.org; Wed, 19 Nov 2014 13:39:16 +0000 Received: by mail-la0-f45.google.com with SMTP id gq15so507351lab.4 for ; Wed, 19 Nov 2014 05:39:10 -0800 (PST) X-Received: by 10.152.120.73 with SMTP id la9mr42286320lab.23.1416404349894; Wed, 19 Nov 2014 05:39:09 -0800 (PST) Received: from uglx0153363.synapse.com ([195.238.92.128]) by mx.google.com with ESMTPSA id q7sm440154laq.32.2014.11.19.05.39.08 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 19 Nov 2014 05:39:09 -0800 (PST) From: Taras Kondratiuk To: lng-odp@lists.linaro.org Date: Wed, 19 Nov 2014 15:39:03 +0200 Message-Id: <1416404343-2188-1-git-send-email-taras.kondratiuk@linaro.org> X-Mailer: git-send-email 1.7.9.5 X-Topics: patch Subject: [lng-odp] [RFC PATCH] cunit: buffer: add buffer tests X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: lng-odp-bounces@lists.linaro.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: taras.kondratiuk@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.48 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Initial version of buffer pool, buffer and packet API tests. Based on Bill's buffer pool API update series [1]. odp_packet_seg_*() functions are not implemented in linux-generic, so these tests are guarded by TEST_PACKET_SEGMENTS define. Current linux-generic has issue in odp_buffer_pool_create(). Created buffer has a wrong number of buffers, so many tests are failing. [1] http://lists.linaro.org/pipermail/lng-odp/2014-November/004992.html Signed-off-by: Taras Kondratiuk --- configure.ac | 1 + test/cunit/Makefile.am | 2 + test/cunit/buffer/Makefile.am | 11 + test/cunit/buffer/odp_buffer_pool_test.c | 213 ++++++++++++++++++ test/cunit/buffer/odp_buffer_test.c | 52 +++++ test/cunit/buffer/odp_buffer_testsuites.c | 51 +++++ test/cunit/buffer/odp_buffer_testsuites.h | 30 +++ test/cunit/buffer/odp_packet_test.c | 337 +++++++++++++++++++++++++++++ 8 files changed, 697 insertions(+) create mode 100644 test/cunit/buffer/Makefile.am create mode 100644 test/cunit/buffer/odp_buffer_pool_test.c create mode 100644 test/cunit/buffer/odp_buffer_test.c create mode 100644 test/cunit/buffer/odp_buffer_testsuites.c create mode 100644 test/cunit/buffer/odp_buffer_testsuites.h create mode 100644 test/cunit/buffer/odp_packet_test.c diff --git a/configure.ac b/configure.ac index fcd7279..7a4b277 100644 --- a/configure.ac +++ b/configure.ac @@ -173,6 +173,7 @@ AC_CONFIG_FILES([Makefile test/Makefile test/api_test/Makefile test/cunit/Makefile + test/cunit/buffer/Makefile pkgconfig/libodp.pc]) AC_SEARCH_LIBS([timer_create],[rt posix4]) diff --git a/test/cunit/Makefile.am b/test/cunit/Makefile.am index 4014bed..04c94b2 100644 --- a/test/cunit/Makefile.am +++ b/test/cunit/Makefile.am @@ -3,6 +3,8 @@ include $(top_srcdir)/test/Makefile.inc AM_CFLAGS += -I$(CUNIT_PATH)/include AM_LDFLAGS += -L$(CUNIT_PATH)/lib +SUBDIRS = buffer + if ODP_CUNIT_ENABLED TESTS = ${bin_PROGRAMS} check_PROGRAMS = ${bin_PROGRAMS} diff --git a/test/cunit/buffer/Makefile.am b/test/cunit/buffer/Makefile.am new file mode 100644 index 0000000..071a701 --- /dev/null +++ b/test/cunit/buffer/Makefile.am @@ -0,0 +1,11 @@ +include $(top_srcdir)/test/Makefile.inc + +if ODP_CUNIT_ENABLED +bin_PROGRAMS = odp_buffer_test +odp_buffer_test_LDFLAGS = $(AM_LDFLAGS) -static -lcunit +endif + +dist_odp_buffer_test_SOURCES = odp_buffer_pool_test.c \ + odp_buffer_test.c \ + odp_packet_test.c \ + odp_buffer_testsuites.c diff --git a/test/cunit/buffer/odp_buffer_pool_test.c b/test/cunit/buffer/odp_buffer_pool_test.c new file mode 100644 index 0000000..cd7711e --- /dev/null +++ b/test/cunit/buffer/odp_buffer_pool_test.c @@ -0,0 +1,213 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "odp_buffer_testsuites.h" + +static int pool_name_number = 1; +static const int default_buffer_size = 1500; +static const int default_buffer_num = 1000; + +odp_buffer_pool_t pool_create(int buf_num, int buf_size, int buf_type) +{ + odp_buffer_pool_t pool; + char pool_name[ODP_BUFFER_POOL_NAME_LEN]; + odp_buffer_pool_param_t params = { + .buf_size = buf_size, + .buf_align = ODP_CACHE_LINE_SIZE, + .num_bufs = buf_num, + .buf_type = buf_type, + }; + + snprintf(pool_name, sizeof(pool_name), + "test_buffer_pool-%d", pool_name_number++); + + pool = odp_buffer_pool_create(pool_name, ODP_SHM_INVALID, ¶ms); + CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID); + + return pool; +} + +static void pool_create_destroy_type(int type) +{ + odp_buffer_pool_t pool; + pool = pool_create(default_buffer_num, default_buffer_size, type); + + CU_ASSERT(odp_buffer_pool_destroy(pool) == 0); +} + +static void pool_create_destroy_raw(void) +{ + pool_create_destroy_type(ODP_BUFFER_TYPE_RAW); +} + +static void pool_create_destroy_packet(void) +{ + pool_create_destroy_type(ODP_BUFFER_TYPE_PACKET); +} + +static void pool_create_destroy_timeout(void) +{ + pool_create_destroy_type(ODP_BUFFER_TYPE_TIMEOUT); +} + +static void pool_create_destroy_any(void) +{ + pool_create_destroy_type(ODP_BUFFER_TYPE_ANY); +} + +static void pool_create_destroy_raw_shm(void) +{ + odp_buffer_pool_t pool; + odp_shm_t test_shm; + odp_buffer_pool_param_t params = { + .buf_size = 1500, + .buf_align = ODP_CACHE_LINE_SIZE, + .num_bufs = 10, + .buf_type = ODP_BUFFER_TYPE_RAW, + }; + + test_shm = odp_shm_reserve("test_shm", + params.buf_size * params.num_bufs * 2, + ODP_CACHE_LINE_SIZE, + 0); + CU_ASSERT_FATAL(test_shm != ODP_SHM_INVALID); + + pool = odp_buffer_pool_create("test_shm_pool", test_shm, ¶ms); + CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID); + + CU_ASSERT(odp_buffer_pool_destroy(pool) == 0); + CU_ASSERT(odp_shm_free(test_shm) == 0); +} + +static void pool_lookup_info_print(void) +{ + odp_buffer_pool_t pool; + const char pool_name[] = "pool_for_lookup_test"; + odp_buffer_pool_info_t info; + odp_shm_t shm; + odp_buffer_pool_param_t params = { + .buf_size = default_buffer_size, + .buf_align = ODP_CACHE_LINE_SIZE, + .num_bufs = default_buffer_num, + .buf_type = ODP_BUFFER_TYPE_RAW, + }; + + pool = odp_buffer_pool_create(pool_name, ODP_SHM_INVALID, ¶ms); + CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID); + + pool = odp_buffer_pool_lookup(pool_name); + CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID); + + CU_ASSERT_FATAL(odp_buffer_pool_info(pool, &shm, &info) == 0); + CU_ASSERT(shm == ODP_SHM_INVALID); + CU_ASSERT(strncmp(pool_name, info.name, sizeof(pool_name)) == 0); + CU_ASSERT(params.buf_size <= info.params.buf_size); + CU_ASSERT(params.buf_align <= info.params.buf_align); + CU_ASSERT(params.num_bufs <= info.params.num_bufs); + CU_ASSERT(params.buf_type == info.params.buf_type); + + odp_buffer_pool_print(pool); + + CU_ASSERT(odp_buffer_pool_destroy(pool) == 0); +} + +static void pool_alloc_buffer_type(int type) +{ + odp_buffer_pool_t pool; + const int buf_num = 3; + const size_t buf_size = 1500; + odp_buffer_t buffer[buf_num]; + int buf_index; + char wrong_type = 0, wrong_size = 0; + + pool = pool_create(buf_num, buf_size, type); + odp_buffer_pool_print(pool); + + /* Try to allocate buf_num buffers from the pool */ + for (buf_index = 0; buf_index < buf_num; buf_index++) { + buffer[buf_index] = odp_buffer_alloc(pool); + if (buffer[buf_index] == ODP_BUFFER_INVALID) + break; + if (odp_buffer_type(buffer[buf_index]) != type) + wrong_type = 1; + if (odp_buffer_size(buffer[buf_index]) < buf_size) + wrong_size = 1; + if (wrong_type || wrong_size) + odp_buffer_print(buffer[buf_index]); + } + + /* Check that the pool had at least buf_num buffers */ + CU_ASSERT(buf_index == buf_num); + /* buf_index points out of buffer[] or it point to an invalid buffer */ + buf_index--; + + /* Check that the pool had correct buffers */ + CU_ASSERT(wrong_type == 0); + CU_ASSERT(wrong_size == 0); + + for (; buf_index >= 0; buf_index--) + odp_buffer_free(buffer[buf_index]); + + CU_ASSERT(odp_buffer_pool_destroy(pool) == 0); +} + +static void pool_alloc_buffer_raw(void) +{ + pool_alloc_buffer_type(ODP_BUFFER_TYPE_RAW); +} + +static void pool_alloc_buffer_packet(void) +{ + pool_alloc_buffer_type(ODP_BUFFER_TYPE_PACKET); +} + +static void pool_alloc_buffer_timeout(void) +{ + pool_alloc_buffer_type(ODP_BUFFER_TYPE_TIMEOUT); +} + +static void pool_alloc_buffer_any(void) +{ + pool_alloc_buffer_type(ODP_BUFFER_TYPE_ANY); +} + +static void pool_free_buffer(void) +{ + odp_buffer_pool_t pool; + odp_buffer_t buffer; + pool = pool_create(1, 64, ODP_BUFFER_TYPE_RAW); + + /* Allocate the only buffer from the pool */ + buffer = odp_buffer_alloc(pool); + CU_ASSERT_FATAL(buffer != ODP_BUFFER_INVALID); + + /** @todo: is it correct to assume the pool had only one buffer? */ + CU_ASSERT_FATAL(odp_buffer_alloc(pool) == ODP_BUFFER_INVALID) + + odp_buffer_free(buffer); + + /* Check that the buffer was returned back to the pool */ + buffer = odp_buffer_alloc(pool); + CU_ASSERT_FATAL(buffer != ODP_BUFFER_INVALID); + + odp_buffer_free(buffer); + CU_ASSERT(odp_buffer_pool_destroy(pool) == 0); +} + +CU_TestInfo buffer_pool_tests[] = { + _CU_TEST_INFO(pool_create_destroy_raw), + _CU_TEST_INFO(pool_create_destroy_packet), + _CU_TEST_INFO(pool_create_destroy_timeout), + _CU_TEST_INFO(pool_create_destroy_any), + _CU_TEST_INFO(pool_create_destroy_raw_shm), + _CU_TEST_INFO(pool_lookup_info_print), + _CU_TEST_INFO(pool_alloc_buffer_raw), + _CU_TEST_INFO(pool_alloc_buffer_packet), + _CU_TEST_INFO(pool_alloc_buffer_timeout), + _CU_TEST_INFO(pool_alloc_buffer_any), + _CU_TEST_INFO(pool_free_buffer), + CU_TEST_INFO_NULL, +}; diff --git a/test/cunit/buffer/odp_buffer_test.c b/test/cunit/buffer/odp_buffer_test.c new file mode 100644 index 0000000..e4a6b68 --- /dev/null +++ b/test/cunit/buffer/odp_buffer_test.c @@ -0,0 +1,52 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "odp_buffer_testsuites.h" + +static odp_buffer_pool_t raw_pool; +static odp_buffer_t raw_buffer = ODP_BUFFER_INVALID; +static const size_t raw_buffer_size = 1500; + +int buffer_tests_init(void) +{ + odp_buffer_pool_param_t params = { + .buf_size = raw_buffer_size, + .buf_align = ODP_CACHE_LINE_SIZE, + .num_bufs = 100, + .buf_type = ODP_BUFFER_TYPE_RAW, + }; + + raw_pool = odp_buffer_pool_create("raw_pool", ODP_SHM_INVALID, ¶ms); + if (raw_pool == ODP_BUFFER_POOL_INVALID) + return -1; + raw_buffer = odp_buffer_alloc(raw_pool); + if (raw_buffer == ODP_BUFFER_INVALID) + return -1; + return 0; +} + +int buffer_tests_finalize(void) +{ + odp_buffer_free(raw_buffer); + if (odp_buffer_pool_destroy(raw_pool) != 0) + return -1; + return 0; +} + +static void buffer_management_basic(void) +{ + CU_ASSERT(odp_buffer_is_valid(raw_buffer) == 1); + CU_ASSERT(odp_buffer_pool(raw_buffer) != ODP_BUFFER_POOL_INVALID); + CU_ASSERT(odp_buffer_type(raw_buffer) == ODP_BUFFER_TYPE_RAW); + CU_ASSERT(odp_buffer_size(raw_buffer) >= raw_buffer_size); + CU_ASSERT(odp_buffer_addr(raw_buffer) != NULL); + odp_buffer_print(raw_buffer); +} + +CU_TestInfo buffer_tests[] = { + _CU_TEST_INFO(buffer_management_basic), + CU_TEST_INFO_NULL, +}; diff --git a/test/cunit/buffer/odp_buffer_testsuites.c b/test/cunit/buffer/odp_buffer_testsuites.c new file mode 100644 index 0000000..ac28c58 --- /dev/null +++ b/test/cunit/buffer/odp_buffer_testsuites.c @@ -0,0 +1,51 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "odp_buffer_testsuites.h" + +static CU_SuiteInfo suites[] = { + { .pName = "buffer Pool tests", + .pTests = buffer_pool_tests, + }, + { .pName = "buffer tests", + .pTests = buffer_tests, + .pInitFunc = buffer_tests_init, + .pCleanupFunc = buffer_tests_finalize, + }, + { .pName = "packet tests", + .pTests = packet_tests, + .pInitFunc = packet_tests_init, + .pCleanupFunc = packet_tests_finalize, + }, + CU_SUITE_INFO_NULL, +}; + +int main(void) +{ + if (odp_init_global(NULL, NULL)) { + ODP_ERR("ODP global init failed.\n"); + return -1; + } + odp_init_local(); + + printf("\tODP version: %s\n", odp_version_api_str()); + + /* initialize the CUnit test registry */ + if (CUE_SUCCESS != CU_initialize_registry()) + return CU_get_error(); + + /* register suites */ + CU_register_suites(suites); + /* Run all tests using the CUnit Basic interface */ + CU_basic_set_mode(CU_BRM_VERBOSE); + CU_basic_run_tests(); + CU_cleanup_registry(); + + odp_term_local(); + odp_term_global(); + + return CU_get_error(); +} diff --git a/test/cunit/buffer/odp_buffer_testsuites.h b/test/cunit/buffer/odp_buffer_testsuites.h new file mode 100644 index 0000000..774c58a --- /dev/null +++ b/test/cunit/buffer/odp_buffer_testsuites.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef ODP_BUFFER_TESTSUITES_H_ +#define ODP_BUFFER_TESTSUITES_H_ + +#include +#include +#include +#include + +/* Helper macro for CU_TestInfo initialization */ +#define _CU_TEST_INFO(test_func) {#test_func, test_func} + +extern CU_TestInfo buffer_pool_tests[]; +extern CU_TestInfo buffer_tests[]; +extern CU_TestInfo packet_tests[]; + +extern int buffer_tests_init(void); +extern int buffer_tests_finalize(void); + +extern int packet_tests_init(void); +extern int packet_tests_finalize(void); + +odp_buffer_pool_t pool_create(int buf_num, int buf_size, int buf_type); + +#endif /* ODP_BUFFER_TESTSUITES_H_ */ diff --git a/test/cunit/buffer/odp_packet_test.c b/test/cunit/buffer/odp_packet_test.c new file mode 100644 index 0000000..79753a9 --- /dev/null +++ b/test/cunit/buffer/odp_packet_test.c @@ -0,0 +1,337 @@ +/* Copyright (c) 2014, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include "odp_buffer_testsuites.h" +/* #define TEST_PACKET_SEGMENTS */ + +static odp_buffer_pool_t packet_pool; +static const size_t packet_size = 1500; +odp_packet_t test_packet; + +int packet_tests_init(void) +{ + odp_buffer_pool_param_t params = { + .buf_size = packet_size, + .buf_align = ODP_CACHE_LINE_SIZE, + .num_bufs = 100, + .buf_type = ODP_BUFFER_TYPE_PACKET, + }; + + packet_pool = odp_buffer_pool_create("packet_pool", ODP_SHM_INVALID, + ¶ms); + if (packet_pool == ODP_BUFFER_POOL_INVALID) + return -1; + + test_packet = odph_packet_alloc(packet_pool); + if (odph_packet_is_valid(test_packet) == 0) + return -1; + + return 0; +} + +int packet_tests_finalize(void) +{ + odph_packet_free(test_packet); + if (odp_buffer_pool_destroy(packet_pool) != 0) + return -1; + return 0; +} + +static void packet_alloc_free(void) +{ + odp_buffer_pool_t pool; + odp_packet_t packet; + pool = pool_create(1, 64, ODP_BUFFER_TYPE_PACKET); + + /* Allocate the only buffer from the pool */ + packet = odph_packet_alloc(pool); + CU_ASSERT_FATAL(packet != ODP_PACKET_INVALID); + + /** @todo: is it correct to assume the pool had only one buffer? */ + CU_ASSERT_FATAL(odph_packet_alloc(pool) == ODP_PACKET_INVALID) + + odph_packet_free(packet); + + /* Check that the buffer was returned back to the pool */ + packet = odph_packet_alloc(pool); + CU_ASSERT_FATAL(packet != ODP_PACKET_INVALID); + + odph_packet_free(packet); + CU_ASSERT(odp_buffer_pool_destroy(pool) == 0); +} + +static void packet_management_basic(void) +{ + CU_ASSERT(odph_packet_is_valid(test_packet) == 1); + /** @todo: What is the expected packet length? */ + CU_ASSERT(odp_packet_addr(test_packet) != NULL); + CU_ASSERT(odp_packet_data(test_packet) != NULL); + odp_packet_print(test_packet); +} + +static void packet_length(void) +{ + CU_ASSERT(odp_packet_get_len(test_packet) == packet_size); + odp_packet_set_len(test_packet, packet_size / 2); + CU_ASSERT(odp_packet_get_len(test_packet) == packet_size / 2); + odp_packet_init(test_packet); + CU_ASSERT(odp_packet_get_len(test_packet) == packet_size); +} + +static void packet_context(void) +{ + char context = 2; + void *prev_context; + + prev_context = odp_packet_get_ctx(test_packet); + odp_packet_set_ctx(test_packet, &context); + CU_ASSERT(odp_packet_get_ctx(test_packet) == &context); + odp_packet_set_ctx(test_packet, prev_context); + odp_packet_init(test_packet); +} + +static void packet_buffer_conversion(void) +{ + odp_packet_t tmp_packet; + odp_buffer_t test_buffer; + + test_buffer = odp_packet_to_buffer(test_packet); + CU_ASSERT_FATAL(test_buffer != ODP_BUFFER_INVALID); + CU_ASSERT(odp_buffer_type(test_buffer) == ODP_BUFFER_TYPE_PACKET); + CU_ASSERT(odp_buffer_size(test_buffer) >= packet_size); + + tmp_packet = odp_packet_from_buffer(test_buffer); + CU_ASSERT_FATAL(tmp_packet != ODP_PACKET_INVALID); + /** @todo: Need an API to compare packets */ +} + +static void packet_layer_offsets(void) +{ + odp_packet_t packet = test_packet; + uint8_t *l2_addr, *l3_addr, *l4_addr; + const size_t l2_off = 2; + const size_t l3_off = l2_off + 14; + const size_t l4_off = l3_off + 14; + /* Set offsets to the same value */ + odp_packet_set_l2_offset(packet, l2_off); + odp_packet_set_l3_offset(packet, l2_off); + odp_packet_set_l4_offset(packet, l2_off); + + /* Addresses should be the same */ + l2_addr = odp_packet_l2(packet); + l3_addr = odp_packet_l3(packet); + l4_addr = odp_packet_l4(packet); + CU_ASSERT(l2_addr != NULL); + CU_ASSERT(l2_addr == l3_addr); + CU_ASSERT(l2_addr == l4_addr); + + /* Set offsets to the different values */ + odp_packet_set_l2_offset(packet, l2_off); + CU_ASSERT(odp_packet_l2_offset(packet) == l2_off); + odp_packet_set_l3_offset(packet, l3_off); + CU_ASSERT(odp_packet_l3_offset(packet) == l3_off); + odp_packet_set_l4_offset(packet, l4_off); + CU_ASSERT(odp_packet_l4_offset(packet) == l4_off); + + /* Addresses should not be the same */ + l2_addr = odp_packet_l2(packet); + CU_ASSERT(l2_addr != NULL); + l3_addr = odp_packet_l3(packet); + CU_ASSERT(l3_addr != NULL); + l4_addr = odp_packet_l4(packet); + CU_ASSERT(l4_addr != NULL); + + CU_ASSERT(l2_addr != l3_addr); + CU_ASSERT(l2_addr != l4_addr); + CU_ASSERT(l3_addr != l4_addr); +} + +#ifdef TEST_PACKET_SEGMENTS +static void _test_one_segment(odp_packet_t packet, + odp_packet_seg_t segment, + odp_packet_seg_info_t *info) +{ + size_t headroom, tailroom; + + CU_ASSERT(odp_packet_seg_info(packet, segment, info) == 0); + CU_ASSERT(info->addr != NULL); + CU_ASSERT(info->data != NULL); + CU_ASSERT(info->data >= info->addr); + CU_ASSERT(info->size != 0); + + CU_ASSERT(odp_packet_seg_addr(packet, segment) == info->addr); + CU_ASSERT(odp_packet_seg_size(packet, segment) == info->size); + CU_ASSERT(odp_packet_seg_data(packet, segment) == info->data); + CU_ASSERT(odp_packet_seg_data_len(packet, segment) == info->data_len); + + headroom = odp_packet_seg_headroom(packet, segment); + tailroom = odp_packet_seg_tailroom(packet, segment); + CU_ASSERT(info->size == + info->data_len + headroom + tailroom); +} + +static void packet_segments(void) +{ + int segment_count, seg_index; + size_t data_size, buf_size; + odp_packet_seg_t segment; + odp_packet_t packet = test_packet; + odp_packet_seg_info_t seg_info; + + CU_ASSERT(odph_packet_is_valid(packet) == 1); + + segment_count = odp_packet_seg_count(packet); + CU_ASSERT(segment_count != 0); + + if (!odp_packet_is_segmented(packet)) { + CU_ASSERT(segment_count == 1); + } + + segment = ODP_PACKET_SEG_INVALID; + buf_size = 0; + data_size = 0; + for (seg_index = 0; seg_index < segment_count; seg_index++) { + segment = odp_packet_seg(packet, seg_index); + if (segment == ODP_PACKET_SEG_INVALID) + break; + _test_one_segment(packet, segment, &seg_info); + + buf_size += seg_info.size; + data_size += seg_info.data_len; + /** @todo: touch memory in a segment */ + } + + CU_ASSERT(seg_index == segment_count); + CU_ASSERT(buf_size == odp_buffer_size(odp_packet_to_buffer(packet))); + CU_ASSERT(data_size == odp_packet_get_len(packet)); + + segment = ODP_PACKET_SEG_INVALID; + buf_size = 0; + data_size = 0; + for (seg_index = 0; seg_index < segment_count; seg_index++) { + segment = odp_packet_seg_next(packet, segment); + + if (segment == ODP_PACKET_SEG_INVALID) + break; + _test_one_segment(packet, segment, &seg_info); + + buf_size += seg_info.size; + data_size += seg_info.data_len; + /** @todo: touch memory in a segment */ + } + + CU_ASSERT(seg_index == segment_count); + CU_ASSERT(buf_size == odp_buffer_size(odp_packet_to_buffer(packet))); + CU_ASSERT(data_size == odp_packet_get_len(packet)); + + if (seg_index == segment_count) { + segment = odp_packet_seg_next(packet, segment); + CU_ASSERT(segment == ODP_PACKET_SEG_INVALID); + } +} + +static void _verify_headroom_shift(odp_packet_t packet, + odp_packet_seg_t segment, + int shift) +{ + size_t room = odp_packet_seg_headroom(packet, segment); + size_t data_len = odp_packet_seg_data_len(packet, segment); + char *data = odp_packet_seg_data(packet, segment); + void *addr; + + if (shift > 0) + addr = odp_packet_seg_push_head(packet, segment, shift); + else + addr = odp_packet_seg_pull_head(packet, segment, -shift); + + CU_ASSERT(addr != NULL); + CU_ASSERT(odp_packet_seg_headroom(packet, segment) == room - shift); + CU_ASSERT(odp_packet_seg_data_len(packet, segment) == data_len + shift); + CU_ASSERT(odp_packet_seg_data(packet, segment) == addr); + CU_ASSERT(addr == data + shift); +} + +static void _verify_tailroom_shift(odp_packet_t packet, + odp_packet_seg_t segment, + int shift) +{ + size_t room = odp_packet_seg_tailroom(packet, segment); + int len; + size_t data_len = odp_packet_seg_data_len(packet, segment); + + if (shift > 0) + len = odp_packet_seg_push_tail(packet, segment, shift); + else + len = odp_packet_seg_pull_tail(packet, segment, -shift); + + CU_ASSERT(len != -1); + CU_ASSERT(odp_packet_seg_tailroom(packet, segment) == room - shift); + /** @todo: tail push/pull return int, but data_len is size_t */ + CU_ASSERT(len == (int)data_len + shift); + CU_ASSERT((int)odp_packet_seg_data_len(packet, segment) == len); +} + +static void packet_segment_headroom(void) +{ + odp_packet_seg_t segment; + odp_packet_t packet = test_packet; + size_t room; + size_t data_len; + int shift = 1; + + segment = odp_packet_seg(packet, 0); + CU_ASSERT_FATAL(segment != ODP_PACKET_SEG_INVALID); + room = odp_packet_seg_headroom(packet, segment); + data_len = odp_packet_seg_data_len(packet, segment); + + /** @todo: Specification doesn't specify what room size to expect */ + if (room == 0 && data_len > 0) + shift = -shift; + else if (room == 0) + CU_FAIL_FATAL("No space for headroom manipulation"); + + _verify_headroom_shift(packet, segment, shift); + _verify_headroom_shift(packet, segment, -shift); +} + +static void packet_segment_tailroom(void) +{ + odp_packet_seg_t segment; + odp_packet_t packet = test_packet; + size_t room; + size_t data_len; + int shift = 1; + + segment = odp_packet_seg(test_packet, 0); + CU_ASSERT_FATAL(segment != ODP_PACKET_SEG_INVALID); + room = odp_packet_seg_tailroom(packet, segment); + data_len = odp_packet_seg_data_len(packet, segment); + + /** @todo: Specification doesn't specify what room size to expect */ + if (room == 0 && data_len > 0) + shift = -shift; + else if (room == 0) + CU_FAIL_FATAL("No space for tailroom manipulation"); + + _verify_tailroom_shift(packet, segment, shift); + _verify_tailroom_shift(packet, segment, -shift); +} +#endif /* TEST_PACKET_SEGMENTS */ + +CU_TestInfo packet_tests[] = { + _CU_TEST_INFO(packet_alloc_free), + _CU_TEST_INFO(packet_management_basic), + _CU_TEST_INFO(packet_length), + _CU_TEST_INFO(packet_context), + _CU_TEST_INFO(packet_buffer_conversion), + _CU_TEST_INFO(packet_layer_offsets), +#ifdef TEST_PACKET_SEGMENTS + _CU_TEST_INFO(packet_segments), + _CU_TEST_INFO(packet_segment_headroom), + _CU_TEST_INFO(packet_segment_tailroom), +#endif /* TEST_PACKET_SEGMENTS */ + CU_TEST_INFO_NULL, +};