xref: /netbsd-src/tests/lib/libc/net/t_servent.sh (revision de630815c58cf8134094b7511af74959be1d062a)
1*de630815Sjschauma# $NetBSD: t_servent.sh,v 1.4 2022/12/04 02:53:17 jschauma Exp $
22517c83dSpgoyette#
32517c83dSpgoyette# Copyright (c) 2008 The NetBSD Foundation, Inc.
42517c83dSpgoyette# All rights reserved.
52517c83dSpgoyette#
62517c83dSpgoyette# Redistribution and use in source and binary forms, with or without
72517c83dSpgoyette# modification, are permitted provided that the following conditions
82517c83dSpgoyette# are met:
92517c83dSpgoyette# 1. Redistributions of source code must retain the above copyright
102517c83dSpgoyette#    notice, this list of conditions and the following disclaimer.
112517c83dSpgoyette# 2. Redistributions in binary form must reproduce the above copyright
122517c83dSpgoyette#    notice, this list of conditions and the following disclaimer in the
132517c83dSpgoyette#    documentation and/or other materials provided with the distribution.
142517c83dSpgoyette#
152517c83dSpgoyette# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
162517c83dSpgoyette# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
172517c83dSpgoyette# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
182517c83dSpgoyette# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
192517c83dSpgoyette# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
202517c83dSpgoyette# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
212517c83dSpgoyette# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
222517c83dSpgoyette# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
232517c83dSpgoyette# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
242517c83dSpgoyette# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
252517c83dSpgoyette# POSSIBILITY OF SUCH DAMAGE.
262517c83dSpgoyette#
27*de630815Sjschauma: ${TEST_FILE:="/etc/services"}
282517c83dSpgoyette
292517c83dSpgoyetteatf_test_case servent
302517c83dSpgoyetteservent_head()
312517c83dSpgoyette{
322517c83dSpgoyette	atf_set "descr" "Checks {get,set,end}servent(3)"
331c9e147dSjoerg	# libc doesn't include aliases
341c9e147dSjoerg	atf_set "require.files" "/var/db/services.cdb"
352517c83dSpgoyette}
362517c83dSpgoyetteservent_body()
372517c83dSpgoyette{
382517c83dSpgoyette	#
392517c83dSpgoyette	# Munge original to:
402517c83dSpgoyette	#  (1) match output format of the test program
412517c83dSpgoyette	#  (2) fold all names for the same port/proto together
422517c83dSpgoyette	#  (3) prune duplicates
432517c83dSpgoyette	#
44*de630815Sjschauma	tr '\t' ' ' <"${TEST_FILE}" | awk '
452517c83dSpgoyette		function add(key, name, i, n, ar) {
462517c83dSpgoyette			n = split(names[key], ar);
472517c83dSpgoyette			for (i=1; i<=n; i++) {
482517c83dSpgoyette				if (name == ar[i]) {
492517c83dSpgoyette					return;
502517c83dSpgoyette				}
512517c83dSpgoyette			}
522517c83dSpgoyette			delete ar;
532517c83dSpgoyette			names[key] = names[key] " " name;
542517c83dSpgoyette		}
552517c83dSpgoyette
562517c83dSpgoyette		{
572517c83dSpgoyette			sub("#.*", "", $0);
582517c83dSpgoyette			gsub("  *", " ", $0);
592517c83dSpgoyette			if (NF==0) {
602517c83dSpgoyette				next;
612517c83dSpgoyette			}
622517c83dSpgoyette			add($2, $1, 0);
632517c83dSpgoyette			for (i=3; i<=NF; i++) {
642517c83dSpgoyette				add($2, $i, 1);
652517c83dSpgoyette			}
662517c83dSpgoyette		}
672517c83dSpgoyette		END {
682517c83dSpgoyette			for (key in names) {
692517c83dSpgoyette				portproto = key;
702517c83dSpgoyette				sub("/", ", proto=", portproto);
712517c83dSpgoyette				portproto = "port=" portproto;
722517c83dSpgoyette
732517c83dSpgoyette				n = split(names[key], ar);
742517c83dSpgoyette				printf "name=%s, %s, aliases=", ar[1], portproto;
752517c83dSpgoyette				for (i=2; i<=n; i++) {
762517c83dSpgoyette					if (i>2) {
772517c83dSpgoyette						printf " ";
782517c83dSpgoyette					}
792517c83dSpgoyette					printf "%s", ar[i];
802517c83dSpgoyette				}
812517c83dSpgoyette				printf "\n";
822517c83dSpgoyette				delete ar;
832517c83dSpgoyette			}
842517c83dSpgoyette		}
852517c83dSpgoyette	' | sort >exp
862517c83dSpgoyette
872517c83dSpgoyette	# run test program
882517c83dSpgoyette	"$(atf_get_srcdir)/h_servent" | sed 's/ *$//' | sort >out
892517c83dSpgoyette
902517c83dSpgoyette	diff -u exp out || atf_fail "Observed output does not match reference output"
912517c83dSpgoyette}
922517c83dSpgoyette
932517c83dSpgoyetteatf_init_test_cases()
942517c83dSpgoyette{
952517c83dSpgoyette	atf_add_test_case servent
962517c83dSpgoyette}
97