Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7578505ybi; Thu, 1 Aug 2019 10:16:54 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHjC1zhVh1C5ORvZPgwUOCH78Dosl8rRfxEOQTMAXybsI/LDbMCo9vG+1+5zorWF5tJp+r X-Received: by 2002:a17:902:e306:: with SMTP id cg6mr127242118plb.263.1564679813926; Thu, 01 Aug 2019 10:16:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564679813; cv=none; d=google.com; s=arc-20160816; b=UmIFWGSxLvb7MhQ+gGmCN7dFDaeN9kxPM7v0kbluKoR7VIJ8LTXfjwq5z6JauUZ4fT 82SDmcGd4PjaTkMo1AzvEAczPxV9tSu+QMly1m8n1rAs6duXhgGPnn+pcskmULTrWzkp 5+1WbbEYQjTiekzNpJ3I6VwXL/JmTEhUXxv9nwbsyM/QQnDLUC7wjpxxD06S+gjeIsf1 CsmbTJUCU9FyZJtu5UllX93+tyWtRFrBWImQ1Xd4YCrI3bF4DEvVmHe1wZw2CJWSgf0M Rvbv4z3wKa0qLLcYuB1K1REiyCECPM5ftNTQA1FfAIiQKTjV489QxaXMWYLvlvEGyMzg 3++Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Cg5HU2TCzBv09V9bSdNwHmJVaCclBCMmp5Q7fTo0WZY=; b=F6Nch+z9yvfQVNoinSliRgRlTFhE9DWkUwWMvy8A/ZZkzN2fQK8JuUer6kVF3hFzlX 43gyFsOn93zFS93Gqhd8+9tNqvf5Cj1ytVwQrX3FO/uP3a5Ag4iEZMsg20hXXg6yv2+M m2P2EiffF43WehECB3LLjPZLkH5jJ4aFyM7TBRlE2J77DjeK3xs8ISBaBCfSSCfoAjdo l5D688mSaOI8qy/P9za5tmiCMS0KvSnzRQEbHFiQkozTHP8NKv8lwbMR8eGkTYMRQCBC EeAiAqmtH71jTLybvnuja+XXRVLqgmFQt7O91RIoko5Ce9xnsx2SLsYCqcJ3L7hcHu97 wCkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q9si1324965pgm.409.2019.08.01.10.16.38; Thu, 01 Aug 2019 10:16:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732669AbfHAP0K (ORCPT + 99 others); Thu, 1 Aug 2019 11:26:10 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58280 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732631AbfHAP0H (ORCPT ); Thu, 1 Aug 2019 11:26:07 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1688F5F151; Thu, 1 Aug 2019 15:26:07 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-117-63.ams2.redhat.com [10.36.117.63]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9D1EA600C4; Thu, 1 Aug 2019 15:26:02 +0000 (UTC) From: Stefano Garzarella To: netdev@vger.kernel.org Cc: kvm@vger.kernel.org, Stefan Hajnoczi , Dexuan Cui , virtualization@lists.linux-foundation.org, "David S. Miller" , Jorgen Hansen , linux-kernel@vger.kernel.org Subject: [PATCH v2 06/11] VSOCK: add send_byte()/recv_byte() test utilities Date: Thu, 1 Aug 2019 17:25:36 +0200 Message-Id: <20190801152541.245833-7-sgarzare@redhat.com> In-Reply-To: <20190801152541.245833-1-sgarzare@redhat.com> References: <20190801152541.245833-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 01 Aug 2019 15:26:07 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stefan Hajnoczi Test cases will want to transfer data. This patch adds utility functions to do this. Signed-off-by: Stefan Hajnoczi Signed-off-by: Stefano Garzarella --- tools/testing/vsock/util.c | 99 ++++++++++++++++++++++++++++++++++++++ tools/testing/vsock/util.h | 2 + 2 files changed, 101 insertions(+) diff --git a/tools/testing/vsock/util.c b/tools/testing/vsock/util.c index 4280a56ba677..d46a6e079b96 100644 --- a/tools/testing/vsock/util.c +++ b/tools/testing/vsock/util.c @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -149,6 +150,104 @@ int vsock_stream_accept(unsigned int cid, unsigned int port, return client_fd; } +/* Transmit one byte and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * 1 Success + */ +void send_byte(int fd, int expected_ret) +{ + const uint8_t byte = 'A'; + ssize_t nwritten; + + timeout_begin(TIMEOUT); + do { + nwritten = write(fd, &byte, sizeof(byte)); + timeout_check("write"); + } while (nwritten < 0 && errno == EINTR); + timeout_end(); + + if (expected_ret < 0) { + if (nwritten != -1) { + fprintf(stderr, "bogus write(2) return value %zd\n", + nwritten); + exit(EXIT_FAILURE); + } + if (errno != -expected_ret) { + perror("write"); + exit(EXIT_FAILURE); + } + return; + } + + if (nwritten < 0) { + perror("write"); + exit(EXIT_FAILURE); + } + if (nwritten == 0) { + if (expected_ret == 0) + return; + + fprintf(stderr, "unexpected EOF while sending byte\n"); + exit(EXIT_FAILURE); + } + if (nwritten != sizeof(byte)) { + fprintf(stderr, "bogus write(2) return value %zd\n", nwritten); + exit(EXIT_FAILURE); + } +} + +/* Receive one byte and check the return value. + * + * expected_ret: + * <0 Negative errno (for testing errors) + * 0 End-of-file + * 1 Success + */ +void recv_byte(int fd, int expected_ret) +{ + uint8_t byte; + ssize_t nread; + + timeout_begin(TIMEOUT); + do { + nread = read(fd, &byte, sizeof(byte)); + timeout_check("read"); + } while (nread < 0 && errno == EINTR); + timeout_end(); + + if (expected_ret < 0) { + if (nread != -1) { + fprintf(stderr, "bogus read(2) return value %zd\n", + nread); + exit(EXIT_FAILURE); + } + if (errno != -expected_ret) { + perror("read"); + exit(EXIT_FAILURE); + } + return; + } + + if (nread < 0) { + perror("read"); + exit(EXIT_FAILURE); + } + if (nread == 0) { + if (expected_ret == 0) + return; + + fprintf(stderr, "unexpected EOF while receiving byte\n"); + exit(EXIT_FAILURE); + } + if (nread != sizeof(byte)) { + fprintf(stderr, "bogus read(2) return value %zd\n", nread); + exit(EXIT_FAILURE); + } +} + /* Run test cases. The program terminates if a failure occurs. */ void run_tests(const struct test_case *test_cases, const struct test_opts *opts) diff --git a/tools/testing/vsock/util.h b/tools/testing/vsock/util.h index 1786305cfddd..fe524d393d67 100644 --- a/tools/testing/vsock/util.h +++ b/tools/testing/vsock/util.h @@ -36,6 +36,8 @@ unsigned int parse_cid(const char *str); int vsock_stream_connect(unsigned int cid, unsigned int port); int vsock_stream_accept(unsigned int cid, unsigned int port, struct sockaddr_vm *clientaddrp); +void send_byte(int fd, int expected_ret); +void recv_byte(int fd, int expected_ret); void run_tests(const struct test_case *test_cases, const struct test_opts *opts); -- 2.20.1