From patchwork Tue Jul 30 08:05:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Coufal X-Patchwork-Id: 94761 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 1FC4F385843B for ; Tue, 30 Jul 2024 08:06:02 +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 88FFD3858C56 for ; Tue, 30 Jul 2024 08:05:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 88FFD3858C56 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 88FFD3858C56 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=1722326740; cv=none; b=sj5JJJpNKndM/EU0o+MwsztLE+CdZF8NblfO+VMtXqwWg4/j5WLzKIz3eogn/zbxhtD4OeEU1ZdcCCi8Jt/uwdReYe/FEALCxsbFqPVP5LT8olzZzNcTxssGB7embmkU66zHUsPgaRMuTuQW0hTNFLCDFpwbvlkrOuXXpS27iqI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722326740; c=relaxed/simple; bh=VhG0HP3OA4Q8ZFX5DVHwI4o1ZWDFD8u/zAjhAFeWRqQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=HMHiZR3+2816D+c666VYZVgT35p8h9MVTDaDxnUm7NQ9PLYiUAbF2XVZoJQiy4uhajqLubniBP1Sdoha057JVaSfwiF/h5SErFWS++hAUDSFuFL/cgNvN8cSbmNM8Xkn5MAY6heZ3MGPdw3vN0Sh72SyWCyWPL+6b7ez7ydAnKg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722326738; 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=edLq2JxboW2p35CP8MNcwnwWWvqwMBl0bV+4hoeEaGk=; b=DLNwbn03GlUGVEpCXRHbVB1dhlBpoRsJfW/EzNvhNvYsJ+ZB1q7dz8S7zoA2Zh57kcWhBN fhkdVNabCRVwLtHVw1dXpqwKXWuNJh4ZPEvh6p5Elnq8XqCcP2hIkXoaDhX4WvvzpqMsGy DE4L8h0G5vEgmjTOl88M5rWzr4z8pQg= Received: from mx-prod-mc-04.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-547-HiOS3uAhOwiHtMmUs73pcQ-1; Tue, 30 Jul 2024 04:05:36 -0400 X-MC-Unique: HiOS3uAhOwiHtMmUs73pcQ-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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A284F1955D4C for ; Tue, 30 Jul 2024 08:05:35 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.224.188]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1EC72195605F; Tue, 30 Jul 2024 08:05:33 +0000 (UTC) From: Martin Coufal To: libc-alpha@sourceware.org Cc: Martin Coufal Subject: [PATCH] misc: add a new test for gethostname Date: Tue, 30 Jul 2024 10:05:03 +0200 Message-ID: <20240730080525.15196-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~patchwork=sourceware.org@sourceware.org --- misc/Makefile | 1 + misc/tst-gethostname.c | 102 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 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..2cb8ae4978 --- /dev/null +++ b/misc/tst-gethostname.c @@ -0,0 +1,102 @@ +/* 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: default hostname + set_hostname("\"\""); + TEST_VERIFY(gethostname(hostname, HOST_NAME_MAX) == 0); + TEST_COMPARE(strlen(hostname), strlen(default_hostname)); + TEST_VERIFY(strcmp(hostname, default_hostname) == 0); + + // 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