xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.server/server-connect.exp (revision ae082add65442546470c0ba499a860ee89eed305)
1# This testcase is part of GDB, the GNU debugger.
2#
3# Copyright 2018-2019 Free Software Foundation, Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18# Test multiple types of connection (IPv4, IPv6, TCP, UDP) and make
19# sure both gdbserver and GDB work.
20
21load_lib gdbserver-support.exp
22
23standard_testfile normal.c
24
25if {[skip_gdbserver_tests]} {
26    return 0
27}
28
29# We want to have control over where we start gdbserver.
30if { [is_remote target] } {
31    return 0
32}
33
34if { [prepare_for_testing "failed to prepare" $testfile $srcfile debug] } {
35    return -1
36}
37
38# Make sure we're disconnected, in case we're testing with an
39# extended-remote board, therefore already connected.
40gdb_test "disconnect" ".*"
41
42set target_exec [gdbserver_download_current_prog]
43
44# An array containing the test instructions for each scenario.  The
45# description of each field is as follows:
46#
47# - The connection specification to be used when starting
48#   gdbserver/GDB.  This string will be used to set the
49#   GDB_TEST_SOCKETHOST when calling gdbserver_start.
50#
51# - A flag indicating whether gdbserver should fail when we attempt to
52#   start it.  Useful when testing erroneous connection specs such as
53#   "tcp8:".
54#
55# - The prefix that should be prepended to the test messages.
56set test_params \
57    { \
58	  { "tcp4:127.0.0.1" 0 "tcp4" } \
59	  { "tcp6:::1"       0 "tcp6" } \
60	  { "tcp6:[::1]"     0 "tcp6-with-brackets" } \
61	  { "tcp:localhost"  0 "tcp" } \
62	  { "udp4:127.0.0.1" 0 "udp4" } \
63	  { "udp6:::1"       0 "udp6" } \
64	  { "udp6:[::1]"     0 "udp6-with-brackets" } \
65	  { "tcp8:123"       1 "tcp8" } \
66	  { "udp123:::"      1 "udp123" } \
67	  { "garbage:1234"   1 "garbage:1234" } \
68    }
69
70# The best way to test different types of connections is to set the
71# GDB_TEST_SOCKETHOST variable accordingly.
72save_vars { GDB_TEST_SOCKETHOST } {
73    foreach line $test_params {
74	set sockhost [lindex $line 0]
75	set gdbserver_should_fail [lindex $line 1]
76	set prefix [lindex $line 2]
77
78	with_test_prefix $prefix {
79	    set GDB_TEST_SOCKETHOST $sockhost
80	    set test "start gdbserver"
81
82	    # Try to start gdbserver.
83	    set catchres [catch {set res [gdbserver_start "" $target_exec]} errmsg]
84
85	    if { $catchres != 0 } {
86		if { $gdbserver_should_fail } {
87		    pass "$test: gdbserver failed as expected"
88		} else {
89		    fail "$test: $errmsg"
90		}
91		continue
92	    } else {
93		if { $gdbserver_should_fail } {
94		    fail "$test: gdbserver should fail but did not"
95		} else {
96		    pass "$test"
97		}
98	    }
99
100	    set gdbserver_protocol [lindex $res 0]
101	    set gdbserver_gdbport [lindex $res 1]
102	    set test "connect to gdbserver using $sockhost"
103
104	    if { [gdb_target_cmd $gdbserver_protocol $gdbserver_gdbport] == 0 } {
105		pass $test
106	    } else {
107		fail $test
108	    }
109	}
110    }
111}
112