From patchwork Wed Jul 31 10:42:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Coufal X-Patchwork-Id: 1967031 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=aX7Zk2J6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4WYpgG3k8lz1yYq for ; Wed, 31 Jul 2024 20:46:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B3A01385828B for ; Wed, 31 Jul 2024 10:46:32 +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 D26663858C33 for ; Wed, 31 Jul 2024 10:46:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D26663858C33 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 D26663858C33 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=1722422779; cv=none; b=oro0CymhcjctvWixZG87sktENEtVbcoS9w5m7hzzIRRHZpMmayUchaCa13wg9h0frB830DTZExcIsnUv8DzLcGErnMaycL9qjKs/n+IMfCxLdxrVDdUs1YMtZsCZfX2OXTCPCORnpothkmvJs2Z2B4zXtT6SXf/xxoReAnZmq6k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722422779; c=relaxed/simple; bh=zw7wETmJJAGPxXGqFqKyRkM9hEqyecMdhAOCYiNU5LE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Pto1h0bHyZa/TG8lxRg1Uhwcvj7aJKpCmX4SQ+npYDbp+7G3eWftkTGAtnh4vUKUMLz7P+sMDrUZjeVAxuTMDuBg3pULhEcE0YrUZy7pZM9fCb+xmWTRDfoXB9v6dtP0tFJkrnligraAIiXhRwUuo+SFhBYKgIJBzBfOhj/1BJk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722422777; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vCGyo2bEOeANIa8SUH7A4oj+ArSFUse1BEmV3iPAcsE=; b=aX7Zk2J6yTB7fJjutVrAzVT25cxqGTmPhcwgUHeBvp/HJUh2ddBe7uiggz+Po4VEyrqtty pEY/kToPT4QhwdICqFcjWJSMzc9Ro6ALAiEkifKtZWAeaH3Qz89VZxpuFiggH4KXfN935r TMKyLZ69r0vf4/siRxrs+MUnqXdyp/Y= Received: from mx-prod-mc-01.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-391-O0d9vORHOBCbgDzIpijoSg-1; Wed, 31 Jul 2024 06:46:16 -0400 X-MC-Unique: O0d9vORHOBCbgDzIpijoSg-1 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 879EC1955D44 for ; Wed, 31 Jul 2024 10:46:15 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.137]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CD7FA19560AA; Wed, 31 Jul 2024 10:46:13 +0000 (UTC) From: Martin Coufal To: libc-alpha@sourceware.org Cc: Martin Coufal Subject: [PATCH v2] misc: add a new test for gethostname Date: Wed, 31 Jul 2024 12:42:11 +0200 Message-ID: <20240731104605.1329319-1-mcoufal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.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_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 --- v1: https://sourceware.org/pipermail/libc-alpha/2024-July/158745.html Changes since v1: delete check for default_hostname, do sanity only --- misc/Makefile | 1 + misc/tst-gethostname.c | 101 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 misc/tst-gethostname.c diff --git a/misc/Makefile b/misc/Makefile index 5d17c562fe..ff45b2887d 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -243,6 +243,7 @@ tests := \ tst-empty \ tst-error1 \ tst-fdset \ + tst-gethostname \ tst-hsearch \ tst-insremque \ tst-ioctl \ diff --git a/misc/tst-gethostname.c b/misc/tst-gethostname.c new file mode 100644 index 0000000000..7c94283e61 --- /dev/null +++ b/misc/tst-gethostname.c @@ -0,0 +1,101 @@ +/* Basic test for gethostname. + 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 + +#ifndef HOST_NAME_MAX +#define HOST_NAME_MAX 64 +#endif + +#define MAX_LEN_INCREASE 10 + +void set_hostname (const char* name){ + char hostnamectl_cmd[] = "hostnamectl set-hostname "; + char cmd[strlen(hostnamectl_cmd) + strlen(name)]; + strcpy(cmd, hostnamectl_cmd); + strcat(cmd, name); + system(cmd); +} + +static int +do_test (void) +{ + char default_hostname[HOST_NAME_MAX + 1] = "\0"; + char hostname[HOST_NAME_MAX + 1] = "\0"; + char generated_hostname[HOST_NAME_MAX + MAX_LEN_INCREASE + 1] = "\0"; + + // check default hostname is not empty string + TEST_VERIFY_EXIT(gethostname(default_hostname, HOST_NAME_MAX) == 0); + TEST_VERIFY(strlen(default_hostname) > 0); + + // input: empty string + // expected output: system's default hostname + set_hostname("\"\""); + TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0); + TEST_VERIFY(strlen(hostname) > 0); // sanity check only, we don't know system's default hostname + + // input: 'a' + // expected output: 'a' + set_hostname("a"); + TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0); + TEST_COMPARE(strlen(hostname), 1); + TEST_VERIFY(strcmp(hostname, "a") == 0); + + // input: abc.def.ghi + // expected output: abc.def.ghi + set_hostname("abc.def.ghi"); + TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0); + TEST_COMPARE(strlen(hostname), 11); + TEST_VERIFY(strcmp(hostname, "abc.def.ghi") == 0); + + // input: exactly HOST_NAME_MAX + // expected output: exactly HOST_NAME_MAX, return -1, errno set + // generate hostname of length == HOST_NAME_MAX + for (int i = 0; i < HOST_NAME_MAX; i++){ + generated_hostname[i] = 'a' + i % 26; + } + set_hostname(generated_hostname); + TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == -1); + TEST_COMPARE(errno, ENAMETOOLONG); + TEST_COMPARE(strlen(hostname), HOST_NAME_MAX); + TEST_VERIFY(strcmp(hostname, generated_hostname) == 0); + + // input: longer than HOST_NAME_MAX + // expected output: cropped to HOST_NAME_MAX, return -1, errno set + // generate hostname LONGER that HOST_NAME_MAX + for (int i = 0; i < HOST_NAME_MAX + MAX_LEN_INCREASE; i++){ + generated_hostname[i] = 'a' + i % 26; + } + set_hostname(generated_hostname); + TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == -1); + TEST_COMPARE(errno, ENAMETOOLONG); + generated_hostname[HOST_NAME_MAX] = '\0'; // crop at HOST_NAME_MAX + TEST_COMPARE(strlen(hostname), HOST_NAME_MAX); + TEST_VERIFY(strcmp(hostname, generated_hostname) == 0); + + // restore default hostname + set_hostname(default_hostname); + return 0; +} + +#include