...

Source file src/github.com/godbus/dbus/v5/transport_unix_test.go

Documentation: github.com/godbus/dbus/v5

     1  package dbus
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  const testString = `This is a test!
     9  This text should be read from the file that is created by this test.`
    10  
    11  type unixFDTest struct {
    12  	t *testing.T
    13  }
    14  
    15  func (t unixFDTest) Testfd(fd UnixFD) (string, *Error) {
    16  	var b [4096]byte
    17  	file := os.NewFile(uintptr(fd), "testfile")
    18  	defer file.Close()
    19  	n, err := file.Read(b[:])
    20  	if err != nil {
    21  		return "", &Error{"com.github.guelfey.test.Error", nil}
    22  	}
    23  	return string(b[:n]), nil
    24  }
    25  
    26  func (t unixFDTest) Testvariant(v Variant) (string, *Error) {
    27  	var b [4096]byte
    28  	fd := v.Value().(UnixFD)
    29  	file := os.NewFile(uintptr(fd), "testfile")
    30  	defer file.Close()
    31  	n, err := file.Read(b[:])
    32  	if err != nil {
    33  		return "", &Error{"com.github.guelfey.test.Error", nil}
    34  	}
    35  	return string(b[:n]), nil
    36  }
    37  
    38  type unixfdContainer struct {
    39  	Fd UnixFD
    40  }
    41  
    42  func (t unixFDTest) Teststruct(s unixfdContainer) (string, *Error) {
    43  	var b [4096]byte
    44  	file := os.NewFile(uintptr(s.Fd), "testfile")
    45  	defer file.Close()
    46  	n, err := file.Read(b[:])
    47  	if err != nil {
    48  		return "", &Error{"com.github.guelfey.test.Error", nil}
    49  	}
    50  	return string(b[:n]), nil
    51  }
    52  
    53  func (t unixFDTest) Testvariantstruct(vs Variant) (string, *Error) {
    54  	var b [4096]byte
    55  	s := vs.Value().([]interface{})
    56  	u := s[0].(UnixFD)
    57  	file := os.NewFile(uintptr(u), "testfile")
    58  	defer file.Close()
    59  	n, err := file.Read(b[:])
    60  	if err != nil {
    61  		return "", &Error{"com.github.guelfey.test.Error", nil}
    62  	}
    63  	return string(b[:n]), nil
    64  }
    65  
    66  type variantContainer struct {
    67  	V Variant
    68  }
    69  
    70  func (t unixFDTest) Teststructvariant(sv variantContainer) (string, *Error) {
    71  	var b [4096]byte
    72  	fd := sv.V.Value().(UnixFD)
    73  	file := os.NewFile(uintptr(fd), "testfile")
    74  	defer file.Close()
    75  	n, err := file.Read(b[:])
    76  	if err != nil {
    77  		return "", &Error{"com.github.guelfey.test.Error", nil}
    78  	}
    79  	return string(b[:n]), nil
    80  }
    81  
    82  func TestUnixFDs(t *testing.T) {
    83  	conn, err := ConnectSessionBus()
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  	defer conn.Close()
    88  	r, w, err := os.Pipe()
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	defer w.Close()
    93  	name := conn.Names()[0]
    94  	test := unixFDTest{t}
    95  	conn.Export(test, "/com/github/guelfey/test", "com.github.guelfey.test")
    96  	var s string
    97  	obj := conn.Object(name, "/com/github/guelfey/test")
    98  
    99  	if _, err := w.Write([]byte(testString)); err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	err = obj.Call("com.github.guelfey.test.Testfd", 0, UnixFD(r.Fd())).Store(&s)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  	if s != testString {
   107  		t.Fatal("got", s, "wanted", testString)
   108  	}
   109  
   110  	if _, err := w.Write([]byte(testString)); err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	err = obj.Call("com.github.guelfey.test.Testvariant", 0, MakeVariant(UnixFD(r.Fd()))).Store(&s)
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  	if s != testString {
   118  		t.Fatal("got", s, "wanted", testString)
   119  	}
   120  
   121  	if _, err := w.Write([]byte(testString)); err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	err = obj.Call("com.github.guelfey.test.Teststruct", 0, unixfdContainer{UnixFD(r.Fd())}).Store(&s)
   125  	if err != nil {
   126  		t.Fatal(err)
   127  	}
   128  	if s != testString {
   129  		t.Fatal("got", s, "wanted", testString)
   130  	}
   131  
   132  	if _, err := w.Write([]byte(testString)); err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	err = obj.Call("com.github.guelfey.test.Testvariantstruct", 0, MakeVariant(unixfdContainer{UnixFD(r.Fd())})).Store(&s)
   136  	if err != nil {
   137  		t.Fatal(err)
   138  	}
   139  	if s != testString {
   140  		t.Fatal("got", s, "wanted", testString)
   141  	}
   142  
   143  	if _, err := w.Write([]byte(testString)); err != nil {
   144  		t.Fatal(err)
   145  	}
   146  	err = obj.Call("com.github.guelfey.test.Teststructvariant", 0, variantContainer{MakeVariant(UnixFD(r.Fd()))}).Store(&s)
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  	if s != testString {
   151  		t.Fatal("got", s, "wanted", testString)
   152  	}
   153  
   154  }
   155  

View as plain text