eval: Allow user override of 'missing'
[nbdkit/ericb.git] / tests / test-export-name.sh
blobda76a3a6ae0b0c99076401888efe3b6e87403bfc
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2019 Red Hat Inc.
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 -e
35 set -x
37 requires nbdsh --version
39 sock=`mktemp -u`
40 files="$sock exportname.pid"
41 rm -f $files
42 cleanup_fn rm -f $files
44 # Create an nbdkit sh plugin which reflects the export name back to
45 # the caller in the virtual device data and size.
46 start_nbdkit -P exportname.pid -U $sock \
47 sh - <<'EOF'
48 case "$1" in
49 open)
50 # The export name is the handle. nbdkit-sh-plugin removes
51 # exactly one trailing \n for us, so we must add one in case
52 # the client's handle also has one.
53 printf '%s\n' "$3"
55 get_size)
56 # Returns the length of the export name / handle in _bytes_.
57 export LC_ALL=C
58 echo ${#2}
60 pread)
61 printf %s "$2" | dd skip=$4 count=$3 iflag=skip_bytes,count_bytes
63 *) exit 2 ;;
64 esac
65 EOF
67 # Try to read back various export names from the plugin.
68 for e in "" "test" "/" "//" " " "/ " "?" "ใƒ†ใ‚นใƒˆ" "-n" '\\' $'\n' "%%" \
69 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
71 export e sock
72 nbdsh -c '
73 import os
75 e = os.environ["e"]
76 h.set_export_name (e)
77 h.connect_unix (os.environ["sock"])
79 size = h.get_size ()
80 print ("size=%r e=%r" % (size, e))
81 assert size == len (e.encode("utf-8"))
83 # Zero-sized reads are not defined in the NBD protocol.
84 if size > 0:
85 buf = h.pread (size, 0)
86 print ("buf=%r e=%r" % (buf, e))
87 assert buf == e.encode("utf-8")
89 done