Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / golang / libnbd_405_pread_structured_test.go
blobac4cb3e580de7818dd07cb8ff7215410b11b8c66
1 /* libnbd golang tests
2 * Copyright Red Hat
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 package libnbd
21 import "bytes"
22 import "encoding/binary"
23 import "fmt"
24 import "testing"
26 var expected405 = make([]byte, 512)
28 func psf(user_data int, buf2 []byte, offset uint64, status uint,
29 error *int) int {
30 if user_data != 42 {
31 panic("expected user_data == 42")
33 if *error != 0 {
34 panic("expected *error == 0")
36 if offset != 0 {
37 panic("expected offset == 0")
39 if status != uint(READ_DATA) {
40 panic("expected status == READ_DATA")
42 if !bytes.Equal(buf2, expected405) {
43 fmt.Printf("buf2 = %s\nexpected = %s\n", buf2, expected405)
44 panic("expected sub-buffer == expected pattern")
46 return 0
49 func Test405PReadStructured(t *testing.T) {
50 // Expected data.
51 for i := 0; i < 512; i += 8 {
52 binary.BigEndian.PutUint64(expected405[i:i+8], uint64(i))
55 h, err := Create()
56 if err != nil {
57 t.Fatalf("could not create handle: %s", err)
59 defer h.Close()
61 err = h.ConnectCommand([]string{
62 "nbdkit", "-s", "--exit-with-parent", "-v",
63 "pattern", "size=512",
65 if err != nil {
66 t.Fatalf("could not connect: %s", err)
69 buf := make([]byte, 512)
70 err = h.PreadStructured(buf, 0,
71 func(buf2 []byte, offset uint64, status uint,
72 error *int) int {
73 return psf(42, buf2, offset, status, error)
74 }, nil)
75 if err != nil {
76 t.Fatalf("%s", err)
78 if !bytes.Equal(buf, expected405) {
79 t.Fatalf("did not read expected data")
82 var optargs PreadStructuredOptargs
83 optargs.FlagsSet = true
84 optargs.Flags = CMD_FLAG_DF
85 wrap := func(buf2 []byte, offset uint64, status uint, error *int) int {
86 return psf(42, buf2, offset, status, error)
88 err = h.PreadStructured(buf, 0, wrap, &optargs)
89 if err != nil {
90 t.Fatalf("%s", err)
92 if !bytes.Equal(buf, expected405) {
93 t.Fatalf("did not read expected data")