xref: /freebsd/bin/hostname/tests/hostname_test.sh (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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    if ! which -s jail; then
46        atf_skip "This test requires jail"
47    fi
48    echo "${test_jail_conf}" | \
49	    sed -e "s/%%test_jail_name%%/${test_jail_name}/" > "./jail.conf"
50    jail -f "./jail.conf" -c ${test_jail_name}
51}
52
53recycle()
54{
55    jail -f "./jail.conf" -r ${test_jail_name}
56    rm "./jail.conf"
57}
58
59atf_test_case basic cleanup
60basic_head()
61{
62    atf_set require.user root
63    atf_set "descr" "basic test for getting hostname"
64}
65basic_body()
66{
67    init
68
69    result=$(jexec ${test_jail_name} "hostname")
70    atf_check_equal "test-hostname.example.org" "${result}"
71
72    result=$(jexec ${test_jail_name} "hostname" -s)
73    atf_check_equal "test-hostname" "${result}"
74
75    result=$(jexec ${test_jail_name} "hostname" -d)
76    atf_check_equal "example.org" "${result}"
77
78    jexec ${test_jail_name} "hostname" "test-bsd2"
79    result=$(jexec ${test_jail_name} "hostname")
80    atf_check_equal "test-bsd2" "${result}"
81}
82basic_cleanup()
83{
84    if ! which -s jail; then
85        atf_skip "This test requires jail"
86    fi
87    recycle
88}
89
90atf_init_test_cases()
91{
92    atf_add_test_case basic
93}
94