xref: /netbsd-src/tests/lib/libc/net/t_protoent.sh (revision de630815c58cf8134094b7511af74959be1d062a)
1*de630815Sjschauma# $NetBSD: t_protoent.sh,v 1.5 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/protocols"}
282517c83dSpgoyette
292517c83dSpgoyetteatf_test_case protoent
302517c83dSpgoyetteprotoent_head()
312517c83dSpgoyette{
322517c83dSpgoyette	atf_set "descr" "Checks {get,set,end}protoent(3)"
332517c83dSpgoyette}
342517c83dSpgoyetteprotoent_body()
352517c83dSpgoyette{
362517c83dSpgoyette	#
372517c83dSpgoyette	# Munge original to:
382517c83dSpgoyette	#  (1) match output format of the test program
392517c83dSpgoyette	#  (2) fold all names for the same port/proto together
402517c83dSpgoyette	#  (3) prune duplicates
412517c83dSpgoyette	#
42*de630815Sjschauma	tr '\t' ' ' <"${TEST_FILE}" | awk '
432517c83dSpgoyette	function add(key, name, i, n, ar) {
442517c83dSpgoyette		n = split(names[key], ar);
452517c83dSpgoyette		for (i = 1; i <= n; i++) {
462517c83dSpgoyette			if (name == ar[i]) {
472517c83dSpgoyette				return;
482517c83dSpgoyette			}
492517c83dSpgoyette		}
502517c83dSpgoyette		delete ar;
512517c83dSpgoyette		names[key] = names[key] " " name;
522517c83dSpgoyette	}
532517c83dSpgoyette	{
542517c83dSpgoyette		sub("#.*", "", $0);
552517c83dSpgoyette		gsub("  *", " ", $0);
562517c83dSpgoyette		if (NF == 0) {
572517c83dSpgoyette			next;
582517c83dSpgoyette		}
592517c83dSpgoyette		add($2, $1, 0);
602517c83dSpgoyette		for (i = 3; i <= NF; i++) {
612517c83dSpgoyette			add($2, $i, 1);
622517c83dSpgoyette		}
632517c83dSpgoyette	}
642517c83dSpgoyette	END {
652517c83dSpgoyette		for (key in names) {
662517c83dSpgoyette			proto = key;
672517c83dSpgoyette
682517c83dSpgoyette			n = split(names[key], ar);
692517c83dSpgoyette			printf "name=%s, proto=%s, aliases=", ar[1], proto;
702517c83dSpgoyette			for (i=2; i<=n; i++) {
712517c83dSpgoyette				if (i>2) {
722517c83dSpgoyette					printf " ";
732517c83dSpgoyette				}
742517c83dSpgoyette				printf "%s", ar[i];
752517c83dSpgoyette			}
762517c83dSpgoyette			printf "\n";
772517c83dSpgoyette			delete ar;
782517c83dSpgoyette		}
792517c83dSpgoyette	}
802517c83dSpgoyette	' | sort >exp
812517c83dSpgoyette
822517c83dSpgoyette	# run test program
832517c83dSpgoyette	"$(atf_get_srcdir)/h_protoent" | sed 's/ *$//' | sort >out
842517c83dSpgoyette
852517c83dSpgoyette	diff -u exp out || \
862517c83dSpgoyette	    atf_fail "Observed output does not match reference output"
872517c83dSpgoyette}
882517c83dSpgoyette
892517c83dSpgoyetteatf_init_test_cases()
902517c83dSpgoyette{
912517c83dSpgoyette	atf_add_test_case protoent
922517c83dSpgoyette}
93