From patchwork Thu Oct 5 14:28:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James R T X-Patchwork-Id: 1843957 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=cfLuIxdt; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4S1Yp66PK9z1yqD for ; Fri, 6 Oct 2023 01:28:46 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id D100583A6C; Thu, 5 Oct 2023 14:28:44 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org D100583A6C Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=cfLuIxdt X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id RGBOpiuGD-VR; Thu, 5 Oct 2023 14:28:43 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp1.osuosl.org (Postfix) with ESMTPS id C481783A50; Thu, 5 Oct 2023 14:28:42 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org C481783A50 Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 80582C0071; Thu, 5 Oct 2023 14:28:42 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 90143C0032 for ; Thu, 5 Oct 2023 14:28:40 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 7899083A51 for ; Thu, 5 Oct 2023 14:28:40 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 7899083A51 X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id e3L1Wdf7JpOb for ; Thu, 5 Oct 2023 14:28:39 +0000 (UTC) Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by smtp1.osuosl.org (Postfix) with ESMTPS id 4782183A50 for ; Thu, 5 Oct 2023 14:28:39 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 4782183A50 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1c88b467ef8so1640015ad.0 for ; Thu, 05 Oct 2023 07:28:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696516118; x=1697120918; darn=openvswitch.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=w9eXrhO54GecgMuCMhyIdmQb74S3ZgpIFWP/4COjeUY=; b=cfLuIxdtiDxQmaaEcLy5I6L/goy2C5DeAk1FLyCQSU67GUCI1cMesIjSqxl0MHtdjO 4G7q8T7C1IGFRyqOlFH8CIjzeJW5rLJ43SRiR2WQsKAFDBe417gjX0s7WxpLbOmwf8cw F+h+ANfL67l9WkryerUaNoZmbCxAcPY/7GDLUmib63+r9RHc8ggcEiI9kcacAs1gF5Je jkYMCc1+5IUsWIWN+14fbB+MJDuY6y87l8WvLm2ZfOdzcuEuF8rfVDF7hEqGiX1R/P8g Hqx3gUE+MAJFlADpOrW1o5d4EesbaofKTBpXSiYbuF2T25TqkgunTDUWqR1IaE25hI4E 3IYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696516118; x=1697120918; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=w9eXrhO54GecgMuCMhyIdmQb74S3ZgpIFWP/4COjeUY=; b=CFkMKOZ0b/jeOsj3juC0slN5XKjtEubvhs0Zsf0RVL4wOnaDP7LtbMAmDIVXLACkcy v9pVg9TORgF6RK9J4nNKYSf1GpkPPzbArKT9QoKUmXxSRIxyWPe90snZIT3q+zisJool ZDSqz7Qh8npdaNULQJPZ9bj5sOr0823vvcIKr09PMTiyoaKNOh9vNE8ccpIywek7xim1 EIiFRHgTcJ591y2UMtegS5kBX3KW4azZKSzG6q9YDUrFNoPsyCOpW8esVTpEGV1ZmLEa FyPeV4yrWyxKdsqRozDEXTkWhv7zrjJH7McyzhVH2zcIzdSg9Do5E3lm6uwU6F+QOb9h qKXQ== X-Gm-Message-State: AOJu0YyFN2qr/18snNX5FiPBEPdSVEx+ZWPVvrohTurtbqqlPFu34j+3 /6mAgx6qRllB82mR1Rrn8iosnqxSwJs= X-Google-Smtp-Source: AGHT+IGlLXZJlfr3y8KH3rmsghEXT+22GkXRumv8mN1qww2pJrj0fPqeffVqBDfgTwFOKOYAckKwoA== X-Received: by 2002:a17:902:a417:b0:1c4:4c0f:8d91 with SMTP id p23-20020a170902a41700b001c44c0f8d91mr4159573plq.69.1696516118111; Thu, 05 Oct 2023 07:28:38 -0700 (PDT) Received: from JRT-PC.lan ([124.197.103.187]) by smtp.gmail.com with ESMTPSA id jg4-20020a17090326c400b001bb0eebd90asm1737659plb.245.2023.10.05.07.28.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 07:28:37 -0700 (PDT) From: James Raphael Tiovalen To: dev@openvswitch.org Date: Thu, 5 Oct 2023 22:28:33 +0800 Message-ID: <20231005142833.728406-1-jamestiotio@gmail.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Cc: James Raphael Tiovalen Subject: [ovs-dev] [PATCH v4] tests: Add some tests for byteq module. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" This commit adds a non-exhaustive list of tests for some of the functions declared in `lib/byteq`. These unit tests have been executed via `make check` and they successfully passed. Signed-off-by: James Raphael Tiovalen Acked-by: Simon Horman --- Revisions: v1 -> v2: Incorporate Mike's suggestions. v2 -> v3: Add Eelco's suggestions to alphabetically order includes and indicate skipped tests. v3 -> v4: Fix checkpatch error and warning. --- tests/automake.mk | 1 + tests/library.at | 13 ++++ tests/test-byteq.c | 160 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 tests/test-byteq.c diff --git a/tests/automake.mk b/tests/automake.mk index 720c94449..f8a925012 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -455,6 +455,7 @@ tests_ovstest_SOURCES = \ tests/test-barrier.c \ tests/test-bundle.c \ tests/test-byte-order.c \ + tests/test-byteq.c \ tests/test-classifier.c \ tests/test-ccmap.c \ tests/test-cmap.c \ diff --git a/tests/library.at b/tests/library.at index 164ae789d..ea714ae9c 100644 --- a/tests/library.at +++ b/tests/library.at @@ -88,6 +88,19 @@ AT_KEYWORDS([byte order]) AT_CHECK([ovstest test-byte-order]) AT_CLEANUP +AT_SETUP([byteq operations]) +AT_KEYWORDS([byteq]) +AT_CHECK([ovstest test-byteq regular], [0], [... +]) +AT_CLEANUP + +AT_SETUP([byteq operations only on non-Windows platforms]) +AT_KEYWORDS([byteq]) +AT_SKIP_IF([test "$IS_WIN32" = "yes"]) +AT_CHECK([ovstest test-byteq non-windows], [0], [. +]) +AT_CLEANUP + AT_SETUP([random number generator]) AT_CHECK([ovstest test-random], [0], [dnl average=7fa2014f diff --git a/tests/test-byteq.c b/tests/test-byteq.c new file mode 100644 index 000000000..26e6129bd --- /dev/null +++ b/tests/test-byteq.c @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2023 Hewlett Packard Enterprise Development LP + * + * Licensed 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. + */ + +#include +#undef NDEBUG +#include +#include +#include +#include +#include +#include "byteq.h" +#include "ovstest.h" +#include "util.h" + +static void test_byteq_main(int argc, char *argv[]); +static void test_byteq_put_get(void); +static void test_byteq_putn_get(void); +static void test_byteq_put_string(void); +static void test_byteq_write_read(void); + +#define SIZE 256 + +static void +test_byteq_put_get(void) +{ + struct byteq bq; + uint8_t buffer[SIZE]; + const char *input = "Open vSwitch"; + const int input_len = strlen(input); + + byteq_init(&bq, buffer, SIZE); + for (int i = 0; i < input_len; i++) { + byteq_put(&bq, input[i]); + } + for (int i = 0; i < input_len; i++) { + ovs_assert(byteq_get(&bq) == input[i]); + } +} + +static void +test_byteq_putn_get(void) +{ + struct byteq bq; + uint8_t buffer[SIZE]; + const char *input = "Open vSwitch"; + const int input_len = strlen(input); + + byteq_init(&bq, buffer, SIZE); + byteq_putn(&bq, input, input_len); + for (int i = 0; i < input_len; i++) { + ovs_assert(byteq_get(&bq) == input[i]); + } +} + +static void +test_byteq_put_string(void) +{ + struct byteq bq; + uint8_t buffer[SIZE]; + const char *input = "Open vSwitch"; + const int input_len = strlen(input); + + byteq_init(&bq, buffer, SIZE); + byteq_put_string(&bq, input); + for (int i = 0; i < input_len; i++) { + ovs_assert(byteq_get(&bq) == input[i]); + } +} + +static void +test_byteq_write_read(void) +{ +#ifndef _WIN32 + int fd[2]; + pid_t childpid; + int rc; + struct byteq bq; + uint8_t buffer[SIZE]; + const char *input = "Open vSwitch"; + const int input_len = strlen(input); + + byteq_init(&bq, buffer, SIZE); + byteq_put_string(&bq, input); + + rc = pipe(fd); + ovs_assert(rc == 0); + + /* Flush stdout */ + fflush(stdout); + + childpid = fork(); + ovs_assert(childpid != -1); + if (childpid == 0) { + /* Child process closes stdout */ + close(STDOUT_FILENO); + /* Child process closes up input side of pipe */ + close(fd[0]); + rc = byteq_write(&bq, fd[1]); + ovs_assert(rc == 0); + exit(0); + } else { + /* Parent process closes up output side of pipe */ + close(fd[1]); + rc = byteq_read(&bq, fd[0]); + ovs_assert(rc == EOF); + for (int i = 0; i < input_len; i++) { + ovs_assert(byteq_get(&bq) == input[i]); + } + } +#endif /* _WIN32 */ +} + +static void +run_test(void (*function)(void)) +{ + function(); + printf("."); +} + +static void +test_byteq_main(int argc, char *argv[]) +{ + if (argc != 2) { + ovs_fatal(0, "exactly one argument required\n" + "the argument must be one of the following:\n" + "\tregular\n" + "\tnon-windows\n"); + } + + /* Check whether to run only non-Windows tests or not. */ + if (strcmp(argv[1], "non-windows") == 0) { + run_test(test_byteq_write_read); + printf("\n"); + } else if (strcmp(argv[1], "regular") == 0) { + run_test(test_byteq_put_get); + run_test(test_byteq_putn_get); + run_test(test_byteq_put_string); + printf("\n"); + } else { + ovs_fatal(0, "invalid argument\n" + "the argument must be one of the following:\n" + "\tregular\n" + "\tnon-windows\n"); + } +} + +OVSTEST_REGISTER("test-byteq", test_byteq_main);