Update Red Hat Copyright Notices
[nbdkit.git] / tests / test-disconnect-tls.sh
blob1d54f95af3467d0f642a15da30bd2ea3820854bb
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright Red Hat
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
33 source ./functions.sh
34 set -x
36 requires nbdsh -c 'exit(not h.supports_tls())'
38 # Does the nbdkit binary support TLS?
39 if ! nbdkit --dump-config | grep -sq tls=yes; then
40 echo "$0: nbdkit built without TLS support"
41 exit 77
44 # Did we create the PSK keys file?
45 # Probably 'certtool' is missing.
46 if [ ! -s keys.psk ]; then
47 echo "$0: PSK keys file was not created by the test harness"
48 exit 77
51 plugin=.libs/test-disconnect-plugin.$SOEXT
52 requires test -f $plugin
54 sock=$(mktemp -u /tmp/nbdkit-test-sock.XXXXXX)
55 files="disconnect-tls.pid $sock"
56 cleanup_fn rm -f $files
58 # Start nbdkit with the disconnect plugin, which has delayed reads and
59 # does disconnect on write based on export name.
60 start_nbdkit -P disconnect-tls.pid --tls require --tls-psk=keys.psk \
61 -U $sock $plugin
63 pid=`cat disconnect-tls.pid`
65 # We can't use 'nbdsh -u "$uri" because of nbd_set_uri_allow_local_file.
66 # Empty export name does soft disconnect on write; the write and the
67 # pending read should still succeed, but second read attempt should fail.
68 nbdsh -c '
69 import errno
71 h.set_tls(nbd.TLS_REQUIRE)
72 h.set_tls_psk_file("keys.psk")
73 h.set_tls_username("qemu")
74 h.connect_unix("'"$sock"'")
76 def waitfor(cookie):
77 while True:
78 c = h.aio_peek_command_completed()
79 if c:
80 break
81 h.poll(-1)
82 assert c == cookie
84 buf = nbd.Buffer(1)
85 c1 = h.aio_pread(buf, 1)
86 c2 = h.aio_pwrite(buf, 2)
87 waitfor(c2)
88 h.aio_command_completed(c2)
89 c3 = h.aio_pread(buf, 3)
90 waitfor(c3)
91 try:
92 h.aio_command_completed(c3)
93 assert False
94 except nbd.Error as ex:
95 assert ex.errnum == errno.ESHUTDOWN
96 waitfor(c1)
97 h.aio_command_completed(c1)
98 h.shutdown()
101 # Non-empty export name does hard disconnect on write. The write and the
102 # pending read should fail with lost connection.
103 nbdsh -c '
104 import errno
106 h.set_tls(nbd.TLS_REQUIRE)
107 h.set_tls_psk_file("keys.psk")
108 h.set_tls_username("qemu")
109 h.set_export_name("a")
110 h.connect_unix("'"$sock"'")
112 buf = nbd.Buffer(1)
113 c1 = h.aio_pread(buf, 1)
114 c2 = h.aio_pwrite(buf, 2)
115 while h.aio_in_flight() > 1:
116 h.poll(-1)
117 assert h.aio_is_ready() is False
118 try:
119 h.aio_command_completed(c1)
120 assert False
121 except nbd.Error as ex:
122 assert ex.errnum == errno.ENOTCONN
123 try:
124 h.aio_command_completed(c2)
125 assert False
126 except nbd.Error as ex:
127 assert ex.errnum == errno.ENOTCONN
130 # nbdkit should still be running
131 kill -s 0 $pid