From patchwork Wed Aug 5 07:14:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 40206 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7B3BA3840C23; Wed, 5 Aug 2020 07:14:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B3BA3840C23 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1596611684; bh=pkUK58zcVou6CwacE9dIWEBUByHYDkQTd55ktYFkbAI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oSdtQvtnerjhag/5dpU/o+1xy599dg7LZb8Bcsa1alhPxp8KJg8uD1s/yPCBDtcmU 9eeFpmGsOnpn1lU6f6iVo5fH9KInsM+jCk7obiPnPPfTSITMi/y3cqW/WPU6g5zQHh oFGKOE+5NwzPXzGOFy71Rgs03f114MD0nE7dbfBk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [205.139.110.61]) by sourceware.org (Postfix) with ESMTP id D5D233840C0E for ; Wed, 5 Aug 2020 07:14:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D5D233840C0E Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-252-lxBSrOLeOT-p-QCiKfUEug-1; Wed, 05 Aug 2020 03:14:37 -0400 X-MC-Unique: lxBSrOLeOT-p-QCiKfUEug-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E75478014D7 for ; Wed, 5 Aug 2020 07:14:36 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-112-189.ams2.redhat.com [10.36.112.189]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3E3F571765 for ; Wed, 5 Aug 2020 07:14:36 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 1/3] login/tst-grantpt: Convert to support framework, more error checking Message-Id: <0c9120dce321f606d15090f8832039b122b056a0.1596611597.git.fweimer@redhat.com> Date: Wed, 05 Aug 2020 09:14:34 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" The test now requires working /dev/pts pseudo-terminals. A new subtest (test_not_ptmx) attempts to call grantpt on a pseudo-terminal that is not a ptmx device. POSIX requires an EINVAL error in this case. Reviewed-by: Adhemerval Zanella --- login/tst-grantpt.c | 93 ++++++++++++++++++++++++++++----------------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/login/tst-grantpt.c b/login/tst-grantpt.c index 65bb344909..1d7a220fcf 100644 --- a/login/tst-grantpt.c +++ b/login/tst-grantpt.c @@ -1,3 +1,21 @@ +/* Test for grantpt error corner cases. + Copyright (C) 2001-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + #include #include #include @@ -6,76 +24,81 @@ #include #include #include +#include +#include +#include -static int +/* Test grantpt with a closed descriptor. */ +static void test_ebadf (void) { int fd, ret, err; fd = posix_openpt (O_RDWR); if (fd == -1) - { - printf ("posix_openpt(O_RDWR) failed\nerrno %d (%s)\n", - errno, strerror (errno)); - /* We don't fail because of this; maybe the system does not have - SUS pseudo terminals. */ - return 0; - } - unlockpt (fd); - close (fd); + FAIL_EXIT1 ("posix_openpt(O_RDWR) failed\nerrno %d (%m)\n", errno); + TEST_COMPARE (unlockpt (fd), 0); + xclose (fd); ret = grantpt (fd); err = errno; if (ret != -1 || err != EBADF) { + support_record_failure (); printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF); printf (" got: return = %d, errno = %d\n", ret, err); - return 1; } - return 0; } -static int +/* Test grantpt on a regular file. */ +static void test_einval (void) { int fd, ret, err; - const char file[] = "./grantpt-einval"; - fd = open (file, O_RDWR | O_CREAT, 0600); - if (fd == -1) - { - printf ("open(\"%s\", O_RDWR) failed\nerrno %d (%s)\n", - file, errno, strerror (errno)); - return 0; - } - unlink (file); + fd = create_temp_file ("tst-grantpt-", NULL); + TEST_VERIFY_EXIT (fd >= 0); ret = grantpt (fd); err = errno; if (ret != -1 || err != EINVAL) { + support_record_failure (); printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL); printf (" got: return = %d, errno = %d\n", ret, err); - ret = 1; } - else - ret = 0; - close (fd); + xclose (fd); +} + +/* Test grantpt on a non-ptmx pseudo-terminal. */ +static void +test_not_ptmx (void) +{ + int ptmx = posix_openpt (O_RDWR); + TEST_VERIFY_EXIT (ptmx >= 0); + TEST_COMPARE (grantpt (ptmx), 0); + TEST_COMPARE (unlockpt (ptmx), 0); + + const char *name = ptsname (ptmx); + TEST_VERIFY_EXIT (name != NULL); + int pts = open (name, O_RDWR | O_NOCTTY); + TEST_VERIFY_EXIT (pts >= 0); + + TEST_COMPARE (grantpt (pts), -1); + TEST_COMPARE (errno, EINVAL); - return ret; + xclose (pts); + xclose (ptmx); } static int do_test (void) { - int result = 0; - - result += test_ebadf (); - result += test_einval (); - - return result; + test_ebadf (); + test_einval (); + test_not_ptmx (); + return 0; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include