xref: /freebsd/bin/hostname/tests/hostname_test.sh (revision 1edb7116)
1#-
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 Lin Lee <leelin2602@gmail.com>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27
28#
29# These tests need to run in a multibyte locale with non-localized
30# error messages.
31#
32export LC_CTYPE=C.UTF-8
33export LC_MESSAGES=C
34
35test_jail_name="test-hostname-jail"
36
37test_jail_conf='%%test_jail_name%% {
38    host.hostname = "test-hostname.example.org";
39    path = "/";
40    persist;
41}'
42
43init()
44{
45    echo "${test_jail_conf}" | \
46	    sed -e "s/%%test_jail_name%%/${test_jail_name}/" > "./jail.conf"
47    jail -f "./jail.conf" -c ${test_jail_name}
48}
49
50recycle()
51{
52    jail -f "./jail.conf" -r ${test_jail_name}
53    rm "./jail.conf"
54}
55
56atf_test_case basic cleanup
57basic_head()
58{
59    atf_set require.user root
60    atf_set "descr" "basic test for getting hostname"
61}
62basic_body()
63{
64    init
65
66    result=$(jexec ${test_jail_name} "hostname")
67    atf_check_equal "test-hostname.example.org" "${result}"
68
69    result=$(jexec ${test_jail_name} "hostname" -s)
70    atf_check_equal "test-hostname" "${result}"
71
72    result=$(jexec ${test_jail_name} "hostname" -d)
73    atf_check_equal "example.org" "${result}"
74
75    jexec ${test_jail_name} "hostname" "test-bsd2"
76    result=$(jexec ${test_jail_name} "hostname")
77    atf_check_equal "test-bsd2" "${result}"
78}
79basic_cleanup()
80{
81    recycle
82}
83
84atf_init_test_cases()
85{
86    atf_add_test_case basic
87}
88