From patchwork Fri Aug 9 19:05:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 1971068 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=SdQnIiTZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (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 4WgYKn4xsxz1yXs for ; Sat, 10 Aug 2024 05:06:21 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CC2E8385DDD3 for ; Fri, 9 Aug 2024 19:06:19 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTP id 54491385B532 for ; Fri, 9 Aug 2024 19:06:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 54491385B532 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 54491385B532 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723230363; cv=none; b=pHWUm5lUZJC8b19e68/dhvYUo+HXRqfHoiavECBCmpH9/ndqGFz15WXJ1NGAexfqh0kjTAwZ8m81NqM7FMnWv5i1cpris89UalxShUNjvxFp7cTeds9g7AUUBB7jA/4p4UqT3yQ5UcP96NUDKqJi2Vj5+krKg0LXAZPa7Snn2ao= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723230363; c=relaxed/simple; bh=UlBHHDbOoGKbGmOvG0Hs+kOSqr8ktaZzJYPMHukYBZ4=; h=DKIM-Signature:Date:Message-Id:From:To:Subject; b=kf/QfIZOAPkUVLg3c3aBLITuiRj5v6oTQ5V+Q6tzjItxzMou4/s4Hqpk3d32cYuZbwtivVZqTp2UnP3mFFDybPiVZJuawBEGGN8J/LjaS8z/btNJPMjPhmUVTXfPxCvTT9QXT/aqskJ6IMaqkkUq9sIlaF15HPEzBoHkQJ+UGEo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723230361; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:content-type:content-type; bh=NByXFr1hRi1g3HGVaVy4syW0f891nOCvP98/Y6XZxGM=; b=SdQnIiTZZ80ixMqRUScZITpbDF1UKGpM7pJeUAqmyK5WNfuERxsVYDeE88JRZe/JOloKSk 46n1L09qV1s2Lq5v+kpHiCjhfKN3oiHrzE7fpZabjC7mj7gRnEvK4o28o7fDyKIDfQl7MA LqhvR4zao0vCVhd0I7syg5pVErevHlw= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-35-TP6FEGjDMpuqTXQvzGs3Lg-1; Fri, 09 Aug 2024 15:05:59 -0400 X-MC-Unique: TP6FEGjDMpuqTXQvzGs3Lg-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1A5E61944A8B for ; Fri, 9 Aug 2024 19:05:58 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.10.97]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 821D01956089 for ; Fri, 9 Aug 2024 19:05:57 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.16.1/8.16.1) with ESMTP id 479J5tIb2030130 for ; Fri, 9 Aug 2024 15:05:55 -0400 Date: Fri, 09 Aug 2024 15:05:55 -0400 Message-Id: From: DJ Delorie To: libc-alpha@sourceware.org Subject: [patch v1] inet: test if_nametoindex and if_indextoname X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 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_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~incoming=patchwork.ozlabs.org@sourceware.org Tests for if_nameindex, if_name2index, and if_index2name Tests that valid results are consistent. Tests that invalid parameters fail correctly. Reviewed-by: Florian Weimer diff --git a/inet/Makefile b/inet/Makefile index 2f03e6f7ee..cb97b45f0f 100644 --- a/inet/Makefile +++ b/inet/Makefile @@ -91,6 +91,7 @@ tests := \ tst-getni1 \ tst-getni2 \ tst-if_index-long \ + tst-if_nameindex \ tst-inet6_rth \ tst-network \ tst-ntoa \ diff --git a/inet/tst-if_nameindex.c b/inet/tst-if_nameindex.c new file mode 100644 index 0000000000..b025cdb3a7 --- /dev/null +++ b/inet/tst-if_nameindex.c @@ -0,0 +1,116 @@ +/* Tests for if_nameindex et al. + Copyright (C) 2024 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 +#include +#include +#include +#include +#include + +static char *buffer; + +static const char *test_names[] = { + "testing", + "invalid", + "dont-match", + "", + "\001\001\001\177", + NULL +}; + +static void +checki (int i) +{ + char *ifname; + + /* Test that a known-invalid index returns NULL. */ + /* BUFFER should not be accessed. */ + + printf ("Testing if_indextoname (%d) == NULL\n", i); + ifname = if_indextoname (i, NULL); + TEST_VERIFY (ifname == NULL); + TEST_VERIFY (errno == ENXIO); +} + +static int +do_test (void) +{ + struct if_nameindex *if_ni, *ifp; + int min_idx, max_idx, buflen = 0; + int i; + + if_ni = if_nameindex (); + TEST_VERIFY (if_ni != NULL); + + min_idx = max_idx = if_ni->if_index; + + for (ifp = if_ni; !(ifp->if_index == 0 && ifp->if_name == NULL); ifp++) + { + printf ("%u: %s\n", ifp->if_index, ifp->if_name); + if (ifp->if_index < min_idx) + min_idx = ifp->if_index; + if (ifp->if_index > max_idx) + max_idx = ifp->if_index; + if (strlen (ifp->if_name) + 1 > buflen) + buflen = strlen (ifp->if_name) + 1; + } + buffer = (char *) xmalloc (buflen); + + /* Check normal operation. */ + for (ifp = if_ni; !(ifp->if_index == 0 && ifp->if_name == NULL); ifp++) + { + unsigned int idx = if_nametoindex (ifp->if_name); + TEST_VERIFY (idx == ifp->if_index); + + char *fn = if_indextoname (ifp->if_index, buffer); + TEST_VERIFY (strcmp (fn, ifp->if_name) == 0); + } + + for (i=-2; iif_index == 0 && ifp->if_name == NULL); ifp++) + if (strcmp (test_names[i], ifp->if_name) == 0) + goto not_this_one; + + printf ("Testing if_nametoindex (%s) == 0\n", test_names[i]); + + unsigned int idx = if_nametoindex (test_names[i]); + TEST_VERIFY (idx == 0); + TEST_VERIFY (errno == ENODEV); + + not_this_one: + } + + + if_freenameindex (if_ni); + + return 0; +} + +#include