1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) 2019 Petr Vorel <pvorel@suse.cz>
4f08c3bdfSopenharmony_ci# Copyright (c) 2017 Oracle and/or its affiliates. All Rights Reserved.
5f08c3bdfSopenharmony_ci# Copyright (c) International Business Machines  Corp., 2001
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ciTST_CNT=2
8f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="traceroute"
9f08c3bdfSopenharmony_ciTST_SETUP="setup"
10f08c3bdfSopenharmony_ciTST_TESTFUNC="test"
11f08c3bdfSopenharmony_ciTST_NEEDS_TMPDIR=1
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_cisetup()
14f08c3bdfSopenharmony_ci{
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_ci	TRACEROUTE=traceroute${TST_IPV6}
17f08c3bdfSopenharmony_ci	tst_require_cmds $TRACEROUTE
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci	tst_res TINFO "$TRACEROUTE version:"
20f08c3bdfSopenharmony_ci	tst_res TINFO $($TRACEROUTE -V 2>&1)
21f08c3bdfSopenharmony_ci}
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cirun_trace()
24f08c3bdfSopenharmony_ci{
25f08c3bdfSopenharmony_ci	local opts="$@"
26f08c3bdfSopenharmony_ci	local ip=$(tst_ipaddr rhost)
27f08c3bdfSopenharmony_ci	local pattern="^[ ]+1[ ]+$ip([ ]+[0-9]+[.][0-9]+ ms){3}"
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_ci	if $TRACEROUTE $opts 2>&1 | grep -q "invalid option"; then
30f08c3bdfSopenharmony_ci		tst_res TCONF "$opts flag not supported"
31f08c3bdfSopenharmony_ci		return
32f08c3bdfSopenharmony_ci	fi
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	# According to man pages, default sizes:
35f08c3bdfSopenharmony_ci	local bytes=60
36f08c3bdfSopenharmony_ci	[ "$TST_IPV6" ] && bytes=80
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	EXPECT_PASS $TRACEROUTE $ip $bytes -n -m 2 $opts \>out.log
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci	grep -q "$bytes byte" out.log
41f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
42f08c3bdfSopenharmony_ci		cat out.log
43f08c3bdfSopenharmony_ci		tst_res TFAIL "'$bytes byte' not found"
44f08c3bdfSopenharmony_ci	else
45f08c3bdfSopenharmony_ci		tst_res TPASS "$TRACEROUTE use $bytes bytes"
46f08c3bdfSopenharmony_ci	fi
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	tail -1 out.log | grep -qE "$pattern"
49f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
50f08c3bdfSopenharmony_ci		cat out.log
51f08c3bdfSopenharmony_ci		tst_res TFAIL "pattern '$pattern' not found in log"
52f08c3bdfSopenharmony_ci	else
53f08c3bdfSopenharmony_ci		tst_res TPASS "$TRACEROUTE test completed with 1 hop"
54f08c3bdfSopenharmony_ci	fi
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_citest1()
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	tst_res TINFO "run $TRACEROUTE with ICMP ECHO"
60f08c3bdfSopenharmony_ci	run_trace -I
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_citest2()
64f08c3bdfSopenharmony_ci{
65f08c3bdfSopenharmony_ci	tst_res TINFO "run $TRACEROUTE with TCP SYN"
66f08c3bdfSopenharmony_ci	run_trace -T
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_ci. tst_net.sh
70f08c3bdfSopenharmony_citst_run
71