1309466Sngie# $NetBSD: t_servent.sh,v 1.2 2016/03/08 08:34:17 joerg Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2008 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343Sngieatf_test_case servent
29272343Sngieservent_head()
30272343Sngie{
31272343Sngie	atf_set "descr" "Checks {get,set,end}servent(3)"
32309466Sngie	# libc doesn't include aliases
33309466Sngie	atf_set "require.files" "/var/db/services.cdb" 
34272343Sngie}
35272343Sngieservent_body()
36272343Sngie{
37272343Sngie	#
38272343Sngie	# Munge original to:
39272343Sngie	#  (1) match output format of the test program
40272343Sngie	#  (2) fold all names for the same port/proto together
41272343Sngie	#  (3) prune duplicates
42272343Sngie	#
43272343Sngie	tr '\t' ' ' </etc/services | awk '
44272343Sngie		function add(key, name,      i, n, ar) {
45272343Sngie		n = split(names[key], ar);
46272343Sngie		for (i=1; i<=n; i++) {
47272343Sngie			if (name == ar[i]) {
48272343Sngie			return;
49272343Sngie			}
50272343Sngie		}
51272343Sngie		delete ar;
52272343Sngie		names[key] = names[key] " " name;
53272343Sngie		}
54272343Sngie
55272343Sngie		{
56272343Sngie		sub("#.*", "", $0);
57272343Sngie		gsub("  *", " ", $0);
58272343Sngie		if (NF==0) {
59272343Sngie			next;
60272343Sngie		}
61272343Sngie		add($2, $1, 0);
62272343Sngie		for (i=3; i<=NF; i++) {
63272343Sngie			add($2, $i, 1);
64272343Sngie		}
65272343Sngie		}
66272343Sngie		END {
67272343Sngie		for (key in names) {
68272343Sngie			portproto = key;
69272343Sngie			sub("/", ", proto=", portproto);
70272343Sngie			portproto = "port=" portproto;
71272343Sngie
72272343Sngie			n = split(names[key], ar);
73272343Sngie			printf "name=%s, %s, aliases=", ar[1], portproto;
74272343Sngie			for (i=2; i<=n; i++) {
75272343Sngie			if (i>2) {
76272343Sngie				printf " ";
77272343Sngie			}
78272343Sngie			printf "%s", ar[i];
79272343Sngie			}
80272343Sngie			printf "\n";
81272343Sngie			delete ar;
82272343Sngie		}
83272343Sngie		}
84272343Sngie	' | sort >exp
85272343Sngie
86273933Sngie	case "$(uname)" in
87273933Sngie	FreeBSD)
88273933Sngie		#  (3) Don't prune duplicates
89273933Sngie		tr '\t' ' ' < /etc/services |
90273933Sngie		    sed 's/#.*//;s/   */ /g; /^$/d;s#\([0-9]\)/#\1 #;s/ *$//' |
91273933Sngie		    sort |
92273933Sngie		    while read l; do
93273933Sngie			set $l
94273933Sngie			name=$1; shift
95273933Sngie			port=$1; shift
96273933Sngie			proto=$1; shift
97273933Sngie			alias="$@"
98273933Sngie			printf "name=%s, port=%s, proto=%s, aliases=%s\n" \
99273933Sngie			    $name $port $proto "$alias"
100273933Sngie		    done > exp
101273933Sngie		;;
102273933Sngie	esac
103273933Sngie
104272343Sngie	# run test program
105272343Sngie	"$(atf_get_srcdir)/h_servent" | sed 's/ *$//' | sort >out
106272343Sngie
107272343Sngie	diff -u exp out || atf_fail "Observed output does not match reference output"
108272343Sngie}
109272343Sngie
110272343Sngieatf_init_test_cases()
111272343Sngie{
112272343Sngie	atf_add_test_case servent
113272343Sngie}
114